-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add code samples for DLP text redaction (#61)
- Loading branch information
1 parent
dd8fffa
commit aea226e
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,52 @@ | |
|
||
'use strict'; | ||
|
||
function redactText(callingProjectId, string, minLikelihood, infoTypes) { | ||
// [START dlp_redact_text] | ||
// Imports the Google Cloud Data Loss Prevention library | ||
const DLP = require('@google-cloud/dlp'); | ||
|
||
// Instantiates a client | ||
const dlp = new DLP.DlpServiceClient(); | ||
|
||
// Construct transformation config which replaces sensitive info with its info type. | ||
// E.g., "Her email is [email protected]" => "Her email is [EMAIL_ADDRESS]" | ||
const replaceWithInfoTypeTransformation = { | ||
primitiveTransformation: { | ||
replaceWithInfoTypeConfig: {}, | ||
}, | ||
}; | ||
|
||
// Construct redaction request | ||
const request = { | ||
parent: dlp.projectPath(callingProjectId), | ||
item: { | ||
value: string, | ||
}, | ||
deidentifyConfig: { | ||
infoTypeTransformations: { | ||
transformations: [replaceWithInfoTypeTransformation], | ||
}, | ||
}, | ||
inspectConfig: { | ||
minLikelihood: minLikelihood, | ||
infoTypes: infoTypes, | ||
}, | ||
}; | ||
|
||
// Run string redaction | ||
dlp | ||
.deidentifyContent(request) | ||
.then(response => { | ||
const resultString = response[0].item.value; | ||
console.log(`Redacted text: ${resultString}`); | ||
}) | ||
.catch(err => { | ||
console.log(`Error in deidentifyContent: ${err.message || err}`); | ||
}); | ||
// [END dlp_redact_text] | ||
} | ||
|
||
function redactImage( | ||
callingProjectId, | ||
filepath, | ||
|
@@ -89,6 +135,18 @@ function redactImage( | |
|
||
const cli = require(`yargs`) | ||
.demand(1) | ||
.command( | ||
`string <string>`, | ||
`Redact a string using the Data Loss Prevention API.`, | ||
{}, | ||
opts => | ||
redactText( | ||
opts.callingProject, | ||
opts.string, | ||
opts.minLikelihood, | ||
opts.infoTypes | ||
) | ||
) | ||
.command( | ||
`image <filepath> <outputPath>`, | ||
`Redact sensitive data from an image using the Data Loss Prevention API.`, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,30 @@ const testResourcePath = 'system-test/resources'; | |
|
||
test.before(tools.checkCredentials); | ||
|
||
test(`should redact a single sensitive data type from a string`, async t => { | ||
const output = await tools.runAsync( | ||
`${cmd} string "My email is [email protected]" -t EMAIL_ADDRESS`, | ||
cwd | ||
); | ||
t.regex(output, /My email is \[EMAIL_ADDRESS\]/); | ||
}); | ||
|
||
test(`should redact multiple sensitive data types from a string`, async t => { | ||
const output = await tools.runAsync( | ||
`${cmd} string "I am 29 years old and my email is [email protected]" -t EMAIL_ADDRESS AGE`, | ||
cwd | ||
); | ||
t.regex(output, /I am \[AGE\] and my email is \[EMAIL_ADDRESS\]/); | ||
}); | ||
|
||
test(`should handle string with no sensitive data`, async t => { | ||
const output = await tools.runAsync( | ||
`${cmd} string "No sensitive data to redact here" -t EMAIL_ADDRESS AGE`, | ||
cwd | ||
); | ||
t.regex(output, /No sensitive data to redact here/); | ||
}); | ||
|
||
// redact_image | ||
test(`should redact a single sensitive data type from an image`, async t => { | ||
const testName = `redact-single-type`; | ||
|
@@ -61,6 +85,14 @@ test(`should redact multiple sensitive data types from an image`, async t => { | |
t.deepEqual(correct, result); | ||
}); | ||
|
||
test(`should report info type errors`, async t => { | ||
const output = await tools.runAsync( | ||
`${cmd} string "My email is [email protected]" -t NONEXISTENT`, | ||
cwd | ||
); | ||
t.regex(output, /Error in deidentifyContent/); | ||
}); | ||
|
||
test(`should report image redaction handling errors`, async t => { | ||
const output = await tools.runAsync( | ||
`${cmd} image ${testImage} nonexistent.result.png -t BAD_TYPE`, | ||
|