From 12937f98801fe0650608e822b48b18d4edc945e0 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Wed, 6 Apr 2022 16:56:42 +0200 Subject: [PATCH] Island, UT: Add String field encryptor This encryptor is going to be used by dict_encryptor to encrypt/decrypt the ssh_keypairs dictionaries --- .../cc/server_utils/encryption/__init__.py | 1 + .../encryption/field_encryptors/__init__.py | 1 + .../field_encryptors/string_encryptor.py | 12 ++++++++++++ .../encryption/test_string_encryptor.py | 17 +++++++++++++++++ 4 files changed, 31 insertions(+) create mode 100644 monkey/monkey_island/cc/server_utils/encryption/field_encryptors/string_encryptor.py create mode 100644 monkey/tests/unit_tests/monkey_island/cc/server_utils/encryption/test_string_encryptor.py diff --git a/monkey/monkey_island/cc/server_utils/encryption/__init__.py b/monkey/monkey_island/cc/server_utils/encryption/__init__.py index 4cfe67fe2..7fa6c77a4 100644 --- a/monkey/monkey_island/cc/server_utils/encryption/__init__.py +++ b/monkey/monkey_island/cc/server_utils/encryption/__init__.py @@ -24,3 +24,4 @@ from .dict_encryptor import ( ) from .field_encryptors.i_field_encryptor import IFieldEncryptor from .field_encryptors.string_list_encryptor import StringListEncryptor +from .field_encryptors.string_encryptor import StringEncryptor diff --git a/monkey/monkey_island/cc/server_utils/encryption/field_encryptors/__init__.py b/monkey/monkey_island/cc/server_utils/encryption/field_encryptors/__init__.py index 1ceedf768..84a635ece 100644 --- a/monkey/monkey_island/cc/server_utils/encryption/field_encryptors/__init__.py +++ b/monkey/monkey_island/cc/server_utils/encryption/field_encryptors/__init__.py @@ -1,2 +1,3 @@ from .i_field_encryptor import IFieldEncryptor from .string_list_encryptor import StringListEncryptor +from .string_encryptor import StringEncryptor diff --git a/monkey/monkey_island/cc/server_utils/encryption/field_encryptors/string_encryptor.py b/monkey/monkey_island/cc/server_utils/encryption/field_encryptors/string_encryptor.py new file mode 100644 index 000000000..28f0f2c93 --- /dev/null +++ b/monkey/monkey_island/cc/server_utils/encryption/field_encryptors/string_encryptor.py @@ -0,0 +1,12 @@ +from ..data_store_encryptor import get_datastore_encryptor +from . import IFieldEncryptor + + +class StringEncryptor(IFieldEncryptor): + @staticmethod + def encrypt(value: str): + return get_datastore_encryptor().encrypt(value) + + @staticmethod + def decrypt(value: str): + return get_datastore_encryptor().decrypt(value) diff --git a/monkey/tests/unit_tests/monkey_island/cc/server_utils/encryption/test_string_encryptor.py b/monkey/tests/unit_tests/monkey_island/cc/server_utils/encryption/test_string_encryptor.py new file mode 100644 index 000000000..637ab1da1 --- /dev/null +++ b/monkey/tests/unit_tests/monkey_island/cc/server_utils/encryption/test_string_encryptor.py @@ -0,0 +1,17 @@ +from monkey_island.cc.server_utils.encryption import StringEncryptor + +MOCK_STRING = "m0nk3y" +EMPTY_STRING = "" + + +def test_encryptor(uses_encryptor): + encrypted_string = StringEncryptor.encrypt(MOCK_STRING) + assert not encrypted_string == MOCK_STRING + decrypted_string = StringEncryptor.decrypt(encrypted_string) + assert decrypted_string == MOCK_STRING + + +def test_empty_string(uses_encryptor): + # Tests that no erros are raised + encrypted_string = StringEncryptor.encrypt(EMPTY_STRING) + StringEncryptor.decrypt(encrypted_string)