From 7760520cc8d62cbda319bf79b75263a53ffa9568 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 14 Jul 2022 07:27:47 -0400 Subject: [PATCH] Island: Remove disused StringListEncryptor --- .../cc/server_utils/encryption/__init__.py | 1 - .../encryption/field_encryptors/__init__.py | 1 - .../field_encryptors/string_list_encryptor.py | 14 ------- .../cc/models/test_report_dal.py | 41 +------------------ .../encryption/test_string_list_encryptor.py | 21 ---------- 5 files changed, 1 insertion(+), 77 deletions(-) delete mode 100644 monkey/monkey_island/cc/server_utils/encryption/field_encryptors/string_list_encryptor.py delete mode 100644 monkey/tests/unit_tests/monkey_island/cc/server_utils/encryption/test_string_list_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 9443fa4ab..d790a71f6 100644 --- a/monkey/monkey_island/cc/server_utils/encryption/__init__.py +++ b/monkey/monkey_island/cc/server_utils/encryption/__init__.py @@ -21,5 +21,4 @@ from .dict_encryptor import ( FieldNotFoundError, ) 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 84a635ece..ccbd75331 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,3 +1,2 @@ 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_list_encryptor.py b/monkey/monkey_island/cc/server_utils/encryption/field_encryptors/string_list_encryptor.py deleted file mode 100644 index bf2555016..000000000 --- a/monkey/monkey_island/cc/server_utils/encryption/field_encryptors/string_list_encryptor.py +++ /dev/null @@ -1,14 +0,0 @@ -from typing import List - -from ..data_store_encryptor import get_datastore_encryptor -from . import IFieldEncryptor - - -class StringListEncryptor(IFieldEncryptor): - @staticmethod - def encrypt(value: List[str]) -> List[str]: - return [get_datastore_encryptor().encrypt(string.encode()).decode() for string in value] - - @staticmethod - def decrypt(value: List[str]) -> List[str]: - return [get_datastore_encryptor().decrypt(string.encode()).decode() for string in value] diff --git a/monkey/tests/unit_tests/monkey_island/cc/models/test_report_dal.py b/monkey/tests/unit_tests/monkey_island/cc/models/test_report_dal.py index 67ac8355e..00154e5fe 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/models/test_report_dal.py +++ b/monkey/tests/unit_tests/monkey_island/cc/models/test_report_dal.py @@ -1,16 +1,14 @@ import copy -from typing import List import pytest from monkey_island.cc.models import Report from monkey_island.cc.models.report import get_report, save_report -from monkey_island.cc.server_utils.encryption import IFieldEncryptor, SensitiveField MOCK_SENSITIVE_FIELD_CONTENTS = ["the_string", "the_string2"] MOCK_REPORT_DICT = { "overview": { - "foo": {"the_key": MOCK_SENSITIVE_FIELD_CONTENTS, "other_key": "other_value"}, + "foo": {"the_key": ["the_string", "the_string2"], "other_key": "other_value"}, "bar": {"the_key": []}, }, "glance": {"foo": "bar"}, @@ -19,43 +17,6 @@ MOCK_REPORT_DICT = { } -class MockStringListEncryptor(IFieldEncryptor): - plaintext = [] - - @staticmethod - def encrypt(value: List[str]) -> List[str]: - return [MockStringListEncryptor._encrypt(v) for v in value] - - @staticmethod - def _encrypt(value: str) -> str: - MockStringListEncryptor.plaintext.append(value) - return f"ENCRYPTED_{str(len(MockStringListEncryptor.plaintext) - 1)}" - - @staticmethod - def decrypt(value: List[str]) -> List[str]: - return MockStringListEncryptor.plaintext - - -@pytest.fixture(autouse=True) -def patch_sensitive_fields(monkeypatch): - mock_sensitive_fields = [ - SensitiveField("overview.foo.the_key", MockStringListEncryptor), - SensitiveField("overview.bar.the_key", MockStringListEncryptor), - ] - monkeypatch.setattr( - "monkey_island.cc.models.report.report_dal.sensitive_fields", mock_sensitive_fields - ) - - -@pytest.mark.usefixtures("uses_database") -def test_report_encryption(): - save_report(MOCK_REPORT_DICT) - - assert Report.objects.first()["overview"]["foo"]["the_key"] == ["ENCRYPTED_0", "ENCRYPTED_1"] - assert Report.objects.first()["overview"]["bar"]["the_key"] == [] - assert get_report()["overview"]["foo"]["the_key"] == MOCK_SENSITIVE_FIELD_CONTENTS - - @pytest.mark.usefixtures("uses_database") def test_report_dot_encoding(): mrd = copy.deepcopy(MOCK_REPORT_DICT) diff --git a/monkey/tests/unit_tests/monkey_island/cc/server_utils/encryption/test_string_list_encryptor.py b/monkey/tests/unit_tests/monkey_island/cc/server_utils/encryption/test_string_list_encryptor.py deleted file mode 100644 index b78cd6ec0..000000000 --- a/monkey/tests/unit_tests/monkey_island/cc/server_utils/encryption/test_string_list_encryptor.py +++ /dev/null @@ -1,21 +0,0 @@ -import pytest - -from monkey_island.cc.server_utils.encryption import StringListEncryptor - -MOCK_STRING_LIST = ["test_1", "test_2"] -EMPTY_LIST = [] - - -@pytest.mark.slow -def test_encryption_and_decryption(uses_encryptor): - encrypted_list = StringListEncryptor.encrypt(MOCK_STRING_LIST) - assert not encrypted_list == MOCK_STRING_LIST - decrypted_list = StringListEncryptor.decrypt(encrypted_list) - assert decrypted_list == MOCK_STRING_LIST - - -@pytest.mark.slow -def test_empty_list(uses_encryptor): - # Tests that no errors are raised - encrypted_list = StringListEncryptor.encrypt(EMPTY_LIST) - StringListEncryptor.decrypt(encrypted_list)