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

The following examples show how to use com.google.privacy.dlp.v2.DeidentifyContentRequest. 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: DLPTextToBigQueryStreaming.java    From dlp-dataflow-deidentification with Apache License 2.0 6 votes vote down vote up
@Setup
public void setup() {
  if (this.inspectTemplateName.isAccessible()) {
    if (this.inspectTemplateName.get() != null) {
      this.inspectTemplateExist = true;
    }
  }
  if (this.deIdentifyTemplateName.isAccessible()) {
    if (this.deIdentifyTemplateName.get() != null) {
      this.requestBuilder =
          DeidentifyContentRequest.newBuilder()
              .setParent(ProjectName.of(this.dlpProjectId.get()).toString())
              .setDeidentifyTemplateName(this.deIdentifyTemplateName.get());
      if (this.inspectTemplateExist) {
        this.requestBuilder.setInspectTemplateName(this.inspectTemplateName.get());
      }
    }
  }
}
 
Example #2
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 #3
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 #4
Source File: DLPTextToBigQueryStreaming.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
@Setup
public void setup() {
  if (this.inspectTemplateName.isAccessible()) {
    if (this.inspectTemplateName.get() != null) {
      this.inspectTemplateExist = true;
    }
  }
  if (this.deIdentifyTemplateName.isAccessible()) {
    if (this.deIdentifyTemplateName.get() != null) {
      this.requestBuilder =
          DeidentifyContentRequest.newBuilder()
              .setParent(ProjectName.of(this.dlpProjectId.get()).toString())
              .setDeidentifyTemplateName(this.deIdentifyTemplateName.get());
      if (this.inspectTemplateExist) {
        this.requestBuilder.setInspectTemplateName(this.inspectTemplateName.get());
      }
    }
  }
}
 
Example #5
Source File: DLPDeidentifyText.java    From beam with Apache License 2.0 6 votes vote down vote up
@Setup
public void setup() throws IOException {
  requestBuilder =
      DeidentifyContentRequest.newBuilder().setParent(ProjectName.of(projectId).toString());
  if (inspectTemplateName != null) {
    requestBuilder.setInspectTemplateName(inspectTemplateName);
  }
  if (inspectConfig != null) {
    requestBuilder.setInspectConfig(inspectConfig);
  }
  if (deidentifyConfig != null) {
    requestBuilder.setDeidentifyConfig(deidentifyConfig);
  }
  if (deidentifyTemplateName != null) {
    requestBuilder.setDeidentifyTemplateName(deidentifyTemplateName);
  }
  dlpServiceClient = DlpServiceClient.create();
}
 
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: 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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #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: DeIdentifyTableBucketing.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static Table deIdentifyTableBucketing(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.
    FixedSizeBucketingConfig fixedSizeBucketingConfig =
        FixedSizeBucketingConfig.newBuilder()
            .setBucketSize(10)
            .setLowerBound(Value.newBuilder().setIntegerValue(0).build())
            .setUpperBound(Value.newBuilder().setIntegerValue(100).build())
            .build();
    PrimitiveTransformation primitiveTransformation =
        PrimitiveTransformation.newBuilder()
            .setFixedSizeBucketingConfig(fixedSizeBucketingConfig)
            .build();

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

    // Associate the encryption with the specified field.
    FieldTransformation fieldTransformation =
        FieldTransformation.newBuilder()
            .setPrimitiveTransformation(primitiveTransformation)
            .addFields(fieldId)
            .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 #17
Source File: DeIdentifyWithFpe.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void deIdentifyWithFpe(
    String projectId, String textToDeIdentify, 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 DeIdentify
    ContentItem contentItem = ContentItem.newBuilder().setValue(textToDeIdentify).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("US_SOCIAL_SECURITY_NUMBER").build();
    InspectConfig inspectConfig =
        InspectConfig.newBuilder().addAllInfoTypes(Arrays.asList(infoType)).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 the info from the inspection should be encrypted.
    InfoType surrogateInfoType = InfoType.newBuilder().setName("SSN_TOKEN").build();
    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)
            .build();
    InfoTypeTransformations transformations =
        InfoTypeTransformations.newBuilder().addTransformations(infoTypeTransformation).build();

    DeidentifyConfig deidentifyConfig =
        DeidentifyConfig.newBuilder().setInfoTypeTransformations(transformations).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 format-preserving encryption: " + response.getItem().getValue());
  }
}
 
Example #18
Source File: DeIdentifyWithDateShift.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void deIdentifyWithDateShift(
    String projectId, Path inputCsvFile, Path outputCsvFile) 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()) {
    // Read the contents of the CSV file into a Table
    List<FieldId> headers;
    List<Table.Row> rows;
    try (BufferedReader input = Files.newBufferedReader(inputCsvFile)) {
      // Parse and convert the first line into header names
      headers =
          Arrays.stream(input.readLine().split(","))
              .map(header -> FieldId.newBuilder().setName(header).build())
              .collect(Collectors.toList());
      // Parse the remainder of the file as Table.Rows
      rows =
          input.lines().map(DeIdentifyWithDateShift::parseLineAsRow).collect(Collectors.toList());
    }
    Table table = Table.newBuilder().addAllHeaders(headers).addAllRows(rows).build();
    ContentItem item = ContentItem.newBuilder().setTable(table).build();

    // Set the maximum days to shift dates backwards (lower bound) or forward (upper bound)
    DateShiftConfig dateShiftConfig =
        DateShiftConfig.newBuilder().setLowerBoundDays(5).setUpperBoundDays(5).build();
    PrimitiveTransformation transformation =
        PrimitiveTransformation.newBuilder().setDateShiftConfig(dateShiftConfig).build();
    // Specify which fields the DateShift should apply too
    List<FieldId> dateFields = Arrays.asList(headers.get(1), headers.get(3));
    FieldTransformation fieldTransformation =
        FieldTransformation.newBuilder()
            .addAllFields(dateFields)
            .setPrimitiveTransformation(transformation)
            .build();
    RecordTransformations recordTransformations =
        RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build();
    // Specify the config for the de-identify request
    DeidentifyConfig deidentifyConfig =
        DeidentifyConfig.newBuilder().setRecordTransformations(recordTransformations).build();

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

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

    // Write the results to the target CSV file
    try (BufferedWriter writer = Files.newBufferedWriter(outputCsvFile)) {
      Table outTable = response.getItem().getTable();
      String headerOut =
          outTable.getHeadersList().stream()
              .map(FieldId::getName)
              .collect(Collectors.joining(","));
      writer.write(headerOut + "\n");

      List<String> rowOutput =
          outTable.getRowsList().stream()
              .map(row -> joinRow(row.getValuesList()))
              .collect(Collectors.toList());
      for (String line : rowOutput) {
        writer.write(line + "\n");
      }
      System.out.println("Content written to file: " + outputCsvFile.toString());
    }
  }
}
 
Example #19
Source File: DeIdentifyTextWithFpe.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void deIdentifyTextWithFpe(
    String projectId, String textToDeIdentify, 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 de-identify.
    ContentItem contentItem = ContentItem.newBuilder().setValue(textToDeIdentify).build();

    // Specify the type of info you want the service to de-identify.
    // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types.
    InfoType infoType = InfoType.newBuilder().setName("PHONE_NUMBER").build();
    InspectConfig inspectConfig =
        InspectConfig.newBuilder().addAllInfoTypes(Arrays.asList(infoType)).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 the info from the inspection should be encrypted.
    InfoType surrogateInfoType = InfoType.newBuilder().setName("PHONE_TOKEN").build();
    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)
            .build();
    InfoTypeTransformations transformations =
        InfoTypeTransformations.newBuilder().addTransformations(infoTypeTransformation).build();

    DeidentifyConfig deidentifyConfig =
        DeidentifyConfig.newBuilder().setInfoTypeTransformations(transformations).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 format-preserving encryption: " + response.getItem().getValue());
  }
}
 
Example #20
Source File: DeIdentifyTableWithFpe.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void deIdentifyTableWithFpe(
    String projectId, Table tableToDeIdentify, 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 de-identify.
    ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).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 the content should be encrypted.
    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)
            .build();
    PrimitiveTransformation primitiveTransformation =
        PrimitiveTransformation.newBuilder()
            .setCryptoReplaceFfxFpeConfig(cryptoReplaceFfxFpeConfig)
            .build();

    // Specify field to be encrypted.
    FieldId fieldId = FieldId.newBuilder().setName("Employee ID").build();

    // Associate the encryption with the specified field.
    FieldTransformation fieldTransformation =
        FieldTransformation.newBuilder()
            .setPrimitiveTransformation(primitiveTransformation)
            .addFields(fieldId)
            .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 format-preserving encryption: " + response.getItem().getTable());
  }
}
 
Example #21
Source File: DeIdentifyWithRedaction.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void deIdentifyWithRedaction(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();
    // Define type of deidentification.
    PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder()
        .setRedactConfig(RedactConfig.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 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 #22
Source File: DeIdentifyWithMasking.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void deIdentifyWithMasking(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();

    // 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("US_SOCIAL_SECURITY_NUMBER").build();
    InspectConfig inspectConfig =
        InspectConfig.newBuilder().addAllInfoTypes(Arrays.asList(infoType)).build();

    // Specify how the info from the inspection should be masked.
    CharacterMaskConfig characterMaskConfig =
        CharacterMaskConfig.newBuilder()
            .setMaskingCharacter("X") // Character to replace the found info with
            .setNumberToMask(5) // How many characters should be masked
            .build();
    PrimitiveTransformation primitiveTransformation =
        PrimitiveTransformation.newBuilder()
            .setReplaceWithInfoTypeConfig(ReplaceWithInfoTypeConfig.getDefaultInstance())
            .build();
    InfoTypeTransformation infoTypeTransformation =
        InfoTypeTransformation.newBuilder()
            .setPrimitiveTransformation(primitiveTransformation)
            .build();
    InfoTypeTransformations transformations =
        InfoTypeTransformations.newBuilder().addTransformations(infoTypeTransformation).build();

    DeidentifyConfig deidentifyConfig =
        DeidentifyConfig.newBuilder().setInfoTypeTransformations(transformations).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 masking: " + response.getItem().getValue());
  }
}