|
| 1 | +/** |
| 2 | + * Copyright 2017 Google Inc. |
| 3 | + * <p> |
| 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 | + * <p> |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 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 com.example.dlp; |
| 18 | + |
| 19 | +import com.google.cloud.dlp.v2beta1.DlpServiceClient; |
| 20 | +import com.google.common.io.BaseEncoding; |
| 21 | +import com.google.privacy.dlp.v2beta1.CharacterMaskConfig; |
| 22 | +import com.google.privacy.dlp.v2beta1.ContentItem; |
| 23 | +import com.google.privacy.dlp.v2beta1.CryptoKey; |
| 24 | +import com.google.privacy.dlp.v2beta1.CryptoReplaceFfxFpeConfig; |
| 25 | +import com.google.privacy.dlp.v2beta1.CryptoReplaceFfxFpeConfig.FfxCommonNativeAlphabet; |
| 26 | +import com.google.privacy.dlp.v2beta1.DeidentifyConfig; |
| 27 | +import com.google.privacy.dlp.v2beta1.DeidentifyContentRequest; |
| 28 | +import com.google.privacy.dlp.v2beta1.DeidentifyContentResponse; |
| 29 | +import com.google.privacy.dlp.v2beta1.InfoTypeTransformations; |
| 30 | +import com.google.privacy.dlp.v2beta1.InfoTypeTransformations.InfoTypeTransformation; |
| 31 | +import com.google.privacy.dlp.v2beta1.KmsWrappedCryptoKey; |
| 32 | +import com.google.privacy.dlp.v2beta1.PrimitiveTransformation; |
| 33 | +import com.google.protobuf.ByteString; |
| 34 | +import org.apache.commons.cli.CommandLine; |
| 35 | +import org.apache.commons.cli.CommandLineParser; |
| 36 | +import org.apache.commons.cli.DefaultParser; |
| 37 | +import org.apache.commons.cli.HelpFormatter; |
| 38 | +import org.apache.commons.cli.Option; |
| 39 | +import org.apache.commons.cli.OptionGroup; |
| 40 | +import org.apache.commons.cli.Options; |
| 41 | +import org.apache.commons.cli.ParseException; |
| 42 | + |
| 43 | +public class DeIdentification { |
| 44 | + |
| 45 | + private static void deIdentifyWithMask( |
| 46 | + String string, |
| 47 | + Character maskingCharacter, |
| 48 | + int numberToMask) { |
| 49 | + // [START dlp_deidentify_mask] |
| 50 | + /** |
| 51 | + * Deidentify a string by masking sensitive information with a character using the DLP API. |
| 52 | + * @param string The string to deidentify. |
| 53 | + * @param maskingCharacter (Optional) The character to mask sensitive data with. |
| 54 | + * @param numberToMask (Optional) The number of characters' worth of sensitive data to mask. |
| 55 | + * Omitting this value or setting it to 0 masks all sensitive chars. |
| 56 | + */ |
| 57 | + |
| 58 | + // instantiate a client |
| 59 | + try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) { |
| 60 | + |
| 61 | + // string = "My SSN is 372819127"; |
| 62 | + // numberToMask = 5; |
| 63 | + // maskingCharacter = 'x'; |
| 64 | + |
| 65 | + ContentItem contentItem = |
| 66 | + ContentItem.newBuilder() |
| 67 | + .setType("text/plain") |
| 68 | + .setValue(string) |
| 69 | + .build(); |
| 70 | + |
| 71 | + CharacterMaskConfig characterMaskConfig = |
| 72 | + CharacterMaskConfig.newBuilder() |
| 73 | + .setMaskingCharacter(maskingCharacter.toString()) |
| 74 | + .setNumberToMask(numberToMask) |
| 75 | + .build(); |
| 76 | + |
| 77 | + // Create the deidentification transformation configuration |
| 78 | + PrimitiveTransformation primitiveTransformation = |
| 79 | + PrimitiveTransformation.newBuilder() |
| 80 | + .setCharacterMaskConfig(characterMaskConfig) |
| 81 | + .build(); |
| 82 | + |
| 83 | + InfoTypeTransformation infoTypeTransformationObject = |
| 84 | + InfoTypeTransformation.newBuilder() |
| 85 | + .setPrimitiveTransformation(primitiveTransformation) |
| 86 | + .build(); |
| 87 | + |
| 88 | + InfoTypeTransformations infoTypeTransformationArray = |
| 89 | + InfoTypeTransformations.newBuilder() |
| 90 | + .addTransformations(infoTypeTransformationObject) |
| 91 | + .build(); |
| 92 | + |
| 93 | + // Create the deidentification request object |
| 94 | + DeidentifyConfig deidentifyConfig = |
| 95 | + DeidentifyConfig.newBuilder() |
| 96 | + .setInfoTypeTransformations(infoTypeTransformationArray) |
| 97 | + .build(); |
| 98 | + |
| 99 | + DeidentifyContentRequest request = |
| 100 | + DeidentifyContentRequest.newBuilder() |
| 101 | + .setDeidentifyConfig(deidentifyConfig) |
| 102 | + .addItems(contentItem) |
| 103 | + .build(); |
| 104 | + |
| 105 | + // Execute the deidentification request |
| 106 | + DeidentifyContentResponse response = dlpServiceClient.deidentifyContent(request); |
| 107 | + |
| 108 | + // Print the character-masked input value |
| 109 | + // e.g. "My SSN is 123456789" --> "My SSN is *********" |
| 110 | + for (ContentItem item : response.getItemsList()) { |
| 111 | + System.out.println(item.getValue()); |
| 112 | + } |
| 113 | + } catch (Exception e) { |
| 114 | + System.out.println("Error in deidentifyWithMask: " + e.getMessage()); |
| 115 | + } |
| 116 | + // [END dlp_deidentify_mask] |
| 117 | + } |
| 118 | + |
| 119 | + private static void deIdentifyWithFpe( |
| 120 | + String string, FfxCommonNativeAlphabet alphabet, String keyName, String wrappedKey) { |
| 121 | + // [START dlp_deidentify_fpe] |
| 122 | + /** |
| 123 | + * Deidentify a string by encrypting sensitive information while preserving format. |
| 124 | + * @param string The string to deidentify. |
| 125 | + * @param alphabet The set of characters to use when encrypting the input. For more information, |
| 126 | + * see cloud.google.com/dlp/docs/reference/rest/v2beta1/content/deidentify |
| 127 | + * @param keyName The name of the Cloud KMS key to use when decrypting the wrapped key. |
| 128 | + * @param wrappedKey The encrypted (or "wrapped") AES-256 encryption key. |
| 129 | + */ |
| 130 | + |
| 131 | + // instantiate a client |
| 132 | + try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) { |
| 133 | + |
| 134 | + // string = "My SSN is 372819127"; |
| 135 | + // alphabet = FfxCommonNativeAlphabet.ALPHA_NUMERIC; |
| 136 | + // keyName = "projects/GCP_PROJECT/locations/REGION/keyRings/KEYRING_ID/cryptoKeys/KEY_NAME"; |
| 137 | + // wrappedKey = "YOUR_ENCRYPTED_AES_256_KEY" |
| 138 | + |
| 139 | + ContentItem contentItem = |
| 140 | + ContentItem.newBuilder() |
| 141 | + .setType("text/plain") |
| 142 | + .setValue(string) |
| 143 | + .build(); |
| 144 | + |
| 145 | + // Create the format-preserving encryption (FPE) configuration |
| 146 | + KmsWrappedCryptoKey kmsWrappedCryptoKey = |
| 147 | + KmsWrappedCryptoKey.newBuilder() |
| 148 | + .setWrappedKey(ByteString.copyFrom(BaseEncoding.base64().decode(wrappedKey))) |
| 149 | + .setCryptoKeyName(keyName) |
| 150 | + .build(); |
| 151 | + |
| 152 | + CryptoKey cryptoKey = |
| 153 | + CryptoKey.newBuilder() |
| 154 | + .setKmsWrapped(kmsWrappedCryptoKey) |
| 155 | + .build(); |
| 156 | + |
| 157 | + CryptoReplaceFfxFpeConfig cryptoReplaceFfxFpeConfig = |
| 158 | + CryptoReplaceFfxFpeConfig.newBuilder() |
| 159 | + .setCryptoKey(cryptoKey) |
| 160 | + .setCommonAlphabet(alphabet) |
| 161 | + .build(); |
| 162 | + |
| 163 | + // Create the deidentification transformation configuration |
| 164 | + PrimitiveTransformation primitiveTransformation = |
| 165 | + PrimitiveTransformation.newBuilder() |
| 166 | + .setCryptoReplaceFfxFpeConfig(cryptoReplaceFfxFpeConfig) |
| 167 | + .build(); |
| 168 | + |
| 169 | + InfoTypeTransformation infoTypeTransformationObject = |
| 170 | + InfoTypeTransformation.newBuilder() |
| 171 | + .setPrimitiveTransformation(primitiveTransformation) |
| 172 | + .build(); |
| 173 | + |
| 174 | + InfoTypeTransformations infoTypeTransformationArray = |
| 175 | + InfoTypeTransformations.newBuilder() |
| 176 | + .addTransformations(infoTypeTransformationObject) |
| 177 | + .build(); |
| 178 | + |
| 179 | + // Create the deidentification request object |
| 180 | + DeidentifyConfig deidentifyConfig = |
| 181 | + DeidentifyConfig.newBuilder() |
| 182 | + .setInfoTypeTransformations(infoTypeTransformationArray) |
| 183 | + .build(); |
| 184 | + |
| 185 | + DeidentifyContentRequest request = |
| 186 | + DeidentifyContentRequest.newBuilder() |
| 187 | + .setDeidentifyConfig(deidentifyConfig) |
| 188 | + .addItems(contentItem) |
| 189 | + .build(); |
| 190 | + |
| 191 | + // Execute the deidentification request |
| 192 | + DeidentifyContentResponse response = dlpServiceClient.deidentifyContent(request); |
| 193 | + |
| 194 | + // Print the deidentified input value |
| 195 | + // e.g. "My SSN is 123456789" --> "My SSN is 7261298621" |
| 196 | + for (ContentItem item : response.getItemsList()) { |
| 197 | + System.out.println(item.getValue()); |
| 198 | + } |
| 199 | + } catch (Exception e) { |
| 200 | + System.out.println("Error in deidentifyWithFpe: " + e.getMessage()); |
| 201 | + } |
| 202 | + // [END dlp_deidentify_fpe] |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Command line application to de-identify data using the Data Loss Prevention API. |
| 207 | + * Supported data format: strings |
| 208 | + */ |
| 209 | + public static void main(String[] args) throws Exception { |
| 210 | + |
| 211 | + OptionGroup optionsGroup = new OptionGroup(); |
| 212 | + optionsGroup.setRequired(true); |
| 213 | + |
| 214 | + Option deidentifyMaskingOption = new Option("m", "mask", true, "deid with character masking"); |
| 215 | + optionsGroup.addOption(deidentifyMaskingOption); |
| 216 | + |
| 217 | + Option deidentifyFpeOption = new Option("f", "fpe", true, "deid with FFX FPE"); |
| 218 | + optionsGroup.addOption(deidentifyFpeOption); |
| 219 | + |
| 220 | + Options commandLineOptions = new Options(); |
| 221 | + commandLineOptions.addOptionGroup(optionsGroup); |
| 222 | + |
| 223 | + Option maskingCharacterOption = |
| 224 | + Option.builder("maskingCharacter").hasArg(true).required(false).build(); |
| 225 | + commandLineOptions.addOption(maskingCharacterOption); |
| 226 | + |
| 227 | + Option numberToMaskOption = |
| 228 | + Option.builder("numberToMask").hasArg(true).required(false).build(); |
| 229 | + commandLineOptions.addOption(numberToMaskOption); |
| 230 | + |
| 231 | + Option alphabetOption = |
| 232 | + Option.builder("commonAlphabet").hasArg(true).required(false).build(); |
| 233 | + commandLineOptions.addOption(alphabetOption); |
| 234 | + |
| 235 | + Option wrappedKeyOption = |
| 236 | + Option.builder("wrappedKey").hasArg(true).required(false).build(); |
| 237 | + commandLineOptions.addOption(wrappedKeyOption); |
| 238 | + |
| 239 | + Option keyNameOption = |
| 240 | + Option.builder("keyName").hasArg(true).required(false).build(); |
| 241 | + commandLineOptions.addOption(keyNameOption); |
| 242 | + |
| 243 | + CommandLineParser parser = new DefaultParser(); |
| 244 | + HelpFormatter formatter = new HelpFormatter(); |
| 245 | + CommandLine cmd; |
| 246 | + |
| 247 | + try { |
| 248 | + cmd = parser.parse(commandLineOptions, args); |
| 249 | + } catch (ParseException e) { |
| 250 | + System.out.println(e.getMessage()); |
| 251 | + formatter.printHelp(DeIdentification.class.getName(), commandLineOptions); |
| 252 | + System.exit(1); |
| 253 | + return; |
| 254 | + } |
| 255 | + |
| 256 | + if (cmd.hasOption("m")) { |
| 257 | + // deidentification with character masking |
| 258 | + int numberToMask = Integer.parseInt(cmd.getOptionValue(numberToMaskOption.getOpt(), "0")); |
| 259 | + char maskingCharacter = cmd.getOptionValue(maskingCharacterOption.getOpt(), "*").charAt(0); |
| 260 | + String val = cmd.getOptionValue(deidentifyMaskingOption.getOpt()); |
| 261 | + deIdentifyWithMask(val, maskingCharacter, numberToMask); |
| 262 | + } else if (cmd.hasOption("f")) { |
| 263 | + // deidentification with FPE |
| 264 | + String wrappedKey = cmd.getOptionValue(wrappedKeyOption.getOpt()); |
| 265 | + String keyName = cmd.getOptionValue(keyNameOption.getOpt()); |
| 266 | + String val = cmd.getOptionValue(deidentifyFpeOption.getOpt()); |
| 267 | + FfxCommonNativeAlphabet alphabet = |
| 268 | + FfxCommonNativeAlphabet.valueOf( |
| 269 | + cmd.getOptionValue( |
| 270 | + alphabetOption.getOpt(), FfxCommonNativeAlphabet.ALPHA_NUMERIC.name())); |
| 271 | + deIdentifyWithFpe(val, alphabet, keyName, wrappedKey); |
| 272 | + } |
| 273 | + } |
| 274 | +} |
0 commit comments