Skip to content

Commit

Permalink
feat: Add code samples for DLP text redaction (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmorrise authored and JustinBeckwith committed Aug 6, 2018
1 parent dd8fffa commit aea226e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
58 changes: 58 additions & 0 deletions dlp/redact.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 xxx@example.com" => "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,
Expand Down Expand Up @@ -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.`,
Expand Down
32 changes: 32 additions & 0 deletions dlp/system-test/redact.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 jenny@example.com" -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 jenny@example.com" -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`;
Expand Down Expand Up @@ -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 jenny@example.com" -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`,
Expand Down

0 comments on commit aea226e

Please sign in to comment.