Skip to content

Commit e28a762

Browse files
DLP: Added sample for deidentify by redacting matched input values (#3128)
* DLP: Added sample for deidentify by redacting matched input values Added unit test cases for same * Resolved linting errors * Renamed `textToInspect` to `string` Added `infoTypes` in the transformation configs Updated test cases --------- Co-authored-by: Karl Weinmeister <[email protected]>
1 parent 1a651e1 commit e28a762

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

dlp/deIdentifyWithRedaction.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// sample-metadata:
18+
// title: De-identify data: Redacting with matched input values
19+
// description: Uses the Data Loss Prevention API to de-identify sensitive data
20+
// in a string by redacting matched input values.
21+
// usage: node deIdentifyWithRedaction.js my-project string infoTypes
22+
23+
function main(projectId, string, infoTypes) {
24+
infoTypes = transformCLI(infoTypes);
25+
// [START dlp_deidentify_redact]
26+
// Imports the Google Cloud Data Loss Prevention library
27+
const DLP = require('@google-cloud/dlp');
28+
29+
// Instantiates a client
30+
const dlp = new DLP.DlpServiceClient();
31+
32+
// TODO(developer): Replace these variables before running the sample.
33+
// const projectId = "your-project-id";
34+
35+
// The string to deidentify
36+
// const string =
37+
// 'My name is Alicia Abernathy, and my email address is [email protected].';
38+
39+
// The infoTypes of information to match
40+
// See https://cloud.google.com/dlp/docs/concepts-infotypes for more information
41+
// about supported infoTypes.
42+
// const infoTypes = [{name: 'EMAIL_ADDRESS'}];
43+
44+
async function deIdentifyRedaction() {
45+
// Construct deidentify configuration
46+
const deidentifyConfig = {
47+
infoTypeTransformations: {
48+
transformations: [
49+
{
50+
infoTypes: infoTypes,
51+
primitiveTransformation: {
52+
redactConfig: {},
53+
},
54+
},
55+
],
56+
},
57+
};
58+
59+
// Construct inspect configuration
60+
const inspectConfig = {
61+
infoTypes: infoTypes,
62+
};
63+
64+
// Construct Item
65+
const item = {
66+
value: string,
67+
};
68+
69+
// Combine configurations into a request for the service.
70+
const request = {
71+
parent: `projects/${projectId}/locations/global`,
72+
item,
73+
deidentifyConfig,
74+
inspectConfig,
75+
};
76+
77+
// Send the request and receive response from the service
78+
const [response] = await dlp.deidentifyContent(request);
79+
80+
// Print the results
81+
console.log(`Text after redaction: ${response.item.value}`);
82+
}
83+
84+
deIdentifyRedaction();
85+
// [END dlp_deidentify_redact]
86+
}
87+
88+
main(...process.argv.slice(2));
89+
process.on('unhandledRejection', err => {
90+
console.error(err.message);
91+
process.exitCode = 1;
92+
});
93+
94+
function transformCLI(infoTypes) {
95+
infoTypes = infoTypes
96+
? infoTypes.split(',').map(type => {
97+
return {name: type};
98+
})
99+
: undefined;
100+
return infoTypes;
101+
}

dlp/system-test/deid.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,36 @@ describe('deid', () => {
178178
}
179179
assert.include(output, 'INVALID_ARGUMENT');
180180
});
181+
182+
// dlp_deidentify_redact
183+
it('should redact the matched input values', () => {
184+
const string =
185+
'My name is Alicia Abernathy, and my email address is [email protected].';
186+
let output;
187+
try {
188+
output = execSync(
189+
`node deIdentifyWithRedaction.js ${projectId} "${string}" EMAIL_ADDRESS`
190+
);
191+
} catch (err) {
192+
output = err.message;
193+
}
194+
assert.include(
195+
output,
196+
'My name is Alicia Abernathy, and my email address is .'
197+
);
198+
});
199+
200+
it('should handle deidentification errors', () => {
201+
let output;
202+
const string =
203+
'My name is Alicia Abernathy, and my email address is [email protected].';
204+
try {
205+
output = execSync(
206+
`node deIdentifyWithRedaction.js ${projectId} "${string}" BAD_TYPE`
207+
);
208+
} catch (err) {
209+
output = err.message;
210+
}
211+
assert.include(output, 'INVALID_ARGUMENT');
212+
});
181213
});

0 commit comments

Comments
 (0)