|
42 | 42 | import com.google.privacy.dlp.v2.RecordTransformations;
|
43 | 43 | import com.google.privacy.dlp.v2.ReidentifyContentRequest;
|
44 | 44 | import com.google.privacy.dlp.v2.ReidentifyContentResponse;
|
| 45 | +import com.google.privacy.dlp.v2.ReplaceWithInfoTypeConfig; |
45 | 46 | import com.google.privacy.dlp.v2.Table;
|
46 | 47 | import com.google.privacy.dlp.v2.Value;
|
47 | 48 | import com.google.protobuf.ByteString;
|
|
71 | 72 |
|
72 | 73 | public class DeIdentification {
|
73 | 74 |
|
| 75 | + // [START dlp_deidentify_replace_with_info_type] |
| 76 | + /** |
| 77 | + * Deidentify a string by replacing sensitive information with its info type using the DLP API. |
| 78 | + * |
| 79 | + * @param string The string to deidentify. |
| 80 | + * @param projectId ID of Google Cloud project to run the API under. |
| 81 | + */ |
| 82 | + private static void deIdentifyReplaceWithInfoType( |
| 83 | + String string, |
| 84 | + List<InfoType> infoTypes, |
| 85 | + String projectId) { |
| 86 | + |
| 87 | + // instantiate a client |
| 88 | + try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) { |
| 89 | + |
| 90 | + ContentItem contentItem = ContentItem.newBuilder().setValue(string).build(); |
| 91 | + |
| 92 | + // Create the deidentification transformation configuration |
| 93 | + PrimitiveTransformation primitiveTransformation = |
| 94 | + PrimitiveTransformation.newBuilder() |
| 95 | + .setReplaceWithInfoTypeConfig(ReplaceWithInfoTypeConfig.getDefaultInstance()) |
| 96 | + .build(); |
| 97 | + |
| 98 | + InfoTypeTransformation infoTypeTransformationObject = |
| 99 | + InfoTypeTransformation.newBuilder() |
| 100 | + .setPrimitiveTransformation(primitiveTransformation) |
| 101 | + .build(); |
| 102 | + |
| 103 | + InfoTypeTransformations infoTypeTransformationArray = |
| 104 | + InfoTypeTransformations.newBuilder() |
| 105 | + .addTransformations(infoTypeTransformationObject) |
| 106 | + .build(); |
| 107 | + |
| 108 | + InspectConfig inspectConfig = |
| 109 | + InspectConfig.newBuilder() |
| 110 | + .addAllInfoTypes(infoTypes) |
| 111 | + .build(); |
| 112 | + |
| 113 | + DeidentifyConfig deidentifyConfig = |
| 114 | + DeidentifyConfig.newBuilder() |
| 115 | + .setInfoTypeTransformations(infoTypeTransformationArray) |
| 116 | + .build(); |
| 117 | + |
| 118 | + // Create the deidentification request object |
| 119 | + DeidentifyContentRequest request = |
| 120 | + DeidentifyContentRequest.newBuilder() |
| 121 | + .setParent(ProjectName.of(projectId).toString()) |
| 122 | + .setInspectConfig(inspectConfig) |
| 123 | + .setDeidentifyConfig(deidentifyConfig) |
| 124 | + .setItem(contentItem) |
| 125 | + .build(); |
| 126 | + |
| 127 | + // Execute the deidentification request |
| 128 | + DeidentifyContentResponse response = dlpServiceClient.deidentifyContent(request); |
| 129 | + |
| 130 | + // Print the redacted input value |
| 131 | + // e.g. "My SSN is 123456789" --> "My SSN is [US_SOCIAL_SECURITY_NUMBER]" |
| 132 | + String result = response.getItem().getValue(); |
| 133 | + System.out.println(result); |
| 134 | + } catch (Exception e) { |
| 135 | + System.out.println("Error in deIdentifyReplaceWithInfoType: " + e.getMessage()); |
| 136 | + } |
| 137 | + } |
| 138 | + // [END dlp_deidentify_replace_with_info_type] |
| 139 | + |
74 | 140 | // [START dlp_deidentify_masking]
|
75 | 141 | /**
|
76 | 142 | * Deidentify a string by masking sensitive information with a character using the DLP API.
|
@@ -512,6 +578,10 @@ public static void main(String[] args) throws Exception {
|
512 | 578 | OptionGroup optionsGroup = new OptionGroup();
|
513 | 579 | optionsGroup.setRequired(true);
|
514 | 580 |
|
| 581 | + Option deidentifyReplaceWithInfoTypeOption = |
| 582 | + new Option("it", "info_type_replace", true, "Deidentify by replacing with info type."); |
| 583 | + optionsGroup.addOption(deidentifyReplaceWithInfoTypeOption); |
| 584 | + |
515 | 585 | Option deidentifyMaskingOption =
|
516 | 586 | new Option("m", "mask", true, "Deidentify with character masking.");
|
517 | 587 | optionsGroup.addOption(deidentifyMaskingOption);
|
@@ -606,7 +676,11 @@ public static void main(String[] args) throws Exception {
|
606 | 676 | }
|
607 | 677 | }
|
608 | 678 |
|
609 |
| - if (cmd.hasOption("m")) { |
| 679 | + if (cmd.hasOption("it")) { |
| 680 | + // replace with info type |
| 681 | + String val = cmd.getOptionValue(deidentifyReplaceWithInfoTypeOption.getOpt()); |
| 682 | + deIdentifyReplaceWithInfoType(val, infoTypesList, projectId); |
| 683 | + } else if (cmd.hasOption("m")) { |
610 | 684 | // deidentification with character masking
|
611 | 685 | int numberToMask = Integer.parseInt(cmd.getOptionValue(numberToMaskOption.getOpt(), "0"));
|
612 | 686 | char maskingCharacter = cmd.getOptionValue(maskingCharacterOption.getOpt(), "*").charAt(0);
|
|
0 commit comments