com.google.privacy.dlp.v2.InspectContentResponse Java Examples

The following examples show how to use com.google.privacy.dlp.v2.InspectContentResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DLPInspectText.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * The transform converts the contents of input PCollection into {@link Table.Row}s and then calls
 * Cloud DLP service to perform the data inspection according to provided settings.
 *
 * @param input input PCollection
 * @return PCollection after transformations
 */
@Override
public PCollection<KV<String, InspectContentResponse>> expand(
    PCollection<KV<String, String>> input) {
  return input
      .apply(ParDo.of(new MapStringToDlpRow(getColumnDelimiter())))
      .apply("Batch Contents", ParDo.of(new BatchRequestForDLP(getBatchSizeBytes())))
      .apply(
          "DLPInspect",
          ParDo.of(
              new InspectData(
                  getProjectId(),
                  getInspectTemplateName(),
                  getInspectConfig(),
                  getHeaderColumns())));
}
 
Example #2
Source File: DLPInspectText.java    From beam with Apache License 2.0 6 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) throws IOException {
  List<FieldId> tableHeaders;
  if (headerColumns != null) {
    tableHeaders =
        c.sideInput(headerColumns).stream()
            .map(header -> FieldId.newBuilder().setName(header).build())
            .collect(Collectors.toList());
  } else {
    tableHeaders = new ArrayList<>();
    tableHeaders.add(FieldId.newBuilder().setName("value").build());
  }
  Table table =
      Table.newBuilder().addAllHeaders(tableHeaders).addAllRows(c.element().getValue()).build();
  ContentItem contentItem = ContentItem.newBuilder().setTable(table).build();
  this.requestBuilder.setItem(contentItem);
  InspectContentResponse response =
      dlpServiceClient.inspectContent(this.requestBuilder.build());
  c.output(KV.of(c.element().getKey(), response));
}
 
Example #3
Source File: DLPTextOperationsIT.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void inspectsText() {
  String projectId = testPipeline.getOptions().as(GcpOptions.class).getProject();
  PCollection<KV<String, InspectContentResponse>> inspectionResult =
      testPipeline
          .apply(Create.of(KV.of("", IDENTIFYING_TEXT)))
          .apply(
              DLPInspectText.newBuilder()
                  .setBatchSizeBytes(524000)
                  .setProjectId(projectId)
                  .setInspectConfig(inspectConfig)
                  .build());
  PAssert.that(inspectionResult).satisfies(new VerifyInspectionResult());
  testPipeline.run().waitUntilFinish();
}
 
Example #4
Source File: DLPTextOperationsIT.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public Void apply(Iterable<KV<String, InspectContentResponse>> input) {
  List<Boolean> matches = new ArrayList<>();
  input.forEach(
      item -> {
        List<Finding> resultList = item.getValue().getResult().getFindingsList();
        matches.add(
            resultList.stream()
                .anyMatch(finding -> finding.getInfoType().equals(emailAddress)));
      });
  assertTrue(matches.contains(Boolean.TRUE));
  return null;
}
 
Example #5
Source File: InspectImageFileAllInfoTypes.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
static void inspectImageFileAllInfoTypes(String projectId, String inputPath)
    throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the content to be inspected.
    ByteString fileBytes = ByteString.readFrom(new FileInputStream(inputPath));
    ByteContentItem byteItem =
        ByteContentItem.newBuilder().setType(BytesType.IMAGE_JPEG).setData(fileBytes).build();

    // Construct the Inspect request to be sent by the client.
    // Do not specify the type of info to inspect.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(ContentItem.newBuilder().setByteItem(byteItem).build())
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results.
    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());
    }
  }
}
 
Example #6
Source File: QuickStart.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void quickstart(String projectId) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
    // Configure that content that will be inspected
    String text = "His name was Robert Frost";
    ByteContentItem byteContentItem =
        ByteContentItem.newBuilder()
            .setType(ByteContentItem.BytesType.TEXT_UTF8)
            .setData(ByteString.copyFromUtf8(text))
            .build();
    ContentItem contentItem = ContentItem.newBuilder().setByteItem(byteContentItem).build();

    // The types of information to match:
    // See: https://cloud.google.com/dlp/docs/infotypes-reference
    List<InfoType> infoTypes =
        Stream.of("PERSON_NAME", "US_STATE")
            .map(it -> InfoType.newBuilder().setName(it).build())
            .collect(Collectors.toList());

    // The minimum likelihood required before returning a match:
    // See: https://cloud.google.com/dlp/docs/likelihood
    Likelihood minLikelihood = Likelihood.POSSIBLE;

    // The maximum number of findings to report (0 = server maximum)
    InspectConfig.FindingLimits findingLimits =
        InspectConfig.FindingLimits.newBuilder().setMaxFindingsPerItem(0).build();

    // Specify the inspection configuration
    InspectConfig inspectConfig =
        InspectConfig.newBuilder()
            .addAllInfoTypes(infoTypes)
            .setMinLikelihood(minLikelihood)
            .setLimits(findingLimits)
            .setIncludeQuote(true)
            .build();

    // Create the request from previous configs
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setInspectConfig(inspectConfig)
            .setItem(contentItem)
            .build();

    // Send the request to the service and receive the results
    InspectContentResponse response = dlpServiceClient.inspectContent(request);

    // Process the results
    System.out.println("Inspect of text complete: ");
    InspectResult result = response.getResult();
    if (result.getFindingsCount() < 0) {
      System.out.println("No findings.");
      return;
    }
    System.out.println("Findings: ");
    for (Finding finding : result.getFindingsList()) {
      System.out.println("\tQuote: " + finding.getQuote());
      System.out.println("\tInfo type: " + finding.getInfoType().getName());
      System.out.println("\tLikelihood: " + finding.getLikelihood());
    }
  }
}
 
Example #7
Source File: InspectTable.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectTable(String projectId, Table tableToInspect) {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the table to be inspected.
    ContentItem item = ContentItem.newBuilder().setTable(tableToInspect).build();

    // Specify the type of info the inspection will look for.
    // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
    InfoType infoType = InfoType.newBuilder().setName("PHONE_NUMBER").build();

    // Construct the configuration for the Inspect request.
    InspectConfig config =
        InspectConfig.newBuilder()
            .addInfoTypes(infoType)
            .setIncludeQuote(true)
            .build();

    // Construct the Inspect request to be sent by the client.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(item)
            .setInspectConfig(config)
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results
    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 inspectString: \n" + e.toString());
  }
}
 
Example #8
Source File: InspectStringMultipleRules.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectStringMultipleRules(String projectId, String textToInspect)
    throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the type and content to be inspected.
    ByteContentItem byteItem =
        ByteContentItem.newBuilder()
            .setType(BytesType.TEXT_UTF8)
            .setData(ByteString.copyFromUtf8(textToInspect))
            .build();
    ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();

    // Construct hotword rules
    HotwordRule patientRule =
        HotwordRule.newBuilder()
            .setHotwordRegex(Regex.newBuilder().setPattern("patient"))
            .setProximity(Proximity.newBuilder().setWindowBefore(10))
            .setLikelihoodAdjustment(
                LikelihoodAdjustment.newBuilder().setFixedLikelihood(Likelihood.VERY_LIKELY))
            .build();

    HotwordRule doctorRule =
        HotwordRule.newBuilder()
            .setHotwordRegex(Regex.newBuilder().setPattern("doctor"))
            .setProximity(Proximity.newBuilder().setWindowBefore(10))
            .setLikelihoodAdjustment(
                LikelihoodAdjustment.newBuilder().setFixedLikelihood(Likelihood.UNLIKELY))
            .build();

    // Construct exclusion rules
    ExclusionRule quasimodoRule =
        ExclusionRule.newBuilder()
            .setDictionary(
                Dictionary.newBuilder().setWordList(WordList.newBuilder().addWords("Quasimodo")))
            .setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH)
            .build();

    ExclusionRule redactedRule =
        ExclusionRule.newBuilder()
            .setRegex(Regex.newBuilder().setPattern("REDACTED"))
            .setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH)
            .build();

    // Construct a ruleset that applies the rules to the PERSON_NAME infotype.
    InspectionRuleSet ruleSet =
        InspectionRuleSet.newBuilder()
            .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME"))
            .addRules(InspectionRule.newBuilder().setHotwordRule(patientRule))
            .addRules(InspectionRule.newBuilder().setHotwordRule(doctorRule))
            .addRules(InspectionRule.newBuilder().setExclusionRule(quasimodoRule))
            .addRules(InspectionRule.newBuilder().setExclusionRule(redactedRule))
            .build();

    // Construct the configuration for the Inspect request, including the ruleset.
    InspectConfig config =
        InspectConfig.newBuilder()
            .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME"))
            .setIncludeQuote(true)
            .addRuleSet(ruleSet)
            .build();

    // Construct the Inspect request to be sent by the client.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(item)
            .setInspectConfig(config)
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results
    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());
    }
  }
}
 
Example #9
Source File: InspectPhoneNumber.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectString(String projectId, String textToInspect) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the type and content to be inspected.
    ContentItem item = ContentItem.newBuilder()
        .setValue(textToInspect)
        .build();

    // Specify the type of info the inspection will look for.
    // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
    InfoType infoType = InfoType.newBuilder().setName("PHONE_NUMBER").build();

    // Construct the configuration for the Inspect request.
    InspectConfig config =
        InspectConfig.newBuilder()
            .setIncludeQuote(true)
            .setMinLikelihood(Likelihood.POSSIBLE)
            .addInfoTypes(infoType)
            .build();

    // Construct the Inspect request to be sent by the client.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(item)
            .setInspectConfig(config)
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results
    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());
    }
  }
}
 
Example #10
Source File: InspectTextFile.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectTextFile(String projectId, String filePath) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the type and content to be inspected.
    ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath));
    ByteContentItem byteItem =
        ByteContentItem.newBuilder().setType(BytesType.TEXT_UTF8).setData(fileBytes).build();
    ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();

    // Specify the type of info the inspection will look for.
    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());
    }

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

    // Construct the Inspect request to be sent by the client.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(item)
            .setInspectConfig(config)
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results
    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());
    }
  }
}
 
Example #11
Source File: InspectStringCustomHotword.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectStringCustomHotword(String projectId, String textToInspect,
    String customHotword) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the type and content to be inspected.
    ByteContentItem byteItem =
        ByteContentItem.newBuilder()
            .setType(BytesType.TEXT_UTF8)
            .setData(ByteString.copyFromUtf8(textToInspect))
            .build();
    ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();

    // Increase likelihood of matches that have customHotword nearby
    HotwordRule hotwordRule =
        HotwordRule.newBuilder()
            .setHotwordRegex(Regex.newBuilder().setPattern(customHotword))
            .setProximity(Proximity.newBuilder().setWindowBefore(50))
            .setLikelihoodAdjustment(
                LikelihoodAdjustment.newBuilder().setFixedLikelihood(Likelihood.VERY_LIKELY))
            .build();

    // Construct a ruleset that applies the hotword rule to the PERSON_NAME infotype.
    InspectionRuleSet ruleSet =
        InspectionRuleSet.newBuilder()
            .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME").build())
            .addRules(InspectionRule.newBuilder().setHotwordRule(hotwordRule))
            .build();

    // Construct the configuration for the Inspect request.
    InspectConfig config =
        InspectConfig.newBuilder()
            .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME").build())
            .setIncludeQuote(true)
            .addRuleSet(ruleSet)
            .setMinLikelihood(Likelihood.VERY_LIKELY)
            .build();

    // Construct the Inspect request to be sent by the client.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(item)
            .setInspectConfig(config)
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results
    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());
    }
  }
}
 
Example #12
Source File: InspectStringWithExclusionDict.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectStringWithExclusionDict(String projectId, String textToInspect,
    List<String> excludedMatchList) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the type and content to be inspected.
    ByteContentItem byteItem =
        ByteContentItem.newBuilder()
            .setType(BytesType.TEXT_UTF8)
            .setData(ByteString.copyFromUtf8(textToInspect))
            .build();
    ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();

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

    // Exclude matches from the specified excludedMatchList.
    ExclusionRule exclusionRule =
        ExclusionRule.newBuilder()
            .setMatchingType(MatchingType.MATCHING_TYPE_FULL_MATCH)
            .setDictionary(Dictionary.newBuilder()
                .setWordList(WordList.newBuilder().addAllWords(excludedMatchList)))
            .build();

    // Construct a ruleset that applies the exclusion rule to the EMAIL_ADDRESSES infotype.
    InspectionRuleSet ruleSet =
        InspectionRuleSet.newBuilder()
            .addInfoTypes(InfoType.newBuilder().setName("EMAIL_ADDRESS"))
            .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule))
            .build();

    // Construct the configuration for the Inspect request, including the ruleset.
    InspectConfig config =
        InspectConfig.newBuilder()
            .addAllInfoTypes(infoTypes)
            .setIncludeQuote(true)
            .addRuleSet(ruleSet)
            .build();

    // Construct the Inspect request to be sent by the client.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(item)
            .setInspectConfig(config)
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results
    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());
    }
  }
}
 
Example #13
Source File: InspectString.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectString(String projectId, String textToInspect) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the type and content to be inspected.
    ByteContentItem byteItem =
        ByteContentItem.newBuilder()
            .setType(BytesType.TEXT_UTF8)
            .setData(ByteString.copyFromUtf8(textToInspect))
            .build();
    ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();

    // Specify the type of info the inspection will look for.
    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());
    }

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

    // Construct the Inspect request to be sent by the client.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(item)
            .setInspectConfig(config)
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results
    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());
    }
  }
}
 
Example #14
Source File: InspectStringCustomOmitOverlap.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectStringCustomOmitOverlap(String projectId, String textToInspect)
    throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the type and content to be inspected.
    ByteContentItem byteItem =
        ByteContentItem.newBuilder()
            .setType(BytesType.TEXT_UTF8)
            .setData(ByteString.copyFromUtf8(textToInspect))
            .build();
    ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();

    // Construct the custom infotype.
    CustomInfoType customInfoType =
        CustomInfoType.newBuilder()
            .setInfoType(InfoType.newBuilder().setName("VIP_DETECTOR"))
            .setRegex(Regex.newBuilder().setPattern("Larry Page|Sergey Brin"))
            .setExclusionType(ExclusionType.EXCLUSION_TYPE_EXCLUDE)
            .build();

    // Exclude matches that also match the custom infotype.
    ExclusionRule exclusionRule =
        ExclusionRule.newBuilder()
            .setExcludeInfoTypes(
                ExcludeInfoTypes.newBuilder().addInfoTypes(customInfoType.getInfoType()))
            .setMatchingType(MatchingType.MATCHING_TYPE_FULL_MATCH)
            .build();

    // Construct a ruleset that applies the exclusion rule to the PERSON_NAME infotype.
    InspectionRuleSet ruleSet =
        InspectionRuleSet.newBuilder()
            .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME"))
            .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule))
            .build();

    // Construct the configuration for the Inspect request, including the ruleset.
    InspectConfig config =
        InspectConfig.newBuilder()
            .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME"))
            .addCustomInfoTypes(customInfoType)
            .setIncludeQuote(true)
            .addRuleSet(ruleSet)
            .build();

    // Construct the Inspect request to be sent by the client.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(item)
            .setInspectConfig(config)
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results
    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());
    }
  }
}
 
Example #15
Source File: InspectWithCustomRegex.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectWithCustomRegex(
    String projectId, String textToInspect, String customRegexPattern) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the type and content to be inspected.
    ByteContentItem byteItem =
        ByteContentItem.newBuilder()
            .setType(BytesType.TEXT_UTF8)
            .setData(ByteString.copyFromUtf8(textToInspect))
            .build();
    ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();

    // Specify the regex pattern the inspection will look for.
    Regex regex = Regex.newBuilder().setPattern(customRegexPattern).build();

    // Construct the custom regex detector.
    InfoType infoType = InfoType.newBuilder().setName("C_MRN").build();
    CustomInfoType customInfoType =
        CustomInfoType.newBuilder()
            .setInfoType(infoType)
            .setRegex(regex)
            .build();

    // Construct the configuration for the Inspect request.
    InspectConfig config =
        InspectConfig.newBuilder()
            .addCustomInfoTypes(customInfoType)
            .setIncludeQuote(true)
            .setMinLikelihood(Likelihood.POSSIBLE)
            .build();

    // Construct the Inspect request to be sent by the client.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString()).setItem(item)
            .setInspectConfig(config)
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results
    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());
    }
  }
}
 
Example #16
Source File: InspectImageFile.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectImageFile(String projectId, String filePath) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the type and content to be inspected.
    ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath));
    ByteContentItem byteItem =
        ByteContentItem.newBuilder().setType(BytesType.IMAGE).setData(fileBytes).build();
    ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();

    // Specify the type of info the inspection will look for.
    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());
    }

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

    // Construct the Inspect request to be sent by the client.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(item)
            .setInspectConfig(config)
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results.
    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());
    }
  }
}
 
Example #17
Source File: InspectStringCustomExcludingSubstring.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectStringCustomExcludingSubstring(String projectId, String textToInspect,
    String customDetectorPattern, List<String> excludedSubstringList) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the type and content to be inspected.
    ByteContentItem byteItem =
        ByteContentItem.newBuilder()
            .setType(BytesType.TEXT_UTF8)
            .setData(ByteString.copyFromUtf8(textToInspect))
            .build();
    ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();

    // Specify the type of info the inspection will look for.
    InfoType infoType = InfoType.newBuilder().setName("CUSTOM_NAME_DETECTOR").build();
    CustomInfoType customInfoType =
        CustomInfoType.newBuilder()
            .setInfoType(infoType).setRegex(Regex.newBuilder().setPattern(customDetectorPattern))
            .build();

    // Exclude partial matches from the specified excludedSubstringList.
    ExclusionRule exclusionRule =
        ExclusionRule.newBuilder()
            .setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH)
            .setDictionary(Dictionary.newBuilder()
                .setWordList(WordList.newBuilder().addAllWords(excludedSubstringList)))
            .build();

    // Construct a ruleset that applies the exclusion rule to the EMAIL_ADDRESSES infotype.
    InspectionRuleSet ruleSet =
        InspectionRuleSet.newBuilder()
            .addInfoTypes(infoType)
            .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule))
            .build();

    // Construct the configuration for the Inspect request, including the ruleset.
    InspectConfig config =
        InspectConfig.newBuilder()
            .addCustomInfoTypes(customInfoType)
            .setIncludeQuote(true)
            .addRuleSet(ruleSet)
            .build();

    // Construct the Inspect request to be sent by the client.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(item)
            .setInspectConfig(config)
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results
    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());
    }
  }
}
 
Example #18
Source File: S3Import.java    From dlp-dataflow-deidentification with Apache License 2.0 4 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) throws IOException {

  try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
    if (!c.element().getValue().isEmpty()) {
      ContentItem contentItem =
          ContentItem.newBuilder().setValue(c.element().getValue()).build();
      this.requestBuilder.setItem(contentItem);

      if (this.requestBuilder.build().getSerializedSize() > DLP_PAYLOAD_LIMIT) {
        String errorMessage =
            String.format(
                "Payload Size %s Exceeded Batch Size %s",
                this.requestBuilder.build().getSerializedSize(), DLP_PAYLOAD_LIMIT);
        c.output(apiResponseFailedElements, errorMessage);
      } else {

        InspectContentResponse response =
            dlpServiceClient.inspectContent(this.requestBuilder.build());

        String timestamp =
            TIMESTAMP_FORMATTER.print(Instant.now().toDateTime(DateTimeZone.UTC));

        response
            .getResult()
            .getFindingsList()
            .forEach(
                finding -> {
                  List<TableCell> cells = new ArrayList<>();
                  TableRow row = new TableRow();

                  cells.add(new TableCell().set("file_name", c.element().getKey()));
                  row.set("file_name", c.element().getKey());

                  cells.add(new TableCell().set("inspection_timestamp", timestamp));
                  row.set("inspection_timestamp", timestamp);

                  cells.add(new TableCell().set("infoType", finding.getInfoType().getName()));
                  row.set("infoType", finding.getInfoType().getName());

                  cells.add(new TableCell().set("likelihood", finding.getLikelihood().name()));
                  row.set("likelihood", finding.getLikelihood().name());

                  row.setF(cells);

                  c.output(apiResponseSuccessElements, KV.of(BQ_TABLE_NAME, row));
                });

        numberOfBytesInspected.inc(contentItem.getSerializedSize());
        response
            .findInitializationErrors()
            .forEach(
                error -> {
                  c.output(apiResponseFailedElements, error.toString());
                });
      }
    }

  } catch (Exception e) {

    c.output(apiResponseFailedElements, e.toString());
  }
}
 
Example #19
Source File: InspectStringWithExclusionRegex.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectStringWithExclusionRegex(String projectId, String textToInspect,
    String excludedRegex) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the type and content to be inspected.
    ByteContentItem byteItem =
        ByteContentItem.newBuilder()
            .setType(BytesType.TEXT_UTF8)
            .setData(ByteString.copyFromUtf8(textToInspect))
            .build();
    ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();

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

    // Exclude matches from the specified excludedMatchList.
    ExclusionRule exclusionRule =
        ExclusionRule.newBuilder()
            .setMatchingType(MatchingType.MATCHING_TYPE_FULL_MATCH)
            .setRegex(Regex.newBuilder().setPattern(excludedRegex))
            .build();

    // Construct a ruleset that applies the exclusion rule to the EMAIL_ADDRESSES infotype.
    InspectionRuleSet ruleSet =
        InspectionRuleSet.newBuilder()
            .addInfoTypes(InfoType.newBuilder().setName("EMAIL_ADDRESS"))
            .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule))
            .build();

    // Construct the configuration for the Inspect request, including the ruleset.
    InspectConfig config =
        InspectConfig.newBuilder()
            .addAllInfoTypes(infoTypes)
            .setIncludeQuote(true)
            .addRuleSet(ruleSet)
            .build();

    // Construct the Inspect request to be sent by the client.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(item)
            .setInspectConfig(config)
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results
    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());
    }
  }
}
 
Example #20
Source File: InspectStringWithExclusionDictSubstring.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectStringWithExclusionDictSubstring(String projectId, String textToInspect,
    List<String> excludedSubstringList) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the type and content to be inspected.
    ByteContentItem byteItem =
        ByteContentItem.newBuilder()
            .setType(BytesType.TEXT_UTF8)
            .setData(ByteString.copyFromUtf8(textToInspect))
            .build();
    ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();

    // Specify the type of info the inspection will look for.
    // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types.
    List<InfoType> infoTypes = new ArrayList<>();
    for (String typeName : new String[]{"EMAIL_ADDRESS", "DOMAIN_NAME", "PHONE_NUMBER",
        "PERSON_NAME"}) {
      infoTypes.add(InfoType.newBuilder().setName(typeName).build());
    }

    // Exclude partial matches from the specified excludedSubstringList.
    ExclusionRule exclusionRule =
        ExclusionRule.newBuilder()
            .setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH)
            .setDictionary(Dictionary.newBuilder()
                .setWordList(WordList.newBuilder().addAllWords(excludedSubstringList)))
            .build();

    // Construct a ruleset that applies the exclusion rule to the EMAIL_ADDRESSES infotype.
    InspectionRuleSet ruleSet =
        InspectionRuleSet.newBuilder()
            .addAllInfoTypes(infoTypes)
            .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule))
            .build();

    // Construct the configuration for the Inspect request, including the ruleset.
    InspectConfig config =
        InspectConfig.newBuilder()
            .addAllInfoTypes(infoTypes)
            .setIncludeQuote(true)
            .addRuleSet(ruleSet)
            .build();

    // Construct the Inspect request to be sent by the client.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(item)
            .setInspectConfig(config)
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results
    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());
    }
  }
}
 
Example #21
Source File: InspectImageFileListedInfoTypes.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
static void inspectImageFileListedInfoTypes(String projectId, String inputPath)
    throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the content to be inspected.
    ByteString fileBytes = ByteString.readFrom(new FileInputStream(inputPath));
    ByteContentItem byteItem =
        ByteContentItem.newBuilder().setType(BytesType.IMAGE_JPEG).setData(fileBytes).build();

    // Specify the type of info the inspection will look for.
    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[] {"US_SOCIAL_SECURITY_NUMBER", "EMAIL_ADDRESS", "PHONE_NUMBER"}) {
      infoTypes.add(InfoType.newBuilder().setName(typeName).build());
    }

    // Construct the configuration for the Inspect request.
    InspectConfig inspectConfig =
        InspectConfig.newBuilder()
            .addAllInfoTypes(infoTypes)
            .build();

    // Construct the Inspect request to be sent by the client.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(ContentItem.newBuilder().setByteItem(byteItem).build())
            .setInspectConfig(inspectConfig)
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results.
    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());
    }
  }
}
 
Example #22
Source File: InspectStringOmitOverlap.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectStringOmitOverlap(String projectId, String textToInspect)
    throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the type and content to be inspected.
    ByteContentItem byteItem =
        ByteContentItem.newBuilder()
            .setType(BytesType.TEXT_UTF8)
            .setData(ByteString.copyFromUtf8(textToInspect))
            .build();
    ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();

    // Specify the type of info the inspection will look for.
    // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types.
    List<InfoType> infoTypes = new ArrayList<>();
    for (String typeName : new String[]{"PERSON_NAME", "EMAIL_ADDRESS"}) {
      infoTypes.add(InfoType.newBuilder().setName(typeName).build());
    }

    // Exclude EMAIL_ADDRESS matches
    ExclusionRule exclusionRule =
        ExclusionRule.newBuilder()
            .setExcludeInfoTypes(ExcludeInfoTypes.newBuilder()
                .addInfoTypes(InfoType.newBuilder().setName("EMAIL_ADDRESS")))
            .setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH)
            .build();

    // Construct a ruleset that applies the exclusion rule to the PERSON_NAME infotype.
    // If a PERSON_NAME match overlaps with an EMAIL_ADDRESS match, the PERSON_NAME match will
    // be excluded.
    InspectionRuleSet ruleSet =
        InspectionRuleSet.newBuilder()
            .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME"))
            .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule))
            .build();

    // Construct the configuration for the Inspect request, including the ruleset.
    InspectConfig config =
        InspectConfig.newBuilder()
            .addAllInfoTypes(infoTypes)
            .setIncludeQuote(true)
            .addRuleSet(ruleSet)
            .build();

    // Construct the Inspect request to be sent by the client.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(item)
            .setInspectConfig(config)
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results
    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());
    }
  }
}
 
Example #23
Source File: InspectWithHotwordRules.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectWithHotwordRules(
    String projectId, String textToInspect, String customRegexPattern, String hotwordRegexPattern)
    throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the type and content to be inspected.
    ByteContentItem byteItem =
        ByteContentItem.newBuilder()
            .setType(BytesType.TEXT_UTF8)
            .setData(ByteString.copyFromUtf8(textToInspect))
            .build();
    ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();

    // Specify the regex pattern the inspection will look for.
    Regex regex = Regex.newBuilder().setPattern(customRegexPattern).build();

    // Construct the custom regex detector.
    InfoType infoType = InfoType.newBuilder().setName("C_MRN").build();
    CustomInfoType customInfoType =
        CustomInfoType.newBuilder().setInfoType(infoType).setRegex(regex).build();

    // Specify hotword likelihood adjustment.
    LikelihoodAdjustment likelihoodAdjustment =
        LikelihoodAdjustment.newBuilder().setFixedLikelihood(Likelihood.VERY_LIKELY).build();

    // Specify a window around a finding to apply a detection rule.
    Proximity proximity = Proximity.newBuilder().setWindowBefore(10).build();

    // Construct hotword rule.
    HotwordRule hotwordRule =
        HotwordRule.newBuilder()
            .setHotwordRegex(Regex.newBuilder().setPattern(hotwordRegexPattern).build())
            .setLikelihoodAdjustment(likelihoodAdjustment)
            .setProximity(proximity)
            .build();

    // Construct rule set for the inspect config.
    InspectionRuleSet inspectionRuleSet =
        InspectionRuleSet.newBuilder()
            .addInfoTypes(infoType)
            .addRules(InspectionRule.newBuilder().setHotwordRule(hotwordRule))
            .build();

    // Construct the configuration for the Inspect request.
    InspectConfig config =
        InspectConfig.newBuilder()
            .addCustomInfoTypes(customInfoType)
            .setIncludeQuote(true)
            .setMinLikelihood(Likelihood.POSSIBLE)
            .addRuleSet(inspectionRuleSet)
            .build();

    // Construct the Inspect request to be sent by the client.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(item)
            .setInspectConfig(config)
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results
    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());
    }
  }
}
 
Example #24
Source File: InspectStringWithoutOverlap.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectStringWithoutOverlap(String projectId, String textToInspect)
    throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the type and content to be inspected.
    ByteContentItem byteItem =
        ByteContentItem.newBuilder()
            .setType(BytesType.TEXT_UTF8)
            .setData(ByteString.copyFromUtf8(textToInspect))
            .build();
    ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();

    // Specify the type of info the inspection will look for.
    // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types.
    List<InfoType> infoTypes = new ArrayList<>();
    for (String typeName : new String[]{"DOMAIN_NAME", "EMAIL_ADDRESS"}) {
      infoTypes.add(InfoType.newBuilder().setName(typeName).build());
    }

    // Define a custom info type to exclude email addresses
    CustomInfoType customInfoType =
        CustomInfoType.newBuilder()
            .setInfoType(InfoType.newBuilder().setName("EMAIL_ADDRESS"))
            .setExclusionType(ExclusionType.EXCLUSION_TYPE_EXCLUDE)
            .build();

    // Exclude EMAIL_ADDRESS matches
    ExclusionRule exclusionRule =
        ExclusionRule.newBuilder()
            .setExcludeInfoTypes(ExcludeInfoTypes.newBuilder()
                .addInfoTypes(InfoType.newBuilder().setName("EMAIL_ADDRESS")))
            .setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH)
            .build();

    // Construct a ruleset that applies the exclusion rule to the DOMAIN_NAME infotype.
    // If a DOMAIN_NAME match is part of an EMAIL_ADDRESS match, the DOMAIN_NAME match will
    // be excluded.
    InspectionRuleSet ruleSet =
        InspectionRuleSet.newBuilder()
            .addInfoTypes(InfoType.newBuilder().setName("DOMAIN_NAME"))
            .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule))
            .build();

    // Construct the configuration for the Inspect request, including the ruleset.
    InspectConfig config =
        InspectConfig.newBuilder()
            .addAllInfoTypes(infoTypes)
            .addCustomInfoTypes(customInfoType)
            .setIncludeQuote(true)
            .addRuleSet(ruleSet)
            .build();

    // Construct the Inspect request to be sent by the client.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(item)
            .setInspectConfig(config)
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results
    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());
    }
  }
}
 
Example #25
Source File: DLPClient.java    From android-docs-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Example showing how to inspect a string to identify names, phone number, and
 * credit card numbers.
 *
 * @param string text to analyze
 * @return a string describing what was identified in the string
 */
@NonNull
public String inspectString(@NonNull String string) {
    // set sources of information to identify
    List<InfoType> infoTypes = ImmutableList.of(
            InfoType.newBuilder().setName("PERSON_NAME").build(),
            InfoType.newBuilder().setName("CREDIT_CARD_NUMBER").build(),
            InfoType.newBuilder().setName("PHONE_NUMBER").build());

    // configuration for the request
    InspectConfig inspectConfig =
            InspectConfig.newBuilder()
                    .addAllInfoTypes(infoTypes)
                    .setMinLikelihood(Likelihood.LIKELIHOOD_UNSPECIFIED)
                    .setLimits(InspectConfig.FindingLimits.newBuilder()
                            .setMaxFindingsPerRequest(0)
                            .build())
                    .setIncludeQuote(true)
                    .build();

    // the content to be analyzed
    ContentItem contentItem = ContentItem.newBuilder()
            .setByteItem(ByteContentItem.newBuilder()
                    .setType(ByteContentItem.BytesType.TEXT_UTF8)
                    .setData(ByteString.copyFromUtf8(string))
                    .build())
            .build();

    // create a request
    InspectContentRequest request =
            InspectContentRequest.newBuilder()
                    .setParent(ProjectName.of(mProjectId).toString())
                    .setInspectConfig(inspectConfig)
                    .setItem(contentItem)
                    .build();

    // call the API
    InspectContentResponse response = mClient.inspectContent(request);

    // format response into a string
    StringBuilder sb = new StringBuilder("Findings:");
    if (response.getResult().getFindingsCount() > 0) {
        for (Finding finding : response.getResult().getFindingsList()) {
            sb.append("\n")
                    .append("  Quote: ").append(finding.getQuote())
                    .append("  Info type: ").append(finding.getInfoType().getName())
                    .append("  Likelihood: ").append(finding.getLikelihood());
        }
    } else {
        sb.append("No findings.");
    }
    return sb.toString();
}