Skip to content

Commit 6897322

Browse files
author
Ace Nassri
authored
Add DLP quickstart + fix incorrect comments (GoogleCloudPlatform#442)
* Fix typo in comments * Add DLP quickstart * Address comments
1 parent efa7268 commit 6897322

File tree

4 files changed

+121
-14
lines changed

4 files changed

+121
-14
lines changed

dlp/inspect.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ function inspectString (string, minLikelihood, maxFindings, infoTypes, includeQu
3131
// const string = 'My name is Gary and my email is [email protected]';
3232

3333
// The minimum likelihood required before returning a match
34-
// const minLikelihood = LIKELIHOOD_UNSPECIFIED;
34+
// const minLikelihood = 'LIKELIHOOD_UNSPECIFIED';
3535

3636
// The maximum number of findings to report (0 = server maximum)
3737
// const maxFindings = 0;
3838

3939
// The infoTypes of information to match
40-
// const infoTypes = ['US_MALE_NAME', 'US_FEMALE_NAME'];
40+
// const infoTypes = [{ name: 'US_MALE_NAME', name: 'US_FEMALE_NAME' }];
4141

4242
// Whether to include the matching string
4343
// const includeQuote = true;
@@ -91,13 +91,13 @@ function inspectFile (filepath, minLikelihood, maxFindings, infoTypes, includeQu
9191
// const fileName = 'path/to/image.png';
9292

9393
// The minimum likelihood required before returning a match
94-
// const minLikelihood = LIKELIHOOD_UNSPECIFIED;
94+
// const minLikelihood = 'LIKELIHOOD_UNSPECIFIED';
9595

9696
// The maximum number of findings to report (0 = server maximum)
9797
// const maxFindings = 0;
9898

9999
// The infoTypes of information to match
100-
// const infoTypes = ['US_MALE_NAME', 'US_FEMALE_NAME'];
100+
// const infoTypes = [{ name: 'US_MALE_NAME' }, { name: 'US_FEMALE_NAME' }];
101101

102102
// Whether to include the matching string
103103
// const includeQuote = true;
@@ -158,13 +158,13 @@ function promiseInspectGCSFile (bucketName, fileName, minLikelihood, maxFindings
158158
// const fileName = 'my-image.png';
159159

160160
// The minimum likelihood required before returning a match
161-
// const minLikelihood = LIKELIHOOD_UNSPECIFIED;
161+
// const minLikelihood = 'LIKELIHOOD_UNSPECIFIED';
162162

163163
// The maximum number of findings to report (0 = server maximum)
164164
// const maxFindings = 0;
165165

166166
// The infoTypes of information to match
167-
// const infoTypes = ['US_MALE_NAME', 'US_FEMALE_NAME'];
167+
// const infoTypes = [{ name: 'US_MALE_NAME' }, { name: 'US_FEMALE_NAME' }];
168168

169169
// Get reference to the file to be inspected
170170
const storageItems = {
@@ -232,13 +232,13 @@ function eventInspectGCSFile (bucketName, fileName, minLikelihood, maxFindings,
232232
// const fileName = 'my-image.png';
233233

234234
// The minimum likelihood required before returning a match
235-
// const minLikelihood = LIKELIHOOD_UNSPECIFIED;
235+
// const minLikelihood = 'LIKELIHOOD_UNSPECIFIED';
236236

237237
// The maximum number of findings to report (0 = server maximum)
238238
// const maxFindings = 0;
239239

240240
// The infoTypes of information to match
241-
// const infoTypes = ['US_MALE_NAME', 'US_FEMALE_NAME'];
241+
// const infoTypes = [{ name: 'US_MALE_NAME' }, { name: 'US_FEMALE_NAME' }];
242242

243243
// Get reference to the file to be inspected
244244
const storageItems = {
@@ -320,13 +320,13 @@ function inspectDatastore (projectId, namespaceId, kind, minLikelihood, maxFindi
320320
// const kind = 'Person';
321321

322322
// The minimum likelihood required before returning a match
323-
// const minLikelihood = LIKELIHOOD_UNSPECIFIED;
323+
// const minLikelihood = 'LIKELIHOOD_UNSPECIFIED';
324324

325325
// The maximum number of findings to report (0 = server maximum)
326326
// const maxFindings = 0;
327327

328328
// The infoTypes of information to match
329-
// const infoTypes = ['US_MALE_NAME', 'US_FEMALE_NAME'];
329+
// const infoTypes = [{ name: 'US_MALE_NAME' }, { name: 'US_FEMALE_NAME' }];
330330

331331
// Get reference to the file to be inspected
332332
const storageItems = {

dlp/quickstart.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
// [START quickstart]
19+
// Imports the Google Cloud Data Loss Prevention library
20+
const DLP = require('@google-cloud/dlp');
21+
22+
// Instantiates a client
23+
const dlp = DLP();
24+
25+
// The string to inspect
26+
const string = 'Robert Frost';
27+
28+
// The minimum likelihood required before returning a match
29+
const minLikelihood = 'LIKELIHOOD_UNSPECIFIED';
30+
31+
// The maximum number of findings to report (0 = server maximum)
32+
const maxFindings = 0;
33+
34+
// The infoTypes of information to match
35+
const infoTypes = [
36+
{ name: 'US_MALE_NAME' },
37+
{ name: 'US_FEMALE_NAME' }
38+
];
39+
40+
// Whether to include the matching string
41+
const includeQuote = true;
42+
43+
// Construct items to inspect
44+
const items = [{ type: 'text/plain', value: string }];
45+
46+
// Construct request
47+
const request = {
48+
inspectConfig: {
49+
infoTypes: infoTypes,
50+
minLikelihood: minLikelihood,
51+
maxFindings: maxFindings,
52+
includeQuote: includeQuote
53+
},
54+
items: items
55+
};
56+
57+
// Run request
58+
dlp.inspectContent(request)
59+
.then((response) => {
60+
const findings = response[0].results[0].findings;
61+
if (findings.length > 0) {
62+
console.log(`Findings:`);
63+
findings.forEach((finding) => {
64+
if (includeQuote) {
65+
console.log(`\tQuote: ${finding.quote}`);
66+
}
67+
console.log(`\tInfo type: ${finding.infoType.name}`);
68+
console.log(`\tLikelihood: ${finding.likelihood}`);
69+
});
70+
} else {
71+
console.log(`No findings.`);
72+
}
73+
})
74+
.catch((err) => {
75+
console.error(`Error in inspectString: ${err.message || err}`);
76+
});
77+
// [END quickstart]

dlp/redact.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ function redactString (string, replaceString, minLikelihood, infoTypes) {
3030
// const replaceString = 'REDACTED';
3131

3232
// The minimum likelihood required before redacting a match
33-
// const minLikelihood = LIKELIHOOD_UNSPECIFIED;
33+
// const minLikelihood = 'LIKELIHOOD_UNSPECIFIED';
3434

3535
// The infoTypes of information to redact
36-
// const infoTypes = ['US_MALE_NAME', 'US_FEMALE_NAME'];
36+
// const infoTypes = [{ name: 'US_MALE_NAME' }, { name: 'US_FEMALE_NAME' }];
3737

3838
const items = [{ type: 'text/plain', value: string }];
3939

@@ -80,10 +80,10 @@ function redactImage (filepath, minLikelihood, infoTypes, outputPath) {
8080
// const fileName = 'path/to/image.png';
8181

8282
// The minimum likelihood required before redacting a match
83-
// const minLikelihood = LIKELIHOOD_UNSPECIFIED;
83+
// const minLikelihood = 'LIKELIHOOD_UNSPECIFIED';
8484

8585
// The infoTypes of information to redact
86-
// const infoTypes = ['EMAIL_ADDRESS', 'PHONE_NUMBER'];
86+
// const infoTypes = [{ name: 'EMAIL_ADDRESS' }, { name: 'PHONE_NUMBER' }];
8787

8888
// The local path to save the resulting image to.
8989
// const outputPath = 'result.png';

dlp/system-test/quickstart.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 path = require('path');
19+
const test = require('ava');
20+
const tools = require('@google-cloud/nodejs-repo-tools');
21+
22+
const cmd = 'node quickstart';
23+
const cwd = path.join(__dirname, `..`);
24+
25+
test.before(tools.checkCredentials);
26+
27+
test(`should run`, async (t) => {
28+
const output = await tools.runAsync(cmd, cwd);
29+
t.regex(output, /Info type: US_MALE_NAME/);
30+
});

0 commit comments

Comments
 (0)