|
| 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)); |
0 commit comments