Skip to content

Commit 5bd1bf6

Browse files
jmorrisechingor13
authored andcommitted
samples: add replace with info type examples, and update README (#1299)
1 parent dd5de07 commit 5bd1bf6

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

dlp/snippets/snippets/src/main/java/com/example/dlp/DeIdentification.java

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.google.privacy.dlp.v2.RecordTransformations;
4343
import com.google.privacy.dlp.v2.ReidentifyContentRequest;
4444
import com.google.privacy.dlp.v2.ReidentifyContentResponse;
45+
import com.google.privacy.dlp.v2.ReplaceWithInfoTypeConfig;
4546
import com.google.privacy.dlp.v2.Table;
4647
import com.google.privacy.dlp.v2.Value;
4748
import com.google.protobuf.ByteString;
@@ -71,6 +72,71 @@
7172

7273
public class DeIdentification {
7374

75+
// [START dlp_deidentify_replace_with_info_type]
76+
/**
77+
* Deidentify a string by replacing sensitive information with its info type using the DLP API.
78+
*
79+
* @param string The string to deidentify.
80+
* @param projectId ID of Google Cloud project to run the API under.
81+
*/
82+
private static void deIdentifyReplaceWithInfoType(
83+
String string,
84+
List<InfoType> infoTypes,
85+
String projectId) {
86+
87+
// instantiate a client
88+
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
89+
90+
ContentItem contentItem = ContentItem.newBuilder().setValue(string).build();
91+
92+
// Create the deidentification transformation configuration
93+
PrimitiveTransformation primitiveTransformation =
94+
PrimitiveTransformation.newBuilder()
95+
.setReplaceWithInfoTypeConfig(ReplaceWithInfoTypeConfig.getDefaultInstance())
96+
.build();
97+
98+
InfoTypeTransformation infoTypeTransformationObject =
99+
InfoTypeTransformation.newBuilder()
100+
.setPrimitiveTransformation(primitiveTransformation)
101+
.build();
102+
103+
InfoTypeTransformations infoTypeTransformationArray =
104+
InfoTypeTransformations.newBuilder()
105+
.addTransformations(infoTypeTransformationObject)
106+
.build();
107+
108+
InspectConfig inspectConfig =
109+
InspectConfig.newBuilder()
110+
.addAllInfoTypes(infoTypes)
111+
.build();
112+
113+
DeidentifyConfig deidentifyConfig =
114+
DeidentifyConfig.newBuilder()
115+
.setInfoTypeTransformations(infoTypeTransformationArray)
116+
.build();
117+
118+
// Create the deidentification request object
119+
DeidentifyContentRequest request =
120+
DeidentifyContentRequest.newBuilder()
121+
.setParent(ProjectName.of(projectId).toString())
122+
.setInspectConfig(inspectConfig)
123+
.setDeidentifyConfig(deidentifyConfig)
124+
.setItem(contentItem)
125+
.build();
126+
127+
// Execute the deidentification request
128+
DeidentifyContentResponse response = dlpServiceClient.deidentifyContent(request);
129+
130+
// Print the redacted input value
131+
// e.g. "My SSN is 123456789" --> "My SSN is [US_SOCIAL_SECURITY_NUMBER]"
132+
String result = response.getItem().getValue();
133+
System.out.println(result);
134+
} catch (Exception e) {
135+
System.out.println("Error in deIdentifyReplaceWithInfoType: " + e.getMessage());
136+
}
137+
}
138+
// [END dlp_deidentify_replace_with_info_type]
139+
74140
// [START dlp_deidentify_masking]
75141
/**
76142
* Deidentify a string by masking sensitive information with a character using the DLP API.
@@ -512,6 +578,10 @@ public static void main(String[] args) throws Exception {
512578
OptionGroup optionsGroup = new OptionGroup();
513579
optionsGroup.setRequired(true);
514580

581+
Option deidentifyReplaceWithInfoTypeOption =
582+
new Option("it", "info_type_replace", true, "Deidentify by replacing with info type.");
583+
optionsGroup.addOption(deidentifyReplaceWithInfoTypeOption);
584+
515585
Option deidentifyMaskingOption =
516586
new Option("m", "mask", true, "Deidentify with character masking.");
517587
optionsGroup.addOption(deidentifyMaskingOption);
@@ -606,7 +676,11 @@ public static void main(String[] args) throws Exception {
606676
}
607677
}
608678

609-
if (cmd.hasOption("m")) {
679+
if (cmd.hasOption("it")) {
680+
// replace with info type
681+
String val = cmd.getOptionValue(deidentifyReplaceWithInfoTypeOption.getOpt());
682+
deIdentifyReplaceWithInfoType(val, infoTypesList, projectId);
683+
} else if (cmd.hasOption("m")) {
610684
// deidentification with character masking
611685
int numberToMask = Integer.parseInt(cmd.getOptionValue(numberToMaskOption.getOpt(), "0"));
612686
char maskingCharacter = cmd.getOptionValue(maskingCharacterOption.getOpt(), "*").charAt(0);

dlp/snippets/snippets/src/test/java/com/example/dlp/DeIdentificationIT.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ public void setUp() {
5757
assertNotNull(System.getenv("DLP_DEID_KEY_NAME"));
5858
}
5959

60+
@Test
61+
public void testDeidReplaceWithInfoType() throws Exception {
62+
String text = "\"My SSN is 372819127\"";
63+
DeIdentification.main(
64+
new String[] {
65+
"-it", text,
66+
"-infoTypes", "US_SOCIAL_SECURITY_NUMBER"
67+
});
68+
String output = bout.toString();
69+
assertThat(output, containsString("My SSN is [US_SOCIAL_SECURITY_NUMBER]"));
70+
}
71+
6072
@Test
6173
public void testDeidStringMasksCharacters() throws Exception {
6274
String text = "\"My SSN is 372819127\"";

0 commit comments

Comments
 (0)