Search in sources :

Example 6 with Likelihood

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();
    }
}
Also used : ByteContentItem(com.google.privacy.dlp.v2.ByteContentItem) Options(org.apache.commons.cli.Options) Likelihood(com.google.privacy.dlp.v2.Likelihood) HelpFormatter(org.apache.commons.cli.HelpFormatter) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) RedactImageResponse(com.google.privacy.dlp.v2.RedactImageResponse) ArrayList(java.util.ArrayList) DefaultParser(org.apache.commons.cli.DefaultParser) ServiceOptions(com.google.cloud.ServiceOptions) URLConnection(java.net.URLConnection) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) Files(java.nio.file.Files) CommandLineParser(org.apache.commons.cli.CommandLineParser) FileOutputStream(java.io.FileOutputStream) InfoType(com.google.privacy.dlp.v2.InfoType) Collectors(java.util.stream.Collectors) ByteString(com.google.protobuf.ByteString) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) List(java.util.List) ProjectName(com.google.privacy.dlp.v2.ProjectName) RedactImageRequest(com.google.privacy.dlp.v2.RedactImageRequest) Paths(java.nio.file.Paths) ParseException(org.apache.commons.cli.ParseException) ByteString(com.google.protobuf.ByteString) ByteContentItem(com.google.privacy.dlp.v2.ByteContentItem) ByteString(com.google.protobuf.ByteString) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) RedactImageRequest(com.google.privacy.dlp.v2.RedactImageRequest) FileOutputStream(java.io.FileOutputStream) RedactImageResponse(com.google.privacy.dlp.v2.RedactImageResponse)

Example 7 with Likelihood

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);
}
Also used : Options(org.apache.commons.cli.Options) ServiceOptions(com.google.cloud.ServiceOptions) Likelihood(com.google.privacy.dlp.v2.Likelihood) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) HelpFormatter(org.apache.commons.cli.HelpFormatter) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) InfoType(com.google.privacy.dlp.v2.InfoType) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 8 with Likelihood

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());
    }
}
Also used : ListInspectTemplatesResponse(com.google.privacy.dlp.v2.ListInspectTemplatesResponse) InspectTemplate(com.google.privacy.dlp.v2.InspectTemplate) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) ListInspectTemplatesRequest(com.google.privacy.dlp.v2.ListInspectTemplatesRequest) ListInspectTemplatesPage(com.google.cloud.dlp.v2.DlpServiceClient.ListInspectTemplatesPage) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) InfoType(com.google.privacy.dlp.v2.InfoType) ParseException(org.apache.commons.cli.ParseException) ListInspectTemplatesPagedResponse(com.google.cloud.dlp.v2.DlpServiceClient.ListInspectTemplatesPagedResponse)

Example 9 with Likelihood

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());
    }
}
Also used : FindingLimits(com.google.privacy.dlp.v2.InspectConfig.FindingLimits) InspectTemplate(com.google.privacy.dlp.v2.InspectTemplate) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) ParseException(org.apache.commons.cli.ParseException) CreateInspectTemplateRequest(com.google.privacy.dlp.v2.CreateInspectTemplateRequest)

Example 10 with Likelihood

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);
    }
}
Also used : Options(org.apache.commons.cli.Options) CloudStorageOptions(com.google.privacy.dlp.v2.CloudStorageOptions) ServiceOptions(com.google.cloud.ServiceOptions) Likelihood(com.google.privacy.dlp.v2.Likelihood) ArrayList(java.util.ArrayList) HelpFormatter(org.apache.commons.cli.HelpFormatter) CommandLine(org.apache.commons.cli.CommandLine) OptionGroup(org.apache.commons.cli.OptionGroup) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) InfoType(com.google.privacy.dlp.v2.InfoType) DefaultParser(org.apache.commons.cli.DefaultParser)

Aggregations

ParseException (org.apache.commons.cli.ParseException)13 DlpServiceClient (com.google.cloud.dlp.v2.DlpServiceClient)10 InfoType (com.google.privacy.dlp.v2.InfoType)10 InspectConfig (com.google.privacy.dlp.v2.InspectConfig)10 Likelihood (com.google.privacy.dlp.v2.Likelihood)9 ServiceOptions (com.google.cloud.ServiceOptions)8 ByteString (com.google.protobuf.ByteString)8 CommandLine (org.apache.commons.cli.CommandLine)8 CommandLineParser (org.apache.commons.cli.CommandLineParser)8 DefaultParser (org.apache.commons.cli.DefaultParser)8 HelpFormatter (org.apache.commons.cli.HelpFormatter)8 Option (org.apache.commons.cli.Option)8 Options (org.apache.commons.cli.Options)8 ByteContentItem (com.google.privacy.dlp.v2.ByteContentItem)7 ArrayList (java.util.ArrayList)7 CloudStorageOptions (com.google.privacy.dlp.v2.CloudStorageOptions)6 ContentItem (com.google.privacy.dlp.v2.ContentItem)6 Finding (com.google.privacy.dlp.v2.Finding)6 FindingLimits (com.google.privacy.dlp.v2.InspectConfig.FindingLimits)6 InspectContentRequest (com.google.privacy.dlp.v2.InspectContentRequest)6