Skip to content

Commit 184c006

Browse files
committed
Updated DeIdentification samples and tests.
1 parent 284902e commit 184c006

File tree

3 files changed

+56
-40
lines changed

3 files changed

+56
-40
lines changed

dlp/src/main/java/com/example/dlp/DeIdentification.java

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ public class DeIdentification {
7777
* @param projectId ID of Google Cloud project to run the API under.
7878
*/
7979
private static void deIdentifyWithMask(
80-
String string,
81-
Character maskingCharacter,
82-
int numberToMask) {
80+
String string, Character maskingCharacter, int numberToMask, String projectId) {
8381
// [START dlp_deidentify_masking]
8482
/**
8583
* Deidentify a string by masking sensitive information with a character using the DLP API.
@@ -95,7 +93,6 @@ private static void deIdentifyWithMask(
9593
// string = "My SSN is 372819127";
9694
// numberToMask = 5;
9795
// maskingCharacter = 'x';
98-
9996
ByteContentItem byteContentItem =
10097
ByteContentItem.newBuilder()
10198
.setType(ByteContentItem.BytesType.TEXT_UTF8)
@@ -112,7 +109,9 @@ private static void deIdentifyWithMask(
112109

113110
// Create the deidentification transformation configuration
114111
PrimitiveTransformation primitiveTransformation =
115-
PrimitiveTransformation.newBuilder().setCharacterMaskConfig(characterMaskConfig).build();
112+
PrimitiveTransformation.newBuilder()
113+
.setCharacterMaskConfig(characterMaskConfig)
114+
.build();
116115

117116
InfoTypeTransformation infoTypeTransformationObject =
118117
InfoTypeTransformation.newBuilder()
@@ -124,15 +123,15 @@ private static void deIdentifyWithMask(
124123
.addTransformations(infoTypeTransformationObject)
125124
.build();
126125

127-
// Create the deidentification request object
128126
DeidentifyConfig deidentifyConfig =
129127
DeidentifyConfig.newBuilder()
130128
.setInfoTypeTransformations(infoTypeTransformationArray)
131129
.build();
132130

131+
// Create the deidentification request object
133132
DeidentifyContentRequest request =
134133
DeidentifyContentRequest.newBuilder()
135-
.setParent(projectId)
134+
.setParent(String.format("projects/%s", projectId))
136135
.setDeidentifyConfig(deidentifyConfig)
137136
.setItem(contentItem)
138137
.build();
@@ -142,10 +141,11 @@ private static void deIdentifyWithMask(
142141

143142
// Print the character-masked input value
144143
// e.g. "My SSN is 123456789" --> "My SSN is *********"
145-
ContentItem item = response.getItem();
146-
System.out.println(item.getValue());
144+
String result = response.getItem().getByteItem().getData().toStringUtf8();
145+
System.out.println(result);
147146
} catch (Exception e) {
148147
System.out.println("Error in deidentifyWithMask: " + e.getMessage());
148+
System.out.println(e.getStackTrace());
149149
}
150150
}
151151
// [END dlp_deidentify_mask]
@@ -223,7 +223,7 @@ private static void deIdentifyWithFpe(
223223

224224
DeidentifyContentRequest request =
225225
DeidentifyContentRequest.newBuilder()
226-
.setParent(projectId)
226+
.setParent(String.format("projects/%s", projectId))
227227
.setDeidentifyConfig(deidentifyConfig)
228228
.setItem(contentItem)
229229
.build();
@@ -233,8 +233,8 @@ private static void deIdentifyWithFpe(
233233

234234
// Print the deidentified input value
235235
// e.g. "My SSN is 123456789" --> "My SSN is 7261298621"
236-
ContentItem item = response.getItem();
237-
System.out.println(item.getValue());
236+
String result = response.getItem().getByteItem().getData().toStringUtf8();
237+
System.out.println(result);
238238
} catch (Exception e) {
239239
System.out.println("Error in deidentifyWithFpe: " + e.getMessage());
240240
}
@@ -292,7 +292,7 @@ private static void deidentifyWithDateShift(
292292
KmsWrappedCryptoKey kmsWrappedCryptoKey =
293293
KmsWrappedCryptoKey.newBuilder()
294294
.setCryptoKeyName(keyName)
295-
.setWrappedKey(ByteString.copyFromUtf8(wrappedKey))
295+
.setWrappedKey(ByteString.copyFrom(BaseEncoding.base64().decode(wrappedKey)))
296296
.build();
297297
dateShiftConfigBuilder.setCryptoKey(
298298
CryptoKey.newBuilder().setKmsWrapped(kmsWrappedCryptoKey).build());
@@ -352,7 +352,7 @@ private static void deidentifyWithDateShift(
352352

353353
DeidentifyContentRequest request =
354354
DeidentifyContentRequest.newBuilder()
355-
.setParent(projectId)
355+
.setParent(String.format("projects/%s", projectId))
356356
.setDeidentifyConfig(deidentifyConfig)
357357
.setItem(tableItem)
358358
.build();
@@ -369,7 +369,7 @@ private static void deidentifyWithDateShift(
369369

370370
File outputFile = outputCsvPath.toFile();
371371
if (!outputFile.exists()) {
372-
outputFile.mkdirs();
372+
outputFile.getParentFile().mkdirs();
373373
outputFile.createNewFile();
374374
}
375375
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(outputFile));
@@ -379,27 +379,19 @@ private static void deidentifyWithDateShift(
379379

380380
// write out each row
381381
for (Table.Row outputRow : outputRows) {
382-
String row =
383-
outputRow
384-
.getValuesList()
385-
.stream()
386-
.map(
387-
value ->
388-
(value.getDateValue() != null)
389-
? (String.valueOf(value.getDateValue().getMonth())
390-
+ "/"
391-
+ String.valueOf(value.getDateValue().getDay())
392-
+ "/"
393-
+ String.valueOf(value.getDateValue().getYear()))
394-
: value.getStringValue())
395-
.collect(Collectors.joining(","));
382+
String row = outputRow.getValuesList()
383+
.stream()
384+
.map(value -> value.getStringValue())
385+
.collect(Collectors.joining(","));
396386
bufferedWriter.append(row + "\n");
397387
}
398388

399389
bufferedWriter.flush();
400390
bufferedWriter.close();
401391

402-
System.out.println("Successfully saved date-shift output to:" + outputCsvPath.getFileName());
392+
System.out.println("Successfully saved date-shift output to: " + outputCsvPath.getFileName());
393+
} catch (Exception e) {
394+
System.out.println("Error in deidentifyWithDateShift: " + e.getMessage());
403395
}
404396
}
405397

@@ -446,15 +438,16 @@ public static void main(String[] args) throws Exception {
446438
optionsGroup.setRequired(true);
447439

448440
Option deidentifyMaskingOption =
449-
new Option("m", "mask", true, "Deidentify with character masking");
441+
new Option("m", "mask", true, "Deidentify with character masking.");
450442
optionsGroup.addOption(deidentifyMaskingOption);
451443

452-
Option deidentifyFpeOption = new Option("f", "fpe", true, "Deidentify with FFX FPE");
444+
Option deidentifyFpeOption =
445+
new Option("f", "fpe", true, "Deidentify with FFX FPE.");
453446
optionsGroup.addOption(deidentifyFpeOption);
454447

455448
Option deidentifyDateShiftOption =
456-
new Option(
457-
"d", "date", true, "Deidentify dates in a CSV file by pseudorandomly shifting them.");
449+
new Option("d", "date", false, "Deidentify dates in a CSV file.");
450+
optionsGroup.addOption(deidentifyDateShiftOption);
458451

459452
Options commandLineOptions = new Options();
460453
commandLineOptions.addOptionGroup(optionsGroup);
@@ -533,8 +526,8 @@ public static void main(String[] args) throws Exception {
533526
cmd.getOptionValue(
534527
alphabetOption.getOpt(), FfxCommonNativeAlphabet.ALPHA_NUMERIC.name()));
535528
deIdentifyWithFpe(val, alphabet, keyName, wrappedKey, projectId);
536-
} // deidentify with date shift
537-
else if (cmd.hasOption("d")) {
529+
} else if (cmd.hasOption("d")) {
530+
//deidentify with date shift
538531
String inputCsv = cmd.getOptionValue(inputCsvPathOption.getOpt());
539532
String outputCsv = cmd.getOptionValue(outputCsvPathOption.getOpt());
540533

dlp/src/test/java/com/example/dlp/DeIdentificationIT.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
package com.example.dlp;
1818

19-
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertThat;
20+
import static org.hamcrest.CoreMatchers.containsString;
2021
import static org.junit.Assert.assertFalse;
2122
import static org.junit.Assert.assertNotNull;
2223
import static org.junit.Assert.assertTrue;
@@ -25,6 +26,7 @@
2526
import java.io.PrintStream;
2627
import java.util.regex.Pattern;
2728
import org.junit.After;
29+
import org.junit.Assert;
2830
import org.junit.Before;
2931
import org.junit.Test;
3032
import org.junit.runner.RunWith;
@@ -64,7 +66,7 @@ public void testDeidStringMasksCharacters() throws Exception {
6466
"-numberToMask", "5"
6567
});
6668
String output = bout.toString();
67-
assertEquals(output, "My SSN is xxxxx9127\n");
69+
assertThat(output, containsString("My SSN is xxxxx9127"));
6870
}
6971

7072
@Test
@@ -77,8 +79,29 @@ public void testDeidStringPerformsFpe() throws Exception {
7779
"-keyName", keyName
7880
});
7981
String output = bout.toString();
80-
assertFalse(output.contains(text));
81-
assertTrue(Pattern.compile("My SSN is \\w+").matcher(output).find());
82+
assertFalse(
83+
"Response contains original SSN.",
84+
output.contains("372819127"));
85+
assertThat(output, containsString("My SSN is "));
86+
}
87+
88+
@Test
89+
public void testDeidentifyWithDateShift() throws Exception {
90+
DeIdentification.main(
91+
new String[] {
92+
"-d",
93+
"-inputCsvPath", "src/test/resources/dates.csv",
94+
"-outputCsvPath", "src/test/resources/results.temp.csv",
95+
"-dateFields", "birth_date,register_date",
96+
"-lowerBoundDays", "5",
97+
"-upperBoundDays", "5",
98+
"-contextField", "name",
99+
"-wrappedKey", wrappedKey,
100+
"-keyName", keyName
101+
});
102+
String output = bout.toString();
103+
assertThat(
104+
output, containsString("Successfully saved date-shift output to: results.temp.csv"));
82105
}
83106

84107
@After

dlp/src/test/resources/dates.csv

Whitespace-only changes.

0 commit comments

Comments
 (0)