|
| 1 | +// Copyright 2024 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: Inspect a string using the Data Loss Prevention API. |
| 20 | +// usage: node inspectString.js my-project string minLikelihood maxFindings infoTypes customInfoTypes includeQuote |
| 21 | + |
| 22 | +async function main( |
| 23 | + projectId, |
| 24 | + repLocation, |
| 25 | + string, |
| 26 | + minLikelihood, |
| 27 | + maxFindings, |
| 28 | + infoTypes, |
| 29 | + customInfoTypes, |
| 30 | + includeQuote |
| 31 | +) { |
| 32 | + [infoTypes, customInfoTypes] = transformCLI(infoTypes, customInfoTypes); |
| 33 | + |
| 34 | + // [START dlp_inspect_string_rep] |
| 35 | + // Imports the Google Cloud Data Loss Prevention library |
| 36 | + const DLP = require('@google-cloud/dlp'); |
| 37 | + |
| 38 | + // Assemble the regional endpoint url using provided rep location |
| 39 | + const opts = { |
| 40 | + apiEndpoint: `dlp.${repLocation}.rep.googleapis.com`, |
| 41 | + }; |
| 42 | + |
| 43 | + // Instantiates a client |
| 44 | + const dlp = new DLP.DlpServiceClient(opts); |
| 45 | + |
| 46 | + // The project ID to run the API call under |
| 47 | + // const projectId = 'my-project'; |
| 48 | + |
| 49 | + // The string to inspect |
| 50 | + // const string = 'My name is Gary and my email is [email protected]'; |
| 51 | + |
| 52 | + // The minimum likelihood required before returning a match |
| 53 | + // const minLikelihood = 'LIKELIHOOD_UNSPECIFIED'; |
| 54 | + |
| 55 | + // The maximum number of findings to report per request (0 = server maximum) |
| 56 | + // const maxFindings = 0; |
| 57 | + |
| 58 | + // The infoTypes of information to match |
| 59 | + // const infoTypes = [{ name: 'PHONE_NUMBER' }, { name: 'EMAIL_ADDRESS' }, { name: 'CREDIT_CARD_NUMBER' }]; |
| 60 | + |
| 61 | + // The customInfoTypes of information to match |
| 62 | + // const customInfoTypes = [{ infoType: { name: 'DICT_TYPE' }, dictionary: { wordList: { words: ['foo', 'bar', 'baz']}}}, |
| 63 | + // { infoType: { name: 'REGEX_TYPE' }, regex: {pattern: '\\(\\d{3}\\) \\d{3}-\\d{4}'}}]; |
| 64 | + |
| 65 | + // Whether to include the matching string |
| 66 | + // const includeQuote = true; |
| 67 | + |
| 68 | + async function inspectString() { |
| 69 | + // Construct item to inspect |
| 70 | + const item = {value: string}; |
| 71 | + |
| 72 | + // Construct request |
| 73 | + const request = { |
| 74 | + parent: `projects/${projectId}/locations/${repLocation}`, |
| 75 | + inspectConfig: { |
| 76 | + infoTypes: infoTypes, |
| 77 | + customInfoTypes: customInfoTypes, |
| 78 | + minLikelihood: minLikelihood, |
| 79 | + includeQuote: includeQuote, |
| 80 | + limits: { |
| 81 | + maxFindingsPerRequest: maxFindings, |
| 82 | + }, |
| 83 | + }, |
| 84 | + item: item, |
| 85 | + }; |
| 86 | + |
| 87 | + // Run request |
| 88 | + const [response] = await dlp.inspectContent(request); |
| 89 | + const findings = response.result.findings; |
| 90 | + if (findings.length > 0) { |
| 91 | + console.log('Findings:'); |
| 92 | + findings.forEach(finding => { |
| 93 | + if (includeQuote) { |
| 94 | + console.log(`\tQuote: ${finding.quote}`); |
| 95 | + } |
| 96 | + console.log(`\tInfo type: ${finding.infoType.name}`); |
| 97 | + console.log(`\tLikelihood: ${finding.likelihood}`); |
| 98 | + }); |
| 99 | + } else { |
| 100 | + console.log('No findings.'); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + await inspectString(); |
| 105 | + // [END dlp_inspect_string_rep] |
| 106 | +} |
| 107 | + |
| 108 | +main(...process.argv.slice(2)).catch(err => { |
| 109 | + console.error(err); |
| 110 | + process.exitCode = 1; |
| 111 | +}); |
| 112 | + |
| 113 | +function transformCLI(infoTypes, customInfoTypes) { |
| 114 | + infoTypes = infoTypes |
| 115 | + ? infoTypes.split(',').map(type => { |
| 116 | + return {name: type}; |
| 117 | + }) |
| 118 | + : undefined; |
| 119 | + |
| 120 | + if (customInfoTypes) { |
| 121 | + customInfoTypes = customInfoTypes.includes(',') |
| 122 | + ? customInfoTypes.split(',').map((dict, idx) => { |
| 123 | + return { |
| 124 | + infoType: {name: 'CUSTOM_DICT_'.concat(idx.toString())}, |
| 125 | + dictionary: {wordList: {words: dict.split(',')}}, |
| 126 | + }; |
| 127 | + }) |
| 128 | + : customInfoTypes.split(',').map((rgx, idx) => { |
| 129 | + return { |
| 130 | + infoType: {name: 'CUSTOM_REGEX_'.concat(idx.toString())}, |
| 131 | + regex: {pattern: rgx}, |
| 132 | + }; |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + return [infoTypes, customInfoTypes]; |
| 137 | +} |
0 commit comments