|
| 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 omit overlapping. |
| 19 | +// description: Inspect a string for sensitive data excluding a list of words. |
| 20 | +// usage: node inspectStringWithExclusionDict.js my-project string infoTypes excludedWords |
| 21 | + |
| 22 | +function main(projectId, string, infoTypes, excludedWords) { |
| 23 | + [infoTypes, excludedWords] = transformCLI(infoTypes, excludedWords); |
| 24 | + // [START dlp_inspect_string_with_exclusion_dict] |
| 25 | + // Imports the Google Cloud Data Loss Prevention library |
| 26 | + const DLP = require('@google-cloud/dlp'); |
| 27 | + |
| 28 | + // Instantiates a client |
| 29 | + const dlp = new DLP.DlpServiceClient(); |
| 30 | + |
| 31 | + // The project ID to run the API call under |
| 32 | + // const projectId = 'my-project'; |
| 33 | + |
| 34 | + // The string to inspect |
| 35 | + // const string = 'Some email addresses: [email protected], [email protected]'; |
| 36 | + |
| 37 | + // The infoTypes of information to match |
| 38 | + // const infoTypes = [{ name: 'EMAIL_ADDRESS' }]; |
| 39 | + |
| 40 | + // Words to exclude |
| 41 | + // const excludedWords = ['[email protected]']; |
| 42 | + |
| 43 | + async function inspectStringWithExclusionDict() { |
| 44 | + // Specify the type and content to be inspected. |
| 45 | + const item = { |
| 46 | + byteItem: { |
| 47 | + type: DLP.protos.google.privacy.dlp.v2.ByteContentItem.BytesType |
| 48 | + .TEXT_UTF8, |
| 49 | + data: Buffer.from(string, 'utf-8'), |
| 50 | + }, |
| 51 | + }; |
| 52 | + |
| 53 | + // Exclude matches from the specified excludedWords. |
| 54 | + const exclusionRule = { |
| 55 | + dictionary: { |
| 56 | + wordList: { |
| 57 | + words: excludedWords, |
| 58 | + }, |
| 59 | + }, |
| 60 | + matchingType: |
| 61 | + DLP.protos.google.privacy.dlp.v2.MatchingType.MATCHING_TYPE_FULL_MATCH, |
| 62 | + }; |
| 63 | + |
| 64 | + // Construct a ruleset that applies the exclusion rule to the specified infotype. |
| 65 | + const ruleSet = [ |
| 66 | + { |
| 67 | + infoTypes: infoTypes, |
| 68 | + rules: [ |
| 69 | + { |
| 70 | + exclusionRule: exclusionRule, |
| 71 | + }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + ]; |
| 75 | + |
| 76 | + // Construct the configuration for the Inspect request, including the ruleset. |
| 77 | + const inspectConfig = { |
| 78 | + infoTypes: infoTypes, |
| 79 | + ruleSet: ruleSet, |
| 80 | + includeQuote: true, |
| 81 | + }; |
| 82 | + |
| 83 | + // Construct the Inspect request to be sent by the client. |
| 84 | + const request = { |
| 85 | + parent: `projects/${projectId}/locations/global`, |
| 86 | + inspectConfig: inspectConfig, |
| 87 | + item: item, |
| 88 | + }; |
| 89 | + |
| 90 | + // Use the client to send the API request. |
| 91 | + const [response] = await dlp.inspectContent(request); |
| 92 | + |
| 93 | + // Print Findings |
| 94 | + const findings = response.result.findings; |
| 95 | + if (findings.length > 0) { |
| 96 | + console.log(`Findings: ${findings.length}\n`); |
| 97 | + findings.forEach(finding => { |
| 98 | + console.log(`InfoType: ${finding.infoType.name}`); |
| 99 | + console.log(`\tQuote: ${finding.quote}`); |
| 100 | + console.log(`\tLikelihood: ${finding.likelihood} \n`); |
| 101 | + }); |
| 102 | + } else { |
| 103 | + console.log('No findings.'); |
| 104 | + } |
| 105 | + } |
| 106 | + inspectStringWithExclusionDict(); |
| 107 | + // [END dlp_inspect_string_with_exclusion_dict] |
| 108 | +} |
| 109 | + |
| 110 | +process.on('unhandledRejection', err => { |
| 111 | + console.error(err.message); |
| 112 | + process.exitCode = 1; |
| 113 | +}); |
| 114 | + |
| 115 | +main(...process.argv.slice(2)); |
| 116 | + |
| 117 | +function transformCLI(infoTypes, excludedWords) { |
| 118 | + infoTypes = infoTypes |
| 119 | + ? infoTypes.split(',').map(type => { |
| 120 | + return {name: type}; |
| 121 | + }) |
| 122 | + : undefined; |
| 123 | + |
| 124 | + excludedWords = excludedWords ? excludedWords.split(',') : []; |
| 125 | + |
| 126 | + return [infoTypes, excludedWords]; |
| 127 | +} |
0 commit comments