Debian Codegraph @

test/integration/targets/secret_masking_callback/runme.sh

raw
1 #!/usr/bin/env bash
2
3 set -eux -o pipefail
4
5 SECRET=Callbacksecret0060Value
6
7 # A secret whose JSON encoding differs from its literal value: it contains a double quote, a backslash
8 # and a non-ASCII character. Callbacks that serialize results to JSON emit one of the escaped forms
9 # rather than the literal, so masking must recognize those forms too.
10 ESCAPED_SECRET='Escaped"Callback\Secretü'
11 ESCAPED_SECRET_JSON_ASCII='Escaped\"Callback\\Secret\u00fc' # json.dumps(..., ensure_ascii=True)
12 ESCAPED_SECRET_JSON_UTF8='Escaped\"Callback\\Secretü' # json.dumps(..., ensure_ascii=False)
13
14 MASKED_OUT="${OUTPUT_DIR}/masked.jsonl"
15 RAW_OUT="${OUTPUT_DIR}/raw.jsonl"
16
17 export ANSIBLE_CALLBACK_PLUGINS="${PWD}/callback_plugins"
18 export ANSIBLE_STDOUT_CALLBACK=masking_probe
19
20 # Test that when callback plugins opt in to secret masking, the raw secret is still available to them.
21 MASKING_PROBE_OUTPUT="${RAW_OUT}" MASKING_PROBE_SUPPORTS_MASKING=1 ansible-playbook test.yml "$@"
22 echo "Callback output written to ${RAW_OUT}"
23 cat "${RAW_OUT}"
24
25 if ! grep -q -- "${SECRET}" "${RAW_OUT}"; then
26 echo "FAIL: opted-in callback did not receive the raw secret; the default-path test proves nothing" >&2
27 cat "${RAW_OUT}" >&2
28 exit 1
29 fi
30 if ! grep -qF -- "${ESCAPED_SECRET_JSON_ASCII}" "${RAW_OUT}"; then
31 echo "FAIL: opted-in callback did not serialize the JSON escaped form of the raw secret; the escaped-form test proves nothing" >&2
32 cat "${RAW_OUT}" >&2
33 exit 1
34 fi
35
36 # Test that when callback plugins do not opt in to secret masking, the raw secret is not available to them.
37 MASKING_PROBE_OUTPUT="${MASKED_OUT}" ansible-playbook test.yml "$@"
38
39 echo "Callback output written to ${MASKED_OUT}"
40 cat "${MASKED_OUT}"
41
42 if grep -q -- "${SECRET}" "${MASKED_OUT}"; then
43 echo "FAIL: secret leaked to a non-opted-in callback's serialized result" >&2
44 cat "${MASKED_OUT}" >&2
45 exit 1
46 fi
47 if ! grep -qF -- "\$REDACTED\$" "${MASKED_OUT}"; then
48 echo "FAIL: expected redacted marker not found in non-opted-in callback output" >&2
49 cat "${MASKED_OUT}" >&2
50 exit 1
51 fi
52 for form in "${ESCAPED_SECRET}" "${ESCAPED_SECRET_JSON_ASCII}" "${ESCAPED_SECRET_JSON_UTF8}"; do
53 if grep -qF -- "${form}" "${MASKED_OUT}"; then
54 echo "FAIL: escapable secret leaked to a non-opted-in callback's serialized result as '${form}'" >&2
55 cat "${MASKED_OUT}" >&2
56 exit 1
57 fi
58 done
59 if ! grep -qF -- "MARKER escaped: \$REDACTED\$" "${MASKED_OUT}"; then
60 echo "FAIL: escapable secret was not redacted in non-opted-in callback output" >&2
61 cat "${MASKED_OUT}" >&2
62 exit 1
63 fi
64
65
66 # Test the built-in callbacks which opt in to ANSIBLE_SUPPORTS_MASKING and are responsible for masking their own output.
67 assert_masked() {
68 local log="$1"
69
70 if grep -q -- "${SECRET}" "${log}"; then
71 echo "FAIL: registered secret leaked in plaintext (${log})" >&2
72 grep -n -- "${SECRET}" "${log}" >&2 || true
73 exit 1
74 fi
75 if ! grep -qF -- "\$REDACTED\$" "${log}"; then
76 echo "FAIL: expected redacted marker not found (${log})" >&2
77 exit 1
78 fi
79
80 # The escapable secret must not appear as its literal value or as either JSON escaped form.
81 # Built-in callbacks serialize results with ensure_ascii=False so the UTF-8 form is what they
82 # would emit; the ASCII form is checked as well to cover any writer that uses the default.
83 local form
84 for form in "${ESCAPED_SECRET}" "${ESCAPED_SECRET_JSON_ASCII}" "${ESCAPED_SECRET_JSON_UTF8}"; do
85 if grep -qF -- "${form}" "${log}"; then
86 echo "FAIL: escapable secret leaked as '${form}' (${log})" >&2
87 grep -nF -- "${form}" "${log}" >&2 || true
88 exit 1
89 fi
90 done
91 # Catch any other encoding of the secret (for example Python repr) by its distinctive fragment.
92 if grep -q -- 'Escaped' "${log}"; then
93 echo "FAIL: a fragment of the escapable secret leaked in another encoding (${log})" >&2
94 grep -n -- 'Escaped' "${log}" >&2 || true
95 exit 1
96 fi
97 # The failed task is the last result emitted, so it is present even in outputs that only keep
98 # the final result per host (tree); both the plain and the escapable secret must be redacted.
99 if ! grep -qF -- "MARKER failed: \$REDACTED\$ \$REDACTED\$" "${log}"; then
100 echo "FAIL: escapable secret was not redacted in the failed task result (${log})" >&2
101 exit 1
102 fi
103 }
104
105 for stdout_callback in default minimal oneline; do
106 LOG="${OUTPUT_DIR}/${stdout_callback}.log"
107 ANSIBLE_STDOUT_CALLBACK="${stdout_callback}" ansible-playbook test.yml -v "$@" 2>&1 | tee "${LOG}"
108 assert_masked "${LOG}"
109 done
110
111 # junit writes a report file
112 JUNIT_DIR="${OUTPUT_DIR}/junit"
113 rm -rf "${JUNIT_DIR}"
114 ANSIBLE_STDOUT_CALLBACK=default ANSIBLE_CALLBACKS_ENABLED=junit JUNIT_OUTPUT_DIR="${JUNIT_DIR}" ansible-playbook test.yml "$@"
115 cat "${JUNIT_DIR}"/*.xml
116 assert_masked "$(ls "${JUNIT_DIR}"/*.xml)"
117
118 # tree writes a per host file
119 TREE_DIR="${OUTPUT_DIR}/tree"
120 rm -rf "${TREE_DIR}"
121 ANSIBLE_STDOUT_CALLBACK=default ANSIBLE_CALLBACKS_ENABLED=tree ANSIBLE_CALLBACK_TREE_DIR="${TREE_DIR}" ansible-playbook test.yml "$@"
122 cat "${TREE_DIR}/localhost"
123 assert_masked "${TREE_DIR}/localhost"
124
125 echo "All built-in callback secret masking scenarios passed."