diff --git a/core/message/from_message.py b/core/message/from_message.py index f9e0bb3f..51b1c4cd 100644 --- a/core/message/from_message.py +++ b/core/message/from_message.py @@ -11,10 +11,11 @@ import core.logging.logger_constants as log_const from core.logging.logger_utils import log from core.utils.masking_message import masking -from core.utils.utils import current_time_ms +from core.utils.utils import current_time_ms, mask_numbers from core.message.msg_validator import MessageValidator from smart_kit.configs import get_app_config +from smart_kit.configs import settings class Headers: @@ -252,7 +253,9 @@ def generate_new_callback_id(self) -> str: @property def masked_value(self) -> str: - masked_data = masking(self.as_dict, self.masking_fields) + mask_numbers_flag = settings.Settings()["template_settings"].get("mask_numbers", False) + masked_data = mask_numbers(masking(self.as_dict, self.masking_fields)) if mask_numbers_flag else \ + masking(self.as_dict, self.masking_fields) return json.dumps(masked_data, ensure_ascii=False) @property diff --git a/core/utils/utils.py b/core/utils/utils.py index f4aa15c3..abca12c5 100644 --- a/core/utils/utils.py +++ b/core/utils/utils.py @@ -1,4 +1,5 @@ # coding=utf-8 +import copy import datetime import gc import json @@ -8,7 +9,7 @@ from collections import OrderedDict from math import isnan, isinf -from typing import Optional +from typing import Optional, Any, Dict from time import time from scenarios.user.user_model import User @@ -143,3 +144,15 @@ def deep_update_dict(original, update): elif isinstance(value, dict): deep_update_dict(value, update[key]) return update + + +def mask_numbers(message: Dict[str, Any]) -> Dict[str, Any]: + masked_message = copy.deepcopy(message) + items = masked_message.get("payload", {}).get("items", []) + for item in items: + if "bubble" in item and "text" in item["bubble"]: + item["bubble"]["text"] = re.sub(r"\d+(?:[.,]\d+)?", "*number*", item["bubble"]["text"]) + pronounce_text = masked_message.get("payload", {}).get("pronounceText") + if pronounce_text is not None: + masked_message["payload"]["pronounceText"] = re.sub(r"\d+(?:[.,]\d+)?", "*number*", pronounce_text) + return masked_message diff --git a/smart_kit/message/smartapp_to_message.py b/smart_kit/message/smartapp_to_message.py index 067c2547..d6fc1a36 100644 --- a/smart_kit/message/smartapp_to_message.py +++ b/smart_kit/message/smartapp_to_message.py @@ -6,6 +6,8 @@ from copy import copy from core.utils.masking_message import masking +from core.utils.utils import mask_numbers +from smart_kit.configs import settings from smart_kit.utils import SmartAppToMessage_pb2 if TYPE_CHECKING: @@ -69,7 +71,9 @@ def as_protobuf_message(data_as_dict): @cached_property def masked_value(self): - masked_data = masking(self.as_dict, self.masking_fields) + mask_numbers_flag = settings.Settings()["template_settings"].get("mask_numbers", False) + masked_data = mask_numbers(masking(self.as_dict, self.masking_fields)) if mask_numbers_flag else \ + masking(self.as_dict, self.masking_fields) if self.command.loader == "json.dumps": return json.dumps(masked_data, ensure_ascii=False) elif self.command.loader == "protobuf": diff --git a/smart_kit/template/static/configs/template_config.yml b/smart_kit/template/static/configs/template_config.yml index 6acdd793..b0fd5ff0 100644 --- a/smart_kit/template/static/configs/template_config.yml +++ b/smart_kit/template/static/configs/template_config.yml @@ -21,3 +21,4 @@ self_service_with_state_save_messages: true project_id: template-app-id consumer_topic: "app" kafka_message_key_recovery_log_level: "DEBUG" +mask_numbers: false diff --git a/tests/core_tests/test_utils/test_mask_numbers.py b/tests/core_tests/test_utils/test_mask_numbers.py new file mode 100644 index 00000000..288b2744 --- /dev/null +++ b/tests/core_tests/test_utils/test_mask_numbers.py @@ -0,0 +1,37 @@ +from unittest import TestCase + +from core.utils.utils import mask_numbers + + +class TestMaskNumbers(TestCase): + def test_1(self): + message = { + "payload": { + "pronounceText": "номер телефона: +79990000000", + "items": [ + { + "bubble": { + "text": "номер телефона: +7 (999) 000-00-00" + } + } + ] + } + } + + masked_message = mask_numbers(message) + self.assertEqual(masked_message["payload"]["pronounceText"], "номер телефона: +*number*") + self.assertEqual(masked_message["payload"]["items"][0]["bubble"]["text"], + "номер телефона: +*number* (*number*) *number*-*number*-*number*") + + self.assertEqual(message, { + "payload": { + "pronounceText": "номер телефона: +79990000000", + "items": [ + { + "bubble": { + "text": "номер телефона: +7 (999) 000-00-00" + } + } + ] + } + })