def redact_dlp_item()

in data-loss-prevention/code/redact/app.py [0:0]


def redact_dlp_item(project_id: str, inspect_item: str):
    """Redact sensitive data from a given string using Google Cloud DLP API.

    This function uses the Google Cloud DLP API to identify and redact
    sensitive information within a given string. It supports redacting
    the following data types:

    - PERSON_NAME
    - US_SOCIAL_SECURITY_NUMBER
    - EMAIL_ADDRESS
    - PHONE_NUMBER

    Args:
        project_id (str): The Google Cloud project ID.
        inspect_item (str): The string to be inspected and redacted.

    Returns:
        str: The redacted string with sensitive data
             replaced by "[SENSITIVE DATA]".
    """
    client = dlp_v2.DlpServiceClient()

    parent = f"projects/{project_id}"
    item = {"value": inspect_item}
    inspect_config = {
        "info_types": [
            {"name": "PERSON_NAME"},
            {"name": "US_SOCIAL_SECURITY_NUMBER"},
            {"name": "EMAIL_ADDRESS"},
            {"name": "PHONE_NUMBER"}
        ]
    }
    deidentify_config = {
      "info_type_transformations": {
          "transformations": [
              {
                  "primitive_transformation": {
                      "replace_config": {
                          "new_value": {"string_value": "[SENSITIVE DATA]"}
                      }
                  }
              }
          ]
      }
    }

    response = client.deidentify_content(
        request={
            "parent": parent,
            "deidentify_config": deidentify_config,
            "inspect_config": inspect_config,
            "item": item,
        }
    )

    return response.item.value