Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PNLP-8004: mask numbers in bubbles and pronounceText #177

Merged
merged 5 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions core/message/from_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion core/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding=utf-8
import copy
import datetime
import gc
import json
Expand All @@ -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
Expand Down Expand Up @@ -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
6 changes: 5 additions & 1 deletion smart_kit/message/smartapp_to_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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":
Expand Down
1 change: 1 addition & 0 deletions smart_kit/template/static/configs/template_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 37 additions & 0 deletions tests/core_tests/test_utils/test_mask_numbers.py
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
})
Loading