Skip to content

Commit

Permalink
Update DLP snippet with proposal for new sample format.
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtisvg committed Oct 15, 2018
1 parent 89ca142 commit 8ba8b0b
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 184 deletions.
4 changes: 1 addition & 3 deletions dlp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.0.9</version>
<relativePath></relativePath>
<version>1.0.10</version>
</parent>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<google.auth.version>0.7.1</google.auth.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down
182 changes: 1 addition & 181 deletions dlp/src/main/java/com/example/dlp/Inspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,165 +72,6 @@

public class Inspect {

/**
* [START dlp_inspect_string] Inspect a text for given InfoTypes
*
* @param string String to instpect
* @param minLikelihood The minimum likelihood required before returning a match
* @param maxFindings The maximum number of findings to report (0 = server maximum)
* @param infoTypes The infoTypes of information to match
* @param includeQuote Whether to include the matching string
* @param projectId Google Cloud project ID
*/
private static void inspectString(
String string,
Likelihood minLikelihood,
int maxFindings,
List<InfoType> infoTypes,
List<CustomInfoType> customInfoTypes,
boolean includeQuote,
String projectId) {
// instantiate a client
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
FindingLimits findingLimits =
FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
InspectConfig inspectConfig =
InspectConfig.newBuilder()
.addAllInfoTypes(infoTypes)
.addAllCustomInfoTypes(customInfoTypes)
.setMinLikelihood(minLikelihood)
.setLimits(findingLimits)
.setIncludeQuote(includeQuote)
.build();

ByteContentItem byteContentItem =
ByteContentItem.newBuilder()
.setType(ByteContentItem.BytesType.TEXT_UTF8)
.setData(ByteString.copyFromUtf8(string))
.build();

ContentItem contentItem = ContentItem.newBuilder().setByteItem(byteContentItem).build();

InspectContentRequest request =
InspectContentRequest.newBuilder()
.setParent(ProjectName.of(projectId).toString())
.setInspectConfig(inspectConfig)
.setItem(contentItem)
.build();
InspectContentResponse response = dlpServiceClient.inspectContent(request);

if (response.getResult().getFindingsCount() > 0) {
System.out.println("Findings: ");
for (Finding finding : response.getResult().getFindingsList()) {
if (includeQuote) {
System.out.print("\tQuote: " + finding.getQuote());
}
System.out.print("\tInfo type: " + finding.getInfoType().getName());
System.out.println("\tLikelihood: " + finding.getLikelihood());
}
} else {
System.out.println("No findings.");
}
} catch (Exception e) {
System.out.println("Error in inspectString: " + e.getMessage());
}
}
// [END dlp_inspect_string]

// [START dlp_inspect_file]
/**
* Inspect a local file
*
* @param filePath The path to a local file to inspect. Can be a text, JPG, or PNG file.
* @param minLikelihood The minimum likelihood required before returning a match
* @param maxFindings The maximum number of findings to report (0 = server maximum)
* @param infoTypes The infoTypes of information to match
* @param includeQuote Whether to include the matching string
* @param projectId Google Cloud project ID
*/
private static void inspectFile(
String filePath,
Likelihood minLikelihood,
int maxFindings,
List<InfoType> infoTypes,
List<CustomInfoType> customInfoTypes,
boolean includeQuote,
String projectId) {
// Instantiates a client
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
// detect file mime type, default to application/octet-stream
String mimeType = URLConnection.guessContentTypeFromName(filePath);
if (mimeType == null) {
mimeType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
}

ByteContentItem.BytesType bytesType;
switch (mimeType) {
case "image/jpeg":
bytesType = ByteContentItem.BytesType.IMAGE_JPEG;
break;
case "image/bmp":
bytesType = ByteContentItem.BytesType.IMAGE_BMP;
break;
case "image/png":
bytesType = ByteContentItem.BytesType.IMAGE_PNG;
break;
case "image/svg":
bytesType = ByteContentItem.BytesType.IMAGE_SVG;
break;
default:
bytesType = ByteContentItem.BytesType.BYTES_TYPE_UNSPECIFIED;
break;
}

byte[] data = Files.readAllBytes(Paths.get(filePath));
ByteContentItem byteContentItem =
ByteContentItem.newBuilder()
.setType(bytesType)
.setData(ByteString.copyFrom(data))
.build();
ContentItem contentItem = ContentItem.newBuilder().setByteItem(byteContentItem).build();

FindingLimits findingLimits =
FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();

InspectConfig inspectConfig =
InspectConfig.newBuilder()
.addAllInfoTypes(infoTypes)
.addAllCustomInfoTypes(customInfoTypes)
.setMinLikelihood(minLikelihood)
.setLimits(findingLimits)
.setIncludeQuote(includeQuote)
.build();

InspectContentRequest request =
InspectContentRequest.newBuilder()
.setParent(ProjectName.of(projectId).toString())
.setInspectConfig(inspectConfig)
.setItem(contentItem)
.build();

InspectContentResponse response = dlpServiceClient.inspectContent(request);

InspectResult result = response.getResult();
if (result.getFindingsCount() > 0) {
System.out.println("Findings: ");
for (Finding finding : result.getFindingsList()) {
if (includeQuote) {
System.out.print("\tQuote: " + finding.getQuote());
}
System.out.print("\tInfo type: " + finding.getInfoType().getName());
System.out.println("\tLikelihood: " + finding.getLikelihood());
}
} else {
System.out.println("No findings.");
}
} catch (Exception e) {
System.out.println("Error in inspectFile: " + e.getMessage());
}
}
// [END dlp_inspect_file]

// [START dlp_inspect_gcs]
/**
* Inspect GCS file for Info types and wait on job completion using Google Cloud Pub/Sub
Expand Down Expand Up @@ -756,28 +597,7 @@ public static void main(String[] args) throws Exception {
}

// string inspection
if (cmd.hasOption("s")) {
String val = cmd.getOptionValue(stringOption.getOpt());
inspectString(
val,
minLikelihood,
maxFindings,
infoTypesList,
customInfoTypesList,
includeQuote,
projectId);
} else if (cmd.hasOption("f")) {
String filePath = cmd.getOptionValue(fileOption.getOpt());
inspectFile(
filePath,
minLikelihood,
maxFindings,
infoTypesList,
customInfoTypesList,
includeQuote,
projectId);
// gcs file inspection
} else if (cmd.hasOption("gcs")) {
if (cmd.hasOption("gcs")) {
String bucketName = cmd.getOptionValue(bucketNameOption.getOpt());
String fileName = cmd.getOptionValue(gcsFileNameOption.getOpt());
inspectGcsFile(
Expand Down
91 changes: 91 additions & 0 deletions dlp/src/main/java/dlp/snippets/InspectFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dlp.snippets;

// [START dlp_inspect_file]
import com.google.cloud.dlp.v2.DlpServiceClient;
import com.google.privacy.dlp.v2.ByteContentItem;
import com.google.privacy.dlp.v2.ByteContentItem.BytesType;
import com.google.privacy.dlp.v2.ContentItem;
import com.google.privacy.dlp.v2.Finding;
import com.google.privacy.dlp.v2.InfoType;
import com.google.privacy.dlp.v2.InspectConfig;
import com.google.privacy.dlp.v2.InspectContentRequest;
import com.google.privacy.dlp.v2.InspectContentResponse;
import com.google.privacy.dlp.v2.ProjectName;
import com.google.protobuf.ByteString;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

public class InspectFile {

// Inspects the specified file.
public static void inspectFile(String projectId, String filePath, String fileType) {
// String projectId = "my-project-id";
// String filePath = "path/to/image.png";
// String fileType = "IMAGE"

// Initialize client with try-with-resources for automatic cleanup of background resources
try (DlpServiceClient dlp = DlpServiceClient.create()) {
// Set project for request
ProjectName project = ProjectName.of(projectId);

// Set content for request
ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath));
ByteContentItem byteItem = ByteContentItem.newBuilder()
.setType(BytesType.valueOf(fileType))
.setData(fileBytes)
.build();
ContentItem item = ContentItem.newBuilder()
.setByteItem(byteItem)
.build();

// Set required InfoTypes for inspection config
List<InfoType> infoTypes = new ArrayList<>();
// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
for (String typeName : new String[] {"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) {
infoTypes.add(InfoType.newBuilder().setName(typeName).build());
}

// Set the inspect configuration for request
InspectConfig config = InspectConfig.newBuilder()
.addAllInfoTypes(infoTypes)
.setIncludeQuote(true)
.build();

// Construct request
InspectContentRequest request = InspectContentRequest.newBuilder()
.setParent(project.toString())
.setItem(item)
.setInspectConfig(config)
.build();

// Run request and parse response
InspectContentResponse response = dlp.inspectContent(request);
System.out.println("Findings: " + response.getResult().getFindingsCount());
for (Finding f : response.getResult().getFindingsList()) {
System.out.println("\tQuote: " + f.getQuote());
System.out.println("\tInfo type: " + f.getInfoType().getName());
System.out.println("\tLikelihood: " + f.getLikelihood());
}
} catch (Exception e) {
System.out.println("Error during inspectFile: \n" + e.toString());
}
}
}
// [START dlp_inspect_file]
86 changes: 86 additions & 0 deletions dlp/src/main/java/dlp/snippets/InspectString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dlp.snippets;

// [START dlp_inspect_string]
import com.google.cloud.dlp.v2.DlpServiceClient;
import com.google.privacy.dlp.v2.ByteContentItem;
import com.google.privacy.dlp.v2.ByteContentItem.BytesType;
import com.google.privacy.dlp.v2.ContentItem;
import com.google.privacy.dlp.v2.Finding;
import com.google.privacy.dlp.v2.InfoType;
import com.google.privacy.dlp.v2.InspectConfig;
import com.google.privacy.dlp.v2.InspectContentRequest;
import com.google.privacy.dlp.v2.InspectContentResponse;
import com.google.privacy.dlp.v2.ProjectName;
import com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.List;

public class InspectString {

// Inspects the provided text.
public static void inspectString(String projectId, String textToInspect) {
// String projectId = "my-project-id";
// String textToInspect = "My name is Gary and my email is gary@example.com";

// Initialize client with try-with-resources for automatic cleanup of background resources
try (DlpServiceClient dlp = DlpServiceClient.create()) {
// Set project for request
ProjectName project = ProjectName.of(projectId);

// Set content for request
ByteContentItem byteItem = ByteContentItem.newBuilder()
.setType(BytesType.TEXT_UTF8)
.setData(ByteString.copyFromUtf8(textToInspect))
.build();
ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();

// Set required InfoTypes for inspection config
List<InfoType> infoTypes = new ArrayList<>();
// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
for (String typeName : new String[] {"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) {
infoTypes.add(InfoType.newBuilder().setName(typeName).build());
}

// Set the inspect configuration for request
InspectConfig config = InspectConfig.newBuilder()
.addAllInfoTypes(infoTypes)
.setIncludeQuote(true)
.build();

// Construct request
InspectContentRequest request = InspectContentRequest.newBuilder()
.setParent(project.toString())
.setItem(item)
.setInspectConfig(config)
.build();

// Run request and parse response
InspectContentResponse response = dlp.inspectContent(request);
System.out.println("Findings: " + response.getResult().getFindingsCount());
for (Finding f : response.getResult().getFindingsList()) {
System.out.println("\tQuote: " + f.getQuote());
System.out.println("\tInfo type: " + f.getInfoType());
System.out.println("\tLikelihood: " + f.getLikelihood());
}
} catch (Exception e) {
System.out.println("Error during inspectString: \n" + e.toString());
}
}
}
// [END dlp_inspect_string]
Loading

0 comments on commit 8ba8b0b

Please sign in to comment.