Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propose new sample format. #1232

Merged
merged 14 commits into from
Feb 25, 2019
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 {
tswast marked this conversation as resolved.
Show resolved Hide resolved

/**
* [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")) {
kurtisvg marked this conversation as resolved.
Show resolved Hide resolved
String bucketName = cmd.getOptionValue(bucketNameOption.getOpt());
String fileName = cmd.getOptionValue(gcsFileNameOption.getOpt());
inspectGcsFile(
Expand Down
Loading