forked from p34709852/monkey
Generalize report_encryptor.py into document_encryptor.py and extract the sensitive fields to report_encryptor.py
This commit is contained in:
parent
089158a976
commit
f1c7cf4047
|
@ -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)
|
|
@ -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)
|
Loading…
Reference in New Issue