def test_encrypt_decrypt_aes256_multiple_secrets_all_wrong(self): plaintext = u'Some text to encrypt in a café' b_vaulttext = self.v.encrypt(plaintext) vault_secrets = [('default', TextVaultSecret('another-wrong-password')), ('wrong-password', TextVaultSecret('wrong-password'))] v_multi = vault.VaultLib(vault_secrets) self.assertRaisesRegexp(errors.AnsibleError, '.*Decryption failed.*', v_multi.decrypt, b_vaulttext, filename='/dev/null/fake/filename')
def setUp(self): self.good_vault_password = "******" good_vault_secret = TextVaultSecret(self.good_vault_password) self.good_vault_secrets = [('good_vault_password', good_vault_secret)] self.good_vault = vault.VaultLib(self.good_vault_secrets) # TODO: make this use two vault secret identities instead of two vaultSecrets self.wrong_vault_password = '******' wrong_vault_secret = TextVaultSecret(self.wrong_vault_password) self.wrong_vault_secrets = [('wrong_vault_password', wrong_vault_secret)] self.wrong_vault = vault.VaultLib(self.wrong_vault_secrets) self.vault = self.good_vault self.vault_secrets = self.good_vault_secrets
def test_encrypt_decrypt_aes256_multiple_secrets_one_valid(self): plaintext = u'Some text to encrypt in a café' b_vaulttext = self.v.encrypt(plaintext) correct_secret = TextVaultSecret(self.vault_password) wrong_secret = TextVaultSecret('wrong-password') vault_secrets = [('default', wrong_secret), ('corect_secret', correct_secret), ('wrong_secret', wrong_secret)] v_multi = vault.VaultLib(vault_secrets) b_plaintext = v_multi.decrypt(b_vaulttext) self.assertNotEqual(b_vaulttext, to_bytes(plaintext), msg="encryption failed") self.assertEqual(b_plaintext, to_bytes(plaintext), msg="decryption failed")
def test_file_encrypted(self): vault_password = "******" text_secret = TextVaultSecret(vault_password) vault_secrets = [('foo', text_secret)] password = '******' # 'some password' encrypted with 'test-ansible-password' password_file_content = '''$ANSIBLE_VAULT;1.1;AES256 61393863643638653437313566313632306462383837303132346434616433313438353634613762 3334363431623364386164616163326537366333353663650a663634306232363432626162353665 39623061353266373631636331643761306665343731376633623439313138396330346237653930 6432643864346136640a653364386634666461306231353765636662316335613235383565306437 3737 ''' tmp_file = tempfile.NamedTemporaryFile(delete=False) tmp_file.write(to_bytes(password_file_content)) tmp_file.close() fake_loader = DictDataLoader({tmp_file.name: 'sdfadf'}) fake_loader._vault.secrets = vault_secrets secret = vault.FileVaultSecret(loader=fake_loader, filename=tmp_file.name) secret.load() os.unlink(tmp_file.name) self.assertEqual(secret.bytes, to_bytes(password))
def test_encrypt_string(self, mock_vault_editor, mock_setup_vault_secrets): mock_setup_vault_secrets.return_value = [('default', TextVaultSecret('password'))] cli = VaultCLI( args=['ansible-vault', 'encrypt_string', 'some string to encrypt']) cli.parse() cli.run()
def setUp(self): self.vault_password = "******" vault_secret = TextVaultSecret(self.vault_password) self.vault_secrets = [('vault_secret', vault_secret)] self.good_vault = vault.VaultLib(self.vault_secrets) self.vault = self.good_vault self.stream = self._build_stream() self.dumper = dumper.AnsibleDumper
def test_encrypt_string_more_args_than_names(self, mock_vault_editor, mock_setup_vault_secrets): mock_setup_vault_secrets.return_value = [('default', TextVaultSecret('password'))] cli = VaultCLI(args=['ansible-vault', 'encrypt_string', '--name', 'foo1', 'some string to encrypt', 'other strings', 'a few more string args']) cli.parse() cli.run()
def test_encrypt_string_stdin(self, mock_stdin_read, mock_vault_editor, mock_setup_vault_secrets): mock_setup_vault_secrets.return_value = [('default', TextVaultSecret('password'))] cli = VaultCLI(args=['ansible-vault', 'encrypt_string', '--stdin-name', 'the_var_from_stdin', '-']) cli.parse() cli.run()
def test_shadowed_encrypt_string_prompt(self, mock_display, mock_vault_editor, mock_setup_vault_secrets): mock_setup_vault_secrets.return_value = [('default', TextVaultSecret('password'))] cli = VaultCLI(args=['ansible-vault', 'encrypt_string', '--prompt', 'some string to encrypt']) cli.parse() cli.run() args, kwargs = mock_display.call_args assert kwargs["private"]
def test_multiple_matches(self): secrets = [('vault_id1', TextVaultSecret('password1')), ('vault_id2', TextVaultSecret('password2')), ('vault_id1', TextVaultSecret('password3')), ('vault_id4', TextVaultSecret('password4'))] vault_ids = ['vault_id1', 'vault_id4'] matches = vault.match_secrets(secrets, vault_ids) self.assertEqual(len(matches), 3) expected = [('vault_id1', TextVaultSecret('password1')), ('vault_id1', TextVaultSecret('password3')), ('vault_id4', TextVaultSecret('password4'))] self.assertEqual([x for x, y in matches], [a for a, b in expected])
def test_wrong_password(self): plaintext = u"Ansible" bob_password = "******" bobs_secret = TextVaultSecret(bob_password) bobs_secrets = [('default', bobs_secret)] bobs_vault = vault.VaultLib(bobs_secrets) ciphertext = bobs_vault.encrypt(plaintext, vault.match_encrypt_secret(bobs_secrets)[1]) try: self.vault.decrypt(ciphertext) except Exception as e: self.assertIsInstance(e, errors.AnsibleError) self.assertEqual(e.message, 'Decryption failed (no vault secrets were found that could decrypt)')
def test_rekey_file(self): self._test_dir = self._create_test_dir() 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 = 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 = '******' 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]) # 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 _secrets(self, password): vault_secret = TextVaultSecret(password) vault_secrets = [('default', vault_secret)] return vault_secrets
def setUp(self): self._test_dir = None self.vault_password = "******" vault_secret = TextVaultSecret(self.vault_password) self.vault_secrets = [('vault_secret', vault_secret), ('default', vault_secret)]
def _vault_secrets_from_password(self, vault_id, password): return [(vault_id, TextVaultSecret(password))]
def setUp(self): self.vault_password = "******" text_secret = TextVaultSecret(self.vault_password) self.vault_secrets = [('default', text_secret), ('test_id', text_secret)] self.v = vault.VaultLib(self.vault_secrets)
def test_no_matches(self): secret = TextVaultSecret('password') matches = vault.match_secrets([('default', secret)], ['not_default']) self.assertEquals(matches, [])
def test_encrypt(self, mock_vault_editor, mock_setup_vault_secrets): mock_setup_vault_secrets.return_value = [('default', TextVaultSecret('password'))] cli = VaultCLI(args=['ansible-vault', 'encrypt', '/dev/null/foo']) cli.parse() cli.run()
def test_single_match(self): secret = TextVaultSecret('password') matches = vault.match_secrets([('default', secret)], ['default']) self.assertEquals(matches, [('default', secret)])
def setUp(self): self.vault_password = "******" text_secret = TextVaultSecret(self.vault_password) self.vault_secrets = [('foo', text_secret)]
def test_get_real_file_vault_wrong_password(self): wrong_vault = [('default', TextVaultSecret('wrong_password'))] self._loader.set_vault_secrets(wrong_vault) self.assertRaises(AnsibleVaultError, self._loader.get_real_file, self.test_vault_data_path)
def setUp(self): self._loader = DataLoader() vault_secrets = [('default', TextVaultSecret('ansible'))] self._loader.set_vault_secrets(vault_secrets) self.test_vault_data_path = os.path.join(os.path.dirname(__file__), 'fixtures', 'vault.yml')
def setUp(self): self._loader = DataLoader() vault_secrets = [('default', TextVaultSecret('ansible'))] self._loader.set_vault_secrets(vault_secrets)
def setUp(self): self.vault_password = "******" vault_secret = TextVaultSecret(self.vault_password) self.vault_secrets = [('vault_secret', vault_secret), ('default', vault_secret)] self.vault = vault.VaultLib(self.vault_secrets)