From f1c7cf40477686e32b83f0c3bc33789a46e47420 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Tue, 21 Sep 2021 15:32:05 +0300 Subject: [PATCH] Generalize report_encryptor.py into document_encryptor.py and extract the sensitive fields to report_encryptor.py --- .../cc/models/utils/document_encryptor.py | 54 +++++++++++++++++++ .../cc/models/utils/report_encryptor.py | 50 ----------------- .../monkey_island/cc/models/__init__.py | 0 3 files changed, 54 insertions(+), 50 deletions(-) create mode 100644 monkey/monkey_island/cc/models/utils/document_encryptor.py create mode 100644 monkey/tests/unit_tests/monkey_island/cc/models/__init__.py diff --git a/monkey/monkey_island/cc/models/utils/document_encryptor.py b/monkey/monkey_island/cc/models/utils/document_encryptor.py new file mode 100644 index 000000000..0a0c2a62e --- /dev/null +++ b/monkey/monkey_island/cc/models/utils/document_encryptor.py @@ -0,0 +1,54 @@ +from abc import ABC, abstractmethod +from dataclasses import dataclass +from typing import Callable, List, Type + +import dpath.util + +from monkey_island.cc.models.utils.field_types.field_type_abc import FieldTypeABC + + +@dataclass +class SensitiveField: + path: str + path_separator = "." + field_type: Type[FieldTypeABC] + + +class DocumentEncryptor(ABC): + @property + @abstractmethod + def sensitive_fields(self) -> List[SensitiveField]: + pass + + @classmethod + def encrypt(cls, document_dict: dict) -> dict: + for sensitive_field in cls.sensitive_fields: + DocumentEncryptor._apply_operation_to_document_field( + document_dict, sensitive_field, sensitive_field.field_type.encrypt + ) + + return document_dict + + @classmethod + def decrypt(cls, document_dict: dict) -> dict: + for sensitive_field in cls.sensitive_fields: + DocumentEncryptor._apply_operation_to_document_field( + document_dict, sensitive_field, sensitive_field.field_type.decrypt + ) + return document_dict + + @staticmethod + def _apply_operation_to_document_field( + report: dict, sensitive_field: SensitiveField, operation: Callable + ): + field_value = dpath.util.get( + report, sensitive_field.path, sensitive_field.path_separator, None + ) + if field_value is None: + raise Exception( + f"Can't encrypt object because the path {sensitive_field.path} doesn't exist." + ) + + modified_value = operation(field_value) + + dpath.util.set(report, sensitive_field.path, modified_value, sensitive_field.path_separator) diff --git a/monkey/monkey_island/cc/models/utils/report_encryptor.py b/monkey/monkey_island/cc/models/utils/report_encryptor.py index d5e31f4d8..e69de29bb 100644 --- a/monkey/monkey_island/cc/models/utils/report_encryptor.py +++ b/monkey/monkey_island/cc/models/utils/report_encryptor.py @@ -1,50 +0,0 @@ -from dataclasses import dataclass -from typing import Callable, Type - -import dpath.util - -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[IFieldEncryptor] - - -sensitive_fields = [ - SensitiveField(path="overview.config_passwords", field_type=StringListEncryptor) -] - - -def encrypt(report: dict) -> dict: - for sensitive_field in sensitive_fields: - _apply_operation_to_report_field( - report, sensitive_field, sensitive_field.field_type.encrypt - ) - - return report - - -def decrypt(report: dict) -> dict: - for sensitive_field in sensitive_fields: - _apply_operation_to_report_field( - report, sensitive_field, sensitive_field.field_type.decrypt - ) - return report - - -def _apply_operation_to_report_field( - report: dict, sensitive_field: SensitiveField, operation: Callable -): - field_value = dpath.util.get(report, sensitive_field.path, sensitive_field.path_separator, None) - if field_value is None: - raise Exception( - f"Can't encrypt object because the path {sensitive_field.path} doesn't exist." - ) - - modified_value = operation(field_value) - - dpath.util.set(report, sensitive_field.path, modified_value, sensitive_field.path_separator) diff --git a/monkey/tests/unit_tests/monkey_island/cc/models/__init__.py b/monkey/tests/unit_tests/monkey_island/cc/models/__init__.py new file mode 100644 index 000000000..e69de29bb