예제 #1
0
    def test_edit_file_symlink(self, mock_sp_call):
        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)

        new_src_contents = to_bytes("The info is different now.")

        def faux_editor(editor_args):
            self._faux_editor(editor_args, new_src_contents)

        mock_sp_call.side_effect = faux_editor

        ve = vault.VaultEditor('password')

        ve.encrypt_file(src_file_path)

        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.edit_file(src_file_link_path)

        new_src_file = open(src_file_path, 'rb')
        new_src_file_contents = new_src_file.read()

        src_file_plaintext = ve.vault.decrypt(new_src_file_contents)

        self._assert_file_is_link(src_file_link_path, src_file_path)

        self.assertEqual(src_file_plaintext, new_src_contents)
예제 #2
0
    def test_decrypt_1_1(self):
        if not HAS_AES or not HAS_COUNTER or not HAS_PBKDF2:
            raise SkipTest

        v11_file = tempfile.NamedTemporaryFile(delete=False)
        with v11_file as f:
            f.write(to_bytes(v11_data))

        ve = vault.VaultEditor("ansible")

        # make sure the password functions for the cipher
        error_hit = False
        try:
            ve.decrypt_file(v11_file.name)
        except errors.AnsibleError:
            error_hit = True

        # verify decrypted content
        f = open(v11_file.name, "rb")
        fdata = to_text(f.read())
        f.close()

        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()
예제 #3
0
    def test_decrypt_1_0(self):
        # Skip testing decrypting 1.0 files if we don't have access to AES, KDF or Counter.
        v10_file = tempfile.NamedTemporaryFile(delete=False)
        with v10_file as f:
            f.write(to_bytes(v10_data))

        ve = vault.VaultEditor("ansible")

        # make sure the password functions for the cipher
        error_hit = False
        try:
            ve.decrypt_file(v10_file.name)
        except errors.AnsibleError:
            error_hit = True

        # verify decrypted content
        f = open(v10_file.name, "rb")
        fdata = to_text(f.read())
        f.close()

        os.unlink(v10_file.name)

        assert error_hit is False, "error decrypting 1.0 file"
        self.assertEquals(fdata.strip(), "foo")
        assert fdata.strip(
        ) == "foo", "incorrect decryption of 1.0 file: %s" % fdata.strip()
예제 #4
0
 def test_methods_exist(self):
     v = vault.VaultEditor(None)
     slots = [
         'create_file', 'decrypt_file', 'edit_file', 'encrypt_file',
         'rekey_file', 'read_data', 'write_data'
     ]
     for slot in slots:
         assert hasattr(v, slot), "VaultLib is missing the %s method" % slot
예제 #5
0
    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 = vault.VaultEditor('password')
        ve.encrypt_file(src_file_path)

        new_password = '******'
        ve.rekey_file(src_file_path, new_password)

        new_ve = vault.VaultEditor(new_password)
        self._assert_file_is_encrypted(new_ve, src_file_path,
                                       src_file_contents)
예제 #6
0
    def test_encrypt_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 = vault.VaultEditor('password')
        ve.encrypt_file(src_file_path)

        self._assert_file_is_encrypted(ve, src_file_path, src_file_contents)
예제 #7
0
    def test_create_file_exists(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')
        self.assertRaisesRegexp(errors.AnsibleError,
                                'please use .edit. instead', ve.create_file,
                                src_file_path)
예제 #8
0
    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')
        self.assertRaisesRegexp(errors.AnsibleError,
                                'input is not vault encrypted data',
                                ve.decrypt_file, src_file_path)
예제 #9
0
    def test_create_file(self, mock_editor_shell_command):
        def sc_side_effect(filename):
            return ['touch', filename]

        mock_editor_shell_command.side_effect = sc_side_effect

        tmp_file = tempfile.NamedTemporaryFile()
        os.unlink(tmp_file.name)

        ve = vault.VaultEditor("ansible")
        ve.create_file(tmp_file.name)

        self.assertTrue(os.path.exists(tmp_file.name))
예제 #10
0
    def test_plaintext(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 = vault.VaultEditor('password')
        ve.encrypt_file(src_file_path)

        res = ve.plaintext(src_file_path)
        self.assertEquals(src_file_contents, res)
예제 #11
0
    def test_real_path_symlink(self):
        self._test_dir = self._create_test_dir()
        file_path = self._create_file(self._test_dir,
                                      'test_file',
                                      content=b'this is a test file')
        file_link_path = os.path.join(self._test_dir, 'a_link_to_test_file')

        os.symlink(file_path, file_link_path)

        ve = vault.VaultEditor('password')

        res = ve._real_path(file_link_path)
        self.assertEqual(res, file_path)
예제 #12
0
    def test_edit_file_helper_empty_target(self, mock_sp_call):
        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)

        mock_sp_call.side_effect = self._faux_command
        ve = vault.VaultEditor('password')

        b_ciphertext = ve._edit_file_helper(src_file_path)

        self.assertNotEqual(src_contents, b_ciphertext)
예제 #13
0
    def test_rekey_file_not_encrypted(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 = vault.VaultEditor('password')

        new_password = '******'
        self.assertRaisesRegexp(errors.AnsibleError,
                                'input is not vault encrypted data',
                                ve.rekey_file, src_file_path, new_password)
예제 #14
0
    def test_rekey_file_no_new_password(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 = vault.VaultEditor('password')
        ve.encrypt_file(src_file_path)

        self.assertRaisesRegexp(errors.AnsibleError,
                                'The value for the new_password to rekey',
                                ve.rekey_file, src_file_path, None)
예제 #15
0
    def test_edit_file_helper_call_exception(self, mock_sp_call):
        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)

        error_txt = 'calling editor raised an exception'
        mock_sp_call.side_effect = errors.AnsibleError(error_txt)

        ve = vault.VaultEditor('password')

        self.assertRaisesRegexp(errors.AnsibleError, error_txt,
                                ve._edit_file_helper, src_file_path)
예제 #16
0
    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)
예제 #17
0
    def test_edit_file_not_encrypted(self, mock_sp_call):
        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)

        new_src_contents = to_bytes("The info is different now.")

        def faux_editor(editor_args):
            self._faux_editor(editor_args, new_src_contents)

        mock_sp_call.side_effect = faux_editor

        ve = vault.VaultEditor('password')
        self.assertRaisesRegexp(errors.AnsibleError,
                                'input is not vault encrypted data',
                                ve.edit_file, src_file_path)
예제 #18
0
    def test_edit_file_helper_no_change(self, mock_sp_call):
        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)

        # editor invocation doesn't change anything
        def faux_editor(editor_args):
            self._faux_editor(editor_args, src_file_contents)

        mock_sp_call.side_effect = faux_editor
        ve = vault.VaultEditor('password')

        ve._edit_file_helper(src_file_path, existing_data=src_file_contents)

        new_target_file = open(src_file_path, 'rb')
        new_target_file_contents = new_target_file.read()
        self.assertEqual(src_file_contents, new_target_file_contents)
예제 #19
0
    def test_edit_file_helper_symlink_target(self, mock_sp_call):
        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)

        src_file_link_path = os.path.join(self._test_dir,
                                          'a_link_to_dest_file')

        os.symlink(src_file_path, src_file_link_path)

        mock_sp_call.side_effect = self._faux_command
        ve = vault.VaultEditor('password')

        b_ciphertext = ve._edit_file_helper(src_file_link_path)

        self.assertNotEqual(
            src_file_contents, b_ciphertext,
            'b_ciphertext should be encrypted and not equal to src_contents')
예제 #20
0
    def test_rekey_migration(self):
        # Skip testing rekeying files if we don't have access to AES, KDF or Counter.
        if not HAS_AES or not HAS_COUNTER or not HAS_PBKDF2:
            raise SkipTest

        v10_file = tempfile.NamedTemporaryFile(delete=False)
        with v10_file as f:
            f.write(to_bytes(v10_data))

        ve = vault.VaultEditor("ansible")

        # make sure the password functions for the cipher
        error_hit = False
        try:
            ve.rekey_file(v10_file.name, 'ansible2')
        except errors.AnsibleError:
            error_hit = True

        # verify decrypted content
        f = open(v10_file.name, "rb")
        fdata = f.read()
        f.close()

        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")
        dec_data = None
        error_hit = False
        try:
            dec_data = vl.decrypt(fdata)
        except errors.AnsibleError:
            error_hit = True

        os.unlink(v10_file.name)

        assert vl.cipher_name == "AES256", "wrong cipher name set after rekey: %s" % vl.cipher_name
        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
예제 #21
0
    def test_real_path_dev_null(self):
        filename = '/dev/null'
        ve = vault.VaultEditor('password')

        res = ve._real_path(filename)
        self.assertEqual(res, '/dev/null')
예제 #22
0
    def test_real_path_dash(self):
        filename = '-'
        ve = vault.VaultEditor('password')

        res = ve._real_path(filename)
        self.assertEqual(res, '-')