|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package dlp.snippets; |
| 18 | + |
| 19 | +// [START dlp_deidentify_table_fpe] |
| 20 | + |
| 21 | +import com.google.cloud.dlp.v2.DlpServiceClient; |
| 22 | +import com.google.common.io.BaseEncoding; |
| 23 | +import com.google.privacy.dlp.v2.ContentItem; |
| 24 | +import com.google.privacy.dlp.v2.CryptoKey; |
| 25 | +import com.google.privacy.dlp.v2.CryptoReplaceFfxFpeConfig; |
| 26 | +import com.google.privacy.dlp.v2.CryptoReplaceFfxFpeConfig.FfxCommonNativeAlphabet; |
| 27 | +import com.google.privacy.dlp.v2.DeidentifyConfig; |
| 28 | +import com.google.privacy.dlp.v2.DeidentifyContentRequest; |
| 29 | +import com.google.privacy.dlp.v2.DeidentifyContentResponse; |
| 30 | +import com.google.privacy.dlp.v2.FieldId; |
| 31 | +import com.google.privacy.dlp.v2.FieldTransformation; |
| 32 | +import com.google.privacy.dlp.v2.InfoType; |
| 33 | +import com.google.privacy.dlp.v2.InfoTypeTransformations; |
| 34 | +import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation; |
| 35 | +import com.google.privacy.dlp.v2.InspectConfig; |
| 36 | +import com.google.privacy.dlp.v2.KmsWrappedCryptoKey; |
| 37 | +import com.google.privacy.dlp.v2.LocationName; |
| 38 | +import com.google.privacy.dlp.v2.PrimitiveTransformation; |
| 39 | +import com.google.privacy.dlp.v2.RecordTransformations; |
| 40 | +import com.google.privacy.dlp.v2.Table; |
| 41 | +import com.google.privacy.dlp.v2.Table.Row; |
| 42 | +import com.google.privacy.dlp.v2.Value; |
| 43 | +import com.google.protobuf.ByteString; |
| 44 | +import java.io.IOException; |
| 45 | +import java.util.Arrays; |
| 46 | + |
| 47 | +public class DeIdentifyTableWithFpe { |
| 48 | + |
| 49 | + public static void deIdentifyTableWithFpe() throws IOException { |
| 50 | + // TODO(developer): Replace these variables before running the sample. |
| 51 | + String projectId = "your-project-id"; |
| 52 | + String kmsKeyName = |
| 53 | + "projects/YOUR_PROJECT/" |
| 54 | + + "locations/YOUR_KEYRING_REGION/" |
| 55 | + + "keyRings/YOUR_KEYRING_NAME/" |
| 56 | + + "cryptoKeys/YOUR_KEY_NAME"; |
| 57 | + String wrappedAesKey = "YOUR_ENCRYPTED_AES_256_KEY"; |
| 58 | + Table tableToDeIdentify = Table.newBuilder() |
| 59 | + .addHeaders(FieldId.newBuilder().setName("Employee ID").build()) |
| 60 | + .addHeaders(FieldId.newBuilder().setName("Date").build()) |
| 61 | + .addHeaders(FieldId.newBuilder().setName("Compensation").build()) |
| 62 | + .addRows(Row.newBuilder() |
| 63 | + .addValues(Value.newBuilder().setStringValue("11111").build()) |
| 64 | + .addValues(Value.newBuilder().setStringValue("2015").build()) |
| 65 | + .addValues(Value.newBuilder().setStringValue("$10").build()) |
| 66 | + .build()) |
| 67 | + .addRows(Row.newBuilder() |
| 68 | + .addValues(Value.newBuilder().setStringValue("11111").build()) |
| 69 | + .addValues(Value.newBuilder().setStringValue("2016").build()) |
| 70 | + .addValues(Value.newBuilder().setStringValue("$20").build()) |
| 71 | + .build()) |
| 72 | + .addRows(Row.newBuilder() |
| 73 | + .addValues(Value.newBuilder().setStringValue("22222").build()) |
| 74 | + .addValues(Value.newBuilder().setStringValue("2016").build()) |
| 75 | + .addValues(Value.newBuilder().setStringValue("$15").build()) |
| 76 | + .build()) |
| 77 | + .build(); |
| 78 | + deIdentifyTableWithFpe(projectId, tableToDeIdentify, kmsKeyName, wrappedAesKey); |
| 79 | + } |
| 80 | + |
| 81 | + public static void deIdentifyTableWithFpe( |
| 82 | + String projectId, Table tableToDeIdentify, String kmsKeyName, String wrappedAesKey) |
| 83 | + throws IOException { |
| 84 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 85 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 86 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 87 | + try (DlpServiceClient dlp = DlpServiceClient.create()) { |
| 88 | + // Specify what content you want the service to de-identify. |
| 89 | + ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build(); |
| 90 | + |
| 91 | + // Specify an encrypted AES-256 key and the name of the Cloud KMS key that encrypted it |
| 92 | + KmsWrappedCryptoKey kmsWrappedCryptoKey = |
| 93 | + KmsWrappedCryptoKey.newBuilder() |
| 94 | + .setWrappedKey(ByteString.copyFrom(BaseEncoding.base64().decode(wrappedAesKey))) |
| 95 | + .setCryptoKeyName(kmsKeyName) |
| 96 | + .build(); |
| 97 | + CryptoKey cryptoKey = CryptoKey.newBuilder().setKmsWrapped(kmsWrappedCryptoKey).build(); |
| 98 | + |
| 99 | + // Specify how the content should be encrypted. |
| 100 | + CryptoReplaceFfxFpeConfig cryptoReplaceFfxFpeConfig = |
| 101 | + CryptoReplaceFfxFpeConfig.newBuilder() |
| 102 | + .setCryptoKey(cryptoKey) |
| 103 | + // Set of characters in the input text. For more info, see |
| 104 | + // https://cloud.google.com/dlp/docs/reference/rest/v2/organizations.deidentifyTemplates#DeidentifyTemplate.FfxCommonNativeAlphabet |
| 105 | + .setCommonAlphabet(FfxCommonNativeAlphabet.NUMERIC) |
| 106 | + .build(); |
| 107 | + PrimitiveTransformation primitiveTransformation = |
| 108 | + PrimitiveTransformation.newBuilder() |
| 109 | + .setCryptoReplaceFfxFpeConfig(cryptoReplaceFfxFpeConfig) |
| 110 | + .build(); |
| 111 | + |
| 112 | + // Specify field to be encrypted. |
| 113 | + FieldId fieldId = FieldId.newBuilder().setName("Employee ID").build(); |
| 114 | + |
| 115 | + // Associate the encryption with the specified field. |
| 116 | + FieldTransformation fieldTransformation = |
| 117 | + FieldTransformation.newBuilder() |
| 118 | + .setPrimitiveTransformation(primitiveTransformation) |
| 119 | + .addFields(fieldId) |
| 120 | + .build(); |
| 121 | + RecordTransformations transformations = |
| 122 | + RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build(); |
| 123 | + |
| 124 | + DeidentifyConfig deidentifyConfig = |
| 125 | + DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); |
| 126 | + |
| 127 | + // Combine configurations into a request for the service. |
| 128 | + DeidentifyContentRequest request = |
| 129 | + DeidentifyContentRequest.newBuilder() |
| 130 | + .setParent(LocationName.of(projectId, "global").toString()) |
| 131 | + .setItem(contentItem) |
| 132 | + .setDeidentifyConfig(deidentifyConfig) |
| 133 | + .build(); |
| 134 | + |
| 135 | + // Send the request and receive response from the service. |
| 136 | + DeidentifyContentResponse response = dlp.deidentifyContent(request); |
| 137 | + |
| 138 | + // Print the results. |
| 139 | + System.out.println( |
| 140 | + "Table after format-preserving encryption: " + response.getItem().getTable()); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | +// [END dlp_deidentify_table_fpe] |
0 commit comments