|
| 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 | +} |
0 commit comments