Debian Codegraph @

test/integration/targets/ansible-vault/runme.sh

raw
1 #!/usr/bin/env bash
2
3 set -euvx
4 source virtualenv.sh
5
6
7 MYTMPDIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir')
8 trap 'chmod -R u+rwx ${MYTMPDIR}; rm -rf "${MYTMPDIR}"' EXIT
9
10 # create a test file
11 TEST_FILE="${MYTMPDIR}/test_file"
12 echo "This is a test file" > "${TEST_FILE}"
13
14 TEST_FILE_1_2="${MYTMPDIR}/test_file_1_2"
15 echo "This is a test file for format 1.2" > "${TEST_FILE_1_2}"
16
17 TEST_FILE_ENC_PASSWORD="${MYTMPDIR}/test_file_enc_password"
18 echo "This is a test file for encrypted with a vault password that is itself vault encrypted" > "${TEST_FILE_ENC_PASSWORD}"
19
20 TEST_FILE_ENC_PASSWORD_DEFAULT="${MYTMPDIR}/test_file_enc_password_default"
21 echo "This is a test file for encrypted with a vault password that is itself vault encrypted using --encrypted-vault-id default" > "${TEST_FILE_ENC_PASSWORD_DEFAULT}"
22
23 TEST_FILE_OUTPUT="${MYTMPDIR}/test_file_output"
24
25 TEST_FILE_EDIT="${MYTMPDIR}/test_file_edit"
26 echo "This is a test file for edit" > "${TEST_FILE_EDIT}"
27
28 TEST_FILE_EDIT2="${MYTMPDIR}/test_file_edit2"
29 echo "This is a test file for edit2" > "${TEST_FILE_EDIT2}"
30
31 # test case for https://github.com/ansible/ansible/issues/35834
32 # (being prompted for new password on vault-edit with no configured passwords)
33
34 TEST_FILE_EDIT3="${MYTMPDIR}/test_file_edit3"
35 echo "This is a test file for edit3" > "${TEST_FILE_EDIT3}"
36
37 # ansible-config view
38 ansible-config view
39
40 # ansible-config
41 ansible-config dump --only-changed
42 ansible-vault encrypt "$@" --vault-id vault-password "${TEST_FILE_EDIT3}"
43 # EDITOR=./faux-editor.py ansible-vault edit "$@" "${TEST_FILE_EDIT3}"
44 EDITOR=./faux-editor.py ansible-vault edit --vault-id vault-password -vvvvv "${TEST_FILE_EDIT3}"
45 echo $?
46
47 # view the vault encrypted password file
48 ansible-vault view "$@" --vault-id vault-password encrypted-vault-password
49
50 # check if ansible-vault fails when destination is not writable
51 # skip check as root as root can always read/write
52 if [ ${UID} -ne "0" ]; then
53 NOT_WRITABLE_DIR="${MYTMPDIR}/not_writable"
54 TEST_FILE_EDIT4="${NOT_WRITABLE_DIR}/testfile"
55 mkdir "${NOT_WRITABLE_DIR}"
56 touch "${TEST_FILE_EDIT4}"
57 chmod ugo-w "${NOT_WRITABLE_DIR}"
58 ansible-vault encrypt "$@" --vault-password-file vault-password "${TEST_FILE_EDIT4}" < /dev/null > log 2>&1 && :
59 grep "not writable" log && :
60 WRONG_RC=$?
61 echo "rc was $WRONG_RC (0 is expected)"
62 [ $WRONG_RC -eq 0 ]
63 fi
64
65 # encrypt with a password from a vault encrypted password file and multiple vault-ids
66 # should fail because we dont know which vault id to use to encrypt with
67 ansible-vault encrypt "$@" --vault-id vault-password --vault-id encrypted-vault-password "${TEST_FILE_ENC_PASSWORD}" && :
68 WRONG_RC=$?
69 echo "rc was $WRONG_RC (5 is expected)"
70 [ $WRONG_RC -eq 5 ]
71
72 # try to view the file encrypted with the vault-password we didn't specify
73 # to verify we didn't choose the wrong vault-id
74 ansible-vault view "$@" --vault-id vault-password encrypted-vault-password
75
76 FORMAT_1_1_HEADER="\$ANSIBLE_VAULT;1.1;AES256"
77 FORMAT_1_2_HEADER="\$ANSIBLE_VAULT;1.2;AES256"
78
79
80 VAULT_PASSWORD_FILE=vault-password
81 # new format, view, using password client script
82 ansible-vault view "$@" --vault-id [email protected] format_1_1_AES256.yml
83
84 # view, using password client script, unknown vault/keyname
85 ansible-vault view "$@" --vault-id [email protected] format_1_1_AES256.yml && :
86
87 # Use linux setsid to test without a tty. No setsid if osx/bsd though...
88 if [ -x "$(command -v setsid)" ]; then
89 # tests related to https://github.com/ansible/ansible/issues/30993
90 CMD='ansible-playbook -i ../../inventory -vvvvv --ask-vault-pass test_vault.yml'
91 setsid sh -c "echo test-vault-password|${CMD}" < /dev/null > log 2>&1 && :
92 WRONG_RC=$?
93 cat log
94 echo "rc was $WRONG_RC (0 is expected)"
95 [ $WRONG_RC -eq 0 ]
96
97 setsid sh -c 'tty; ansible-vault view --ask-vault-pass -vvvvv test_vault.yml' < /dev/null > log 2>&1 && :
98 WRONG_RC=$?
99 echo "rc was $WRONG_RC (1 is expected)"
100 [ $WRONG_RC -eq 1 ]
101 cat log
102
103 setsid sh -c 'tty; echo passbhkjhword|ansible-playbook -i ../../inventory -vvvvv --ask-vault-pass test_vault.yml' < /dev/null > log 2>&1 && :
104 WRONG_RC=$?
105 echo "rc was $WRONG_RC (1 is expected)"
106 [ $WRONG_RC -eq 1 ]
107 cat log
108
109 setsid sh -c 'tty; echo test-vault-password |ansible-playbook -i ../../inventory -vvvvv --ask-vault-pass test_vault.yml' < /dev/null > log 2>&1
110 echo $?
111 cat log
112
113 setsid sh -c 'tty; echo test-vault-password|ansible-playbook -i ../../inventory -vvvvv --ask-vault-pass test_vault.yml' < /dev/null > log 2>&1
114 echo $?
115 cat log
116
117 setsid sh -c 'tty; echo test-vault-password |ansible-playbook -i ../../inventory -vvvvv --ask-vault-pass test_vault.yml' < /dev/null > log 2>&1
118 echo $?
119 cat log
120
121 setsid sh -c 'tty; echo test-vault-password|ansible-vault view --ask-vault-pass -vvvvv vaulted.inventory' < /dev/null > log 2>&1
122 echo $?
123 cat log
124
125 # test using --ask-vault-password option
126 CMD='ansible-playbook -i ../../inventory -vvvvv --ask-vault-password test_vault.yml'
127 setsid sh -c "echo test-vault-password|${CMD}" < /dev/null > log 2>&1 && :
128 WRONG_RC=$?
129 cat log
130 echo "rc was $WRONG_RC (0 is expected)"
131 [ $WRONG_RC -eq 0 ]
132 fi
133
134 ansible-vault view "$@" --vault-password-file vault-password-wrong format_1_1_AES256.yml && :
135 WRONG_RC=$?
136 echo "rc was $WRONG_RC (1 is expected)"
137 [ $WRONG_RC -eq 1 ]
138
139 set -eux
140
141
142 # new format, view
143 ansible-vault view "$@" --vault-password-file vault-password format_1_1_AES256.yml
144
145 # new format, view with vault-id
146 ansible-vault view "$@" --vault-id=vault-password format_1_1_AES256.yml
147
148 # new format, view, using password script
149 ansible-vault view "$@" --vault-password-file password-script.py format_1_1_AES256.yml
150
151 # new format, view, using password script with vault-id
152 ansible-vault view "$@" --vault-id password-script.py format_1_1_AES256.yml
153
154 # new 1.2 format, view
155 ansible-vault view "$@" --vault-password-file vault-password format_1_2_AES256.yml
156
157 # new 1.2 format, view with vault-id
158 ansible-vault view "$@" --vault-id=test_vault_id@vault-password format_1_2_AES256.yml
159
160 # new 1,2 format, view, using password script
161 ansible-vault view "$@" --vault-password-file password-script.py format_1_2_AES256.yml
162
163 # new 1.2 format, view, using password script with vault-id
164 ansible-vault view "$@" --vault-id password-script.py format_1_2_AES256.yml
165
166 # newish 1.1 format, view, using a vault-id list from config env var
167 ANSIBLE_VAULT_IDENTITY_LIST='wrong-password@vault-password-wrong,default@vault-password' ansible-vault view "$@" --vault-id password-script.py format_1_1_AES256.yml
168
169 # new 1.2 format, view, ENFORCE_IDENTITY_MATCH=true, should fail, no 'test_vault_id' vault_id
170 ANSIBLE_VAULT_ID_MATCH=1 ansible-vault view "$@" --vault-password-file vault-password format_1_2_AES256.yml && :
171 WRONG_RC=$?
172 echo "rc was $WRONG_RC (1 is expected)"
173 [ $WRONG_RC -eq 1 ]
174
175 # new 1.2 format, view with vault-id, ENFORCE_IDENTITY_MATCH=true, should work, 'test_vault_id' is provided
176 ANSIBLE_VAULT_ID_MATCH=1 ansible-vault view "$@" --vault-id=test_vault_id@vault-password format_1_2_AES256.yml
177
178 # new 1,2 format, view, using password script, ENFORCE_IDENTITY_MATCH=true, should fail, no 'test_vault_id'
179 ANSIBLE_VAULT_ID_MATCH=1 ansible-vault view "$@" --vault-password-file password-script.py format_1_2_AES256.yml && :
180 WRONG_RC=$?
181 echo "rc was $WRONG_RC (1 is expected)"
182 [ $WRONG_RC -eq 1 ]
183
184
185 # new 1.2 format, view, using password script with vault-id, ENFORCE_IDENTITY_MATCH=true, should fail
186 ANSIBLE_VAULT_ID_MATCH=1 ansible-vault view "$@" --vault-id password-script.py format_1_2_AES256.yml && :
187 WRONG_RC=$?
188 echo "rc was $WRONG_RC (1 is expected)"
189 [ $WRONG_RC -eq 1 ]
190
191 # test if vault password file is not a directory
192 ANSIBLE_VAULT_PASSWORD_FILE='' ansible-vault view "$@" format_1_1_AES.yml && :
193 WRONG_RC=$?
194 echo "rc was $WRONG_RC (1 is expected)"
195 [ $WRONG_RC -eq 1 ]
196
197 # new 1.2 format, view, using password script with vault-id, ENFORCE_IDENTITY_MATCH=true, 'test_vault_id' provided should work
198 ANSIBLE_VAULT_ID_MATCH=1 ansible-vault view "$@" [email protected] format_1_2_AES256.yml
199
200 # test with a default vault password set via config/env, right password
201 ANSIBLE_VAULT_PASSWORD_FILE=vault-password ansible-vault view "$@" format_1_1_AES256.yml
202
203 # test with a default vault password set via config/env, wrong password
204 ANSIBLE_VAULT_PASSWORD_FILE=vault-password-wrong ansible-vault view "$@" format_1_1_AES.yml && :
205 WRONG_RC=$?
206 echo "rc was $WRONG_RC (1 is expected)"
207 [ $WRONG_RC -eq 1 ]
208
209 # test with a default vault-id list set via config/env, right password
210 ANSIBLE_VAULT_PASSWORD_FILE=wrong@vault-password-wrong,correct@vault-password ansible-vault view "$@" format_1_1_AES.yml && :
211
212 # test with a default vault-id list set via config/env,wrong passwords
213 ANSIBLE_VAULT_PASSWORD_FILE=wrong@vault-password-wrong,alsowrong@vault-password-wrong ansible-vault view "$@" format_1_1_AES.yml && :
214 WRONG_RC=$?
215 echo "rc was $WRONG_RC (1 is expected)"
216 [ $WRONG_RC -eq 1 ]
217
218 # try specifying a --encrypt-vault-id that doesn't exist, should exit with an error indicating
219 # that --encrypt-vault-id and the known vault-ids
220 ansible-vault encrypt "$@" --vault-password-file vault-password --encrypt-vault-id doesnt_exist "${TEST_FILE}" && :
221 WRONG_RC=$?
222 echo "rc was $WRONG_RC (1 is expected)"
223 [ $WRONG_RC -eq 1 ]
224
225 # encrypt it
226 ansible-vault encrypt "$@" --vault-password-file vault-password "${TEST_FILE}"
227
228 ansible-vault view "$@" --vault-password-file vault-password "${TEST_FILE}"
229
230 # view with multiple vault-password files, including a wrong one
231 ansible-vault view "$@" --vault-password-file vault-password --vault-password-file vault-password-wrong "${TEST_FILE}"
232
233 # view with multiple vault-password files, including a wrong one, using vault-id
234 ansible-vault view "$@" --vault-id vault-password --vault-id vault-password-wrong "${TEST_FILE}"
235
236 # And with the password files specified in a different order
237 ansible-vault view "$@" --vault-password-file vault-password-wrong --vault-password-file vault-password "${TEST_FILE}"
238
239 # And with the password files specified in a different order, using vault-id
240 ansible-vault view "$@" --vault-id vault-password-wrong --vault-id vault-password "${TEST_FILE}"
241
242 # And with the password files specified in a different order, using --vault-id and non default vault_ids
243 ansible-vault view "$@" --vault-id test_vault_id@vault-password-wrong --vault-id test_vault_id@vault-password "${TEST_FILE}"
244
245 ansible-vault decrypt "$@" --vault-password-file vault-password "${TEST_FILE}"
246
247 # encrypt it, using a vault_id so we write a 1.2 format file
248 ansible-vault encrypt "$@" --vault-id test_vault_1_2@vault-password "${TEST_FILE_1_2}"
249
250 ansible-vault view "$@" --vault-id vault-password "${TEST_FILE_1_2}"
251 ansible-vault view "$@" --vault-id test_vault_1_2@vault-password "${TEST_FILE_1_2}"
252
253 # view with multiple vault-password files, including a wrong one
254 ansible-vault view "$@" --vault-id vault-password --vault-id wrong_password@vault-password-wrong "${TEST_FILE_1_2}"
255
256 # And with the password files specified in a different order, using vault-id
257 ansible-vault view "$@" --vault-id vault-password-wrong --vault-id vault-password "${TEST_FILE_1_2}"
258
259 # And with the password files specified in a different order, using --vault-id and non default vault_ids
260 ansible-vault view "$@" --vault-id test_vault_id@vault-password-wrong --vault-id test_vault_id@vault-password "${TEST_FILE_1_2}"
261
262 ansible-vault decrypt "$@" --vault-id test_vault_1_2@vault-password "${TEST_FILE_1_2}"
263
264 # multiple vault passwords
265 ansible-vault view "$@" --vault-password-file vault-password --vault-password-file vault-password-wrong format_1_1_AES256.yml
266
267 # multiple vault passwords, --vault-id
268 ansible-vault view "$@" --vault-id test_vault_id@vault-password --vault-id test_vault_id@vault-password-wrong format_1_1_AES256.yml
269
270 # encrypt it, with password from password script
271 ansible-vault encrypt "$@" --vault-password-file password-script.py "${TEST_FILE}"
272
273 ansible-vault view "$@" --vault-password-file password-script.py "${TEST_FILE}"
274
275 ansible-vault decrypt "$@" --vault-password-file password-script.py "${TEST_FILE}"
276
277 # encrypt it, with password from password script
278 ansible-vault encrypt "$@" --vault-id [email protected] "${TEST_FILE}"
279
280 ansible-vault view "$@" --vault-id [email protected] "${TEST_FILE}"
281
282 ansible-vault decrypt "$@" --vault-id [email protected] "${TEST_FILE}"
283
284 # new password file for rekeyed file
285 NEW_VAULT_PASSWORD="${MYTMPDIR}/new-vault-password"
286 echo "newpassword" > "${NEW_VAULT_PASSWORD}"
287
288 ansible-vault encrypt "$@" --vault-password-file vault-password "${TEST_FILE}"
289
290 ansible-vault rekey "$@" --vault-password-file vault-password --new-vault-password-file "${NEW_VAULT_PASSWORD}" "${TEST_FILE}"
291
292 # --new-vault-password-file and --new-vault-id should cause options error
293 ansible-vault rekey "$@" --vault-password-file vault-password --new-vault-id=foobar --new-vault-password-file "${NEW_VAULT_PASSWORD}" "${TEST_FILE}" && :
294 WRONG_RC=$?
295 echo "rc was $WRONG_RC (2 is expected)"
296 [ $WRONG_RC -eq 2 ]
297
298 ansible-vault view "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" "${TEST_FILE}"
299
300 # view file with unicode in filename
301 ansible-vault view "$@" --vault-password-file vault-password vault-café.yml
302
303 # view with old password file and new password file
304 ansible-vault view "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" --vault-password-file vault-password "${TEST_FILE}"
305
306 # view with old password file and new password file, different order
307 ansible-vault view "$@" --vault-password-file vault-password --vault-password-file "${NEW_VAULT_PASSWORD}" "${TEST_FILE}"
308
309 # view with old password file and new password file and another wrong
310 ansible-vault view "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" --vault-password-file vault-password-wrong --vault-password-file vault-password "${TEST_FILE}"
311
312 # view with old password file and new password file and another wrong, using --vault-id
313 ansible-vault view "$@" --vault-id "tmp_new_password@${NEW_VAULT_PASSWORD}" --vault-id wrong_password@vault-password-wrong --vault-id myorg@vault-password "${TEST_FILE}"
314
315 ansible-vault decrypt "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" "${TEST_FILE}"
316
317 # reading/writing to/from stdin/stdin (See https://github.com/ansible/ansible/issues/23567)
318 ansible-vault encrypt "$@" --vault-password-file "${VAULT_PASSWORD_FILE}" --output="${TEST_FILE_OUTPUT}" < "${TEST_FILE}"
319 OUTPUT=$(ansible-vault decrypt "$@" --vault-password-file "${VAULT_PASSWORD_FILE}" --output=- < "${TEST_FILE_OUTPUT}")
320 echo "${OUTPUT}" | grep 'This is a test file'
321
322 OUTPUT_DASH=$(ansible-vault decrypt "$@" --vault-password-file "${VAULT_PASSWORD_FILE}" --output=- "${TEST_FILE_OUTPUT}")
323 echo "${OUTPUT_DASH}" | grep 'This is a test file'
324
325 OUTPUT_DASH_SPACE=$(ansible-vault decrypt "$@" --vault-password-file "${VAULT_PASSWORD_FILE}" --output - "${TEST_FILE_OUTPUT}")
326 echo "${OUTPUT_DASH_SPACE}" | grep 'This is a test file'
327
328
329 # test using an empty vault password file
330 ansible-vault view "$@" --vault-password-file empty-password format_1_1_AES256.yml && :
331 WRONG_RC=$?
332 echo "rc was $WRONG_RC (1 is expected)"
333 [ $WRONG_RC -eq 1 ]
334
335 ansible-vault view "$@" --vault-id=empty@empty-password --vault-password-file empty-password format_1_1_AES256.yml && :
336 WRONG_RC=$?
337 echo "rc was $WRONG_RC (1 is expected)"
338 [ $WRONG_RC -eq 1 ]
339
340 echo 'foo' > some_file.txt
341 ansible-vault encrypt "$@" --vault-password-file empty-password some_file.txt && :
342 WRONG_RC=$?
343 echo "rc was $WRONG_RC (1 is expected)"
344 [ $WRONG_RC -eq 1 ]
345
346
347 ansible-vault encrypt_string "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" "a test string"
348
349 # Test with multiple vault password files
350 # https://github.com/ansible/ansible/issues/57172
351 env ANSIBLE_VAULT_PASSWORD_FILE=vault-password ansible-vault encrypt_string "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" --encrypt-vault-id default "a test string"
352
353 ansible-vault encrypt_string "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" --name "blippy" "a test string names blippy"
354
355 ansible-vault encrypt_string "$@" --vault-id "${NEW_VAULT_PASSWORD}" "a test string"
356
357 ansible-vault encrypt_string "$@" --vault-id "${NEW_VAULT_PASSWORD}" --name "blippy" "a test string names blippy"
358
359
360 # from stdin
361 ansible-vault encrypt_string "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" < "${TEST_FILE}"
362
363 ansible-vault encrypt_string "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" --stdin-name "the_var_from_stdin" < "${TEST_FILE}"
364
365 # write to file
366 ansible-vault encrypt_string "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" --name "blippy" "a test string names blippy" --output "${MYTMPDIR}/enc_string_test_file"
367
368 [ -f "${MYTMPDIR}/enc_string_test_file" ];
369
370 # test ansible-vault edit with a faux editor
371 ansible-vault encrypt "$@" --vault-password-file vault-password "${TEST_FILE_EDIT}"
372
373 # edit a 1.1 format with no vault-id, should stay 1.1
374 EDITOR=./faux-editor.py ansible-vault edit "$@" --vault-password-file vault-password "${TEST_FILE_EDIT}"
375 head -1 "${TEST_FILE_EDIT}" | grep "${FORMAT_1_1_HEADER}"
376
377 # edit a 1.1 format with vault-id, should stay 1.1
378 cat "${TEST_FILE_EDIT}"
379 EDITOR=./faux-editor.py ansible-vault edit "$@" --vault-id vault_password@vault-password "${TEST_FILE_EDIT}"
380 cat "${TEST_FILE_EDIT}"
381 head -1 "${TEST_FILE_EDIT}" | grep "${FORMAT_1_1_HEADER}"
382
383 ansible-vault encrypt "$@" --vault-id vault_password@vault-password "${TEST_FILE_EDIT2}"
384
385 # verify that we aren't prompted for a new vault password on edit if we are running interactively (ie, with prompts)
386 # have to use setsid and --ask-vault-pass to force a prompt to simulate.
387 # See https://github.com/ansible/ansible/issues/35834
388 setsid sh -c 'tty; echo password |ansible-vault edit --ask-vault-pass vault_test.yml' < /dev/null > log 2>&1 && :
389 grep 'New Vault password' log && :
390 WRONG_RC=$?
391 echo "The stdout log had 'New Vault password' in it and it is not supposed to. rc of grep was $WRONG_RC (1 is expected)"
392 [ $WRONG_RC -eq 1 ]
393
394 # edit a 1.2 format with vault id, should keep vault id and 1.2 format
395 EDITOR=./faux-editor.py ansible-vault edit "$@" --vault-id vault_password@vault-password "${TEST_FILE_EDIT2}"
396 head -1 "${TEST_FILE_EDIT2}" | grep "${FORMAT_1_2_HEADER};vault_password"
397
398 # edit a 1.2 file with no vault-id, should keep vault id and 1.2 format
399 EDITOR=./faux-editor.py ansible-vault edit "$@" --vault-password-file vault-password "${TEST_FILE_EDIT2}"
400 head -1 "${TEST_FILE_EDIT2}" | grep "${FORMAT_1_2_HEADER};vault_password"
401
402 # verify an aborted edit reports an error and does not corrupt the vault file
403 ORIGINAL_FILE_EDIT="${TEST_FILE_EDIT}.original"
404
405 cp "${TEST_FILE_EDIT}" "${ORIGINAL_FILE_EDIT}"
406
407 if EDITOR=./fail-editor.py ansible-vault edit "$@" --vault-password-file vault-password "${TEST_FILE_EDIT}"; then
408 echo "ansible-vault did not fail after an edit was aborted"
409 exit 1
410 fi
411
412 diff "${TEST_FILE_EDIT}" "${ORIGINAL_FILE_EDIT}" >/dev/null || (echo "vault file corrupted after failed edit"; exit 1)
413
414 # encrypt with a password from a vault encrypted password file and multiple vault-ids
415 # should fail because we dont know which vault id to use to encrypt with
416 ansible-vault encrypt "$@" --vault-id vault-password --vault-id encrypted-vault-password "${TEST_FILE_ENC_PASSWORD}" && :
417 WRONG_RC=$?
418 echo "rc was $WRONG_RC (5 is expected)"
419 [ $WRONG_RC -eq 5 ]
420
421
422 # encrypt with a password from a vault encrypted password file and multiple vault-ids
423 # but this time specify with --encrypt-vault-id, but specifying vault-id names (instead of default)
424 # ansible-vault encrypt "$@" --vault-id from_vault_password@vault-password --vault-id from_encrypted_vault_password@encrypted-vault-password --encrypt-vault-id from_encrypted_vault_password "${TEST_FILE(_ENC_PASSWORD}"
425
426 # try to view the file encrypted with the vault-password we didn't specify
427 # to verify we didn't choose the wrong vault-id
428 # ansible-vault view "$@" --vault-id vault-password "${TEST_FILE_ENC_PASSWORD}" && :
429 # WRONG_RC=$?
430 # echo "rc was $WRONG_RC (1 is expected)"
431 # [ $WRONG_RC -eq 1 ]
432
433 ansible-vault encrypt "$@" --vault-id vault-password "${TEST_FILE_ENC_PASSWORD}"
434
435 # view the file encrypted with a password from a vault encrypted password file
436 ansible-vault view "$@" --vault-id vault-password --vault-id encrypted-vault-password "${TEST_FILE_ENC_PASSWORD}"
437
438 # try to view the file encrypted with a password from a vault encrypted password file but without the password to the password file.
439 # This should fail with an
440 ansible-vault view "$@" --vault-id encrypted-vault-password "${TEST_FILE_ENC_PASSWORD}" && :
441 WRONG_RC=$?
442 echo "rc was $WRONG_RC (1 is expected)"
443 [ $WRONG_RC -eq 1 ]
444
445
446 # test playbooks using vaulted files
447 ansible-playbook test_vault.yml -i ../../inventory -v "$@" --vault-password-file vault-password --list-tasks
448 ansible-playbook test_vault.yml -i ../../inventory -v "$@" --vault-password-file vault-password --list-hosts
449 ansible-playbook test_vault.yml -i ../../inventory -v "$@" --vault-password-file vault-password --syntax-check
450 ansible-playbook test_vault.yml -i ../../inventory -v "$@" --vault-password-file vault-password
451 ansible-playbook test_vault_embedded.yml -i ../../inventory -v "$@" --vault-password-file vault-password --syntax-check
452 ansible-playbook test_vault_embedded.yml -i ../../inventory -v "$@" --vault-password-file vault-password
453 ansible-playbook test_vaulted_inventory.yml -i vaulted.inventory -v "$@" --vault-password-file vault-password
454 ansible-playbook test_vaulted_template.yml -i ../../inventory -v "$@" --vault-password-file vault-password
455
456 # test using --vault-pass-file option
457 ansible-playbook test_vault.yml -i ../../inventory -v "$@" --vault-pass-file vault-password
458
459 # install TOML for parse toml inventory
460 # test playbooks using vaulted files(toml)
461 pip install tomli
462 ansible-vault encrypt ./inventory.toml -v "$@" --vault-password-file=./vault-password
463 ansible-playbook test_vaulted_inventory_toml.yml -i ./inventory.toml -v "$@" --vault-password-file vault-password
464 ansible-vault decrypt ./inventory.toml -v "$@" --vault-password-file=./vault-password
465
466 # test a playbook with a host_var whose value is non-ascii utf8 (see https://github.com/ansible/ansible/issues/37258)
467 ansible-playbook -i ../../inventory -v "$@" --vault-id vault-password test_vaulted_utf8_value.yml
468
469 # test with password from password script
470 ansible-playbook test_vault.yml -i ../../inventory -v "$@" --vault-password-file password-script.py
471 ansible-playbook test_vault_embedded.yml -i ../../inventory -v "$@" --vault-password-file password-script.py
472
473 # with multiple password files
474 ansible-playbook test_vault.yml -i ../../inventory -v "$@" --vault-password-file vault-password --vault-password-file vault-password-wrong
475 ansible-playbook test_vault.yml -i ../../inventory -v "$@" --vault-password-file vault-password-wrong --vault-password-file vault-password
476
477 ansible-playbook test_vault_embedded.yml -i ../../inventory -v "$@" --vault-password-file vault-password --vault-password-file vault-password-wrong --syntax-check
478 ansible-playbook test_vault_embedded.yml -i ../../inventory -v "$@" --vault-password-file vault-password-wrong --vault-password-file vault-password
479
480 # test with a default vault password file set in config
481 ANSIBLE_VAULT_PASSWORD_FILE=vault-password ansible-playbook test_vault_embedded.yml -i ../../inventory -v "$@" --vault-password-file vault-password-wrong
482
483 # test using vault_identity_list config
484 ANSIBLE_VAULT_IDENTITY_LIST='wrong-password@vault-password-wrong,default@vault-password' ansible-playbook test_vault.yml -i ../../inventory -v "$@"
485
486 # test that we can have a vault encrypted yaml file that includes embedded vault vars
487 # that were encrypted with a different vault secret
488 ansible-playbook test_vault_file_encrypted_embedded.yml -i ../../inventory "$@" --vault-id encrypted_file_encrypted_var_password --vault-id vault-password
489
490 # with multiple password files, --vault-id, ordering
491 ansible-playbook test_vault.yml -i ../../inventory -v "$@" --vault-id vault-password --vault-id vault-password-wrong
492 ansible-playbook test_vault.yml -i ../../inventory -v "$@" --vault-id vault-password-wrong --vault-id vault-password
493
494 ansible-playbook test_vault_embedded.yml -i ../../inventory -v "$@" --vault-id vault-password --vault-id vault-password-wrong --syntax-check
495 ansible-playbook test_vault_embedded.yml -i ../../inventory -v "$@" --vault-id vault-password-wrong --vault-id vault-password
496
497 # test with multiple password files, including a script, and a wrong password
498 ansible-playbook test_vault_embedded.yml -i ../../inventory -v "$@" --vault-password-file vault-password-wrong --vault-password-file password-script.py --vault-password-file vault-password
499
500 # test with multiple password files, including a script, and a wrong password, and a mix of --vault-id and --vault-password-file
501 ansible-playbook test_vault_embedded.yml -i ../../inventory -v "$@" --vault-password-file vault-password-wrong --vault-id password-script.py --vault-id vault-password
502
503 # test with multiple password files, including a script, and a wrong password, and a mix of --vault-id and --vault-password-file
504 ansible-playbook test_vault_embedded_ids.yml -i ../../inventory -v "$@" \
505 --vault-password-file vault-password-wrong \
506 --vault-id password-script.py --vault-id example1@example1_password \
507 --vault-id example2@example2_password --vault-password-file example3_password \
508 --vault-id vault-password
509
510 # with wrong password
511 ansible-playbook test_vault.yml -i ../../inventory -v "$@" --vault-password-file vault-password-wrong && :
512 WRONG_RC=$?
513 echo "rc was $WRONG_RC (1 is expected)"
514 [ $WRONG_RC -eq 1 ]
515
516 # with multiple wrong passwords
517 ansible-playbook test_vault.yml -i ../../inventory -v "$@" --vault-password-file vault-password-wrong --vault-password-file vault-password-wrong && :
518 WRONG_RC=$?
519 echo "rc was $WRONG_RC (1 is expected)"
520 [ $WRONG_RC -eq 1 ]
521
522 # with wrong password, --vault-id
523 ansible-playbook test_vault.yml -i ../../inventory -v "$@" --vault-id vault-password-wrong && :
524 WRONG_RC=$?
525 echo "rc was $WRONG_RC (1 is expected)"
526 [ $WRONG_RC -eq 1 ]
527
528 # with multiple wrong passwords with --vault-id
529 ansible-playbook test_vault.yml -i ../../inventory -v "$@" --vault-id vault-password-wrong --vault-id vault-password-wrong && :
530 WRONG_RC=$?
531 echo "rc was $WRONG_RC (1 is expected)"
532 [ $WRONG_RC -eq 1 ]
533
534 # with multiple wrong passwords with --vault-id
535 ansible-playbook test_vault.yml -i ../../inventory -v "$@" --vault-id wrong1@vault-password-wrong --vault-id wrong2@vault-password-wrong && :
536 WRONG_RC=$?
537 echo "rc was $WRONG_RC (1 is expected)"
538 [ $WRONG_RC -eq 1 ]
539
540 # with empty password file
541 ansible-playbook test_vault.yml -i ../../inventory -v "$@" --vault-id empty@empty-password && :
542 WRONG_RC=$?
543 echo "rc was $WRONG_RC (1 is expected)"
544 [ $WRONG_RC -eq 1 ]
545
546 # test invalid format ala https://github.com/ansible/ansible/issues/28038
547 EXPECTED_ERROR='Vault format unhexlify error: Non-hexadecimal digit found'
548 ansible-playbook "$@" -i invalid_format/inventory --vault-id invalid_format/vault-secret invalid_format/broken-host-vars-tasks.yml 2>&1 | grep "${EXPECTED_ERROR}"
549
550 EXPECTED_ERROR='Vault format unhexlify error: Odd-length string'
551 ansible-playbook "$@" -i invalid_format/inventory --vault-id invalid_format/vault-secret invalid_format/broken-group-vars-tasks.yml 2>&1 | grep "${EXPECTED_ERROR}"
552
553 # Run playbook with vault file with unicode in filename (https://github.com/ansible/ansible/issues/50316)
554 ansible-playbook -i ../../inventory -v "$@" --vault-password-file vault-password test_utf8_value_in_filename.yml
555
556 # Ensure we don't leave unencrypted temp files dangling
557 ansible-playbook -v "$@" --vault-password-file vault-password test_dangling_temp.yml
558
559 ansible-playbook "$@" --vault-password-file vault-password single_vault_as_string.yml
560
561 # Test that only one accessible vault password is required
562 export ANSIBLE_VAULT_IDENTITY_LIST="id1@./nonexistent, id2@${MYTMPDIR}/unreadable, id3@./vault-password"
563
564 touch "${MYTMPDIR}/unreadable"
565 sudo chmod 000 "${MYTMPDIR}/unreadable"
566
567 ansible-vault encrypt_string content
568 ansible-vault encrypt_string content --encrypt-vault-id id3
569
570 # Try to use a missing vault password file
571 if ansible-vault encrypt_string content --encrypt-vault-id id1 > out.txt 2>&1; then
572 echo "command did not fail"
573 exit 1
574 fi
575 grep out.txt -e '\[WARNING\]: Error getting vault password file (id1)'
576 grep out.txt -e "\[ERROR\]: Did not find a match for --encrypt-vault-id=id1 in the known vault-ids \['id3'\]"
577
578 # Try to use an inaccessible vault password file
579 if ansible-vault encrypt_string content --encrypt-vault-id id2 > out.txt 2>&1; then
580 echo "command did not fail"
581 exit 1
582 fi
583 grep out.txt -e "\[WARNING\]: Error in vault password file loading (id2)"
584 grep out.txt -e "\[ERROR\]: Did not find a match for --encrypt-vault-id=id2 in the known vault-ids \['id3'\]"
585
586 unset ANSIBLE_VAULT_IDENTITY_LIST
587
588 # 'real script'
589 ansible-playbook realpath.yml "$@" --vault-password-file script/vault-secret.sh
590
591 # using symlink
592 ansible-playbook symlink.yml "$@" --vault-password-file symlink/get-password-symlink
593
594 ### NEGATIVE TESTS
595
596 ER='Attempting to decrypt but no vault secrets found'
597 #### no secrets
598 # 'real script'
599 ansible-playbook realpath.yml "$@" 2>&1 |grep "${ER}"
600
601 # using symlink
602 ansible-playbook symlink.yml "$@" 2>&1 |grep "${ER}"
603
604 ER='no vault secrets were found that could decrypt'
605 ### wrong secrets
606 # 'real script'
607 ansible-playbook realpath.yml "$@" --vault-password-file symlink/get-password-symlink 2>&1 |grep "${ER}"
608
609 # using symlink
610 ansible-playbook symlink.yml "$@" --vault-password-file script/vault-secret.sh 2>&1 |grep "${ER}"
611
612 ### SALT TESTING ###
613 # prep files for encryption
614 for salted in test1 test2 test3
615 do
616 echo 'this is salty' > "salted_${salted}"
617 done
618
619 # encrypt files
620 ANSIBLE_VAULT_ENCRYPT_SALT=salty ansible-vault encrypt salted_test1 --vault-password-file example1_password "$@"
621 ANSIBLE_VAULT_ENCRYPT_SALT=salty ansible-vault encrypt salted_test2 --vault-password-file example1_password "$@"
622 ansible-vault encrypt salted_test3 --vault-password-file example1_password "$@"
623
624 # should be the same
625 out=$(diff salted_test1 salted_test2)
626 [ "${out}" == "" ]
627
628 # should be diff
629 out=$(diff salted_test1 salted_test3 || true)
630 [ "${out}" != "" ]