27
27
import com .google .privacy .dlp .v2 .CloudStorageOptions ;
28
28
import com .google .privacy .dlp .v2 .ContentItem ;
29
29
import com .google .privacy .dlp .v2 .CreateDlpJobRequest ;
30
+ import com .google .privacy .dlp .v2 .CustomInfoType ;
31
+ import com .google .privacy .dlp .v2 .CustomInfoType .Dictionary ;
32
+ import com .google .privacy .dlp .v2 .CustomInfoType .Dictionary .WordList ;
33
+ import com .google .privacy .dlp .v2 .CustomInfoType .Regex ;
30
34
import com .google .privacy .dlp .v2 .DatastoreOptions ;
31
35
import com .google .privacy .dlp .v2 .DlpJob ;
32
36
import com .google .privacy .dlp .v2 .Finding ;
52
56
import java .nio .file .Files ;
53
57
import java .nio .file .Paths ;
54
58
import java .util .ArrayList ;
59
+ import java .util .Arrays ;
55
60
import java .util .Collections ;
56
61
import java .util .List ;
57
62
import java .util .concurrent .TimeUnit ;
@@ -82,6 +87,7 @@ private static void inspectString(
82
87
Likelihood minLikelihood ,
83
88
int maxFindings ,
84
89
List <InfoType > infoTypes ,
90
+ List <CustomInfoType > customInfoTypes ,
85
91
boolean includeQuote ,
86
92
String projectId ) {
87
93
// instantiate a client
@@ -91,6 +97,7 @@ private static void inspectString(
91
97
InspectConfig inspectConfig =
92
98
InspectConfig .newBuilder ()
93
99
.addAllInfoTypes (infoTypes )
100
+ .addAllCustomInfoTypes (customInfoTypes )
94
101
.setMinLikelihood (minLikelihood )
95
102
.setLimits (findingLimits )
96
103
.setIncludeQuote (includeQuote )
@@ -146,6 +153,7 @@ private static void inspectFile(
146
153
Likelihood minLikelihood ,
147
154
int maxFindings ,
148
155
List <InfoType > infoTypes ,
156
+ List <CustomInfoType > customInfoTypes ,
149
157
boolean includeQuote ,
150
158
String projectId ) {
151
159
// Instantiates a client
@@ -189,6 +197,7 @@ private static void inspectFile(
189
197
InspectConfig inspectConfig =
190
198
InspectConfig .newBuilder ()
191
199
.addAllInfoTypes (infoTypes )
200
+ .addAllCustomInfoTypes (customInfoTypes )
192
201
.setMinLikelihood (minLikelihood )
193
202
.setLimits (findingLimits )
194
203
.setIncludeQuote (includeQuote )
@@ -242,6 +251,7 @@ private static void inspectGcsFile(
242
251
String fileName ,
243
252
Likelihood minLikelihood ,
244
253
List <InfoType > infoTypes ,
254
+ List <CustomInfoType > customInfoTypes ,
245
255
int maxFindings ,
246
256
String topicId ,
247
257
String subscriptionId ,
@@ -266,6 +276,7 @@ private static void inspectGcsFile(
266
276
InspectConfig inspectConfig =
267
277
InspectConfig .newBuilder ()
268
278
.addAllInfoTypes (infoTypes )
279
+ .addAllCustomInfoTypes (customInfoTypes )
269
280
.setMinLikelihood (minLikelihood )
270
281
.setLimits (findingLimits )
271
282
.build ();
@@ -363,6 +374,7 @@ private static void inspectDatastore(
363
374
String kind ,
364
375
Likelihood minLikelihood ,
365
376
List <InfoType > infoTypes ,
377
+ List <CustomInfoType > customInfoTypes ,
366
378
int maxFindings ,
367
379
String topicId ,
368
380
String subscriptionId ) {
@@ -388,6 +400,7 @@ private static void inspectDatastore(
388
400
InspectConfig inspectConfig =
389
401
InspectConfig .newBuilder ()
390
402
.addAllInfoTypes (infoTypes )
403
+ .addAllCustomInfoTypes (customInfoTypes )
391
404
.setMinLikelihood (minLikelihood )
392
405
.setLimits (findingLimits )
393
406
.build ();
@@ -486,6 +499,7 @@ private static void inspectBigquery(
486
499
String tableId ,
487
500
Likelihood minLikelihood ,
488
501
List <InfoType > infoTypes ,
502
+ List <CustomInfoType > customInfoTypes ,
489
503
int maxFindings ,
490
504
String topicId ,
491
505
String subscriptionId ) {
@@ -511,6 +525,7 @@ private static void inspectBigquery(
511
525
InspectConfig inspectConfig =
512
526
InspectConfig .newBuilder ()
513
527
.addAllInfoTypes (infoTypes )
528
+ .addAllCustomInfoTypes (customInfoTypes )
514
529
.setMinLikelihood (minLikelihood )
515
530
.setLimits (findingLimits )
516
531
.build ();
@@ -629,6 +644,16 @@ public static void main(String[] args) throws Exception {
629
644
infoTypesOption .setArgs (Option .UNLIMITED_VALUES );
630
645
commandLineOptions .addOption (infoTypesOption );
631
646
647
+ Option customDictionariesOption =
648
+ Option .builder ("customDictionaries" ).hasArg (true ).required (false ).build ();
649
+ customDictionariesOption .setArgs (Option .UNLIMITED_VALUES );
650
+ commandLineOptions .addOption (customDictionariesOption );
651
+
652
+ Option customRegexesOption =
653
+ Option .builder ("customRegexes" ).hasArg (true ).required (false ).build ();
654
+ customRegexesOption .setArgs (Option .UNLIMITED_VALUES );
655
+ commandLineOptions .addOption (customRegexesOption );
656
+
632
657
Option includeQuoteOption = Option .builder ("includeQuote" ).hasArg (true ).required (false ).build ();
633
658
commandLineOptions .addOption (includeQuoteOption );
634
659
@@ -695,13 +720,62 @@ public static void main(String[] args) throws Exception {
695
720
infoTypesList .add (InfoType .newBuilder ().setName (infoType ).build ());
696
721
}
697
722
}
723
+
724
+ List <CustomInfoType > customInfoTypesList = new ArrayList <>();
725
+ if (cmd .hasOption (customDictionariesOption .getOpt ())) {
726
+ String [] dictionaryStrings = cmd .getOptionValues (customDictionariesOption .getOpt ());
727
+ for (int i = 0 ; i < dictionaryStrings .length ; i ++) {
728
+ String [] dictionaryWords = dictionaryStrings [i ].split ("," );
729
+ CustomInfoType customInfoType =
730
+ CustomInfoType
731
+ .newBuilder ()
732
+ .setInfoType (
733
+ InfoType .newBuilder ().setName (String .format ("CUSTOM_DICTIONARY_%s" , i )))
734
+ .setDictionary (
735
+ Dictionary
736
+ .newBuilder ()
737
+ .setWordList (
738
+ WordList
739
+ .newBuilder ()
740
+ .addAllWords (Arrays .<String >asList (dictionaryWords ))))
741
+ .build ();
742
+ customInfoTypesList .add (customInfoType );
743
+ }
744
+ }
745
+ if (cmd .hasOption (customRegexesOption .getOpt ())) {
746
+ String [] patterns = cmd .getOptionValues (customRegexesOption .getOpt ());
747
+ for (int i = 0 ; i < patterns .length ; i ++) {
748
+ CustomInfoType customInfoType =
749
+ CustomInfoType
750
+ .newBuilder ()
751
+ .setInfoType (InfoType .newBuilder ().setName (String .format ("CUSTOM_REGEX_%s" , i )))
752
+ .setRegex (Regex .newBuilder ().setPattern (patterns [i ]))
753
+ .build ();
754
+ customInfoTypesList .add (customInfoType );
755
+ }
756
+ }
757
+
698
758
// string inspection
699
759
if (cmd .hasOption ("s" )) {
700
760
String val = cmd .getOptionValue (stringOption .getOpt ());
701
- inspectString (val , minLikelihood , maxFindings , infoTypesList , includeQuote , projectId );
761
+ inspectString (
762
+ val ,
763
+ minLikelihood ,
764
+ maxFindings ,
765
+ infoTypesList ,
766
+ customInfoTypesList ,
767
+ includeQuote ,
768
+ projectId );
702
769
} else if (cmd .hasOption ("f" )) {
703
770
String filePath = cmd .getOptionValue (fileOption .getOpt ());
704
- inspectFile (filePath , minLikelihood , maxFindings , infoTypesList , includeQuote , projectId );
771
+ inspectFile (
772
+ filePath ,
773
+ minLikelihood ,
774
+ maxFindings ,
775
+ infoTypesList ,
776
+ customInfoTypesList ,
777
+ includeQuote ,
778
+ projectId );
705
779
// gcs file inspection
706
780
} else if (cmd .hasOption ("gcs" )) {
707
781
String bucketName = cmd .getOptionValue (bucketNameOption .getOpt ());
@@ -711,6 +785,7 @@ public static void main(String[] args) throws Exception {
711
785
fileName ,
712
786
minLikelihood ,
713
787
infoTypesList ,
788
+ customInfoTypesList ,
714
789
maxFindings ,
715
790
topicId ,
716
791
subscriptionId ,
@@ -726,6 +801,7 @@ public static void main(String[] args) throws Exception {
726
801
kind ,
727
802
minLikelihood ,
728
803
infoTypesList ,
804
+ customInfoTypesList ,
729
805
maxFindings ,
730
806
topicId ,
731
807
subscriptionId );
@@ -739,6 +815,7 @@ public static void main(String[] args) throws Exception {
739
815
tableId ,
740
816
minLikelihood ,
741
817
infoTypesList ,
818
+ customInfoTypesList ,
742
819
maxFindings ,
743
820
topicId ,
744
821
subscriptionId );
0 commit comments