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

The following examples show how to use com.google.privacy.dlp.v2.ContentItem. 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: DLPTokenizationDoFn.java    From dlp-dataflow-deidentification with Apache License 2.0 6 votes vote down vote up
public DeidentifyContentRequest buildDeidentifyContentRequest(ContentItem tableItem) {
  DeidentifyContentRequest request;
  if (this.inspectTemplateExist) {
    request =
        DeidentifyContentRequest.newBuilder()
            .setParent(ProjectName.of(this.projectId).toString())
            .setDeidentifyTemplateName(this.deIdentifyTemplateName.get())
            .setInspectTemplateName(this.inspectTemplateName.get())
            .setItem(tableItem)
            .build();
  } else {
    request =
        DeidentifyContentRequest.newBuilder()
            .setParent(ProjectName.of(this.projectId).toString())
            .setDeidentifyTemplateName(this.deIdentifyTemplateName.get())
            .setItem(tableItem)
            .build();
  }
  return request;
}
 
Example #2
Source File: DLPTokenizationDoFnTest.java    From dlp-dataflow-deidentification with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildDeidentifyContentRequest() {
  DLPTokenizationDoFn dlp =
      new DLPTokenizationDoFn(
          "Project Name",
          ValueProvider.StaticValueProvider.of("DeidentifyTemplateName"),
          ValueProvider.StaticValueProvider.of("IdentifyTemplateName"));

  ContentItem contentItem = ContentItem.newBuilder().build();

  dlp.setInspectTemplateExist();
  DeidentifyContentRequest request = dlp.buildDeidentifyContentRequest(contentItem);

  assertEquals(request.getParent(), "projects/Project Name");
  assertEquals(request.getDeidentifyTemplateName(), "DeidentifyTemplateName");
  assertEquals(request.getInspectTemplateName(), "IdentifyTemplateName");
}
 
Example #3
Source File: DLPDeidentifyText.java    From beam with Apache License 2.0 6 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) throws IOException {
  String fileName = c.element().getKey();
  List<FieldId> dlpTableHeaders;
  if (headerColumns != null) {
    dlpTableHeaders =
        c.sideInput(headerColumns).stream()
            .map(header -> FieldId.newBuilder().setName(header).build())
            .collect(Collectors.toList());
  } else {
    // handle unstructured input
    dlpTableHeaders = new ArrayList<>();
    dlpTableHeaders.add(FieldId.newBuilder().setName("value").build());
  }
  Table table =
      Table.newBuilder()
          .addAllHeaders(dlpTableHeaders)
          .addAllRows(c.element().getValue())
          .build();
  ContentItem contentItem = ContentItem.newBuilder().setTable(table).build();
  this.requestBuilder.setItem(contentItem);
  DeidentifyContentResponse response =
      dlpServiceClient.deidentifyContent(this.requestBuilder.build());
  c.output(KV.of(fileName, response));
}
 
Example #4
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 #5
Source File: DLPReidentifyText.java    From beam with Apache License 2.0 6 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext context) throws IOException {
  List<FieldId> tableHeaders;
  if (headerColumns != null) {
    tableHeaders =
        context.sideInput(headerColumns).stream()
            .map(header -> FieldId.newBuilder().setName(header).build())
            .collect(Collectors.toList());
  } else {
    // handle unstructured input.
    tableHeaders = new ArrayList<>();
    tableHeaders.add(FieldId.newBuilder().setName("value").build());
  }
  Table table =
      Table.newBuilder()
          .addAllHeaders(tableHeaders)
          .addAllRows(context.element().getValue())
          .build();
  ContentItem contentItem = ContentItem.newBuilder().setTable(table).build();
  this.requestBuilder.setItem(contentItem);
  ReidentifyContentResponse response =
      dlpServiceClient.reidentifyContent(requestBuilder.build());
  context.output(KV.of(context.element().getKey(), response));
}
 
Example #6
Source File: TextStreamingPipeline.java    From dlp-dataflow-deidentification with Apache License 2.0 6 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) throws IOException {

  try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {

    ContentItem contentItem = ContentItem.newBuilder().setValue(c.element()).build();

    DeidentifyContentRequest request =
        DeidentifyContentRequest.newBuilder()
            .setParent(ProjectName.of(this.projectId).toString())
            .setDeidentifyTemplateName(this.deIdentifyTemplateName.get())
            .setInspectTemplateName(this.inspectTemplateName.get())
            .setItem(contentItem)
            .build();

    DeidentifyContentResponse response = dlpServiceClient.deidentifyContent(request);

    String encryptedData = response.getItem().getValue();
    LOG.info(
        "Successfully tokenized request size: " + request.toByteString().size() + " bytes");
    c.output(encryptedData);
  }
}
 
Example #7
Source File: DLPTextToBigQueryStreaming.java    From dlp-dataflow-deidentification with Apache License 2.0 5 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) {
  String key = c.element().getKey();
  Table nonEncryptedData = c.element().getValue();
  ContentItem tableItem = ContentItem.newBuilder().setTable(nonEncryptedData).build();
  this.requestBuilder.setItem(tableItem);
  DeidentifyContentResponse response =
      dlpServiceClient.deidentifyContent(this.requestBuilder.build());

  Table tokenizedData = response.getItem().getTable();
  numberOfRowsTokenized.update(tokenizedData.getRowsList().size());
  numberOfBytesTokenized.update(tokenizedData.toByteArray().length);
  c.output(KV.of(key, tokenizedData));
}
 
Example #8
Source File: DLPTokenizationDoFn.java    From dlp-dataflow-deidentification with Apache License 2.0 5 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) {

  String key = c.element().getKey();
  Table nonEncryptedData = c.element().getValue();
  setInspectTemplateExist();
  ContentItem tableItem = ContentItem.newBuilder().setTable(nonEncryptedData).build();

  DeidentifyContentResponse response;
  DeidentifyContentRequest request = buildDeidentifyContentRequest(tableItem);
  response = dlpServiceClient.deidentifyContent(request);
  Table encryptedData = response.getItem().getTable();
  LOG.info(
      "Request Size Successfully Tokenized:{} rows {} bytes ",
      encryptedData.getRowsList().size(),
      request.toByteString().size());

  List<String> outputHeaders =
      encryptedData.getHeadersList().stream().map(FieldId::getName).collect(Collectors.toList());
  String[] header = new String[outputHeaders.size()];

  for (int i = 0; i < header.length; i++) {
    header[i] = Util.checkHeaderName(outputHeaders.get(i));
  }
  List<Table.Row> outputRows = encryptedData.getRowsList();

  for (Table.Row outputRow : outputRows) {
    Row row = convertTableRowToRow(header, key, outputRow);
    c.output(row);
  }
}
 
Example #9
Source File: DLPTextToBigQueryStreaming.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) {
  String key = c.element().getKey();
  Table nonEncryptedData = c.element().getValue();
  ContentItem tableItem = ContentItem.newBuilder().setTable(nonEncryptedData).build();
  this.requestBuilder.setItem(tableItem);
  DeidentifyContentResponse response =
      dlpServiceClient.deidentifyContent(this.requestBuilder.build());
  Table tokenizedData = response.getItem().getTable();
  numberOfRowsTokenized.update(tokenizedData.getRowsList().size());
  numberOfBytesTokenized.update(tokenizedData.toByteArray().length);
  c.output(KV.of(key, tokenizedData));
}
 
Example #10
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 #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: 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 #13
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 #14
Source File: DeIdentifyTableConditionInfoTypes.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static Table deIdentifyTableConditionInfoTypes(String projectId, Table tableToDeIdentify)
    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 what content you want the service to de-identify.
    ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build();

    // Specify how the content should be de-identified.
    // Select type of info to be replaced.
    InfoType infoType = InfoType.newBuilder().setName("PERSON_NAME").build();
    // Specify that findings should be replaced with corresponding info type name.
    ReplaceWithInfoTypeConfig replaceWithInfoTypeConfig =
        ReplaceWithInfoTypeConfig.getDefaultInstance();
    PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder()
        .setReplaceWithInfoTypeConfig(replaceWithInfoTypeConfig).build();
    // Associate info type with the replacement strategy
    InfoTypeTransformation infoTypeTransformation =
        InfoTypeTransformation.newBuilder()
            .addInfoTypes(infoType)
            .setPrimitiveTransformation(primitiveTransformation)
            .build();
    InfoTypeTransformations infoTypeTransformations =
        InfoTypeTransformations.newBuilder()
            .addTransformations(infoTypeTransformation)
            .build();

    // Specify fields to be de-identified.
    List<FieldId> fieldIds = Stream.of("PATIENT", "FACTOID")
        .map(id -> FieldId.newBuilder().setName(id).build())
        .collect(Collectors.toList());

    // Specify when the above fields should be de-identified.
    Condition condition = Condition.newBuilder()
        .setField(FieldId.newBuilder().setName("AGE").build())
        .setOperator(RelationalOperator.GREATER_THAN)
        .setValue(Value.newBuilder().setIntegerValue(89).build())
        .build();
    // Apply the condition to records
    RecordCondition recordCondition = RecordCondition.newBuilder()
        .setExpressions(Expressions.newBuilder()
            .setConditions(Conditions.newBuilder()
                .addConditions(condition)
                .build())
            .build())
        .build();

    // Associate the de-identification and conditions with the specified fields.
    FieldTransformation fieldTransformation =
        FieldTransformation.newBuilder()
            .setInfoTypeTransformations(infoTypeTransformations)
            .addAllFields(fieldIds)
            .setCondition(recordCondition)
            .build();
    RecordTransformations transformations =
        RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build();

    DeidentifyConfig deidentifyConfig =
        DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build();

    // Combine configurations into a request for the service.
    DeidentifyContentRequest request =
        DeidentifyContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(contentItem)
            .setDeidentifyConfig(deidentifyConfig)
            .build();

    // Send the request and receive response from the service.
    DeidentifyContentResponse response = dlp.deidentifyContent(request);

    // Print the results.
    System.out.println(
        "Table after de-identification: " + response.getItem().getTable());

    return response.getItem().getTable();
  }
}
 
Example #15
Source File: DeIdentifyTableInfoTypes.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static Table deIdentifyTableInfoTypes(String projectId, Table tableToDeIdentify)
    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 what content you want the service to de-identify.
    ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build();

    // Specify how the content should be de-identified.
    // Select type of info to be replaced.
    InfoType infoType = InfoType.newBuilder().setName("PERSON_NAME").build();
    // Specify that findings should be replaced with corresponding info type name.
    ReplaceWithInfoTypeConfig replaceWithInfoTypeConfig =
        ReplaceWithInfoTypeConfig.getDefaultInstance();
    PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder()
        .setReplaceWithInfoTypeConfig(replaceWithInfoTypeConfig).build();
    // Associate info type with the replacement strategy
    InfoTypeTransformation infoTypeTransformation =
        InfoTypeTransformation.newBuilder()
            .addInfoTypes(infoType)
            .setPrimitiveTransformation(primitiveTransformation)
            .build();
    InfoTypeTransformations infoTypeTransformations =
        InfoTypeTransformations.newBuilder()
            .addTransformations(infoTypeTransformation)
            .build();

    // Specify fields to be de-identified.
    List<FieldId> fieldIds = Stream.of("PATIENT", "FACTOID")
        .map(id -> FieldId.newBuilder().setName(id).build())
        .collect(Collectors.toList());

    // Associate the de-identification and conditions with the specified field.
    FieldTransformation fieldTransformation =
        FieldTransformation.newBuilder()
            .setInfoTypeTransformations(infoTypeTransformations)
            .addAllFields(fieldIds)
            .build();
    RecordTransformations transformations =
        RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build();

    DeidentifyConfig deidentifyConfig =
        DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build();

    // Combine configurations into a request for the service.
    DeidentifyContentRequest request =
        DeidentifyContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(contentItem)
            .setDeidentifyConfig(deidentifyConfig)
            .build();

    // Send the request and receive response from the service.
    DeidentifyContentResponse response = dlp.deidentifyContent(request);

    // Print the results.
    System.out.println(
        "Table after de-identification: " + response.getItem().getTable());

    return response.getItem().getTable();
  }
}
 
Example #16
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 #17
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 #18
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 #19
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 #20
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 #21
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 #22
Source File: DeIdentifyWithInfoType.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void deIdentifyWithInfoType(String projectId, String textToRedact)
    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.
    ContentItem item = ContentItem.newBuilder()
        .setValue(textToRedact).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("EMAIL_ADDRESS").build();
    InspectConfig inspectConfig = InspectConfig.newBuilder().addInfoTypes(infoType).build();
    // Specify replacement string to be used for the finding.
    ReplaceWithInfoTypeConfig replaceWithInfoTypeConfig =
        ReplaceWithInfoTypeConfig.newBuilder().build();
    // Define type of deidentification as replacement with info type.
    PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder()
        .setReplaceWithInfoTypeConfig(replaceWithInfoTypeConfig)
        .build();
    // Associate deidentification type with info type.
    InfoTypeTransformation transformation = InfoTypeTransformation.newBuilder()
        .addInfoTypes(infoType)
        .setPrimitiveTransformation(primitiveTransformation)
        .build();
    // Construct the configuration for the Redact request and list all desired transformations.
    DeidentifyConfig redactConfig = DeidentifyConfig.newBuilder()
        .setInfoTypeTransformations(InfoTypeTransformations.newBuilder()
            .addTransformations(transformation))
        .build();

    // Construct the Redact request to be sent by the client.
    DeidentifyContentRequest request =
        DeidentifyContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(item)
            .setDeidentifyConfig(redactConfig)
            .setInspectConfig(inspectConfig)
            .build();

    // Use the client to send the API request.
    DeidentifyContentResponse response = dlp.deidentifyContent(request);

    // Parse the response and process results
    System.out.println("Text after redaction: " + response.getItem().getValue());
  }
}
 
Example #23
Source File: DeIdentifyTableConditionMasking.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static Table deIdentifyTableConditionMasking(String projectId, Table tableToDeIdentify)
    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 what content you want the service to de-identify.
    ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build();

    // Specify how the content should be de-identified.
    CharacterMaskConfig characterMaskConfig =
        CharacterMaskConfig.newBuilder()
            .setMaskingCharacter("*")
            .build();
    PrimitiveTransformation primitiveTransformation =
        PrimitiveTransformation.newBuilder()
            .setCharacterMaskConfig(characterMaskConfig)
            .build();

    // Specify field to be de-identified.
    FieldId fieldId = FieldId.newBuilder().setName("HAPPINESS SCORE").build();

    // Specify when the above field should be de-identified.
    Condition condition = Condition.newBuilder()
        .setField(FieldId.newBuilder().setName("AGE").build())
        .setOperator(RelationalOperator.GREATER_THAN)
        .setValue(Value.newBuilder().setIntegerValue(89).build())
        .build();
    // Apply the condition to records
    RecordCondition recordCondition = RecordCondition.newBuilder()
        .setExpressions(Expressions.newBuilder()
            .setConditions(Conditions.newBuilder()
                .addConditions(condition)
                .build())
            .build())
        .build();

    // Associate the de-identification and conditions with the specified field.
    FieldTransformation fieldTransformation =
        FieldTransformation.newBuilder()
            .setPrimitiveTransformation(primitiveTransformation)
            .addFields(fieldId)
            .setCondition(recordCondition)
            .build();
    RecordTransformations transformations =
        RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build();

    DeidentifyConfig deidentifyConfig =
        DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build();

    // Combine configurations into a request for the service.
    DeidentifyContentRequest request =
        DeidentifyContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(contentItem)
            .setDeidentifyConfig(deidentifyConfig)
            .build();

    // Send the request and receive response from the service.
    DeidentifyContentResponse response = dlp.deidentifyContent(request);

    // Print the results.
    System.out.println(
        "Table after de-identification: " + response.getItem().getTable());

    return response.getItem().getTable();
  }
}
 
Example #24
Source File: ReIdentifyTextWithFpe.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void reIdentifyTextWithFpe(
    String projectId, String textToReIdentify, String kmsKeyName, String wrappedAesKey)
    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 what content you want the service to re-identify.
    ContentItem contentItem = ContentItem.newBuilder().setValue(textToReIdentify).build();

    // Specify the type of info the inspection will re-identify. This must use the same custom
    // into type that was used as a surrogate during the initial encryption.
    InfoType surrogateInfoType = InfoType.newBuilder().setName("PHONE_NUMBER").build();

    CustomInfoType customInfoType =
        CustomInfoType.newBuilder()
            .setInfoType(surrogateInfoType)
            .setSurrogateType(SurrogateType.getDefaultInstance())
            .build();
    InspectConfig inspectConfig =
        InspectConfig.newBuilder().addCustomInfoTypes(customInfoType).build();

    // Specify an encrypted AES-256 key and the name of the Cloud KMS key that encrypted it.
    KmsWrappedCryptoKey kmsWrappedCryptoKey =
        KmsWrappedCryptoKey.newBuilder()
            .setWrappedKey(ByteString.copyFrom(BaseEncoding.base64().decode(wrappedAesKey)))
            .setCryptoKeyName(kmsKeyName)
            .build();
    CryptoKey cryptoKey = CryptoKey.newBuilder().setKmsWrapped(kmsWrappedCryptoKey).build();

    // Specify how to un-encrypt the previously de-identified information.
    CryptoReplaceFfxFpeConfig cryptoReplaceFfxFpeConfig =
        CryptoReplaceFfxFpeConfig.newBuilder()
            .setCryptoKey(cryptoKey)
            // Set of characters in the input text. For more info, see
            // https://cloud.google.com/dlp/docs/reference/rest/v2/organizations.deidentifyTemplates#DeidentifyTemplate.FfxCommonNativeAlphabet
            .setCommonAlphabet(FfxCommonNativeAlphabet.NUMERIC)
            .setSurrogateInfoType(surrogateInfoType)
            .build();
    PrimitiveTransformation primitiveTransformation =
        PrimitiveTransformation.newBuilder()
            .setCryptoReplaceFfxFpeConfig(cryptoReplaceFfxFpeConfig)
            .build();
    InfoTypeTransformation infoTypeTransformation =
        InfoTypeTransformation.newBuilder()
            .setPrimitiveTransformation(primitiveTransformation)
            .addInfoTypes(surrogateInfoType)
            .build();
    InfoTypeTransformations transformations =
        InfoTypeTransformations.newBuilder().addTransformations(infoTypeTransformation).build();

    DeidentifyConfig reidentifyConfig =
        DeidentifyConfig.newBuilder().setInfoTypeTransformations(transformations).build();

    // Combine configurations into a request for the service.
    ReidentifyContentRequest request =
        ReidentifyContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(contentItem)
            .setInspectConfig(inspectConfig)
            .setReidentifyConfig(reidentifyConfig)
            .build();

    // Send the request and receive response from the service
    ReidentifyContentResponse response = dlp.reidentifyContent(request);

    // Print the results
    System.out.println("Text after re-identification: " + response.getItem().getValue());
  }
}
 
Example #25
Source File: DeIdentifyWithSimpleWordList.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void deidentifyWithSimpleWordList(String projectId, String textToDeIdentify)
    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 what content you want the service to DeIdentify.
    ContentItem contentItem = ContentItem.newBuilder().setValue(textToDeIdentify).build();

    // Construct the word list to be detected
    Dictionary wordList =
        Dictionary.newBuilder()
            .setWordList(
                WordList.newBuilder()
                    .addWords("RM-GREEN")
                    .addWords("RM-YELLOW")
                    .addWords("RM-ORANGE")
                    .build())
            .build();

    // Specify the word list custom info type the inspection will look for.
    InfoType infoType = InfoType.newBuilder().setName("CUSTOM_ROOM_ID").build();
    CustomInfoType customInfoType =
        CustomInfoType.newBuilder().setInfoType(infoType).setDictionary(wordList).build();
    InspectConfig inspectConfig =
        InspectConfig.newBuilder().addCustomInfoTypes(customInfoType).build();

    // Define type of deidentification as replacement.
    PrimitiveTransformation primitiveTransformation =
        PrimitiveTransformation.newBuilder()
            .setReplaceWithInfoTypeConfig(ReplaceWithInfoTypeConfig.getDefaultInstance())
            .build();

    // Associate deidentification type with info type.
    InfoTypeTransformation transformation =
        InfoTypeTransformation.newBuilder()
            .addInfoTypes(infoType)
            .setPrimitiveTransformation(primitiveTransformation)
            .build();

    // Construct the configuration for the Redact request and list all desired transformations.
    DeidentifyConfig deidentifyConfig =
        DeidentifyConfig.newBuilder()
            .setInfoTypeTransformations(
                InfoTypeTransformations.newBuilder().addTransformations(transformation))
            .build();

    // Combine configurations into a request for the service.
    DeidentifyContentRequest request =
        DeidentifyContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(contentItem)
            .setInspectConfig(inspectConfig)
            .setDeidentifyConfig(deidentifyConfig)
            .build();

    // Send the request and receive response from the service
    DeidentifyContentResponse response = dlp.deidentifyContent(request);

    // Print the results
    System.out.println(
        "Text after replace with infotype config: " + response.getItem().getValue());
  }
}
 
Example #26
Source File: DeIdentifyWithReplacement.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void deIdentifyWithReplacement(String projectId, String textToRedact) {
  // 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.
    ContentItem item = ContentItem.newBuilder()
        .setValue(textToRedact).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("EMAIL_ADDRESS").build();
    InspectConfig inspectConfig = InspectConfig.newBuilder().addInfoTypes(infoType).build();
    // Specify replacement string to be used for the finding.
    ReplaceValueConfig replaceValueConfig = ReplaceValueConfig.newBuilder()
        .setNewValue(Value.newBuilder().setStringValue("[email-address]").build())
        .build();
    // Define type of deidentification as replacement.
    PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder()
        .setReplaceConfig(replaceValueConfig)
        .build();
    // Associate deidentification type with info type.
    InfoTypeTransformation transformation = InfoTypeTransformation.newBuilder()
        .addInfoTypes(infoType)
        .setPrimitiveTransformation(primitiveTransformation)
        .build();
    // Construct the configuration for the Redact request and list all desired transformations.
    DeidentifyConfig redactConfig = DeidentifyConfig.newBuilder()
        .setInfoTypeTransformations(InfoTypeTransformations.newBuilder()
            .addTransformations(transformation))
        .build();

    // Construct the Redact request to be sent by the client.
    DeidentifyContentRequest request =
        DeidentifyContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(item)
            .setDeidentifyConfig(redactConfig)
            .setInspectConfig(inspectConfig)
            .build();

    // Use the client to send the API request.
    DeidentifyContentResponse response = dlp.deidentifyContent(request);

    // Parse the response and process results
    System.out.println("Text after redaction: " + response.getItem().getValue());
  } catch (Exception e) {
    System.out.println("Error during inspectString: \n" + e.toString());
  }
}
 
Example #27
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 #28
Source File: DeIdentifyTableRowSuppress.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static Table deIdentifyTableRowSuppress(String projectId, Table tableToDeIdentify)
    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 what content you want the service to de-identify.
    ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build();

    // Specify when the content should be de-identified.
    Condition condition = Condition.newBuilder()
        .setField(FieldId.newBuilder().setName("AGE").build())
        .setOperator(RelationalOperator.GREATER_THAN)
        .setValue(Value.newBuilder().setIntegerValue(89).build()).build();
    // Apply the condition to record suppression.
    RecordSuppression recordSuppressions =
        RecordSuppression.newBuilder()
            .setCondition(RecordCondition.newBuilder()
                .setExpressions(Expressions.newBuilder()
                    .setConditions(Conditions.newBuilder().addConditions(condition).build())
                    .build())
                .build())
            .build();
    // Use record suppression as the only transformation
    RecordTransformations transformations =
        RecordTransformations.newBuilder()
            .addRecordSuppressions(recordSuppressions)
            .build();

    DeidentifyConfig deidentifyConfig =
        DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build();

    // Combine configurations into a request for the service.
    DeidentifyContentRequest request =
        DeidentifyContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(contentItem)
            .setDeidentifyConfig(deidentifyConfig)
            .build();

    // Send the request and receive response from the service.
    DeidentifyContentResponse response = dlp.deidentifyContent(request);

    // Print the results.
    System.out.println(
        "Table after de-identification: " + response.getItem().getTable());

    return response.getItem().getTable();
  }
}
 
Example #29
Source File: DeIdentifyWithExceptionList.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void deIdentifyWithExceptionList(String projectId, String textToDeIdentify)
    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 what content you want the service to DeIdentify.
    ContentItem contentItem = ContentItem.newBuilder().setValue(textToDeIdentify).build();

    // Construct the custom word list to be detected.
    Dictionary wordList =
        Dictionary.newBuilder()
            .setWordList(
                WordList.newBuilder()
                    .addWords("[email protected]")
                    .addWords("[email protected]")
                    .build())
            .build();

    // Construct the custom dictionary detector associated with the word list.
    InfoType developerEmail = InfoType.newBuilder().setName("DEVELOPER_EMAIL").build();
    CustomInfoType customInfoType =
        CustomInfoType.newBuilder().setInfoType(developerEmail).setDictionary(wordList).build();

    // Specify the word list custom info type and build-in info type the inspection will look for.
    InfoType emailAddress = InfoType.newBuilder().setName("EMAIL_ADDRESS").build();
    InspectConfig inspectConfig =
        InspectConfig.newBuilder()
            .addInfoTypes(emailAddress)
            .addCustomInfoTypes(customInfoType)
            .build();

    // Define type of deidentification as replacement.
    PrimitiveTransformation primitiveTransformation =
        PrimitiveTransformation.newBuilder()
            .setReplaceWithInfoTypeConfig(ReplaceWithInfoTypeConfig.getDefaultInstance())
            .build();

    // Associate de-identification type with info type.
    InfoTypeTransformation transformation =
        InfoTypeTransformation.newBuilder()
            .addInfoTypes(emailAddress)
            .setPrimitiveTransformation(primitiveTransformation)
            .build();

    // Construct the configuration for the de-id request and list all desired transformations.
    DeidentifyConfig deidentifyConfig =
        DeidentifyConfig.newBuilder()
            .setInfoTypeTransformations(
                InfoTypeTransformations.newBuilder().addTransformations(transformation))
            .build();

    // Combine configurations into a request for the service.
    DeidentifyContentRequest request =
        DeidentifyContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(contentItem)
            .setInspectConfig(inspectConfig)
            .setDeidentifyConfig(deidentifyConfig)
            .build();

    // Send the request and receive response from the service
    DeidentifyContentResponse response = dlp.deidentifyContent(request);

    // Print the results
    System.out.println(
        "Text after replace with infotype config: " + response.getItem().getValue());
  }
}
 
Example #30
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());
    }
  }
}