revamp PII masking

This commit is contained in:
Kenneth Kehl
2024-06-17 11:13:03 -07:00
20 changed files with 256 additions and 180 deletions

View File

@@ -148,15 +148,14 @@ class PIIFilter(logging.Filter):
phones = re.findall("(?:\\+ *)?\\d[\\d\\- ]{7,}\\d", msg)
phones = [phone.replace("-", "").replace(" ", "") for phone in phones]
for phone in phones:
msg = msg.replace(phone, f"1XXXXX{phone[-5:]}")
msg = msg.replace(phone, "1XXXXXXXXXX")
emails = re.findall(
r"[\w\.-]+@[\w\.-]+", msg
) # ['alice@google.com', 'bob@abc.com']
for email in emails:
# do something with each found email string
email_parts = email.split("@")
masked_email = f"{email_parts[0][0:3]}XXX@{email_parts[1][0:7]}XXX"
masked_email = "XXXXX@XXXXXXX"
msg = msg.replace(email, masked_email)
return msg