Data protection - cloud watch

0

Hi, I have a json payload as

"contactDetails": {
          "firstName": "Peter Alpha",
          "companyName": "OFN Test",
          "email": "test@ab.com",
          "phoneNumber": "0000000000"
        }

I am trying to mask the contact detail or any field inside it by enabling the data protection policy and adding the predefined data identifiers as Name, email, phone number etc for masking. When policy is applied its masking only email and phone number, not the firstName. I have tried using the regex as well but no luck with that as well. If you have done something similar could you please help?

Thanks, Ravi

demandé il y a 4 mois135 vues
1 réponse
0

Hi raviv,

please go through the below script and steps once i hope it will helps to resolve your issue.

Python Script for Masking Sensitive Data in JSON

import re
import json

# Sample JSON payload
json_payload = """
{
  "contactDetails": {
    "firstName": "Peter Alpha",
    "companyName": "OFN Test",
    "email": "test@ab.com",
    "phoneNumber": "0000000000"
  }
}
"""

# Convert JSON string to a Python dictionary
data = json.loads(json_payload)

# Regular expressions for matching sensitive data
email_regex = r"[^@]+@[^@]+\.[^@]+"
phone_regex = r"\b\d{10}\b"
name_regex = r"\b[A-Z][a-z]*\s[A-Z][a-z]*\b"  # Example for FirstName LastName

# Function to mask sensitive data
def mask_data(data):
    if isinstance(data, dict):
        for key, value in data.items():
            if isinstance(value, str):
                if re.match(email_regex, value):
                    data[key] = "*****@*****.com"
                elif re.match(phone_regex, value):
                    data[key] = "**********"
                elif re.match(name_regex, value):
                    data[key] = "***** *****"
            else:
                mask_data(value)
    elif isinstance(data, list):
        for item in data:
            mask_data(item)

# Mask the data
mask_data(data)

# Convert the dictionary back to a JSON string
masked_json_payload = json.dumps(data, indent=2)
print(masked_json_payload)

Explanation:

Regex Patterns:

  • Email: Matches standard email formats.
  • Phone: Matches a 10-digit phone number.
  • Name: A simple regex to match "FirstName LastName". This can be adjusted based on name formats you expect.

Function mask_data:

  • Recursively checks each value in the JSON structure.
  • Applies masking based on the regex patterns.

Execution:

  • Converts the JSON payload to a dictionary.
  • Applies the mask_data function.
  • Converts the dictionary back to a JSON string and prints it.

Customization

  • Name Regex: The name_regex pattern can be customized to better match your specific name formats.
  • Masking Strings: Adjust the replacement strings ("***** ", "@*****.com", "**********") as needed.
EXPERT
répondu il y a 4 mois

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions