use of com.google.privacy.dlp.v2.Likelihood in project java-docs-samples by GoogleCloudPlatform.
the class Inspect method inspectGcsFile.
// [END dlp_inspect_file]
// [START dlp_inspect_gcs]
/**
* Inspect GCS file for Info types and wait on job completion using Google Cloud Pub/Sub
* notification
*
* @param bucketName The name of the bucket where the file resides.
* @param fileName The path to the file within the bucket to inspect (can include wildcards, eg.
* my-image.*)
* @param minLikelihood The minimum likelihood required before returning a match
* @param infoTypes The infoTypes of information to match
* @param maxFindings The maximum number of findings to report (0 = server maximum)
* @param topicId Google Cloud Pub/Sub topic Id to notify of job status
* @param subscriptionId Google Cloud Subscription to above topic to listen for job status updates
* @param projectId Google Cloud project ID
*/
private static void inspectGcsFile(String bucketName, String fileName, Likelihood minLikelihood, List<InfoType> infoTypes, int maxFindings, String topicId, String subscriptionId, String projectId) throws Exception {
// Instantiates a client
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
CloudStorageOptions cloudStorageOptions = CloudStorageOptions.newBuilder().setFileSet(CloudStorageOptions.FileSet.newBuilder().setUrl("gs://" + bucketName + "/" + fileName)).build();
StorageConfig storageConfig = StorageConfig.newBuilder().setCloudStorageOptions(cloudStorageOptions).build();
FindingLimits findingLimits = FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setMinLikelihood(minLikelihood).setLimits(findingLimits).build();
String pubSubTopic = String.format("projects/%s/topics/%s", projectId, topicId);
Action.PublishToPubSub publishToPubSub = Action.PublishToPubSub.newBuilder().setTopic(pubSubTopic).build();
Action action = Action.newBuilder().setPubSub(publishToPubSub).build();
InspectJobConfig inspectJobConfig = InspectJobConfig.newBuilder().setStorageConfig(storageConfig).setInspectConfig(inspectConfig).addActions(action).build();
// Semi-synchronously submit an inspect job, and wait on results
CreateDlpJobRequest createDlpJobRequest = CreateDlpJobRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setInspectJob(inspectJobConfig).build();
DlpJob dlpJob = dlpServiceClient.createDlpJob(createDlpJobRequest);
System.out.println("Job created with ID:" + dlpJob.getName());
final SettableApiFuture<Boolean> done = SettableApiFuture.create();
// Set up a Pub/Sub subscriber to listen on the job completion status
Subscriber subscriber = Subscriber.newBuilder(ProjectSubscriptionName.of(projectId, subscriptionId), (pubsubMessage, ackReplyConsumer) -> {
if (pubsubMessage.getAttributesCount() > 0 && pubsubMessage.getAttributesMap().get("DlpJobName").equals(dlpJob.getName())) {
// notify job completion
done.set(true);
ackReplyConsumer.ack();
}
}).build();
subscriber.startAsync();
// For long jobs, consider using a truly asynchronous execution model such as Cloud Functions
try {
done.get(1, TimeUnit.MINUTES);
// Wait for the job to become available
Thread.sleep(500);
} catch (Exception e) {
System.out.println("Unable to verify job completion.");
}
DlpJob completedJob = dlpServiceClient.getDlpJob(GetDlpJobRequest.newBuilder().setName(dlpJob.getName()).build());
System.out.println("Job status: " + completedJob.getState());
InspectDataSourceDetails inspectDataSourceDetails = completedJob.getInspectDetails();
InspectDataSourceDetails.Result result = inspectDataSourceDetails.getResult();
if (result.getInfoTypeStatsCount() > 0) {
System.out.println("Findings: ");
for (InfoTypeStats infoTypeStat : result.getInfoTypeStatsList()) {
System.out.print("\tInfo type: " + infoTypeStat.getInfoType().getName());
System.out.println("\tCount: " + infoTypeStat.getCount());
}
} else {
System.out.println("No findings.");
}
}
}
use of com.google.privacy.dlp.v2.Likelihood in project java-docs-samples by GoogleCloudPlatform.
the class Inspect method inspectFile.
// [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, 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).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());
}
}
use of com.google.privacy.dlp.v2.Likelihood in project java-docs-samples by GoogleCloudPlatform.
the class Templates method main.
// [END dlp_delete_inspect_template]
/**
* Command line application to create, list and delete DLP inspect templates.
*/
public static void main(String[] args) throws Exception {
OptionGroup optionsGroup = new OptionGroup();
optionsGroup.setRequired(true);
Option createOption = new Option("c", "create", false, "Create inspect template");
optionsGroup.addOption(createOption);
Option listOption = new Option("l", "list", false, "List inspect templates");
optionsGroup.addOption(listOption);
Option deleteOption = new Option("d", "delete", false, "Delete inspect template");
optionsGroup.addOption(deleteOption);
Options commandLineOptions = new Options();
commandLineOptions.addOptionGroup(optionsGroup);
Option projectIdOption = Option.builder("projectId").hasArg(true).required(false).build();
commandLineOptions.addOption(projectIdOption);
Option minLikelihoodOption = Option.builder("minLikelihood").hasArg(true).required(false).build();
commandLineOptions.addOption(minLikelihoodOption);
Option infoTypesOption = Option.builder("infoTypes").hasArg(true).required(false).build();
infoTypesOption.setArgs(Option.UNLIMITED_VALUES);
commandLineOptions.addOption(infoTypesOption);
Option templateIdOption = Option.builder("templateId").hasArg(true).required(false).build();
commandLineOptions.addOption(templateIdOption);
Option templateDescription = Option.builder("description").hasArg(true).required(false).build();
commandLineOptions.addOption(templateDescription);
Option templateDisplayNameOption = Option.builder("displayName").hasArg(true).required(false).build();
commandLineOptions.addOption(templateDisplayNameOption);
Option includeQuoteOption = Option.builder("includeQuote").hasArg(true).required(false).build();
commandLineOptions.addOption(includeQuoteOption);
Option maxFindingsOption = Option.builder("maxFindings").hasArg(true).required(false).build();
commandLineOptions.addOption(maxFindingsOption);
CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
CommandLine cmd;
try {
cmd = parser.parse(commandLineOptions, args);
} catch (ParseException e) {
System.out.println(e.getMessage());
formatter.printHelp(Redact.class.getName(), commandLineOptions);
System.exit(1);
return;
}
String projectId = cmd.getOptionValue(projectIdOption.getOpt(), ServiceOptions.getDefaultProjectId());
if (cmd.hasOption(createOption.getOpt())) {
String templateId = cmd.getOptionValue(templateIdOption.getOpt());
String displayName = cmd.getOptionValue(templateDisplayNameOption.getOpt());
String description = cmd.getOptionValue(templateDescription.getOpt());
Likelihood minLikelihood = Likelihood.valueOf(cmd.getOptionValue(minLikelihoodOption.getOpt(), Likelihood.LIKELIHOOD_UNSPECIFIED.name()));
List<InfoType> infoTypesList = new ArrayList<>();
String[] infoTypes = cmd.getOptionValues(infoTypesOption.getOpt());
if (infoTypes != null) {
for (String infoType : infoTypes) {
infoTypesList.add(InfoType.newBuilder().setName(infoType).build());
}
}
int maxFindings = Integer.valueOf(cmd.getOptionValue(maxFindingsOption.getOpt(), "0"));
createInspectTemplate(displayName, templateId, description, projectId, infoTypesList, minLikelihood, maxFindings);
} else if (cmd.hasOption(listOption.getOpt())) {
listInspectTemplates(projectId);
} else if (cmd.hasOption(deleteOption.getOpt())) {
String templateId = cmd.getOptionValue(templateIdOption.getOpt());
deleteInspectTemplate(projectId, templateId);
}
}
use of com.google.privacy.dlp.v2.Likelihood in project java-docs-samples by GoogleCloudPlatform.
the class Triggers method createTrigger.
// [START dlp_create_trigger]
/**
* Schedule a DLP inspection trigger for a GCS location.
*
* @param triggerId (Optional) name of the trigger to be created
* @param displayName (Optional) display name for the trigger to be created
* @param description (Optional) description for the trigger to be created
* @param scanPeriod How often to wait between scans, in days (minimum = 1 day)
* @param infoTypes infoTypes of information to match eg. InfoType.PHONE_NUMBER,
* InfoType.EMAIL_ADDRESS
* @param minLikelihood minimum likelihood required before returning a match
* @param maxFindings maximum number of findings to report per request (0 = server maximum)
* @param projectId The project ID to run the API call under
*/
private static void createTrigger(String triggerId, String displayName, String description, String bucketName, String fileName, int scanPeriod, List<InfoType> infoTypes, Likelihood minLikelihood, int maxFindings, String projectId) throws Exception {
// instantiate a client
DlpServiceClient dlpServiceClient = DlpServiceClient.create();
try {
CloudStorageOptions cloudStorageOptions = CloudStorageOptions.newBuilder().setFileSet(CloudStorageOptions.FileSet.newBuilder().setUrl("gs://" + bucketName + "/" + fileName)).build();
StorageConfig storageConfig = StorageConfig.newBuilder().setCloudStorageOptions(cloudStorageOptions).build();
InspectConfig.FindingLimits findingLimits = InspectConfig.FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setMinLikelihood(minLikelihood).setLimits(findingLimits).build();
InspectJobConfig inspectJobConfig = InspectJobConfig.newBuilder().setInspectConfig(inspectConfig).setStorageConfig(storageConfig).build();
// Schedule scan of GCS bucket every scanPeriod number of days (minimum = 1 day)
Duration duration = Duration.newBuilder().setSeconds(scanPeriod * 24 * 3600).build();
Schedule schedule = Schedule.newBuilder().setRecurrencePeriodDuration(duration).build();
JobTrigger.Trigger trigger = JobTrigger.Trigger.newBuilder().setSchedule(schedule).build();
JobTrigger jobTrigger = JobTrigger.newBuilder().setInspectJob(inspectJobConfig).setName(triggerId).setDisplayName(displayName).setDescription(description).setStatus(JobTrigger.Status.HEALTHY).addTriggers(trigger).build();
// Create scan request
CreateJobTriggerRequest createJobTriggerRequest = CreateJobTriggerRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setJobTrigger(jobTrigger).build();
JobTrigger createdJobTrigger = dlpServiceClient.createJobTrigger(createJobTriggerRequest);
System.out.println("Created Trigger: " + createdJobTrigger.getName());
} catch (Exception e) {
System.out.println("Error creating trigger: " + e.getMessage());
}
}
Aggregations