use of com.google.privacy.dlp.v2.Likelihood in project java-docs-samples by GoogleCloudPlatform.
the class Redact method redactImage.
// [START dlp_redact_image]
/*
* Redact sensitive data from an image using the Data Loss Prevention API.
*
* @param filePath The path to a local file to inspect. Can be a JPG or PNG image file.
* @param minLikelihood The minimum likelihood required before redacting a match.
* @param infoTypes The infoTypes of information to redact.
* @param outputPath The local path to save the resulting image to.
* @param projectId The project ID to run the API call under.
*/
private static void redactImage(String filePath, Likelihood minLikelihood, List<InfoType> infoTypes, String outputPath, String projectId) throws Exception {
// Instantiate the DLP client
try (DlpServiceClient dlpClient = DlpServiceClient.create()) {
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));
InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setMinLikelihood(minLikelihood).build();
ByteContentItem byteContentItem = ByteContentItem.newBuilder().setType(bytesType).setData(ByteString.copyFrom(data)).build();
List<RedactImageRequest.ImageRedactionConfig> imageRedactionConfigs = infoTypes.stream().map(infoType -> RedactImageRequest.ImageRedactionConfig.newBuilder().setInfoType(infoType).build()).collect(Collectors.toList());
RedactImageRequest redactImageRequest = RedactImageRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).addAllImageRedactionConfigs(imageRedactionConfigs).setByteItem(byteContentItem).setInspectConfig(inspectConfig).build();
RedactImageResponse redactImageResponse = dlpClient.redactImage(redactImageRequest);
// redacted image data
ByteString redactedImageData = redactImageResponse.getRedactedImage();
FileOutputStream outputStream = new FileOutputStream(outputPath);
outputStream.write(redactedImageData.toByteArray());
outputStream.close();
}
}
use of com.google.privacy.dlp.v2.Likelihood in project java-docs-samples by GoogleCloudPlatform.
the class Redact method main.
// [END dlp_redact_image]
/**
* Command line application to redact strings, images using the Data Loss Prevention API.
*/
public static void main(String[] args) throws Exception {
Options commandLineOptions = new Options();
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 inputFilePathOption = Option.builder("f").hasArg(true).longOpt("inputFilePath").required(false).build();
commandLineOptions.addOption(inputFilePathOption);
Option outputFilePathOption = Option.builder("o").hasArg(true).longOpt("outputFilePath").required(false).build();
commandLineOptions.addOption(outputFilePathOption);
Option projectIdOption = Option.builder("projectId").hasArg(true).required(false).build();
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;
}
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());
}
}
Likelihood minLikelihood = Likelihood.valueOf(cmd.getOptionValue(minLikelihoodOption.getOpt(), Likelihood.LIKELIHOOD_UNSPECIFIED.name()));
String inputFilePath = cmd.getOptionValue(inputFilePathOption.getOpt());
String outputFilePath = cmd.getOptionValue(outputFilePathOption.getOpt());
String projectId = cmd.getOptionValue(projectIdOption.getOpt(), ServiceOptions.getDefaultProjectId());
redactImage(inputFilePath, minLikelihood, infoTypesList, outputFilePath, projectId);
}
use of com.google.privacy.dlp.v2.Likelihood in project java-docs-samples by GoogleCloudPlatform.
the class Templates method listInspectTemplates.
// [END dlp_create_inspect_template]
// [START dlp_list_inspect_templates]
/**
* List DLP inspection templates created in a given project
*
* @param projectId Google Cloud Project ID
*/
private static void listInspectTemplates(String projectId) {
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
ListInspectTemplatesRequest request = ListInspectTemplatesRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setPageSize(1).build();
ListInspectTemplatesPagedResponse response = dlpServiceClient.listInspectTemplates(request);
ListInspectTemplatesPage page = response.getPage();
ListInspectTemplatesResponse templatesResponse = page.getResponse();
for (InspectTemplate template : templatesResponse.getInspectTemplatesList()) {
System.out.printf("Template name: %s\n", template.getName());
if (template.getDisplayName() != null) {
System.out.printf("\tDisplay name: %s \n", template.getDisplayName());
System.out.printf("\tCreate time: %s \n", template.getCreateTime());
System.out.printf("\tUpdate time: %s \n", template.getUpdateTime());
// print inspection config
InspectConfig inspectConfig = template.getInspectConfig();
for (InfoType infoType : inspectConfig.getInfoTypesList()) {
System.out.printf("\tInfoType: %s\n", infoType.getName());
}
System.out.printf("\tMin likelihood: %s\n", inspectConfig.getMinLikelihood());
System.out.printf("\tLimits: %s\n", inspectConfig.getLimits().getMaxFindingsPerRequest());
}
}
} catch (Exception e) {
System.out.printf("Error creating template: %s", e.getMessage());
}
}
use of com.google.privacy.dlp.v2.Likelihood in project java-docs-samples by GoogleCloudPlatform.
the class Templates method createInspectTemplate.
// [START dlp_create_inspect_template]
/**
* Create a new DLP inspection configuration template.
*
* @param displayName (Optional) The human-readable name to give the template
* @param projectId Google Cloud Project ID to call the API under
* @param templateId (Optional) The name of the template to be created
* @param infoTypeList The infoTypes of information to match
* @param minLikelihood The minimum likelihood required before returning a match
* @param maxFindings The maximum number of findings to report per request (0 = server maximum)
*/
private static void createInspectTemplate(String displayName, String templateId, String description, String projectId, List<InfoType> infoTypeList, Likelihood minLikelihood, int maxFindings) {
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
FindingLimits findingLimits = FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
// Construct the inspection configuration for the template
InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypeList).setMinLikelihood(minLikelihood).setLimits(findingLimits).build();
InspectTemplate inspectTemplate = InspectTemplate.newBuilder().setInspectConfig(inspectConfig).setDisplayName(displayName).setDescription(description).build();
CreateInspectTemplateRequest createInspectTemplateRequest = CreateInspectTemplateRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setInspectTemplate(inspectTemplate).setTemplateId(templateId).build();
InspectTemplate response = dlpServiceClient.createInspectTemplate(createInspectTemplateRequest);
System.out.printf("Template created: %s", response.getName());
} catch (Exception e) {
System.out.printf("Error creating template: %s", e.getMessage());
}
}
use of com.google.privacy.dlp.v2.Likelihood in project java-docs-samples by GoogleCloudPlatform.
the class Triggers method main.
// [END dlp_delete_trigger]
/**
* Command line application to crate, list and delete triggers.
*/
public static void main(String[] args) throws Exception {
OptionGroup optionsGroup = new OptionGroup();
optionsGroup.setRequired(true);
Option createTriggerOption = new Option("c", "create", false, "Create trigger to scan a GCS bucket");
optionsGroup.addOption(createTriggerOption);
Option listTriggersOption = new Option("l", "list", false, "List triggers");
optionsGroup.addOption(listTriggersOption);
Option deleteTriggerOption = new Option("d", "delete", false, "Delete trigger");
optionsGroup.addOption(deleteTriggerOption);
Options commandLineOptions = new Options();
commandLineOptions.addOptionGroup(optionsGroup);
Option bucketNameOption = Option.builder("bucketName").hasArg(true).required(false).build();
commandLineOptions.addOption(bucketNameOption);
Option gcsFileNameOption = Option.builder("fileName").hasArg(true).required(false).build();
commandLineOptions.addOption(gcsFileNameOption);
Option minLikelihoodOption = Option.builder("minLikelihood").hasArg(true).required(false).build();
commandLineOptions.addOption(minLikelihoodOption);
Option maxFindingsOption = Option.builder("maxFindings").hasArg(true).required(false).build();
commandLineOptions.addOption(maxFindingsOption);
Option infoTypesOption = Option.builder("infoTypes").hasArg(true).required(false).build();
infoTypesOption.setArgs(Option.UNLIMITED_VALUES);
commandLineOptions.addOption(infoTypesOption);
Option projectIdOption = Option.builder("projectId").hasArg(true).required(false).build();
commandLineOptions.addOption(projectIdOption);
Option triggerIdOption = Option.builder("triggerId").hasArg(true).required(false).build();
commandLineOptions.addOption(triggerIdOption);
Option displayNameOption = Option.builder("displayName").hasArg(true).required(false).build();
commandLineOptions.addOption(displayNameOption);
Option descriptionOption = Option.builder("description").hasArg(true).required(false).build();
commandLineOptions.addOption(descriptionOption);
Option scanPeriodOption = Option.builder("scanPeriod").hasArg(true).required(false).build();
commandLineOptions.addOption(scanPeriodOption);
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(DeIdentification.class.getName(), commandLineOptions);
System.exit(1);
return;
}
String projectId = cmd.getOptionValue(projectIdOption.getOpt(), ServiceOptions.getDefaultProjectId());
if (cmd.hasOption("c")) {
Likelihood minLikelihood = Likelihood.valueOf(cmd.getOptionValue(minLikelihoodOption.getOpt(), Likelihood.LIKELIHOOD_UNSPECIFIED.name()));
int maxFindings = Integer.parseInt(cmd.getOptionValue(maxFindingsOption.getOpt(), "0"));
String triggerId = cmd.getOptionValue(triggerIdOption.getOpt());
String displayName = cmd.getOptionValue(displayNameOption.getOpt(), "");
String description = cmd.getOptionValue(descriptionOption.getOpt(), "");
String bucketName = cmd.getOptionValue(bucketNameOption.getOpt());
String fileName = cmd.getOptionValue(gcsFileNameOption.getOpt());
int scanPeriod = Integer.valueOf(cmd.getOptionValue(scanPeriodOption.getOpt()));
List<InfoType> infoTypesList = new ArrayList<>();
if (cmd.hasOption(infoTypesOption.getOpt())) {
infoTypesList = new ArrayList<>();
String[] infoTypes = cmd.getOptionValues(infoTypesOption.getOpt());
for (String infoType : infoTypes) {
infoTypesList.add(InfoType.newBuilder().setName(infoType).build());
}
}
createTrigger(triggerId, displayName, description, bucketName, fileName, scanPeriod, infoTypesList, minLikelihood, maxFindings, projectId);
} else if (cmd.hasOption("l")) {
// list triggers
listTriggers(projectId);
} else if (cmd.hasOption("d")) {
String triggerId = cmd.getOptionValue(triggerIdOption.getOpt());
deleteTrigger(projectId, triggerId);
}
}
Aggregations