mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-24 05:40:23 -07:00
Support multiple vault passwords (#22756)
Fixes #13243 ** Add --vault-id to name/identify multiple vault passwords Use --vault-id to indicate id and path/type --vault-id=prompt # prompt for default vault id password --vault-id=myorg@prompt # prompt for a vault_id named 'myorg' --vault-id=a_password_file # load ./a_password_file for default id --vault-id=myorg@a_password_file # load file for 'myorg' vault id vault_id's are created implicitly for existing --vault-password-file and --ask-vault-pass options. Vault ids are just for UX purposes and bookkeeping. Only the vault payload and the password bytestring is needed to decrypt a vault blob. Replace passing password around everywhere with a VaultSecrets object. If we specify a vault_id, mention that in password prompts Specifying multiple -vault-password-files will now try each until one works ** Rev vault format in a backwards compatible way The 1.2 vault format adds the vault_id to the header line of the vault text. This is backwards compatible with older versions of ansible. Old versions will just ignore it and treat it as the default (and only) vault id. Note: only 2.4+ supports multiple vault passwords, so while earlier ansible versions can read the vault-1.2 format, it does not make them magically support multiple vault passwords. use 1.1 format for 'default' vault_id Vaulted items that need to include a vault_id will be written in 1.2 format. If we set a new DEFAULT_VAULT_IDENTITY, then the default will use version 1.2 vault will only use a vault_id if one is specified. So if none is specified and C.DEFAULT_VAULT_IDENTITY is 'default' we use the old format. ** Changes/refactors needed to implement multiple vault passwords raise exceptions on decrypt fail, check vault id early split out parsing the vault plaintext envelope (with the sha/original plaintext) to _split_plaintext_envelope() some cli fixups for specifying multiple paths in the unfrack_paths optparse callback fix py3 dict.keys() 'dict_keys object is not indexable' error pluralize cli.options.vault_password_file -> vault_password_files pluralize cli.options.new_vault_password_file -> new_vault_password_files pluralize cli.options.vault_id -> cli.options.vault_ids ** Add a config option (vault_id_match) to force vault id matching. With 'vault_id_match=True' and an ansible vault that provides a vault_id, then decryption will require that a matching vault_id is required. (via --vault-id=my_vault_id@password_file, for ex). In other words, if the config option is true, then only the vault secrets with matching vault ids are candidates for decrypting a vault. If option is false (the default), then all of the provided vault secrets will be selected. If a user doesn't want all vault secrets to be tried to decrypt any vault content, they can enable this option. Note: The vault id used for the match is not encrypted or cryptographically signed. It is just a label/id/nickname used for referencing a specific vault secret.
This commit is contained in:
parent
a328e96455
commit
934b645191
34 changed files with 1922 additions and 345 deletions
|
@ -30,8 +30,11 @@ from ansible.compat.tests.mock import patch
|
|||
|
||||
from ansible import errors
|
||||
from ansible.parsing import vault
|
||||
from ansible.parsing.vault import VaultLib, VaultEditor, match_encrypt_secret
|
||||
|
||||
from ansible.module_utils._text import to_bytes, to_text
|
||||
|
||||
from units.mock.vault_helper import TextVaultSecret
|
||||
|
||||
v10_data = """$ANSIBLE_VAULT;1.0;AES
|
||||
53616c7465645f5fd0026926a2d415a28a2622116273fbc90e377225c12a347e1daf4456d36a77f9
|
||||
|
@ -52,6 +55,14 @@ class TestVaultEditor(unittest.TestCase):
|
|||
|
||||
def setUp(self):
|
||||
self._test_dir = None
|
||||
self.vault_password = "test-vault-password"
|
||||
vault_secret = TextVaultSecret(self.vault_password)
|
||||
self.vault_secrets = [('vault_secret', vault_secret),
|
||||
('default', vault_secret)]
|
||||
|
||||
@property
|
||||
def vault_secret(self):
|
||||
return match_encrypt_secret(self.vault_secrets)[1]
|
||||
|
||||
def tearDown(self):
|
||||
if self._test_dir:
|
||||
|
@ -59,6 +70,11 @@ class TestVaultEditor(unittest.TestCase):
|
|||
# shutil.rmtree(self._test_dir)
|
||||
self._test_dir = None
|
||||
|
||||
def _secrets(self, password):
|
||||
vault_secret = TextVaultSecret(password)
|
||||
vault_secrets = [('default', vault_secret)]
|
||||
return vault_secrets
|
||||
|
||||
def test_methods_exist(self):
|
||||
v = vault.VaultEditor(None)
|
||||
slots = ['create_file',
|
||||
|
@ -83,6 +99,11 @@ class TestVaultEditor(unittest.TestCase):
|
|||
opened_file.close()
|
||||
return file_path
|
||||
|
||||
def _vault_editor(self, vault_secrets=None):
|
||||
if vault_secrets is None:
|
||||
vault_secrets = self._secrets(self.vault_password)
|
||||
return VaultEditor(VaultLib(vault_secrets))
|
||||
|
||||
@patch('ansible.parsing.vault.call')
|
||||
def test_edit_file_helper_empty_target(self, mock_sp_call):
|
||||
self._test_dir = self._create_test_dir()
|
||||
|
@ -91,9 +112,9 @@ class TestVaultEditor(unittest.TestCase):
|
|||
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_contents)
|
||||
|
||||
mock_sp_call.side_effect = self._faux_command
|
||||
ve = vault.VaultEditor('password')
|
||||
ve = self._vault_editor()
|
||||
|
||||
b_ciphertext = ve._edit_file_helper(src_file_path)
|
||||
b_ciphertext = ve._edit_file_helper(src_file_path, self.vault_secret)
|
||||
|
||||
self.assertNotEqual(src_contents, b_ciphertext)
|
||||
|
||||
|
@ -107,12 +128,13 @@ class TestVaultEditor(unittest.TestCase):
|
|||
error_txt = 'calling editor raised an exception'
|
||||
mock_sp_call.side_effect = errors.AnsibleError(error_txt)
|
||||
|
||||
ve = vault.VaultEditor('password')
|
||||
ve = self._vault_editor()
|
||||
|
||||
self.assertRaisesRegexp(errors.AnsibleError,
|
||||
error_txt,
|
||||
ve._edit_file_helper,
|
||||
src_file_path)
|
||||
src_file_path,
|
||||
self.vault_secret)
|
||||
|
||||
@patch('ansible.parsing.vault.call')
|
||||
def test_edit_file_helper_symlink_target(self, mock_sp_call):
|
||||
|
@ -126,9 +148,9 @@ class TestVaultEditor(unittest.TestCase):
|
|||
os.symlink(src_file_path, src_file_link_path)
|
||||
|
||||
mock_sp_call.side_effect = self._faux_command
|
||||
ve = vault.VaultEditor('password')
|
||||
ve = self._vault_editor()
|
||||
|
||||
b_ciphertext = ve._edit_file_helper(src_file_link_path)
|
||||
b_ciphertext = ve._edit_file_helper(src_file_link_path, self.vault_secret)
|
||||
|
||||
self.assertNotEqual(src_file_contents, b_ciphertext,
|
||||
'b_ciphertext should be encrypted and not equal to src_contents')
|
||||
|
@ -160,9 +182,9 @@ class TestVaultEditor(unittest.TestCase):
|
|||
self._faux_editor(editor_args, src_file_contents)
|
||||
|
||||
mock_sp_call.side_effect = faux_editor
|
||||
ve = vault.VaultEditor('password')
|
||||
ve = self._vault_editor()
|
||||
|
||||
ve._edit_file_helper(src_file_path, existing_data=src_file_contents)
|
||||
ve._edit_file_helper(src_file_path, self.vault_secret, existing_data=src_file_contents)
|
||||
|
||||
new_target_file = open(src_file_path, 'rb')
|
||||
new_target_file_contents = new_target_file.read()
|
||||
|
@ -193,13 +215,17 @@ class TestVaultEditor(unittest.TestCase):
|
|||
src_file_contents = to_bytes("some info in a file\nyup.")
|
||||
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_file_contents)
|
||||
|
||||
ve = vault.VaultEditor('password')
|
||||
ve.encrypt_file(src_file_path)
|
||||
ve = self._vault_editor()
|
||||
ve.encrypt_file(src_file_path, self.vault_secret)
|
||||
|
||||
# FIXME: update to just set self._secrets or just a new vault secret id
|
||||
new_password = 'password2:electricbugaloo'
|
||||
ve.rekey_file(src_file_path, new_password)
|
||||
new_vault_secret = TextVaultSecret(new_password)
|
||||
new_vault_secrets = [('default', new_vault_secret)]
|
||||
ve.rekey_file(src_file_path, vault.match_encrypt_secret(new_vault_secrets)[1])
|
||||
|
||||
new_ve = vault.VaultEditor(new_password)
|
||||
# FIXME: can just update self._secrets here
|
||||
new_ve = vault.VaultEditor(VaultLib(new_vault_secrets))
|
||||
self._assert_file_is_encrypted(new_ve, src_file_path, src_file_contents)
|
||||
|
||||
def test_rekey_file_no_new_password(self):
|
||||
|
@ -208,8 +234,8 @@ class TestVaultEditor(unittest.TestCase):
|
|||
src_file_contents = to_bytes("some info in a file\nyup.")
|
||||
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_file_contents)
|
||||
|
||||
ve = vault.VaultEditor('password')
|
||||
ve.encrypt_file(src_file_path)
|
||||
ve = self._vault_editor()
|
||||
ve.encrypt_file(src_file_path, self.vault_secret)
|
||||
|
||||
self.assertRaisesRegexp(errors.AnsibleError,
|
||||
'The value for the new_password to rekey',
|
||||
|
@ -223,7 +249,7 @@ class TestVaultEditor(unittest.TestCase):
|
|||
src_file_contents = to_bytes("some info in a file\nyup.")
|
||||
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_file_contents)
|
||||
|
||||
ve = vault.VaultEditor('password')
|
||||
ve = self._vault_editor()
|
||||
|
||||
new_password = 'password2:electricbugaloo'
|
||||
self.assertRaisesRegexp(errors.AnsibleError,
|
||||
|
@ -237,11 +263,11 @@ class TestVaultEditor(unittest.TestCase):
|
|||
src_file_contents = to_bytes("some info in a file\nyup.")
|
||||
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_file_contents)
|
||||
|
||||
ve = vault.VaultEditor('password')
|
||||
ve.encrypt_file(src_file_path)
|
||||
ve = self._vault_editor()
|
||||
ve.encrypt_file(src_file_path, self.vault_secret)
|
||||
|
||||
res = ve.plaintext(src_file_path)
|
||||
self.assertEquals(src_file_contents, res)
|
||||
self.assertEqual(src_file_contents, res)
|
||||
|
||||
def test_plaintext_not_encrypted(self):
|
||||
self._test_dir = self._create_test_dir()
|
||||
|
@ -249,7 +275,7 @@ class TestVaultEditor(unittest.TestCase):
|
|||
src_file_contents = to_bytes("some info in a file\nyup.")
|
||||
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_file_contents)
|
||||
|
||||
ve = vault.VaultEditor('password')
|
||||
ve = self._vault_editor()
|
||||
self.assertRaisesRegexp(errors.AnsibleError,
|
||||
'input is not vault encrypted data',
|
||||
ve.plaintext,
|
||||
|
@ -260,8 +286,8 @@ class TestVaultEditor(unittest.TestCase):
|
|||
src_file_contents = to_bytes("some info in a file\nyup.")
|
||||
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_file_contents)
|
||||
|
||||
ve = vault.VaultEditor('password')
|
||||
ve.encrypt_file(src_file_path)
|
||||
ve = self._vault_editor()
|
||||
ve.encrypt_file(src_file_path, self.vault_secret)
|
||||
|
||||
self._assert_file_is_encrypted(ve, src_file_path, src_file_contents)
|
||||
|
||||
|
@ -274,8 +300,8 @@ class TestVaultEditor(unittest.TestCase):
|
|||
src_file_link_path = os.path.join(self._test_dir, 'a_link_to_dest_file')
|
||||
os.symlink(src_file_path, src_file_link_path)
|
||||
|
||||
ve = vault.VaultEditor('password')
|
||||
ve.encrypt_file(src_file_link_path)
|
||||
ve = self._vault_editor()
|
||||
ve.encrypt_file(src_file_link_path, self.vault_secret)
|
||||
|
||||
self._assert_file_is_encrypted(ve, src_file_path, src_file_contents)
|
||||
self._assert_file_is_encrypted(ve, src_file_link_path, src_file_contents)
|
||||
|
@ -296,9 +322,9 @@ class TestVaultEditor(unittest.TestCase):
|
|||
|
||||
mock_sp_call.side_effect = faux_editor
|
||||
|
||||
ve = vault.VaultEditor('password')
|
||||
ve = self._vault_editor()
|
||||
|
||||
ve.encrypt_file(src_file_path)
|
||||
ve.encrypt_file(src_file_path, self.vault_secret)
|
||||
ve.edit_file(src_file_path)
|
||||
|
||||
new_src_file = open(src_file_path, 'rb')
|
||||
|
@ -308,7 +334,6 @@ class TestVaultEditor(unittest.TestCase):
|
|||
self.assertEqual(src_file_plaintext, new_src_contents)
|
||||
|
||||
new_stat = os.stat(src_file_path)
|
||||
print(new_stat)
|
||||
|
||||
@patch('ansible.parsing.vault.call')
|
||||
def test_edit_file_symlink(self, mock_sp_call):
|
||||
|
@ -324,9 +349,9 @@ class TestVaultEditor(unittest.TestCase):
|
|||
|
||||
mock_sp_call.side_effect = faux_editor
|
||||
|
||||
ve = vault.VaultEditor('password')
|
||||
ve = self._vault_editor()
|
||||
|
||||
ve.encrypt_file(src_file_path)
|
||||
ve.encrypt_file(src_file_path, self.vault_secret)
|
||||
|
||||
src_file_link_path = os.path.join(self._test_dir, 'a_link_to_dest_file')
|
||||
|
||||
|
@ -360,7 +385,7 @@ class TestVaultEditor(unittest.TestCase):
|
|||
|
||||
mock_sp_call.side_effect = faux_editor
|
||||
|
||||
ve = vault.VaultEditor('password')
|
||||
ve = self._vault_editor()
|
||||
self.assertRaisesRegexp(errors.AnsibleError,
|
||||
'input is not vault encrypted data',
|
||||
ve.edit_file,
|
||||
|
@ -371,18 +396,19 @@ class TestVaultEditor(unittest.TestCase):
|
|||
src_contents = to_bytes("some info in a file\nyup.")
|
||||
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_contents)
|
||||
|
||||
ve = vault.VaultEditor('password')
|
||||
ve = self._vault_editor()
|
||||
self.assertRaisesRegexp(errors.AnsibleError,
|
||||
'please use .edit. instead',
|
||||
ve.create_file,
|
||||
src_file_path)
|
||||
src_file_path,
|
||||
self.vault_secret)
|
||||
|
||||
def test_decrypt_file_exception(self):
|
||||
self._test_dir = self._create_test_dir()
|
||||
src_contents = to_bytes("some info in a file\nyup.")
|
||||
src_file_path = self._create_file(self._test_dir, 'src_file', content=src_contents)
|
||||
|
||||
ve = vault.VaultEditor('password')
|
||||
ve = self._vault_editor()
|
||||
self.assertRaisesRegexp(errors.AnsibleError,
|
||||
'input is not vault encrypted data',
|
||||
ve.decrypt_file,
|
||||
|
@ -398,8 +424,9 @@ class TestVaultEditor(unittest.TestCase):
|
|||
tmp_file = tempfile.NamedTemporaryFile()
|
||||
os.unlink(tmp_file.name)
|
||||
|
||||
ve = vault.VaultEditor("ansible")
|
||||
ve.create_file(tmp_file.name)
|
||||
_secrets = self._secrets('ansible')
|
||||
ve = self._vault_editor(_secrets)
|
||||
ve.create_file(tmp_file.name, vault.match_encrypt_secret(_secrets)[1])
|
||||
|
||||
self.assertTrue(os.path.exists(tmp_file.name))
|
||||
|
||||
|
@ -409,7 +436,7 @@ class TestVaultEditor(unittest.TestCase):
|
|||
with v10_file as f:
|
||||
f.write(to_bytes(v10_data))
|
||||
|
||||
ve = vault.VaultEditor("ansible")
|
||||
ve = self._vault_editor(self._secrets("ansible"))
|
||||
|
||||
# make sure the password functions for the cipher
|
||||
error_hit = False
|
||||
|
@ -417,6 +444,7 @@ class TestVaultEditor(unittest.TestCase):
|
|||
ve.decrypt_file(v10_file.name)
|
||||
except errors.AnsibleError:
|
||||
error_hit = True
|
||||
raise
|
||||
|
||||
# verify decrypted content
|
||||
f = open(v10_file.name, "rb")
|
||||
|
@ -426,7 +454,7 @@ class TestVaultEditor(unittest.TestCase):
|
|||
os.unlink(v10_file.name)
|
||||
|
||||
assert error_hit is False, "error decrypting 1.0 file"
|
||||
self.assertEquals(fdata.strip(), "foo")
|
||||
self.assertEqual(fdata.strip(), "foo")
|
||||
assert fdata.strip() == "foo", "incorrect decryption of 1.0 file: %s" % fdata.strip()
|
||||
|
||||
def test_decrypt_1_1(self):
|
||||
|
@ -434,13 +462,14 @@ class TestVaultEditor(unittest.TestCase):
|
|||
with v11_file as f:
|
||||
f.write(to_bytes(v11_data))
|
||||
|
||||
ve = vault.VaultEditor("ansible")
|
||||
ve = self._vault_editor(self._secrets("ansible"))
|
||||
|
||||
# make sure the password functions for the cipher
|
||||
error_hit = False
|
||||
try:
|
||||
ve.decrypt_file(v11_file.name)
|
||||
except errors.AnsibleError:
|
||||
raise
|
||||
error_hit = True
|
||||
|
||||
# verify decrypted content
|
||||
|
@ -450,21 +479,23 @@ class TestVaultEditor(unittest.TestCase):
|
|||
|
||||
os.unlink(v11_file.name)
|
||||
|
||||
assert error_hit is False, "error decrypting 1.0 file"
|
||||
assert fdata.strip() == "foo", "incorrect decryption of 1.0 file: %s" % fdata.strip()
|
||||
assert error_hit is False, "error decrypting 1.1 file"
|
||||
assert fdata.strip() == "foo", "incorrect decryption of 1.1 file: %s" % fdata.strip()
|
||||
|
||||
def test_rekey_migration(self):
|
||||
v10_file = tempfile.NamedTemporaryFile(delete=False)
|
||||
with v10_file as f:
|
||||
f.write(to_bytes(v10_data))
|
||||
|
||||
ve = vault.VaultEditor("ansible")
|
||||
ve = self._vault_editor(self._secrets("ansible"))
|
||||
|
||||
# make sure the password functions for the cipher
|
||||
error_hit = False
|
||||
new_secrets = self._secrets("ansible2")
|
||||
try:
|
||||
ve.rekey_file(v10_file.name, 'ansible2')
|
||||
ve.rekey_file(v10_file.name, vault.match_encrypt_secret(new_secrets)[1])
|
||||
except errors.AnsibleError:
|
||||
raise
|
||||
error_hit = True
|
||||
|
||||
# verify decrypted content
|
||||
|
@ -475,30 +506,31 @@ class TestVaultEditor(unittest.TestCase):
|
|||
assert error_hit is False, "error rekeying 1.0 file to 1.1"
|
||||
|
||||
# ensure filedata can be decrypted, is 1.1 and is AES256
|
||||
vl = vault.VaultLib("ansible2")
|
||||
vl = VaultLib(new_secrets)
|
||||
dec_data = None
|
||||
error_hit = False
|
||||
try:
|
||||
dec_data = vl.decrypt(fdata)
|
||||
except errors.AnsibleError:
|
||||
raise
|
||||
error_hit = True
|
||||
|
||||
os.unlink(v10_file.name)
|
||||
|
||||
assert vl.cipher_name == "AES256", "wrong cipher name set after rekey: %s" % vl.cipher_name
|
||||
self.assertIn(b'AES256', fdata, 'AES256 was not found in vault file %s' % to_text(fdata))
|
||||
assert error_hit is False, "error decrypting migrated 1.0 file"
|
||||
assert dec_data.strip() == b"foo", "incorrect decryption of rekeyed/migrated file: %s" % dec_data
|
||||
|
||||
def test_real_path_dash(self):
|
||||
filename = '-'
|
||||
ve = vault.VaultEditor('password')
|
||||
ve = self._vault_editor()
|
||||
|
||||
res = ve._real_path(filename)
|
||||
self.assertEqual(res, '-')
|
||||
|
||||
def test_real_path_dev_null(self):
|
||||
filename = '/dev/null'
|
||||
ve = vault.VaultEditor('password')
|
||||
ve = self._vault_editor()
|
||||
|
||||
res = ve._real_path(filename)
|
||||
self.assertEqual(res, '/dev/null')
|
||||
|
@ -510,7 +542,7 @@ class TestVaultEditor(unittest.TestCase):
|
|||
|
||||
os.symlink(file_path, file_link_path)
|
||||
|
||||
ve = vault.VaultEditor('password')
|
||||
ve = self._vault_editor()
|
||||
|
||||
res = ve._real_path(file_link_path)
|
||||
self.assertEqual(res, file_path)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue