diff --git a/monkey/monkey_island/cc/models/utils/field_encryptors/i_field_encryptor.py b/monkey/monkey_island/cc/models/utils/field_encryptors/i_field_encryptor.py new file mode 100644 index 000000000..6a675bf4d --- /dev/null +++ b/monkey/monkey_island/cc/models/utils/field_encryptors/i_field_encryptor.py @@ -0,0 +1,24 @@ +from abc import ABC, abstractmethod +from typing import Any + + +class IFieldEncryptor(ABC): + @staticmethod + @abstractmethod + def encrypt(value: Any) -> Any: + """Encrypts data and returns the ciphertext. + + :param value: Data that will be encrypted + :return: Ciphertext generated by encrypting value + :rtype: Any + """ + + @staticmethod + @abstractmethod + def decrypt(value: Any): + """Decrypts data and returns the plaintext. + + :param value: Ciphertext that will be decrypted + :return: Plaintext generated by decrypting value + :rtype: Any + """ diff --git a/monkey/monkey_island/cc/models/utils/field_types/string_list.py b/monkey/monkey_island/cc/models/utils/field_encryptors/string_list_encryptor.py similarity index 71% rename from monkey/monkey_island/cc/models/utils/field_types/string_list.py rename to monkey/monkey_island/cc/models/utils/field_encryptors/string_list_encryptor.py index 39e8888e7..4c3fce7ee 100644 --- a/monkey/monkey_island/cc/models/utils/field_types/string_list.py +++ b/monkey/monkey_island/cc/models/utils/field_encryptors/string_list_encryptor.py @@ -1,10 +1,10 @@ from typing import List -from monkey_island.cc.models.utils.field_types.field_type_abc import FieldTypeABC +from monkey_island.cc.models.utils.field_encryptors.i_field_encryptor import IFieldEncryptor from monkey_island.cc.server_utils.encryption import string_encryptor -class StringList(FieldTypeABC): +class StringListEncryptor(IFieldEncryptor): @staticmethod def encrypt(value: List[str]): return [string_encryptor.encrypt(string) for string in value] diff --git a/monkey/monkey_island/cc/models/utils/field_types/field_type_abc.py b/monkey/monkey_island/cc/models/utils/field_types/field_type_abc.py deleted file mode 100644 index c2d87ff92..000000000 --- a/monkey/monkey_island/cc/models/utils/field_types/field_type_abc.py +++ /dev/null @@ -1,14 +0,0 @@ -from abc import ABC, abstractmethod -from typing import Any - - -class FieldTypeABC(ABC): - @staticmethod - @abstractmethod - def encrypt(value: Any): - raise NotImplementedError() - - @staticmethod - @abstractmethod - def decrypt(value: Any): - raise NotImplementedError() diff --git a/monkey/monkey_island/cc/models/utils/report_encryptor.py b/monkey/monkey_island/cc/models/utils/report_encryptor.py index 93f8bdd03..d5e31f4d8 100644 --- a/monkey/monkey_island/cc/models/utils/report_encryptor.py +++ b/monkey/monkey_island/cc/models/utils/report_encryptor.py @@ -3,18 +3,20 @@ from typing import Callable, Type import dpath.util -from monkey_island.cc.models.utils.field_types.field_type_abc import FieldTypeABC -from monkey_island.cc.models.utils.field_types.string_list import StringList +from monkey_island.cc.models.utils.field_encryptors.i_field_encryptor import IFieldEncryptor +from monkey_island.cc.models.utils.field_encryptors.string_list_encryptor import StringListEncryptor @dataclass class SensitiveField: path: str path_separator = "." - field_type: Type[FieldTypeABC] + field_type: Type[IFieldEncryptor] -sensitive_fields = [SensitiveField(path="overview.config_passwords", field_type=StringList)] +sensitive_fields = [ + SensitiveField(path="overview.config_passwords", field_type=StringListEncryptor) +] def encrypt(report: dict) -> dict: diff --git a/monkey/tests/unit_tests/monkey_island/cc/models/test_report_model.py b/monkey/tests/unit_tests/monkey_island/cc/models/test_report_model.py index b719f95a5..6eedb33b8 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/models/test_report_model.py +++ b/monkey/tests/unit_tests/monkey_island/cc/models/test_report_model.py @@ -1,7 +1,7 @@ import pytest from monkey_island.cc.models import Report -from monkey_island.cc.models.utils.field_types.string_list import StringList +from monkey_island.cc.models.utils.field_encryptors.string_list_encryptor import StringListEncryptor from monkey_island.cc.models.utils.report_encryptor import SensitiveField from monkey_island.cc.server_utils.encryptor import initialize_encryptor @@ -13,7 +13,7 @@ MOCK_REPORT_DICT = { "meta_info": {"foo": "bar"}, } -MOCK_SENSITIVE_FIELDS = [SensitiveField("overview.foo.the_key", StringList)] +MOCK_SENSITIVE_FIELDS = [SensitiveField("overview.foo.the_key", StringListEncryptor)] @pytest.mark.usefixtures("uses_database")