Skip to content

Commit aea226e

Browse files
jmorriseJustinBeckwith
authored andcommitted
feat: Add code samples for DLP text redaction (#61)
1 parent dd8fffa commit aea226e

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

dlp/redact.js

+58
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,52 @@
1515

1616
'use strict';
1717

18+
function redactText(callingProjectId, string, minLikelihood, infoTypes) {
19+
// [START dlp_redact_text]
20+
// Imports the Google Cloud Data Loss Prevention library
21+
const DLP = require('@google-cloud/dlp');
22+
23+
// Instantiates a client
24+
const dlp = new DLP.DlpServiceClient();
25+
26+
// Construct transformation config which replaces sensitive info with its info type.
27+
// E.g., "Her email is [email protected]" => "Her email is [EMAIL_ADDRESS]"
28+
const replaceWithInfoTypeTransformation = {
29+
primitiveTransformation: {
30+
replaceWithInfoTypeConfig: {},
31+
},
32+
};
33+
34+
// Construct redaction request
35+
const request = {
36+
parent: dlp.projectPath(callingProjectId),
37+
item: {
38+
value: string,
39+
},
40+
deidentifyConfig: {
41+
infoTypeTransformations: {
42+
transformations: [replaceWithInfoTypeTransformation],
43+
},
44+
},
45+
inspectConfig: {
46+
minLikelihood: minLikelihood,
47+
infoTypes: infoTypes,
48+
},
49+
};
50+
51+
// Run string redaction
52+
dlp
53+
.deidentifyContent(request)
54+
.then(response => {
55+
const resultString = response[0].item.value;
56+
console.log(`Redacted text: ${resultString}`);
57+
})
58+
.catch(err => {
59+
console.log(`Error in deidentifyContent: ${err.message || err}`);
60+
});
61+
// [END dlp_redact_text]
62+
}
63+
1864
function redactImage(
1965
callingProjectId,
2066
filepath,
@@ -89,6 +135,18 @@ function redactImage(
89135

90136
const cli = require(`yargs`)
91137
.demand(1)
138+
.command(
139+
`string <string>`,
140+
`Redact a string using the Data Loss Prevention API.`,
141+
{},
142+
opts =>
143+
redactText(
144+
opts.callingProject,
145+
opts.string,
146+
opts.minLikelihood,
147+
opts.infoTypes
148+
)
149+
)
92150
.command(
93151
`image <filepath> <outputPath>`,
94152
`Redact sensitive data from an image using the Data Loss Prevention API.`,

dlp/system-test/redact.test.js

+32
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,30 @@ const testResourcePath = 'system-test/resources';
2828

2929
test.before(tools.checkCredentials);
3030

31+
test(`should redact a single sensitive data type from a string`, async t => {
32+
const output = await tools.runAsync(
33+
`${cmd} string "My email is [email protected]" -t EMAIL_ADDRESS`,
34+
cwd
35+
);
36+
t.regex(output, /My email is \[EMAIL_ADDRESS\]/);
37+
});
38+
39+
test(`should redact multiple sensitive data types from a string`, async t => {
40+
const output = await tools.runAsync(
41+
`${cmd} string "I am 29 years old and my email is [email protected]" -t EMAIL_ADDRESS AGE`,
42+
cwd
43+
);
44+
t.regex(output, /I am \[AGE\] and my email is \[EMAIL_ADDRESS\]/);
45+
});
46+
47+
test(`should handle string with no sensitive data`, async t => {
48+
const output = await tools.runAsync(
49+
`${cmd} string "No sensitive data to redact here" -t EMAIL_ADDRESS AGE`,
50+
cwd
51+
);
52+
t.regex(output, /No sensitive data to redact here/);
53+
});
54+
3155
// redact_image
3256
test(`should redact a single sensitive data type from an image`, async t => {
3357
const testName = `redact-single-type`;
@@ -61,6 +85,14 @@ test(`should redact multiple sensitive data types from an image`, async t => {
6185
t.deepEqual(correct, result);
6286
});
6387

88+
test(`should report info type errors`, async t => {
89+
const output = await tools.runAsync(
90+
`${cmd} string "My email is [email protected]" -t NONEXISTENT`,
91+
cwd
92+
);
93+
t.regex(output, /Error in deidentifyContent/);
94+
});
95+
6496
test(`should report image redaction handling errors`, async t => {
6597
const output = await tools.runAsync(
6698
`${cmd} image ${testImage} nonexistent.result.png -t BAD_TYPE`,

0 commit comments

Comments
 (0)