Skip to content

Commit 5f7b982

Browse files
authored
Add code sample and tests for redaction (#4037)
Add A DLP code sample for redacting text. Code will be linked to this documentation: https://cloud.google.com/dlp/docs/deidentify-sensitive-data
1 parent 6da5a37 commit 5f7b982

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

dlp/deid.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,64 @@ def deidentify_with_mask(
8383

8484
# [END dlp_deidentify_masking]
8585

86+
# [START dlp_deidentify_redact]
87+
def deidentify_with_redact(
88+
project,
89+
input_str,
90+
info_types,
91+
):
92+
"""Uses the Data Loss Prevention API to deidentify sensitive data in a
93+
string by redacting matched input values.
94+
Args:
95+
project: The Google Cloud project id to use as a parent resource.
96+
input_str: The string to deidentify (will be treated as text).
97+
info_types: A list of strings representing info types to look for.
98+
Returns:
99+
None; the response from the API is printed to the terminal.
100+
"""
101+
import google.cloud.dlp
102+
103+
# Instantiate a client
104+
dlp = google.cloud.dlp_v2.DlpServiceClient()
105+
106+
# Convert the project id into a full resource id.
107+
parent = dlp.project_path(project)
108+
109+
# Construct inspect configuration dictionary
110+
inspect_config = {
111+
"info_types": [{"name": info_type} for info_type in info_types]
112+
}
113+
114+
# Construct deidentify configuration dictionary
115+
deidentify_config = {
116+
"info_type_transformations": {
117+
"transformations": [
118+
{
119+
"primitive_transformation": {
120+
"redact_config": {}
121+
}
122+
}
123+
]
124+
}
125+
}
126+
127+
# Construct item
128+
item = {"value": input_str}
129+
130+
# Call the API
131+
response = dlp.deidentify_content(
132+
parent,
133+
inspect_config=inspect_config,
134+
deidentify_config=deidentify_config,
135+
item=item,
136+
)
137+
138+
# Print out the results.
139+
print(response.item.value)
140+
141+
142+
# [END dlp_deidentify_redact]
143+
86144
# [START dlp_deidentify_replace]
87145
def deidentify_with_replace(
88146
project,

dlp/deid_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ def test_deidentify_with_mask_masking_number_specified(capsys):
8888
assert "My SSN is *******27" in out
8989

9090

91+
def test_deidentify_with_redact(capsys):
92+
deid.deidentify_with_redact(
93+
GCLOUD_PROJECT, HARMFUL_STRING + "!", ["US_SOCIAL_SECURITY_NUMBER"]
94+
)
95+
out, _ = capsys.readouterr()
96+
assert "My SSN is !" in out
97+
98+
9199
def test_deidentify_with_replace(capsys):
92100
deid.deidentify_with_replace(
93101
GCLOUD_PROJECT, HARMFUL_STRING, ["US_SOCIAL_SECURITY_NUMBER"],

0 commit comments

Comments
 (0)