Island: Rename FieldType to FieldEncryptor

* Switch FieldTypeABC from abstract class to interface, since there's no
  intention of ever implementing FieldTypeABC's methods.

* Rename FieldTypeABC to IFieldEncryptor and rename StringList to
  StringListEncryptor.
This commit is contained in:
Mike Salvatore 2021-09-21 12:28:57 -04:00
parent 5077d84269
commit 13ba0b9091
5 changed files with 34 additions and 22 deletions

View File

@ -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
"""

View File

@ -1,10 +1,10 @@
from typing import List 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 from monkey_island.cc.server_utils.encryption import string_encryptor
class StringList(FieldTypeABC): class StringListEncryptor(IFieldEncryptor):
@staticmethod @staticmethod
def encrypt(value: List[str]): def encrypt(value: List[str]):
return [string_encryptor.encrypt(string) for string in value] return [string_encryptor.encrypt(string) for string in value]

View File

@ -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()

View File

@ -3,18 +3,20 @@ from typing import Callable, Type
import dpath.util import dpath.util
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.models.utils.field_types.string_list import StringList from monkey_island.cc.models.utils.field_encryptors.string_list_encryptor import StringListEncryptor
@dataclass @dataclass
class SensitiveField: class SensitiveField:
path: str path: str
path_separator = "." 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: def encrypt(report: dict) -> dict:

View File

@ -1,7 +1,7 @@
import pytest import pytest
from monkey_island.cc.models import Report 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.models.utils.report_encryptor import SensitiveField
from monkey_island.cc.server_utils.encryptor import initialize_encryptor from monkey_island.cc.server_utils.encryptor import initialize_encryptor
@ -13,7 +13,7 @@ MOCK_REPORT_DICT = {
"meta_info": {"foo": "bar"}, "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") @pytest.mark.usefixtures("uses_database")