Generalize report_encryptor.py into document_encryptor.py and extract the sensitive fields to report_encryptor.py

This commit is contained in:
VakarisZ 2021-09-21 15:32:05 +03:00
parent 089158a976
commit f1c7cf4047
3 changed files with 54 additions and 50 deletions

View File

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

View File

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