Skip to content

Commit

Permalink
DLP samples (#322)
Browse files Browse the repository at this point in the history
* First draft of DLP samples

* Fix DLP tests

* Add README

* Fix README bugs
  • Loading branch information
Ace Nassri authored Mar 9, 2017
0 parents commit 1900ae8
Show file tree
Hide file tree
Showing 11 changed files with 957 additions and 0 deletions.
401 changes: 401 additions & 0 deletions dlp/inspect.js

Large diffs are not rendered by default.

116 changes: 116 additions & 0 deletions dlp/metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* Copyright 2017, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const API_URL = 'https://dlp.googleapis.com/v2beta1';
const requestPromise = require('request-promise');

function listInfoTypes (authToken, category) {
// [START list_info_types]
// Your gcloud auth token.
// const authToken = 'YOUR_AUTH_TOKEN';

// The category of info types to list.
// const category = 'CATEGORY_TO_LIST';

// Construct REST request
const options = {
url: `${API_URL}/rootCategories/${category}/infoTypes`,
headers: {
'Authorization': `Bearer ${authToken}`,
'Content-Type': 'application/json'
},
json: true
};

// Run REST request
requestPromise.get(options)
.then((body) => {
console.log(body);
})
.catch((err) => {
console.log('Error in listInfoTypes:', err);
});
// [END list_info_types]
}

function listCategories (authToken) {
// [START list_categories]
// Your gcloud auth token.
// const authToken = 'YOUR_AUTH_TOKEN';

// Construct REST request
const options = {
url: `${API_URL}/rootCategories`,
headers: {
'Authorization': `Bearer ${authToken}`,
'Content-Type': 'application/json'
},
json: true
};

// Run REST request
requestPromise.get(options)
.then((body) => {
const categories = body.categories;
console.log(categories);
})
.catch((err) => {
console.log('Error in listCategories:', err);
});
// [END list_categories]
}

if (module === require.main) {
const auth = require('google-auto-auth')({
keyFilename: process.env.GOOGLE_APPLICATION_CREDENTIALS,
scopes: ['https://www.googleapis.com/auth/cloud-platform']
});
auth.getToken((err, token) => {
if (err) {
console.err('Error fetching auth token:', err);
process.exit(1);
}

const cli = require(`yargs`)
.demand(1)
.command(
`infoTypes <category>`,
`List types of sensitive information within a category.`,
{},
(opts) => listInfoTypes(opts.authToken, opts.category)
)
.command(
`categories`,
`List root categories of sensitive information.`,
{},
(opts) => listCategories(opts.authToken)
)
.option('a', {
alias: 'authToken',
default: token,
type: 'string',
global: true
})
.example(`node $0 infoTypes GOVERNMENT`)
.example(`node $0 categories`)
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/dlp/docs`);

cli.help().strict().argv;
});
}
40 changes: 40 additions & 0 deletions dlp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "dlp-cli",
"description": "Command-line interface for Google Cloud Platform's Data Loss Prevention API",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"contributors": [
{
"name": "Ace Nassri",
"email": "anassri@google.com"
},
{
"name": "Jason Dobry",
"email": "jason.dobry@gmail.com"
},
{
"name": "Jon Wayne Parrott",
"email": "jonwayne@google.com"
}
],
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"scripts": {
"test": "ava system-test/*.test.js -c 20 -T 240s"
},
"engines": {
"node": ">=4.3.2"
},
"dependencies": {
"google-auth-library": "^0.10.0",
"google-auto-auth": "^0.5.2",
"mime": "1.3.4",
"request": "2.79.0",
"request-promise": "4.1.1",
"yargs": "6.6.0"
}
}
130 changes: 130 additions & 0 deletions dlp/redact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* Copyright 2017, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const API_URL = 'https://dlp.googleapis.com/v2beta1';
const requestPromise = require('request-promise');

function redactString (authToken, string, replaceString, inspectConfig) {
// [START redact_string]
// Your gcloud auth token
// const authToken = 'YOUR_AUTH_TOKEN';

// The string to inspect
// const string = 'My name is Gary and my email is gary@example.com';

// The string to replace sensitive data with
// const replaceString = 'REDACTED';

// Construct items to inspect
const items = [{ type: 'text/plain', value: string }];

// Construct info types + replacement configs
const replaceConfigs = inspectConfig.infoTypes.map((infoType) => {
return {
infoType: infoType,
replaceWith: replaceString
};
});

// Construct REST request body
const requestBody = {
inspectConfig: {
infoTypes: inspectConfig.infoTypes,
minLikelihood: inspectConfig.minLikelihood
},
items: items,
replaceConfigs: replaceConfigs
};

// Construct REST request
const options = {
url: `${API_URL}/content:redact`,
headers: {
'Authorization': `Bearer ${authToken}`,
'Content-Type': 'application/json'
},
json: requestBody
};

// Run REST request
requestPromise.post(options)
.then((body) => {
const results = body.items[0].value;
console.log(results);
})
.catch((err) => {
console.log('Error in redactString:', err);
});
// [END redact_string]
}

if (module === require.main) {
const auth = require('google-auto-auth')({
keyFilename: process.env.GOOGLE_APPLICATION_CREDENTIALS,
scopes: ['https://www.googleapis.com/auth/cloud-platform']
});
auth.getToken((err, token) => {
if (err) {
console.err('Error fetching auth token:', err);
process.exit(1);
}

const cli = require(`yargs`)
.demand(1)
.command(
`string <string> <replaceString>`,
`Redact sensitive data from a string using the Data Loss Prevention API.`,
{},
(opts) => redactString(opts.authToken, opts.string, opts.replaceString, opts)
)
.option('m', {
alias: 'minLikelihood',
default: 'LIKELIHOOD_UNSPECIFIED',
type: 'string',
choices: [
'LIKELIHOOD_UNSPECIFIED',
'VERY_UNLIKELY',
'UNLIKELY',
'POSSIBLE',
'LIKELY',
'VERY_LIKELY'
],
global: true
})
.option('a', {
alias: 'authToken',
default: token,
type: 'string',
global: true
})
.option('t', {
alias: 'infoTypes',
required: true,
type: 'array',
global: true,
coerce: (infoTypes) => infoTypes.map((type) => {
return { name: type };
})
})
.example(`node $0 string "My name is Gary" "REDACTED" -t US_MALE_NAME`)
.wrap(120)
.recommendCommands()
.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`);

cli.help().strict().argv;
});
}
1 change: 1 addition & 0 deletions dlp/resources/accounts.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
My credit card number is 1234 5678 9012 3456, and my CVV is 789.
1 change: 1 addition & 0 deletions dlp/resources/harmless.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file is mostly harmless.
Binary file added dlp/resources/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions dlp/resources/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
My phone number is (223) 456-7890 and my email address is gary@somedomain.com.
Loading

0 comments on commit 1900ae8

Please sign in to comment.