Skip to content

Commit f53ac56

Browse files
committed
Update DLP snippet with proposal for new sample format.
1 parent 89ca142 commit f53ac56

File tree

5 files changed

+261
-184
lines changed

5 files changed

+261
-184
lines changed

dlp/pom.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@
2929
<parent>
3030
<groupId>com.google.cloud.samples</groupId>
3131
<artifactId>shared-configuration</artifactId>
32-
<version>1.0.9</version>
33-
<relativePath></relativePath>
32+
<version>1.0.10</version>
3433
</parent>
3534

3635
<properties>
3736
<maven.compiler.source>1.8</maven.compiler.source>
3837
<maven.compiler.target>1.8</maven.compiler.target>
39-
<google.auth.version>0.7.1</google.auth.version>
4038
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4139
</properties>
4240

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

Lines changed: 1 addition & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -72,165 +72,6 @@
7272

7373
public class Inspect {
7474

75-
/**
76-
* [START dlp_inspect_string] Inspect a text for given InfoTypes
77-
*
78-
* @param string String to instpect
79-
* @param minLikelihood The minimum likelihood required before returning a match
80-
* @param maxFindings The maximum number of findings to report (0 = server maximum)
81-
* @param infoTypes The infoTypes of information to match
82-
* @param includeQuote Whether to include the matching string
83-
* @param projectId Google Cloud project ID
84-
*/
85-
private static void inspectString(
86-
String string,
87-
Likelihood minLikelihood,
88-
int maxFindings,
89-
List<InfoType> infoTypes,
90-
List<CustomInfoType> customInfoTypes,
91-
boolean includeQuote,
92-
String projectId) {
93-
// instantiate a client
94-
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
95-
FindingLimits findingLimits =
96-
FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
97-
InspectConfig inspectConfig =
98-
InspectConfig.newBuilder()
99-
.addAllInfoTypes(infoTypes)
100-
.addAllCustomInfoTypes(customInfoTypes)
101-
.setMinLikelihood(minLikelihood)
102-
.setLimits(findingLimits)
103-
.setIncludeQuote(includeQuote)
104-
.build();
105-
106-
ByteContentItem byteContentItem =
107-
ByteContentItem.newBuilder()
108-
.setType(ByteContentItem.BytesType.TEXT_UTF8)
109-
.setData(ByteString.copyFromUtf8(string))
110-
.build();
111-
112-
ContentItem contentItem = ContentItem.newBuilder().setByteItem(byteContentItem).build();
113-
114-
InspectContentRequest request =
115-
InspectContentRequest.newBuilder()
116-
.setParent(ProjectName.of(projectId).toString())
117-
.setInspectConfig(inspectConfig)
118-
.setItem(contentItem)
119-
.build();
120-
InspectContentResponse response = dlpServiceClient.inspectContent(request);
121-
122-
if (response.getResult().getFindingsCount() > 0) {
123-
System.out.println("Findings: ");
124-
for (Finding finding : response.getResult().getFindingsList()) {
125-
if (includeQuote) {
126-
System.out.print("\tQuote: " + finding.getQuote());
127-
}
128-
System.out.print("\tInfo type: " + finding.getInfoType().getName());
129-
System.out.println("\tLikelihood: " + finding.getLikelihood());
130-
}
131-
} else {
132-
System.out.println("No findings.");
133-
}
134-
} catch (Exception e) {
135-
System.out.println("Error in inspectString: " + e.getMessage());
136-
}
137-
}
138-
// [END dlp_inspect_string]
139-
140-
// [START dlp_inspect_file]
141-
/**
142-
* Inspect a local file
143-
*
144-
* @param filePath The path to a local file to inspect. Can be a text, JPG, or PNG file.
145-
* @param minLikelihood The minimum likelihood required before returning a match
146-
* @param maxFindings The maximum number of findings to report (0 = server maximum)
147-
* @param infoTypes The infoTypes of information to match
148-
* @param includeQuote Whether to include the matching string
149-
* @param projectId Google Cloud project ID
150-
*/
151-
private static void inspectFile(
152-
String filePath,
153-
Likelihood minLikelihood,
154-
int maxFindings,
155-
List<InfoType> infoTypes,
156-
List<CustomInfoType> customInfoTypes,
157-
boolean includeQuote,
158-
String projectId) {
159-
// Instantiates a client
160-
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
161-
// detect file mime type, default to application/octet-stream
162-
String mimeType = URLConnection.guessContentTypeFromName(filePath);
163-
if (mimeType == null) {
164-
mimeType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
165-
}
166-
167-
ByteContentItem.BytesType bytesType;
168-
switch (mimeType) {
169-
case "image/jpeg":
170-
bytesType = ByteContentItem.BytesType.IMAGE_JPEG;
171-
break;
172-
case "image/bmp":
173-
bytesType = ByteContentItem.BytesType.IMAGE_BMP;
174-
break;
175-
case "image/png":
176-
bytesType = ByteContentItem.BytesType.IMAGE_PNG;
177-
break;
178-
case "image/svg":
179-
bytesType = ByteContentItem.BytesType.IMAGE_SVG;
180-
break;
181-
default:
182-
bytesType = ByteContentItem.BytesType.BYTES_TYPE_UNSPECIFIED;
183-
break;
184-
}
185-
186-
byte[] data = Files.readAllBytes(Paths.get(filePath));
187-
ByteContentItem byteContentItem =
188-
ByteContentItem.newBuilder()
189-
.setType(bytesType)
190-
.setData(ByteString.copyFrom(data))
191-
.build();
192-
ContentItem contentItem = ContentItem.newBuilder().setByteItem(byteContentItem).build();
193-
194-
FindingLimits findingLimits =
195-
FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
196-
197-
InspectConfig inspectConfig =
198-
InspectConfig.newBuilder()
199-
.addAllInfoTypes(infoTypes)
200-
.addAllCustomInfoTypes(customInfoTypes)
201-
.setMinLikelihood(minLikelihood)
202-
.setLimits(findingLimits)
203-
.setIncludeQuote(includeQuote)
204-
.build();
205-
206-
InspectContentRequest request =
207-
InspectContentRequest.newBuilder()
208-
.setParent(ProjectName.of(projectId).toString())
209-
.setInspectConfig(inspectConfig)
210-
.setItem(contentItem)
211-
.build();
212-
213-
InspectContentResponse response = dlpServiceClient.inspectContent(request);
214-
215-
InspectResult result = response.getResult();
216-
if (result.getFindingsCount() > 0) {
217-
System.out.println("Findings: ");
218-
for (Finding finding : result.getFindingsList()) {
219-
if (includeQuote) {
220-
System.out.print("\tQuote: " + finding.getQuote());
221-
}
222-
System.out.print("\tInfo type: " + finding.getInfoType().getName());
223-
System.out.println("\tLikelihood: " + finding.getLikelihood());
224-
}
225-
} else {
226-
System.out.println("No findings.");
227-
}
228-
} catch (Exception e) {
229-
System.out.println("Error in inspectFile: " + e.getMessage());
230-
}
231-
}
232-
// [END dlp_inspect_file]
233-
23475
// [START dlp_inspect_gcs]
23576
/**
23677
* Inspect GCS file for Info types and wait on job completion using Google Cloud Pub/Sub
@@ -756,28 +597,7 @@ public static void main(String[] args) throws Exception {
756597
}
757598

758599
// string inspection
759-
if (cmd.hasOption("s")) {
760-
String val = cmd.getOptionValue(stringOption.getOpt());
761-
inspectString(
762-
val,
763-
minLikelihood,
764-
maxFindings,
765-
infoTypesList,
766-
customInfoTypesList,
767-
includeQuote,
768-
projectId);
769-
} else if (cmd.hasOption("f")) {
770-
String filePath = cmd.getOptionValue(fileOption.getOpt());
771-
inspectFile(
772-
filePath,
773-
minLikelihood,
774-
maxFindings,
775-
infoTypesList,
776-
customInfoTypesList,
777-
includeQuote,
778-
projectId);
779-
// gcs file inspection
780-
} else if (cmd.hasOption("gcs")) {
600+
if (cmd.hasOption("gcs")) {
781601
String bucketName = cmd.getOptionValue(bucketNameOption.getOpt());
782602
String fileName = cmd.getOptionValue(gcsFileNameOption.getOpt());
783603
inspectGcsFile(
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dlp.snippets;
18+
19+
// [START dlp_inspect_file]
20+
import com.google.cloud.dlp.v2.DlpServiceClient;
21+
import com.google.privacy.dlp.v2.ByteContentItem;
22+
import com.google.privacy.dlp.v2.ByteContentItem.BytesType;
23+
import com.google.privacy.dlp.v2.ContentItem;
24+
import com.google.privacy.dlp.v2.Finding;
25+
import com.google.privacy.dlp.v2.InfoType;
26+
import com.google.privacy.dlp.v2.InspectConfig;
27+
import com.google.privacy.dlp.v2.InspectContentRequest;
28+
import com.google.privacy.dlp.v2.InspectContentResponse;
29+
import com.google.privacy.dlp.v2.ProjectName;
30+
import com.google.protobuf.ByteString;
31+
import java.io.FileInputStream;
32+
import java.util.ArrayList;
33+
import java.util.List;
34+
35+
public class InspectFile {
36+
37+
// Inspects the specified file.
38+
public static void inspectFile(String projectId, String filePath, String fileType) {
39+
// String projectId = "my-project-id";
40+
// String filePath = "path/to/image.png";
41+
// String fileType = "IMAGE"
42+
43+
// Initialize client with try-with-resources for automatic cleanup of background resources
44+
try (DlpServiceClient dlp = DlpServiceClient.create()) {
45+
// Set project for request
46+
ProjectName project = ProjectName.of(projectId);
47+
48+
// Set content for request
49+
ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath));
50+
ByteContentItem byteItem = ByteContentItem.newBuilder()
51+
.setType(BytesType.valueOf(fileType))
52+
.setData(fileBytes)
53+
.build();
54+
ContentItem item = ContentItem.newBuilder()
55+
.setByteItem(byteItem)
56+
.build();
57+
58+
// Set required InfoTypes for inspection config
59+
List<InfoType> infoTypes = new ArrayList<>();
60+
// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
61+
for (String typeName : new String[] {"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) {
62+
infoTypes.add(InfoType.newBuilder().setName(typeName).build());
63+
}
64+
65+
// Set the inspect configuration for request
66+
InspectConfig config = InspectConfig.newBuilder()
67+
.addAllInfoTypes(infoTypes)
68+
.setIncludeQuote(true)
69+
.build();
70+
71+
// Construct request
72+
InspectContentRequest request = InspectContentRequest.newBuilder()
73+
.setParent(project.toString())
74+
.setItem(item)
75+
.setInspectConfig(config)
76+
.build();
77+
78+
// Run request and parse response
79+
InspectContentResponse response = dlp.inspectContent(request);
80+
System.out.println("Findings: " + response.getResult().getFindingsCount());
81+
for (Finding f : response.getResult().getFindingsList()) {
82+
System.out.println("\tQuote: " + f.getQuote());
83+
System.out.println("\tInfo type: " + f.getInfoType().getName());
84+
System.out.println("\tLikelihood: " + f.getLikelihood());
85+
}
86+
} catch (Exception e) {
87+
System.out.println("Error during inspectFile: \n" + e.toString());
88+
}
89+
}
90+
}
91+
// [START dlp_inspect_file]
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dlp.snippets;
18+
19+
// [START dlp_inspect_string]
20+
import com.google.cloud.dlp.v2.DlpServiceClient;
21+
import com.google.privacy.dlp.v2.ByteContentItem;
22+
import com.google.privacy.dlp.v2.ByteContentItem.BytesType;
23+
import com.google.privacy.dlp.v2.ContentItem;
24+
import com.google.privacy.dlp.v2.Finding;
25+
import com.google.privacy.dlp.v2.InfoType;
26+
import com.google.privacy.dlp.v2.InspectConfig;
27+
import com.google.privacy.dlp.v2.InspectContentRequest;
28+
import com.google.privacy.dlp.v2.InspectContentResponse;
29+
import com.google.privacy.dlp.v2.ProjectName;
30+
import com.google.protobuf.ByteString;
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
34+
public class InspectString {
35+
36+
// Inspects the provided text.
37+
public static void inspectString(String projectId, String textToInspect) {
38+
// String projectId = "my-project-id";
39+
// String textToInspect = "My name is Gary and my email is [email protected]";
40+
41+
// Initialize client with try-with-resources for automatic cleanup of background resources
42+
try (DlpServiceClient dlp = DlpServiceClient.create()) {
43+
// Set project for request
44+
ProjectName project = ProjectName.of(projectId);
45+
46+
// Set content for request
47+
ByteContentItem byteItem = ByteContentItem.newBuilder()
48+
.setType(BytesType.TEXT_UTF8)
49+
.setData(ByteString.copyFromUtf8(textToInspect))
50+
.build();
51+
ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();
52+
53+
// Set required InfoTypes for inspection config
54+
List<InfoType> infoTypes = new ArrayList<>();
55+
// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
56+
for (String typeName : new String[] {"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) {
57+
infoTypes.add(InfoType.newBuilder().setName(typeName).build());
58+
}
59+
60+
// Set the inspect configuration for request
61+
InspectConfig config = InspectConfig.newBuilder()
62+
.addAllInfoTypes(infoTypes)
63+
.setIncludeQuote(true)
64+
.build();
65+
66+
// Construct request
67+
InspectContentRequest request = InspectContentRequest.newBuilder()
68+
.setParent(project.toString())
69+
.setItem(item)
70+
.setInspectConfig(config)
71+
.build();
72+
73+
// Run request and parse response
74+
InspectContentResponse response = dlp.inspectContent(request);
75+
System.out.println("Findings: " + response.getResult().getFindingsCount());
76+
for (Finding f : response.getResult().getFindingsList()) {
77+
System.out.println("\tQuote: " + f.getQuote());
78+
System.out.println("\tInfo type: " + f.getInfoType().getName());
79+
System.out.println("\tLikelihood: " + f.getLikelihood());
80+
}
81+
} catch (Exception e) {
82+
System.out.println("Error during inspectString: \n" + e.toString());
83+
}
84+
}
85+
}
86+
// [END dlp_inspect_string]

0 commit comments

Comments
 (0)