Skip to content

Commit 030e0cf

Browse files
author
Ace Nassri
authored
Add DLP samples (BigQuery, DeID, RiskAnalysis) (#474)
* Add BigQuery samples + a few minor tweaks * Update comments + fix failing test * Sync w/codegen changes * Add DeID samples * Add DeID tests + remove infoTypes from DeID samples * Remove unused option * Add risk analysis samples * Update README * Add region tags + fix comment
1 parent 091d519 commit 030e0cf

11 files changed

+842
-32
lines changed

dlp/deid.js

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/**
2+
* Copyright 2017, Google, Inc.
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+
16+
'use strict';
17+
18+
function deidentifyWithMask (string, maskingCharacter, numberToMask) {
19+
// [START deidentify_masking]
20+
// Imports the Google Cloud Data Loss Prevention library
21+
const DLP = require('@google-cloud/dlp');
22+
23+
// Instantiates a client
24+
const dlp = new DLP.DlpServiceClient();
25+
26+
// The string to deidentify
27+
// const string = 'My SSN is 372819127';
28+
29+
// (Optional) The maximum number of sensitive characters to mask in a match
30+
// If omitted from the request or set to 0, the API will mask any matching characters
31+
// const numberToMask = 5;
32+
33+
// (Optional) The character to mask matching sensitive data with
34+
// const maskingCharacter = 'x';
35+
36+
// Construct deidentification request
37+
const items = [{ type: 'text/plain', value: string }];
38+
const request = {
39+
deidentifyConfig: {
40+
infoTypeTransformations: {
41+
transformations: [{
42+
primitiveTransformation: {
43+
characterMaskConfig: {
44+
maskingCharacter: maskingCharacter,
45+
numberToMask: numberToMask
46+
}
47+
}
48+
}]
49+
}
50+
},
51+
items: items
52+
};
53+
54+
// Run deidentification request
55+
dlp.deidentifyContent(request)
56+
.then((response) => {
57+
const deidentifiedItems = response[0].items;
58+
console.log(deidentifiedItems[0].value);
59+
})
60+
.catch((err) => {
61+
console.log(`Error in deidentifyWithMask: ${err.message || err}`);
62+
});
63+
// [END deidentify_masking]
64+
}
65+
66+
function deidentifyWithFpe (string, alphabet, keyName, wrappedKey) {
67+
// [START deidentify_fpe]
68+
// Imports the Google Cloud Data Loss Prevention library
69+
const DLP = require('@google-cloud/dlp');
70+
71+
// Instantiates a client
72+
const dlp = new DLP.DlpServiceClient();
73+
74+
// The string to deidentify
75+
// const string = 'My SSN is 372819127';
76+
77+
// The set of characters to replace sensitive ones with
78+
// For more information, see https://cloud.google.com/dlp/docs/reference/rest/v2beta1/content/deidentify#FfxCommonNativeAlphabet
79+
// const alphabet = 'ALPHA_NUMERIC';
80+
81+
// The name of the Cloud KMS key used to encrypt ('wrap') the AES-256 key
82+
// const keyName = 'projects/YOUR_GCLOUD_PROJECT/locations/YOUR_LOCATION/keyRings/YOUR_KEYRING_NAME/cryptoKeys/YOUR_KEY_NAME';
83+
84+
// The encrypted ('wrapped') AES-256 key to use
85+
// This key should be encrypted using the Cloud KMS key specified above
86+
// const wrappedKey = 'YOUR_ENCRYPTED_AES_256_KEY'
87+
88+
// Construct deidentification request
89+
const items = [{ type: 'text/plain', value: string }];
90+
const request = {
91+
deidentifyConfig: {
92+
infoTypeTransformations: {
93+
transformations: [{
94+
primitiveTransformation: {
95+
cryptoReplaceFfxFpeConfig: {
96+
cryptoKey: {
97+
kmsWrapped: {
98+
wrappedKey: wrappedKey,
99+
cryptoKeyName: keyName
100+
}
101+
},
102+
commonAlphabet: alphabet
103+
}
104+
}
105+
}]
106+
}
107+
},
108+
items: items
109+
};
110+
111+
// Run deidentification request
112+
dlp.deidentifyContent(request)
113+
.then((response) => {
114+
const deidentifiedItems = response[0].items;
115+
console.log(deidentifiedItems[0].value);
116+
})
117+
.catch((err) => {
118+
console.log(`Error in deidentifyWithFpe: ${err.message || err}`);
119+
});
120+
// [END deidentify_fpe]
121+
}
122+
123+
const cli = require(`yargs`)
124+
.demand(1)
125+
.command(
126+
`mask <string>`,
127+
`Deidentify sensitive data by masking it with a character.`,
128+
{
129+
maskingCharacter: {
130+
type: 'string',
131+
alias: 'c',
132+
default: ''
133+
},
134+
numberToMask: {
135+
type: 'number',
136+
alias: 'n',
137+
default: 0
138+
}
139+
},
140+
(opts) => deidentifyWithMask(opts.string, opts.maskingCharacter, opts.numberToMask)
141+
)
142+
.command(
143+
`fpe <string> <wrappedKey> <keyName>`,
144+
`Deidentify sensitive data using Format Preserving Encryption (FPE).`,
145+
{
146+
alphabet: {
147+
type: 'string',
148+
alias: 'a',
149+
default: 'ALPHA_NUMERIC',
150+
choices: ['NUMERIC', 'HEXADECIMAL', 'UPPER_CASE_ALPHA_NUMERIC', 'ALPHA_NUMERIC']
151+
}
152+
},
153+
(opts) => deidentifyWithFpe(opts.string, opts.alphabet, opts.keyName, opts.wrappedKey)
154+
)
155+
.example(`node $0 mask "My SSN is 372819127"`)
156+
.example(`node $0 fpe "My SSN is 372819127" <YOUR_ENCRYPTED_AES_256_KEY> <YOUR_KEY_NAME>`)
157+
.wrap(120)
158+
.recommendCommands()
159+
.epilogue(`For more information, see https://cloud.google.com/dlp/docs.`);
160+
161+
if (module === require.main) {
162+
cli.help().strict().argv; // eslint-disable-line
163+
}

0 commit comments

Comments
 (0)