Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exhaust list of vault ids before failing #75540

Merged
merged 4 commits into from Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,2 @@
bugfixes:
- vault - Warn instead of fail for missing vault IDs if at least one valid vault secret is found.
23 changes: 18 additions & 5 deletions lib/ansible/cli/__init__.py
Expand Up @@ -175,6 +175,7 @@ def setup_vault_secrets(loader, vault_ids, vault_password_files=None,
create_new_password,
auto_prompt=auto_prompt)

last_exception = found_vault_secret = None
for vault_id_slug in vault_ids:
vault_id_name, vault_id_value = CLI.split_vault_id(vault_id_slug)
if vault_id_value in ['prompt', 'prompt_ask_vault_pass']:
Expand All @@ -198,6 +199,7 @@ def setup_vault_secrets(loader, vault_ids, vault_password_files=None,
display.warning('Error in vault password prompt (%s): %s' % (vault_id_name, exc))
raise

found_vault_secret = True
vault_secrets.append((built_vault_id, prompted_vault_secret))

# update loader with new secrets incrementally, so we can load a vault password
Expand All @@ -208,17 +210,23 @@ def setup_vault_secrets(loader, vault_ids, vault_password_files=None,
# assuming anything else is a password file
display.vvvvv('Reading vault password file: %s' % vault_id_value)
# read vault_pass from a file
file_vault_secret = get_file_vault_secret(filename=vault_id_value,
vault_id=vault_id_name,
loader=loader)
try:
file_vault_secret = get_file_vault_secret(filename=vault_id_value,
vault_id=vault_id_name,
loader=loader)
except AnsibleError as exc:
display.warning('Error getting vault password file (%s): %s' % (vault_id_name, to_text(exc)))
last_exception = exc
continue

# an invalid password file will error globally
try:
s-hertel marked this conversation as resolved.
Show resolved Hide resolved
file_vault_secret.load()
except AnsibleError as exc:
display.warning('Error in vault password file loading (%s): %s' % (vault_id_name, to_text(exc)))
raise
last_exception = exc
continue

found_vault_secret = True
if vault_id_name:
vault_secrets.append((vault_id_name, file_vault_secret))
else:
Expand All @@ -227,6 +235,11 @@ def setup_vault_secrets(loader, vault_ids, vault_password_files=None,
# update loader with as-yet-known vault secrets
loader.set_vault_secrets(vault_secrets)

# An invalid or missing password file will error globally
# if no valid vault secret was found.
if last_exception and not found_vault_secret:
raise last_exception

return vault_secrets

@staticmethod
Expand Down
27 changes: 26 additions & 1 deletion test/integration/targets/ansible-vault/runme.sh
Expand Up @@ -521,4 +521,29 @@ ansible-playbook -i ../../inventory -v "$@" --vault-password-file vault-password
# Ensure we don't leave unencrypted temp files dangling
ansible-playbook -v "$@" --vault-password-file vault-password test_dangling_temp.yml

ansible-playbook "$@" --vault-password-file vault-password single_vault_as_string.yml
ansible-playbook "$@" --vault-password-file vault-password single_vault_as_string.yml

# Test that only one accessible vault password is required
export ANSIBLE_VAULT_IDENTITY_LIST="id1@./nonexistent, id2@${MYTMPDIR}/unreadable, id3@./vault-password"

touch "${MYTMPDIR}/unreadable"
sudo chmod 000 "${MYTMPDIR}/unreadable"

ansible-vault encrypt_string content
ansible-vault encrypt_string content --encrypt-vault-id id3

set +e

# Try to use a missing vault password file
ansible-vault encrypt_string content --encrypt-vault-id id1 2>&1 | tee out.txt
test $? -ne 0
grep out.txt -e '[WARNING]: Error getting vault password file (id1)'
grep out.txt -e "ERROR! Did not find a match for --encrypt-vault-id=id2 in the known vault-ids ['id3']"

# Try to use an inaccessible vault password file
ansible-vault encrypt_string content --encrypt-vault-id id2 2>&1 | tee out.txt
test $? -ne 0
grep out.txt -e "[WARNING]: Error in vault password file loading (id2)"
grep out.txt -e "ERROR! Did not find a match for --encrypt-vault-id=id2 in the known vault-ids ['id3']"

set -e