From a24eb841c132aa189d769fe7c97f2ce3bf3ef1d6 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Tue, 28 Sep 2021 10:58:23 +0300 Subject: [PATCH] Extract DAL interface for report model into a separate report_dal.py file --- monkey/monkey_island/cc/models/report.py | 60 ------------------- .../monkey_island/cc/models/report/report.py | 13 ++++ .../cc/models/report/report_dal.py | 52 ++++++++++++++++ .../cc/services/reporting/report.py | 7 ++- 4 files changed, 69 insertions(+), 63 deletions(-) delete mode 100644 monkey/monkey_island/cc/models/report.py create mode 100644 monkey/monkey_island/cc/models/report/report.py create mode 100644 monkey/monkey_island/cc/models/report/report_dal.py diff --git a/monkey/monkey_island/cc/models/report.py b/monkey/monkey_island/cc/models/report.py deleted file mode 100644 index cbdde0dcb..000000000 --- a/monkey/monkey_island/cc/models/report.py +++ /dev/null @@ -1,60 +0,0 @@ -from __future__ import annotations - -from bson import json_util -from mongoengine import DictField, Document - -from monkey_island.cc.utils import SensitiveField, dict_encryptor -from monkey_island.cc.utils.field_encryptors import StringListEncryptor - -sensitive_fields = [ - SensitiveField(path="overview.config_passwords", field_encryptor=StringListEncryptor) -] - - -class Report(Document): - - overview = DictField(required=True) - glance = DictField(required=True) - recommendations = DictField(required=True) - meta_info = DictField(required=True) - - meta = {"strict": False} - - @staticmethod - def save_report(report_dict: dict): - report_dict = _encode_dot_char_before_mongo_insert(report_dict) - report_dict = dict_encryptor.encrypt(sensitive_fields, report_dict) - Report.objects.delete() - Report( - overview=report_dict["overview"], - glance=report_dict["glance"], - recommendations=report_dict["recommendations"], - meta_info=report_dict["meta_info"], - ).save() - - @staticmethod - def get_report() -> dict: - report_dict = Report.objects.first().to_mongo() - return _decode_dot_char_before_mongo_insert( - dict_encryptor.decrypt(sensitive_fields, report_dict) - ) - - -def _encode_dot_char_before_mongo_insert(report_dict): - """ - mongodb doesn't allow for '.' and '$' in a key's name, this function replaces the '.' - char with the unicode - ,,, combo instead. - :return: dict with formatted keys with no dots. - """ - report_as_json = json_util.dumps(report_dict).replace(".", ",,,") - return json_util.loads(report_as_json) - - -def _decode_dot_char_before_mongo_insert(report_dict): - """ - this function replaces the ',,,' combo with the '.' char instead. - :return: report dict with formatted keys (',,,' -> '.') - """ - report_as_json = json_util.dumps(report_dict).replace(",,,", ".") - return json_util.loads(report_as_json) diff --git a/monkey/monkey_island/cc/models/report/report.py b/monkey/monkey_island/cc/models/report/report.py new file mode 100644 index 000000000..8de3124e6 --- /dev/null +++ b/monkey/monkey_island/cc/models/report/report.py @@ -0,0 +1,13 @@ +from __future__ import annotations + +from mongoengine import DictField, Document + + +class Report(Document): + + overview = DictField(required=True) + glance = DictField(required=True) + recommendations = DictField(required=True) + meta_info = DictField(required=True) + + meta = {"strict": False} diff --git a/monkey/monkey_island/cc/models/report/report_dal.py b/monkey/monkey_island/cc/models/report/report_dal.py new file mode 100644 index 000000000..be7bade9e --- /dev/null +++ b/monkey/monkey_island/cc/models/report/report_dal.py @@ -0,0 +1,52 @@ +from __future__ import annotations + +from bson import json_util + +from monkey_island.cc.models.report.report import Report +from monkey_island.cc.server_utils.encryption import ( + SensitiveField, + StringListEncryptor, + decrypt_dict, + encrypt_dict, +) + +sensitive_fields = [ + SensitiveField(path="overview.config_passwords", field_encryptor=StringListEncryptor) +] + + +def save_report(report_dict: dict): + report_dict = _encode_dot_char_before_mongo_insert(report_dict) + report_dict = encrypt_dict(sensitive_fields, report_dict) + Report.objects.delete() + Report( + overview=report_dict["overview"], + glance=report_dict["glance"], + recommendations=report_dict["recommendations"], + meta_info=report_dict["meta_info"], + ).save() + + +def get_report() -> dict: + report_dict = Report.objects.first().to_mongo() + return _decode_dot_char_before_mongo_insert(decrypt_dict(sensitive_fields, report_dict)) + + +def _encode_dot_char_before_mongo_insert(report_dict): + """ + mongodb doesn't allow for '.' and '$' in a key's name, this function replaces the '.' + char with the unicode + ,,, combo instead. + :return: dict with formatted keys with no dots. + """ + report_as_json = json_util.dumps(report_dict).replace(".", ",,,") + return json_util.loads(report_as_json) + + +def _decode_dot_char_before_mongo_insert(report_dict): + """ + this function replaces the ',,,' combo with the '.' char instead. + :return: report dict with formatted keys (',,,' -> '.') + """ + report_as_json = json_util.dumps(report_dict).replace(",,,", ".") + return json_util.loads(report_as_json) diff --git a/monkey/monkey_island/cc/services/reporting/report.py b/monkey/monkey_island/cc/services/reporting/report.py index 17e6ce037..d0ac2939f 100644 --- a/monkey/monkey_island/cc/services/reporting/report.py +++ b/monkey/monkey_island/cc/services/reporting/report.py @@ -14,7 +14,8 @@ from common.config_value_paths import ( from common.network.network_range import NetworkRange from common.network.segmentation_utils import get_ip_in_src_and_not_in_dst from monkey_island.cc.database import mongo -from monkey_island.cc.models import Monkey, Report +from monkey_island.cc.models import Monkey +from monkey_island.cc.models.report import get_report, save_report from monkey_island.cc.models.telemetries import get_telemetry_by_query from monkey_island.cc.services.config import ConfigService from monkey_island.cc.services.configuration.utils import ( @@ -635,7 +636,7 @@ class ReportService: "meta_info": {"latest_monkey_modifytime": monkey_latest_modify_time}, } ReportExporterManager().export(report) - Report.save_report(report) + save_report(report) return report @staticmethod @@ -697,4 +698,4 @@ class ReportService: if not ReportService.is_latest_report_exists(): return safe_generate_regular_report() - return Report.get_report() + return get_report()