1
1
/**
2
- * Copyright 2017, Google, Inc.
2
+ * Copyright 2017 Google Inc.
3
+ *
3
4
* Licensed under the Apache License, Version 2.0 (the "License");
4
5
* you may not use this file except in compliance with the License.
5
6
* You may obtain a copy of the License at
6
7
*
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
+ * http://www.apache.org/licenses/LICENSE-2.0
8
9
*
9
10
* Unless required by applicable law or agreed to in writing, software
10
11
* distributed under the License is distributed on an "AS IS" BASIS,
38
39
import com .google .privacy .dlp .v2beta1 .ResultName ;
39
40
import com .google .privacy .dlp .v2beta1 .StorageConfig ;
40
41
import com .google .protobuf .ByteString ;
41
- import org .apache .commons .cli .CommandLine ;
42
- import org .apache .commons .cli .CommandLineParser ;
43
- import org .apache .commons .cli .DefaultParser ;
44
- import org .apache .commons .cli .HelpFormatter ;
45
- import org .apache .commons .cli .Option ;
46
- import org .apache .commons .cli .OptionGroup ;
47
- import org .apache .commons .cli .Options ;
48
- import org .apache .commons .cli .ParseException ;
49
-
50
42
import java .net .URLConnection ;
51
43
import java .nio .file .Files ;
52
44
import java .nio .file .Path ;
55
47
import java .util .Collections ;
56
48
import java .util .List ;
57
49
import javax .activation .MimetypesFileTypeMap ;
50
+ import org .apache .commons .cli .CommandLine ;
51
+ import org .apache .commons .cli .CommandLineParser ;
52
+ import org .apache .commons .cli .DefaultParser ;
53
+ import org .apache .commons .cli .HelpFormatter ;
54
+ import org .apache .commons .cli .Option ;
55
+ import org .apache .commons .cli .OptionGroup ;
56
+ import org .apache .commons .cli .Options ;
57
+ import org .apache .commons .cli .ParseException ;
58
58
59
59
public class Inspect {
60
60
61
- private static void inspectString (String string , Likelihood minLikelihood , int maxFindings ,
62
- List <InfoType > infoTypes , boolean includeQuote ) {
61
+ private static void inspectString (
62
+ String string ,
63
+ Likelihood minLikelihood ,
64
+ int maxFindings ,
65
+ List <InfoType > infoTypes ,
66
+ boolean includeQuote ) {
63
67
// [START dlp_inspect_string]
64
68
// instantiate a client
65
69
try (DlpServiceClient dlpServiceClient = DlpServiceClient .create ()) {
@@ -75,24 +79,24 @@ private static void inspectString(String string, Likelihood minLikelihood, int m
75
79
76
80
// Whether to include the matching string
77
81
// includeQuote = true;
78
- InspectConfig inspectConfig = InspectConfig .newBuilder ()
79
- .addAllInfoTypes (infoTypes )
80
- .setMinLikelihood (minLikelihood )
81
- .setMaxFindings (maxFindings )
82
- .setIncludeQuote (includeQuote )
83
- .build ();
82
+ InspectConfig inspectConfig =
83
+ InspectConfig .newBuilder ()
84
+ .addAllInfoTypes (infoTypes )
85
+ .setMinLikelihood (minLikelihood )
86
+ .setMaxFindings (maxFindings )
87
+ .setIncludeQuote (includeQuote )
88
+ .build ();
84
89
85
90
// The string to inspect
86
91
// string = 'My name is Gary and my email is gary@example.com';
87
- ContentItem contentItem = ContentItem .newBuilder ()
88
- .setType ("text/plain" )
89
- .setValue (string )
90
- .build ();
91
-
92
- InspectContentRequest request = InspectContentRequest .newBuilder ()
93
- .setInspectConfig (inspectConfig )
94
- .addItems (contentItem )
95
- .build ();
92
+ ContentItem contentItem =
93
+ ContentItem .newBuilder ().setType ("text/plain" ).setValue (string ).build ();
94
+
95
+ InspectContentRequest request =
96
+ InspectContentRequest .newBuilder ()
97
+ .setInspectConfig (inspectConfig )
98
+ .addItems (contentItem )
99
+ .build ();
96
100
InspectContentResponse response = dlpServiceClient .inspectContent (request );
97
101
98
102
for (InspectResult result : response .getResultsList ()) {
@@ -115,8 +119,12 @@ private static void inspectString(String string, Likelihood minLikelihood, int m
115
119
// [END dlp_inspect_string]
116
120
}
117
121
118
- private static void inspectFile (String filePath , Likelihood minLikelihood , int maxFindings ,
119
- List <InfoType > infoTypes , boolean includeQuote ) {
122
+ private static void inspectFile (
123
+ String filePath ,
124
+ Likelihood minLikelihood ,
125
+ int maxFindings ,
126
+ List <InfoType > infoTypes ,
127
+ boolean includeQuote ) {
120
128
// [START dlp_inspect_file]
121
129
// Instantiates a client
122
130
try (DlpServiceClient dlpServiceClient = DlpServiceClient .create ()) {
@@ -146,22 +154,22 @@ private static void inspectFile(String filePath, Likelihood minLikelihood, int m
146
154
}
147
155
148
156
byte [] data = Files .readAllBytes (path );
149
- ContentItem contentItem = ContentItem . newBuilder ()
150
- . setType (mimeType )
151
- . setData ( ByteString . copyFrom ( data ))
152
- . build ();
153
-
154
- InspectConfig inspectConfig = InspectConfig . newBuilder ( )
155
- . addAllInfoTypes ( infoTypes )
156
- . setMinLikelihood ( minLikelihood )
157
- . setMaxFindings ( maxFindings )
158
- . setIncludeQuote ( includeQuote )
159
- . build ();
160
-
161
- InspectContentRequest request = InspectContentRequest .newBuilder ()
162
- .setInspectConfig (inspectConfig )
163
- .addItems (contentItem )
164
- .build ();
157
+ ContentItem contentItem =
158
+ ContentItem . newBuilder (). setType (mimeType ). setData ( ByteString . copyFrom ( data )). build ();
159
+
160
+ InspectConfig inspectConfig =
161
+ InspectConfig . newBuilder ()
162
+ . addAllInfoTypes ( infoTypes )
163
+ . setMinLikelihood ( minLikelihood )
164
+ . setMaxFindings ( maxFindings )
165
+ . setIncludeQuote ( includeQuote )
166
+ . build ();
167
+
168
+ InspectContentRequest request =
169
+ InspectContentRequest .newBuilder ()
170
+ .setInspectConfig (inspectConfig )
171
+ .addItems (contentItem )
172
+ .build ();
165
173
InspectContentResponse response = dlpServiceClient .inspectContent (request );
166
174
167
175
for (InspectResult result : response .getResultsList ()) {
@@ -185,13 +193,13 @@ private static void inspectFile(String filePath, Likelihood minLikelihood, int m
185
193
// [END dlp_inspect_file]
186
194
}
187
195
188
- private static void inspectGcsFile (String bucketName , String fileName ,
189
- Likelihood minLikelihood , List <InfoType > infoTypes )
196
+ private static void inspectGcsFile (
197
+ String bucketName , String fileName , Likelihood minLikelihood , List <InfoType > infoTypes )
190
198
throws Exception {
191
199
// [START dlp_inspect_gcs]
192
200
// Instantiates a client
193
201
try (DlpServiceClient dlpServiceClient = DlpServiceClient .create ()) {
194
- // The name of the bucket where the file resides.
202
+ // The name of the bucket where the file resides.
195
203
// bucketName = 'YOUR-BUCKET';
196
204
197
205
// The path to the file within the bucket to inspect.
@@ -207,21 +215,19 @@ private static void inspectGcsFile(String bucketName, String fileName,
207
215
// The infoTypes of information to match
208
216
// infoTypes = ['US_MALE_NAME', 'US_FEMALE_NAME'];
209
217
210
- CloudStorageOptions cloudStorageOptions = CloudStorageOptions
211
- .newBuilder ()
212
- .setFileSet (FileSet .newBuilder ().setUrl (
213
- "gs://" + bucketName + "/" + fileName
214
- ))
215
- .build ();
218
+ CloudStorageOptions cloudStorageOptions =
219
+ CloudStorageOptions .newBuilder ()
220
+ .setFileSet (FileSet .newBuilder ().setUrl ("gs://" + bucketName + "/" + fileName ))
221
+ .build ();
216
222
217
- StorageConfig storageConfig = StorageConfig .newBuilder ()
218
- .setCloudStorageOptions (cloudStorageOptions )
219
- .build ();
223
+ StorageConfig storageConfig =
224
+ StorageConfig .newBuilder ().setCloudStorageOptions (cloudStorageOptions ).build ();
220
225
221
- InspectConfig inspectConfig = InspectConfig .newBuilder ()
222
- .addAllInfoTypes (infoTypes )
223
- .setMinLikelihood (minLikelihood )
224
- .build ();
226
+ InspectConfig inspectConfig =
227
+ InspectConfig .newBuilder ()
228
+ .addAllInfoTypes (infoTypes )
229
+ .setMinLikelihood (minLikelihood )
230
+ .build ();
225
231
226
232
// optionally provide an output configuration to store results, default : none
227
233
OutputStorageConfig outputConfig = OutputStorageConfig .getDefaultInstance ();
@@ -252,8 +258,12 @@ private static void inspectGcsFile(String bucketName, String fileName,
252
258
// [END dlp_inspect_gcs]
253
259
}
254
260
255
- private static void inspectDatastore (String projectId , String namespaceId , String kind ,
256
- Likelihood minLikelihood , List <InfoType > infoTypes ) {
261
+ private static void inspectDatastore (
262
+ String projectId ,
263
+ String namespaceId ,
264
+ String kind ,
265
+ Likelihood minLikelihood ,
266
+ List <InfoType > infoTypes ) {
257
267
// [START dlp_inspect_datastore]
258
268
// Instantiates a client
259
269
try (DlpServiceClient dlpServiceClient = DlpServiceClient .create ()) {
@@ -274,19 +284,24 @@ private static void inspectDatastore(String projectId, String namespaceId, Strin
274
284
// The infoTypes of information to match
275
285
// infoTypes = ['US_MALE_NAME', 'US_FEMALE_NAME'];
276
286
277
- // Get reference to the file to be inspected
278
- PartitionId partitionId = PartitionId .newBuilder ().setProjectId (projectId )
279
- .setNamespaceId (namespaceId ).build ();
287
+ // Reference to the Datastore namespace
288
+ PartitionId partitionId =
289
+ PartitionId .newBuilder ().setProjectId (projectId ).setNamespaceId (namespaceId ).build ();
290
+
291
+ // Reference to the Datastore kind
280
292
KindExpression kindExpression = KindExpression .newBuilder ().setName (kind ).build ();
281
- DatastoreOptions datastoreOptions = DatastoreOptions .newBuilder ()
282
- .setKind (kindExpression ).setPartitionId (partitionId ).build ();
283
- StorageConfig storageConfig = StorageConfig .newBuilder ()
284
- .setDatastoreOptions (datastoreOptions ).build ();
293
+ DatastoreOptions datastoreOptions =
294
+ DatastoreOptions .newBuilder ().setKind (kindExpression ).setPartitionId (partitionId ).build ();
295
+
296
+ // Construct Datastore configuration to be inspected
297
+ StorageConfig storageConfig =
298
+ StorageConfig .newBuilder ().setDatastoreOptions (datastoreOptions ).build ();
285
299
286
- InspectConfig inspectConfig = InspectConfig .newBuilder ()
287
- .addAllInfoTypes (infoTypes )
288
- .setMinLikelihood (minLikelihood )
289
- .build ();
300
+ InspectConfig inspectConfig =
301
+ InspectConfig .newBuilder ()
302
+ .addAllInfoTypes (infoTypes )
303
+ .setMinLikelihood (minLikelihood )
304
+ .build ();
290
305
291
306
// optionally provide an output configuration to store results, default : none
292
307
OutputStorageConfig outputConfig = OutputStorageConfig .getDefaultInstance ();
@@ -317,6 +332,10 @@ private static void inspectDatastore(String projectId, String namespaceId, Strin
317
332
// [END dlp_inspect_datastore]
318
333
}
319
334
335
+ /**
336
+ * Command line application to inspect data using the Data Loss Prevention API.
337
+ * Supported data formats : string, file, text files on GCS and Datastore entities
338
+ */
320
339
public static void main (String [] args ) throws Exception {
321
340
322
341
OptionGroup optionsGroup = new OptionGroup ();
@@ -336,61 +355,37 @@ public static void main(String[] args) throws Exception {
336
355
Options commandLineOptions = new Options ();
337
356
commandLineOptions .addOptionGroup (optionsGroup );
338
357
339
- Option minLikelihoodOption = Option .builder ("minLikelihood" )
340
- .hasArg (true )
341
- .required (false )
342
- .build ();
358
+ Option minLikelihoodOption =
359
+ Option .builder ("minLikelihood" ).hasArg (true ).required (false ).build ();
343
360
344
361
commandLineOptions .addOption (minLikelihoodOption );
345
362
346
- Option maxFindingsOption = Option .builder ("maxFindings" )
347
- .hasArg (true )
348
- .required (false )
349
- .build ();
363
+ Option maxFindingsOption = Option .builder ("maxFindings" ).hasArg (true ).required (false ).build ();
350
364
351
365
commandLineOptions .addOption (maxFindingsOption );
352
366
353
- Option infoTypesOption = Option .builder ("infoTypes" )
354
- .hasArg (true )
355
- .required (false )
356
- .build ();
367
+ Option infoTypesOption = Option .builder ("infoTypes" ).hasArg (true ).required (false ).build ();
357
368
infoTypesOption .setArgs (Option .UNLIMITED_VALUES );
358
369
commandLineOptions .addOption (infoTypesOption );
359
370
360
- Option includeQuoteOption = Option .builder ("includeQuote" )
361
- .hasArg (true )
362
- .required (false )
363
- .build ();
371
+ Option includeQuoteOption = Option .builder ("includeQuote" ).hasArg (true ).required (false ).build ();
364
372
commandLineOptions .addOption (includeQuoteOption );
365
373
366
- Option bucketNameOption = Option .builder ("bucketName" )
367
- .hasArg (true )
368
- .required (false )
369
- .build ();
374
+ Option bucketNameOption = Option .builder ("bucketName" ).hasArg (true ).required (false ).build ();
370
375
commandLineOptions .addOption (bucketNameOption );
371
376
372
- Option gcsFileNameOption = Option .builder ("fileName" )
373
- .hasArg (true )
374
- .required (false )
375
- .build ();
377
+ Option gcsFileNameOption = Option .builder ("fileName" ).hasArg (true ).required (false ).build ();
376
378
commandLineOptions .addOption (gcsFileNameOption );
377
379
378
- Option datastoreProjectIdOption = Option .builder ("projectId" )
379
- .hasArg (true )
380
- .required (false )
381
- .build ();
380
+ Option datastoreProjectIdOption =
381
+ Option .builder ("projectId" ).hasArg (true ).required (false ).build ();
382
382
commandLineOptions .addOption (datastoreProjectIdOption );
383
383
384
- Option datastoreNamespaceOption = Option .builder ("namespace" )
385
- .hasArg (true )
386
- .required (false )
387
- .build ();
384
+ Option datastoreNamespaceOption =
385
+ Option .builder ("namespace" ).hasArg (true ).required (false ).build ();
388
386
commandLineOptions .addOption (datastoreNamespaceOption );
389
387
390
- Option datastoreKindOption = Option .builder ("kind" )
391
- .hasArg (true )
392
- .required (false )
393
- .build ();
388
+ Option datastoreKindOption = Option .builder ("kind" ).hasArg (true ).required (false ).build ();
394
389
commandLineOptions .addOption (datastoreKindOption );
395
390
396
391
CommandLineParser parser = new DefaultParser ();
@@ -406,11 +401,13 @@ public static void main(String[] args) throws Exception {
406
401
return ;
407
402
}
408
403
409
- Likelihood minLikelihood = Likelihood .valueOf (cmd .getOptionValue (minLikelihoodOption .getOpt (),
410
- Likelihood .LIKELIHOOD_UNSPECIFIED .name ()));
404
+ Likelihood minLikelihood =
405
+ Likelihood .valueOf (
406
+ cmd .getOptionValue (
407
+ minLikelihoodOption .getOpt (), Likelihood .LIKELIHOOD_UNSPECIFIED .name ()));
411
408
int maxFindings = Integer .parseInt (cmd .getOptionValue (maxFindingsOption .getOpt (), "0" ));
412
- boolean includeQuote = Boolean
413
- .parseBoolean (cmd .getOptionValue (includeQuoteOption .getOpt (), "true" ));
409
+ boolean includeQuote =
410
+ Boolean .parseBoolean (cmd .getOptionValue (includeQuoteOption .getOpt (), "true" ));
414
411
415
412
List <InfoType > infoTypesList = Collections .emptyList ();
416
413
if (cmd .hasOption (infoTypesOption .getOpt ())) {
@@ -437,8 +434,9 @@ public static void main(String[] args) throws Exception {
437
434
String namespaceId = cmd .getOptionValue (datastoreNamespaceOption .getOpt (), "" );
438
435
String kind = cmd .getOptionValue (datastoreKindOption .getOpt ());
439
436
// use default project id when project id is not specified
440
- String projectId = cmd .getOptionValue (datastoreProjectIdOption .getOpt (),
441
- ServiceOptions .getDefaultProjectId ());
437
+ String projectId =
438
+ cmd .getOptionValue (
439
+ datastoreProjectIdOption .getOpt (), ServiceOptions .getDefaultProjectId ());
442
440
inspectDatastore (projectId , namespaceId , kind , minLikelihood , infoTypesList );
443
441
}
444
442
}
0 commit comments