19
19
import com .google .cloud .ServiceOptions ;
20
20
import com .google .cloud .dlp .v2 .DlpServiceClient ;
21
21
import com .google .common .io .BaseEncoding ;
22
- import com .google .privacy .dlp .v2 .ByteContentItem ;
23
22
import com .google .privacy .dlp .v2 .CharacterMaskConfig ;
24
23
import com .google .privacy .dlp .v2 .ContentItem ;
25
24
import com .google .privacy .dlp .v2 .CryptoKey ;
26
25
import com .google .privacy .dlp .v2 .CryptoReplaceFfxFpeConfig ;
27
26
import com .google .privacy .dlp .v2 .CryptoReplaceFfxFpeConfig .FfxCommonNativeAlphabet ;
27
+ import com .google .privacy .dlp .v2 .CustomInfoType ;
28
+ import com .google .privacy .dlp .v2 .CustomInfoType .SurrogateType ;
28
29
import com .google .privacy .dlp .v2 .DateShiftConfig ;
29
30
import com .google .privacy .dlp .v2 .DeidentifyConfig ;
30
31
import com .google .privacy .dlp .v2 .DeidentifyContentRequest ;
31
32
import com .google .privacy .dlp .v2 .DeidentifyContentResponse ;
32
33
import com .google .privacy .dlp .v2 .FieldId ;
33
34
import com .google .privacy .dlp .v2 .FieldTransformation ;
35
+ import com .google .privacy .dlp .v2 .InfoType ;
34
36
import com .google .privacy .dlp .v2 .InfoTypeTransformations ;
35
37
import com .google .privacy .dlp .v2 .InfoTypeTransformations .InfoTypeTransformation ;
38
+ import com .google .privacy .dlp .v2 .InspectConfig ;
36
39
import com .google .privacy .dlp .v2 .KmsWrappedCryptoKey ;
37
40
import com .google .privacy .dlp .v2 .PrimitiveTransformation ;
38
41
import com .google .privacy .dlp .v2 .ProjectName ;
39
42
import com .google .privacy .dlp .v2 .RecordTransformations ;
43
+ import com .google .privacy .dlp .v2 .ReidentifyContentRequest ;
44
+ import com .google .privacy .dlp .v2 .ReidentifyContentResponse ;
40
45
import com .google .privacy .dlp .v2 .Table ;
41
46
import com .google .privacy .dlp .v2 .Value ;
42
47
import com .google .protobuf .ByteString ;
46
51
import java .io .File ;
47
52
import java .io .FileReader ;
48
53
import java .io .FileWriter ;
49
- import java .nio .charset .StandardCharsets ;
50
54
import java .nio .file .Path ;
51
55
import java .nio .file .Paths ;
52
56
import java .time .LocalDate ;
@@ -82,14 +86,11 @@ private static void deIdentifyWithMask(
82
86
// instantiate a client
83
87
try (DlpServiceClient dlpServiceClient = DlpServiceClient .create ()) {
84
88
85
- ByteContentItem byteContentItem =
86
- ByteContentItem .newBuilder ()
87
- .setType (ByteContentItem .BytesType .TEXT_UTF8 )
88
- .setData (ByteString .copyFrom (string , StandardCharsets .UTF_8 ))
89
+ ContentItem contentItem =
90
+ ContentItem .newBuilder ()
91
+ .setValue (string )
89
92
.build ();
90
93
91
- ContentItem contentItem = ContentItem .newBuilder ().setByteItem (byteContentItem ).build ();
92
-
93
94
CharacterMaskConfig characterMaskConfig =
94
95
CharacterMaskConfig .newBuilder ()
95
96
.setMaskingCharacter (maskingCharacter .toString ())
@@ -130,7 +131,7 @@ private static void deIdentifyWithMask(
130
131
131
132
// Print the character-masked input value
132
133
// e.g. "My SSN is 123456789" --> "My SSN is *********"
133
- String result = response .getItem ().getByteItem (). getData (). toStringUtf8 ();
134
+ String result = response .getItem ().getValue ();
134
135
System .out .println (result );
135
136
} catch (Exception e ) {
136
137
System .out .println ("Error in deidentifyWithMask: " + e .getMessage ());
@@ -154,16 +155,11 @@ private static void deIdentifyWithFpe(
154
155
FfxCommonNativeAlphabet alphabet ,
155
156
String keyName ,
156
157
String wrappedKey ,
157
- String projectId ) {
158
+ String projectId ,
159
+ String surrogateType ) {
158
160
// instantiate a client
159
161
try (DlpServiceClient dlpServiceClient = DlpServiceClient .create ()) {
160
-
161
- ByteContentItem byteContentItem =
162
- ByteContentItem .newBuilder ()
163
- .setData (ByteString .copyFrom (string , StandardCharsets .UTF_8 ))
164
- .build ();
165
-
166
- ContentItem contentItem = ContentItem .newBuilder ().setByteItem (byteContentItem ).build ();
162
+ ContentItem contentItem = ContentItem .newBuilder ().setValue (string ).build ();
167
163
168
164
// Create the format-preserving encryption (FPE) configuration
169
165
KmsWrappedCryptoKey kmsWrappedCryptoKey =
@@ -178,6 +174,7 @@ private static void deIdentifyWithFpe(
178
174
CryptoReplaceFfxFpeConfig .newBuilder ()
179
175
.setCryptoKey (cryptoKey )
180
176
.setCommonAlphabet (alphabet )
177
+ .setSurrogateInfoType (InfoType .newBuilder ().setName (surrogateType ).build ())
181
178
.build ();
182
179
183
180
// Create the deidentification transformation configuration
@@ -214,14 +211,113 @@ private static void deIdentifyWithFpe(
214
211
215
212
// Print the deidentified input value
216
213
// e.g. "My SSN is 123456789" --> "My SSN is 7261298621"
217
- String result = response .getItem ().getByteItem (). getData (). toStringUtf8 ();
214
+ String result = response .getItem ().getValue ();
218
215
System .out .println (result );
219
216
} catch (Exception e ) {
220
217
System .out .println ("Error in deidentifyWithFpe: " + e .getMessage ());
221
218
}
222
219
}
223
220
// [END dlp_deidentify_fpe]
224
221
222
+ // [START dlp_reidentify_fpe]
223
+ /**
224
+ * Reidentify a string by encrypting sensitive information while preserving format.
225
+ *
226
+ * @param string The string to reidentify.
227
+ * @param alphabet The set of characters used when encrypting the input. For more information,
228
+ * see cloud.google.com/dlp/docs/reference/rest/v2/content/deidentify
229
+ * @param keyName The name of the Cloud KMS key to use when decrypting the wrapped key.
230
+ * @param wrappedKey The encrypted (or "wrapped") AES-256 encryption key.
231
+ * @param projectId ID of Google Cloud project to run the API under.
232
+ * @param surrogateType The name of the surrogate custom info type to used
233
+ * during the encryption process.
234
+ */
235
+ private static void reIdentifyWithFpe (
236
+ String string ,
237
+ FfxCommonNativeAlphabet alphabet ,
238
+ String keyName ,
239
+ String wrappedKey ,
240
+ String projectId ,
241
+ String surrogateType ) {
242
+ // instantiate a client
243
+ try (DlpServiceClient dlpServiceClient = DlpServiceClient .create ()) {
244
+ ContentItem contentItem = ContentItem .newBuilder ().setValue (string ).build ();
245
+
246
+
247
+ InfoType surrogateTypeObject = InfoType .newBuilder ()
248
+ .setName (surrogateType )
249
+ .build ();
250
+
251
+ // Create the format-preserving encryption (FPE) configuration
252
+ KmsWrappedCryptoKey kmsWrappedCryptoKey =
253
+ KmsWrappedCryptoKey .newBuilder ()
254
+ .setWrappedKey (ByteString .copyFrom (BaseEncoding .base64 ().decode (wrappedKey )))
255
+ .setCryptoKeyName (keyName )
256
+ .build ();
257
+
258
+ CryptoKey cryptoKey = CryptoKey .newBuilder ().setKmsWrapped (kmsWrappedCryptoKey ).build ();
259
+
260
+ CryptoReplaceFfxFpeConfig cryptoReplaceFfxFpeConfig =
261
+ CryptoReplaceFfxFpeConfig .newBuilder ()
262
+ .setCryptoKey (cryptoKey )
263
+ .setCommonAlphabet (alphabet )
264
+ .setSurrogateInfoType (surrogateTypeObject )
265
+ .build ();
266
+
267
+ // Create the deidentification transformation configuration
268
+ PrimitiveTransformation primitiveTransformation =
269
+ PrimitiveTransformation .newBuilder ()
270
+ .setCryptoReplaceFfxFpeConfig (cryptoReplaceFfxFpeConfig )
271
+ .build ();
272
+
273
+ InfoTypeTransformation infoTypeTransformationObject =
274
+ InfoTypeTransformation .newBuilder ()
275
+ .setPrimitiveTransformation (primitiveTransformation )
276
+ .addInfoTypes (surrogateTypeObject )
277
+ .build ();
278
+
279
+ InfoTypeTransformations infoTypeTransformationArray =
280
+ InfoTypeTransformations .newBuilder ()
281
+ .addTransformations (infoTypeTransformationObject )
282
+ .build ();
283
+
284
+ // Create the inspection config
285
+ CustomInfoType customInfoType = CustomInfoType .newBuilder ()
286
+ .setInfoType (surrogateTypeObject )
287
+ .setSurrogateType (SurrogateType .newBuilder ().build ())
288
+ .build ();
289
+
290
+ InspectConfig inspectConfig =
291
+ InspectConfig .newBuilder ()
292
+ .addCustomInfoTypes (customInfoType ).build ();
293
+
294
+ // Create the reidentification request object
295
+ DeidentifyConfig reidentifyConfig =
296
+ DeidentifyConfig .newBuilder ()
297
+ .setInfoTypeTransformations (infoTypeTransformationArray )
298
+ .build ();
299
+
300
+ ReidentifyContentRequest request =
301
+ ReidentifyContentRequest .newBuilder ()
302
+ .setParent (ProjectName .of (projectId ).toString ())
303
+ .setReidentifyConfig (reidentifyConfig )
304
+ .setInspectConfig (inspectConfig )
305
+ .setItem (contentItem )
306
+ .build ();
307
+
308
+ // Execute the deidentification request
309
+ ReidentifyContentResponse response = dlpServiceClient .reidentifyContent (request );
310
+
311
+ // Print the reidentified input value
312
+ // e.g. "My SSN is 7261298621" --> "My SSN is 123456789"
313
+ String result = response .getItem ().getValue ();
314
+ System .out .println (result );
315
+ } catch (Exception e ) {
316
+ System .out .println ("Error in reidentifyWithFpe: " + e .getMessage ());
317
+ }
318
+ }
319
+ // [END dlp_reidentify_fpe]
320
+
225
321
// [START dlp_deidentify_date_shift]
226
322
/**
227
323
*
@@ -413,6 +509,10 @@ public static void main(String[] args) throws Exception {
413
509
new Option ("f" , "fpe" , true , "Deidentify with format-preserving encryption." );
414
510
optionsGroup .addOption (deidentifyFpeOption );
415
511
512
+ Option reidentifyFpeOption =
513
+ new Option ("r" , "reid" , true , "Reidentify with format-preserving encryption." );
514
+ optionsGroup .addOption (reidentifyFpeOption );
515
+
416
516
Option deidentifyDateShiftOption =
417
517
new Option ("d" , "date" , false , "Deidentify dates in a CSV file." );
418
518
optionsGroup .addOption (deidentifyDateShiftOption );
@@ -424,6 +524,10 @@ public static void main(String[] args) throws Exception {
424
524
Option .builder ("maskingCharacter" ).hasArg (true ).required (false ).build ();
425
525
commandLineOptions .addOption (maskingCharacterOption );
426
526
527
+ Option surrogateTypeOption =
528
+ Option .builder ("surrogateType" ).hasArg (true ).required (false ).build ();
529
+ commandLineOptions .addOption (surrogateTypeOption );
530
+
427
531
Option numberToMaskOption = Option .builder ("numberToMask" ).hasArg (true ).required (false ).build ();
428
532
commandLineOptions .addOption (numberToMaskOption );
429
533
@@ -489,11 +593,12 @@ public static void main(String[] args) throws Exception {
489
593
String wrappedKey = cmd .getOptionValue (wrappedKeyOption .getOpt ());
490
594
String keyName = cmd .getOptionValue (keyNameOption .getOpt ());
491
595
String val = cmd .getOptionValue (deidentifyFpeOption .getOpt ());
596
+ String surrogateType = cmd .getOptionValue (surrogateTypeOption .getOpt ());
492
597
FfxCommonNativeAlphabet alphabet =
493
598
FfxCommonNativeAlphabet .valueOf (
494
599
cmd .getOptionValue (
495
600
alphabetOption .getOpt (), FfxCommonNativeAlphabet .ALPHA_NUMERIC .name ()));
496
- deIdentifyWithFpe (val , alphabet , keyName , wrappedKey , projectId );
601
+ deIdentifyWithFpe (val , alphabet , keyName , wrappedKey , projectId , surrogateType );
497
602
} else if (cmd .hasOption ("d" )) {
498
603
//deidentify with date shift
499
604
String inputCsv = cmd .getOptionValue (inputCsvPathOption .getOpt ());
@@ -518,6 +623,17 @@ public static void main(String[] args) throws Exception {
518
623
wrappedKey ,
519
624
keyName ,
520
625
projectId );
626
+ } else if (cmd .hasOption ("r" )) {
627
+ // reidentification with FPE
628
+ String wrappedKey = cmd .getOptionValue (wrappedKeyOption .getOpt ());
629
+ String keyName = cmd .getOptionValue (keyNameOption .getOpt ());
630
+ String val = cmd .getOptionValue (reidentifyFpeOption .getOpt ());
631
+ String surrogateType = cmd .getOptionValue (surrogateTypeOption .getOpt ());
632
+ FfxCommonNativeAlphabet alphabet =
633
+ FfxCommonNativeAlphabet .valueOf (
634
+ cmd .getOptionValue (
635
+ alphabetOption .getOpt (), FfxCommonNativeAlphabet .ALPHA_NUMERIC .name ()));
636
+ reIdentifyWithFpe (val , alphabet , keyName , wrappedKey , projectId , surrogateType );
521
637
}
522
638
}
523
639
}
0 commit comments