Skip to content

Commit 034b9fb

Browse files
jabubakechingor13
authored andcommitted
samples: DLP samples (#752)
1 parent 6ded200 commit 034b9fb

File tree

8 files changed

+902
-0
lines changed

8 files changed

+902
-0
lines changed

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

Lines changed: 444 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* Copyright 2017, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package com.example.dlp;
17+
18+
import com.google.cloud.dlp.v2beta1.DlpServiceClient;
19+
import com.google.privacy.dlp.v2beta1.CategoryDescription;
20+
import com.google.privacy.dlp.v2beta1.InfoTypeDescription;
21+
import com.google.privacy.dlp.v2beta1.ListInfoTypesResponse;
22+
import com.google.privacy.dlp.v2beta1.ListRootCategoriesResponse;
23+
import org.apache.commons.cli.CommandLine;
24+
import org.apache.commons.cli.CommandLineParser;
25+
import org.apache.commons.cli.DefaultParser;
26+
import org.apache.commons.cli.HelpFormatter;
27+
import org.apache.commons.cli.Option;
28+
import org.apache.commons.cli.Options;
29+
import org.apache.commons.cli.ParseException;
30+
31+
import java.util.List;
32+
33+
public class Metadata {
34+
35+
private static void listInfoTypes(String category, String languageCode) throws Exception {
36+
// [START dlp_list_info_types]
37+
// Instantiate a DLP client
38+
try (DlpServiceClient dlpClient = DlpServiceClient.create()) {
39+
// The category of info types to list, e.g. category = 'GOVERNMENT';
40+
// Optional BCP-47 language code for localized info type friendly names, e.g. 'en-US'
41+
ListInfoTypesResponse infoTypesResponse = dlpClient.listInfoTypes(category, languageCode);
42+
List<InfoTypeDescription> infoTypeDescriptions = infoTypesResponse.getInfoTypesList();
43+
for (InfoTypeDescription infoTypeDescription : infoTypeDescriptions) {
44+
System.out.println("Name : " + infoTypeDescription.getName());
45+
System.out.println("Display name : " + infoTypeDescription.getDisplayName());
46+
}
47+
}
48+
// [END dlp_list_info_types]
49+
}
50+
51+
private static void listRootCategories(String languageCode) throws Exception {
52+
// [START dlp_list_root_categories]
53+
// Instantiate a DLP client
54+
try (DlpServiceClient dlpClient = DlpServiceClient.create()) {
55+
// The BCP-47 language code to use, e.g. 'en-US'
56+
// languageCode = 'en-US'
57+
ListRootCategoriesResponse rootCategoriesResponse = dlpClient
58+
.listRootCategories(languageCode);
59+
for (CategoryDescription categoryDescription : rootCategoriesResponse.getCategoriesList()) {
60+
System.out.println("Name : " + categoryDescription.getName());
61+
System.out.println("Display name : " + categoryDescription.getDisplayName());
62+
}
63+
}
64+
// [END dlp_list_root_categories]
65+
}
66+
67+
public static void main(String[] args) throws Exception {
68+
Options options = new Options();
69+
Option languageCodeOption = new Option("language", null, true, "BCP-47 language code");
70+
languageCodeOption.setRequired(false);
71+
options.addOption(languageCodeOption);
72+
73+
Option categoryOption = new Option("category", null, true, "Category of info types to list.");
74+
categoryOption.setRequired(false);
75+
options.addOption(categoryOption);
76+
77+
CommandLineParser parser = new DefaultParser();
78+
HelpFormatter formatter = new HelpFormatter();
79+
CommandLine cmd;
80+
try {
81+
cmd = parser.parse(options, args);
82+
} catch (ParseException e) {
83+
System.out.println(e.getMessage());
84+
formatter.printHelp(Metadata.class.getName(), options);
85+
System.exit(1);
86+
return;
87+
}
88+
String languageCode = cmd.getOptionValue(languageCodeOption.getOpt(), "en-US");
89+
if (cmd.hasOption(categoryOption.getOpt())) {
90+
String category = cmd.getOptionValue(categoryOption.getOpt());
91+
listInfoTypes(category, languageCode);
92+
} else {
93+
listRootCategories(languageCode);
94+
}
95+
}
96+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/**
2+
* Copyright 2017, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package com.example.dlp;
17+
18+
import com.google.cloud.dlp.v2beta1.DlpServiceClient;
19+
import com.google.privacy.dlp.v2beta1.ContentItem;
20+
import com.google.privacy.dlp.v2beta1.InfoType;
21+
import com.google.privacy.dlp.v2beta1.InspectConfig;
22+
import com.google.privacy.dlp.v2beta1.Likelihood;
23+
import com.google.privacy.dlp.v2beta1.RedactContentRequest.ReplaceConfig;
24+
import com.google.privacy.dlp.v2beta1.RedactContentResponse;
25+
import com.google.protobuf.ByteString;
26+
import org.apache.commons.cli.CommandLine;
27+
import org.apache.commons.cli.CommandLineParser;
28+
import org.apache.commons.cli.DefaultParser;
29+
import org.apache.commons.cli.HelpFormatter;
30+
import org.apache.commons.cli.Option;
31+
import org.apache.commons.cli.Options;
32+
import org.apache.commons.cli.ParseException;
33+
34+
import java.util.ArrayList;
35+
import java.util.Collections;
36+
import java.util.List;
37+
38+
public class Redact {
39+
40+
private static void redactString(String string, String replacement, Likelihood minLikelihood,
41+
List<InfoType> infoTypes) throws Exception {
42+
// [START dlp_redact_string]
43+
// Instantiate the DLP client
44+
try (DlpServiceClient dlpClient = DlpServiceClient.create()) {
45+
// The minimum likelihood required before returning a match
46+
// eg.minLikelihood = LIKELIHOOD_VERY_LIKELY;
47+
InspectConfig inspectConfig = InspectConfig.newBuilder()
48+
.addAllInfoTypes(infoTypes)
49+
.setMinLikelihood(minLikelihood)
50+
.build();
51+
52+
ContentItem contentItem = ContentItem.newBuilder()
53+
.setType("text/plain")
54+
.setData(ByteString.copyFrom(string.getBytes()))
55+
.build();
56+
57+
List<ReplaceConfig> replaceConfigs = new ArrayList<>();
58+
59+
if (infoTypes.isEmpty()) {
60+
// replace all detected sensitive elements with replacement string
61+
replaceConfigs.add(
62+
ReplaceConfig.newBuilder()
63+
.setReplaceWith(replacement)
64+
.build());
65+
} else {
66+
// Replace select info types with chosen replacement string
67+
for (InfoType infoType : infoTypes) {
68+
replaceConfigs.add(
69+
ReplaceConfig.newBuilder()
70+
.setInfoType(infoType)
71+
.setReplaceWith(replacement)
72+
.build());
73+
}
74+
}
75+
76+
RedactContentResponse contentResponse = dlpClient.redactContent(
77+
inspectConfig, Collections.singletonList(contentItem), replaceConfigs);
78+
for (ContentItem responseItem : contentResponse.getItemsList()) {
79+
// print out string with redacted content
80+
System.out.println(responseItem.getData().toStringUtf8());
81+
}
82+
}
83+
// [END dlp_redact_string]
84+
}
85+
86+
// Command line application to redact strings using the Data Loss Prevention API
87+
public static void main(String[] args) throws Exception {
88+
Options commandLineOptions = new Options();
89+
90+
Option stringOption = Option.builder("s")
91+
.longOpt("source string")
92+
.hasArg(true)
93+
.required(true)
94+
.build();
95+
commandLineOptions.addOption(stringOption);
96+
97+
Option replaceOption = Option.builder("r")
98+
.longOpt("replace string")
99+
.hasArg(true)
100+
.required(true)
101+
.build();
102+
commandLineOptions.addOption(replaceOption);
103+
104+
Option minLikelihoodOption = Option.builder("minLikelihood")
105+
.hasArg(true)
106+
.required(false)
107+
.build();
108+
commandLineOptions.addOption(minLikelihoodOption);
109+
110+
Option infoTypesOption = Option.builder("infoTypes")
111+
.hasArg(true)
112+
.required(false)
113+
.build();
114+
infoTypesOption.setArgs(Option.UNLIMITED_VALUES);
115+
commandLineOptions.addOption(infoTypesOption);
116+
117+
CommandLineParser parser = new DefaultParser();
118+
HelpFormatter formatter = new HelpFormatter();
119+
CommandLine cmd;
120+
121+
try {
122+
cmd = parser.parse(commandLineOptions, args);
123+
} catch (ParseException e) {
124+
System.out.println(e.getMessage());
125+
formatter.printHelp(Redact.class.getName(), commandLineOptions);
126+
System.exit(1);
127+
return;
128+
}
129+
130+
String source = cmd.getOptionValue(stringOption.getOpt());
131+
String replacement = cmd.getOptionValue(replaceOption.getOpt());
132+
133+
List<InfoType> infoTypesList = new ArrayList<>();
134+
String[] infoTypes = cmd.getOptionValues(infoTypesOption.getOpt());
135+
if (infoTypes != null) {
136+
for (String infoType : infoTypes) {
137+
infoTypesList.add(InfoType.newBuilder().setName(infoType).build());
138+
}
139+
}
140+
redactString(source, replacement, Likelihood.LIKELIHOOD_UNSPECIFIED, infoTypesList);
141+
}
142+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* Copyright 2017, Google, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you
3+
* may not use this file except in compliance with the License. You may obtain a copy of the License
4+
* at
5+
*
6+
* <p>http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
9+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
10+
* express or implied. See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
package com.example.dlp;
14+
15+
import static org.junit.Assert.assertNotNull;
16+
import static org.junit.Assert.assertTrue;
17+
18+
import org.junit.After;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.junit.runners.JUnit4;
23+
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.File;
26+
import java.io.PrintStream;
27+
28+
@RunWith(JUnit4.class)
29+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
30+
public class InspectIT {
31+
private ByteArrayOutputStream bout;
32+
private PrintStream out;
33+
34+
// Update to Google Cloud Storage path containing test.txt
35+
private String bucketName = System.getenv("GOOGLE_CLOUD_PROJECT") + "/dlp";
36+
37+
// Update to Google Cloud Datastore Kind containing an entity
38+
// with phone number and email address properties.
39+
private String datastoreKind = "dlp";
40+
41+
@Before
42+
public void setUp() {
43+
bout = new ByteArrayOutputStream();
44+
out = new PrintStream(bout);
45+
System.setOut(out);
46+
assertNotNull(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));
47+
}
48+
49+
@Test
50+
public void testStringInspectionReturnsInfoTypes() throws Exception {
51+
String text =
52+
"\"My phone number is (234) 456-7890 and my email address is gary@somedomain.com\"";
53+
Inspect.main(new String[] {"-s", text});
54+
String output = bout.toString();
55+
assertTrue(output.contains("PHONE_NUMBER"));
56+
assertTrue(output.contains("EMAIL_ADDRESS"));
57+
}
58+
59+
@Test
60+
public void testTextFileInspectionReturnsInfoTypes() throws Exception {
61+
ClassLoader classLoader = getClass().getClassLoader();
62+
File file = new File(classLoader.getResource("test.txt").getFile());
63+
Inspect.main(new String[] {"-f", file.getAbsolutePath()});
64+
String output = bout.toString();
65+
assertTrue(output.contains("PHONE_NUMBER"));
66+
assertTrue(output.contains("EMAIL_ADDRESS"));
67+
}
68+
69+
@Test
70+
public void testImageFileInspectionReturnsInfoTypes() throws Exception {
71+
ClassLoader classLoader = getClass().getClassLoader();
72+
File file = new File(classLoader.getResource("test.png").getFile());
73+
Inspect.main(new String[] {"-f", file.getAbsolutePath()});
74+
String output = bout.toString();
75+
assertTrue(output.contains("PHONE_NUMBER"));
76+
assertTrue(output.contains("EMAIL_ADDRESS"));
77+
}
78+
79+
// Requires that bucket by the specified name exists
80+
@Test
81+
public void testGcsFileInspectionReturnsInfoTypes() throws Exception {
82+
Inspect.main(new String[] {"-gcs", "-bucketName", bucketName, "-fileName", "test.txt"});
83+
String output = bout.toString();
84+
assertTrue(output.contains("PHONE_NUMBER"));
85+
assertTrue(output.contains("EMAIL_ADDRESS"));
86+
}
87+
88+
// Requires a Datastore kind containing an entity
89+
// with phone number and email address properties.
90+
@Test
91+
public void testDatastoreInspectionReturnsInfoTypes() throws Exception {
92+
Inspect.main(new String[] {"-ds", "-kind", datastoreKind});
93+
String output = bout.toString();
94+
assertTrue(output.contains("PHONE_NUMBER"));
95+
assertTrue(output.contains("EMAIL_ADDRESS"));
96+
}
97+
98+
@After
99+
public void tearDown() {
100+
System.setOut(null);
101+
bout.reset();
102+
}
103+
}

0 commit comments

Comments
 (0)