Skip to content

Commit 1900ae8

Browse files
author
Ace Nassri
authored
DLP samples (#322)
* First draft of DLP samples * Fix DLP tests * Add README * Fix README bugs
0 parents  commit 1900ae8

11 files changed

+957
-0
lines changed

dlp/inspect.js

+401
Large diffs are not rendered by default.

dlp/metadata.js

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
const API_URL = 'https://dlp.googleapis.com/v2beta1';
19+
const requestPromise = require('request-promise');
20+
21+
function listInfoTypes (authToken, category) {
22+
// [START list_info_types]
23+
// Your gcloud auth token.
24+
// const authToken = 'YOUR_AUTH_TOKEN';
25+
26+
// The category of info types to list.
27+
// const category = 'CATEGORY_TO_LIST';
28+
29+
// Construct REST request
30+
const options = {
31+
url: `${API_URL}/rootCategories/${category}/infoTypes`,
32+
headers: {
33+
'Authorization': `Bearer ${authToken}`,
34+
'Content-Type': 'application/json'
35+
},
36+
json: true
37+
};
38+
39+
// Run REST request
40+
requestPromise.get(options)
41+
.then((body) => {
42+
console.log(body);
43+
})
44+
.catch((err) => {
45+
console.log('Error in listInfoTypes:', err);
46+
});
47+
// [END list_info_types]
48+
}
49+
50+
function listCategories (authToken) {
51+
// [START list_categories]
52+
// Your gcloud auth token.
53+
// const authToken = 'YOUR_AUTH_TOKEN';
54+
55+
// Construct REST request
56+
const options = {
57+
url: `${API_URL}/rootCategories`,
58+
headers: {
59+
'Authorization': `Bearer ${authToken}`,
60+
'Content-Type': 'application/json'
61+
},
62+
json: true
63+
};
64+
65+
// Run REST request
66+
requestPromise.get(options)
67+
.then((body) => {
68+
const categories = body.categories;
69+
console.log(categories);
70+
})
71+
.catch((err) => {
72+
console.log('Error in listCategories:', err);
73+
});
74+
// [END list_categories]
75+
}
76+
77+
if (module === require.main) {
78+
const auth = require('google-auto-auth')({
79+
keyFilename: process.env.GOOGLE_APPLICATION_CREDENTIALS,
80+
scopes: ['https://www.googleapis.com/auth/cloud-platform']
81+
});
82+
auth.getToken((err, token) => {
83+
if (err) {
84+
console.err('Error fetching auth token:', err);
85+
process.exit(1);
86+
}
87+
88+
const cli = require(`yargs`)
89+
.demand(1)
90+
.command(
91+
`infoTypes <category>`,
92+
`List types of sensitive information within a category.`,
93+
{},
94+
(opts) => listInfoTypes(opts.authToken, opts.category)
95+
)
96+
.command(
97+
`categories`,
98+
`List root categories of sensitive information.`,
99+
{},
100+
(opts) => listCategories(opts.authToken)
101+
)
102+
.option('a', {
103+
alias: 'authToken',
104+
default: token,
105+
type: 'string',
106+
global: true
107+
})
108+
.example(`node $0 infoTypes GOVERNMENT`)
109+
.example(`node $0 categories`)
110+
.wrap(120)
111+
.recommendCommands()
112+
.epilogue(`For more information, see https://cloud.google.com/dlp/docs`);
113+
114+
cli.help().strict().argv;
115+
});
116+
}

dlp/package.json

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "dlp-cli",
3+
"description": "Command-line interface for Google Cloud Platform's Data Loss Prevention API",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache Version 2.0",
7+
"author": "Google Inc.",
8+
"contributors": [
9+
{
10+
"name": "Ace Nassri",
11+
"email": "[email protected]"
12+
},
13+
{
14+
"name": "Jason Dobry",
15+
"email": "[email protected]"
16+
},
17+
{
18+
"name": "Jon Wayne Parrott",
19+
"email": "[email protected]"
20+
}
21+
],
22+
"repository": {
23+
"type": "git",
24+
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
25+
},
26+
"scripts": {
27+
"test": "ava system-test/*.test.js -c 20 -T 240s"
28+
},
29+
"engines": {
30+
"node": ">=4.3.2"
31+
},
32+
"dependencies": {
33+
"google-auth-library": "^0.10.0",
34+
"google-auto-auth": "^0.5.2",
35+
"mime": "1.3.4",
36+
"request": "2.79.0",
37+
"request-promise": "4.1.1",
38+
"yargs": "6.6.0"
39+
}
40+
}

dlp/redact.js

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
const API_URL = 'https://dlp.googleapis.com/v2beta1';
19+
const requestPromise = require('request-promise');
20+
21+
function redactString (authToken, string, replaceString, inspectConfig) {
22+
// [START redact_string]
23+
// Your gcloud auth token
24+
// const authToken = 'YOUR_AUTH_TOKEN';
25+
26+
// The string to inspect
27+
// const string = 'My name is Gary and my email is [email protected]';
28+
29+
// The string to replace sensitive data with
30+
// const replaceString = 'REDACTED';
31+
32+
// Construct items to inspect
33+
const items = [{ type: 'text/plain', value: string }];
34+
35+
// Construct info types + replacement configs
36+
const replaceConfigs = inspectConfig.infoTypes.map((infoType) => {
37+
return {
38+
infoType: infoType,
39+
replaceWith: replaceString
40+
};
41+
});
42+
43+
// Construct REST request body
44+
const requestBody = {
45+
inspectConfig: {
46+
infoTypes: inspectConfig.infoTypes,
47+
minLikelihood: inspectConfig.minLikelihood
48+
},
49+
items: items,
50+
replaceConfigs: replaceConfigs
51+
};
52+
53+
// Construct REST request
54+
const options = {
55+
url: `${API_URL}/content:redact`,
56+
headers: {
57+
'Authorization': `Bearer ${authToken}`,
58+
'Content-Type': 'application/json'
59+
},
60+
json: requestBody
61+
};
62+
63+
// Run REST request
64+
requestPromise.post(options)
65+
.then((body) => {
66+
const results = body.items[0].value;
67+
console.log(results);
68+
})
69+
.catch((err) => {
70+
console.log('Error in redactString:', err);
71+
});
72+
// [END redact_string]
73+
}
74+
75+
if (module === require.main) {
76+
const auth = require('google-auto-auth')({
77+
keyFilename: process.env.GOOGLE_APPLICATION_CREDENTIALS,
78+
scopes: ['https://www.googleapis.com/auth/cloud-platform']
79+
});
80+
auth.getToken((err, token) => {
81+
if (err) {
82+
console.err('Error fetching auth token:', err);
83+
process.exit(1);
84+
}
85+
86+
const cli = require(`yargs`)
87+
.demand(1)
88+
.command(
89+
`string <string> <replaceString>`,
90+
`Redact sensitive data from a string using the Data Loss Prevention API.`,
91+
{},
92+
(opts) => redactString(opts.authToken, opts.string, opts.replaceString, opts)
93+
)
94+
.option('m', {
95+
alias: 'minLikelihood',
96+
default: 'LIKELIHOOD_UNSPECIFIED',
97+
type: 'string',
98+
choices: [
99+
'LIKELIHOOD_UNSPECIFIED',
100+
'VERY_UNLIKELY',
101+
'UNLIKELY',
102+
'POSSIBLE',
103+
'LIKELY',
104+
'VERY_LIKELY'
105+
],
106+
global: true
107+
})
108+
.option('a', {
109+
alias: 'authToken',
110+
default: token,
111+
type: 'string',
112+
global: true
113+
})
114+
.option('t', {
115+
alias: 'infoTypes',
116+
required: true,
117+
type: 'array',
118+
global: true,
119+
coerce: (infoTypes) => infoTypes.map((type) => {
120+
return { name: type };
121+
})
122+
})
123+
.example(`node $0 string "My name is Gary" "REDACTED" -t US_MALE_NAME`)
124+
.wrap(120)
125+
.recommendCommands()
126+
.epilogue(`For more information, see https://cloud.google.com/dlp/docs. Optional flags are explained at https://cloud.google.com/dlp/docs/reference/rest/v2beta1/content/inspect#InspectConfig`);
127+
128+
cli.help().strict().argv;
129+
});
130+
}

dlp/resources/accounts.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
My credit card number is 1234 5678 9012 3456, and my CVV is 789.

dlp/resources/harmless.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file is mostly harmless.

dlp/resources/test.png

20.9 KB
Loading

dlp/resources/test.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
My phone number is (223) 456-7890 and my email address is [email protected].

0 commit comments

Comments
 (0)