Skip to content

Commit 183a16b

Browse files
dinesh-crestpattishinkweinmeister
authored
DLP: Added sample for inspect string with custom regex (#3107)
* DLP: Added sample for inspect string with custom regex Added unit test cases for the same * Resolved PR comments --------- Co-authored-by: Patti Shin <[email protected]> Co-authored-by: Karl Weinmeister <[email protected]>
1 parent a2474f6 commit 183a16b

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

dlp/inspectWithCustomRegex.js

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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: Inspects strings
19+
// description: Inspects a string using custom regex pattern.
20+
// usage: node inspectWithCustomRegex.js my-project string customRegex
21+
22+
function main(projectId, string, customRegex) {
23+
// [START dlp_inspect_custom_regex]
24+
// Imports the Google Cloud Data Loss Prevention library
25+
const DLP = require('@google-cloud/dlp');
26+
27+
// Instantiates a client
28+
const dlp = new DLP.DlpServiceClient();
29+
30+
// The project ID to run the API call under
31+
// const projectId = 'my-project';
32+
33+
// The string to inspect
34+
// const string = 'Patients MRN 444-5-22222';
35+
36+
// The regex pattern to match for
37+
// const customRegex = '[1-9]{3}-[1-9]{1}-[1-9]{5}';
38+
39+
async function inspectWithCustomRegex() {
40+
// Construct item to inspect
41+
const item = {
42+
byteItem: {
43+
type: DLP.protos.google.privacy.dlp.v2.ByteContentItem.BytesType
44+
.TEXT_UTF8,
45+
data: Buffer.from(string, 'utf-8'),
46+
},
47+
};
48+
49+
// Construct the custom regex detector.
50+
const customInfoTypes = [
51+
{
52+
infoType: {
53+
name: 'C_MRN',
54+
},
55+
likelihood: DLP.protos.google.privacy.dlp.v2.Likelihood.POSSIBLE,
56+
regex: {
57+
pattern: customRegex,
58+
},
59+
},
60+
];
61+
62+
// Construct request
63+
const request = {
64+
parent: `projects/${projectId}/locations/global`,
65+
inspectConfig: {
66+
customInfoTypes: customInfoTypes,
67+
includeQuote: true,
68+
},
69+
item: item,
70+
};
71+
72+
// Run request
73+
const [response] = await dlp.inspectContent(request);
74+
const findings = response.result.findings;
75+
if (findings.length > 0) {
76+
console.log('Findings: \n');
77+
findings.forEach(finding => {
78+
console.log(`InfoType: ${finding.infoType.name}`);
79+
console.log(`\tQuote: ${finding.quote}`);
80+
console.log(`\tLikelihood: ${finding.likelihood} \n`);
81+
});
82+
} else {
83+
console.log('No findings.');
84+
}
85+
}
86+
inspectWithCustomRegex();
87+
// [END dlp_inspect_custom_regex]
88+
}
89+
90+
process.on('unhandledRejection', err => {
91+
console.error(err.message);
92+
process.exitCode = 1;
93+
});
94+
95+
main(...process.argv.slice(2));

dlp/system-test/inspect.test.js

+34
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,38 @@ describe('inspect', () => {
309309
assert.notMatch(outputB, /EMAIL_ADDRESS/);
310310
assert.match(outputB, /PHONE_NUMBER/);
311311
});
312+
313+
// dlp_inspect_custom_regex
314+
it('should inspect a string using custom regex', () => {
315+
const string = 'Patients MRN 444-5-22222';
316+
const custRegex = '[1-9]{3}-[1-9]{1}-[1-9]{5}';
317+
const output = execSync(
318+
`node inspectWithCustomRegex.js ${projectId} "${string}" "${custRegex}"`
319+
);
320+
assert.match(output, /InfoType: C_MRN/);
321+
assert.match(output, /Likelihood: POSSIBLE/);
322+
});
323+
324+
it('should handle string with no match', () => {
325+
const string = 'Patients MRN 444-5-22222';
326+
const custRegex = '[1-9]{3}-[1-9]{2}-[1-9]{5}';
327+
const output = execSync(
328+
`node inspectWithCustomRegex.js ${projectId} "${string}" "${custRegex}"`
329+
);
330+
assert.include(output, 'No findings');
331+
});
332+
333+
it('should report any errors while inspecting a string', () => {
334+
let output;
335+
const string = 'Patients MRN 444-5-22222';
336+
const custRegex = '[1-9]{3}-[1-9]{2}-[1-9]{5}';
337+
try {
338+
output = execSync(
339+
`node inspectWithCustomRegex.js BAD_PROJECT_ID "${string}" "${custRegex}"`
340+
);
341+
} catch (err) {
342+
output = err.message;
343+
}
344+
assert.include(output, 'INVALID_ARGUMENT');
345+
});
312346
});

0 commit comments

Comments
 (0)