@@ -435,6 +435,61 @@ def write_data(data):
435
435
# [END dlp_deidentify_date_shift]
436
436
437
437
438
+ # [START dlp_redact_sensitive_data]
439
+ def redact_sensitive_data (project , item , info_types ):
440
+ """Uses the Data Loss Prevention API to redact sensitive data in a
441
+ string by replacing it with the info type.
442
+ Args:
443
+ project: The Google Cloud project id to use as a parent resource.
444
+ item: The string to redact (will be treated as text).
445
+ info_types: A list of strings representing info types to look for.
446
+ A full list of info type categories can be fetched from the API.
447
+ Returns:
448
+ None; the response from the API is printed to the terminal.
449
+ """
450
+
451
+ # Import the client library
452
+ import google .cloud .dlp
453
+
454
+ # Instantiate a client
455
+ dlp = google .cloud .dlp_v2 .DlpServiceClient ()
456
+
457
+ # Convert the project id into a full resource id.
458
+ parent = dlp .project_path (project )
459
+
460
+ # Construct inspect configuration dictionary
461
+ inspect_config = {
462
+ "info_types" : [{"name" : info_type } for info_type in info_types ]
463
+ }
464
+
465
+ # Construct deidentify configuration dictionary
466
+ deidentify_config = {
467
+ "info_type_transformations" : {
468
+ "transformations" : [
469
+ {
470
+ "primitive_transformation" : {
471
+ "replace_with_info_type_config" : {}
472
+ }
473
+ }
474
+ ]
475
+ }
476
+ }
477
+
478
+ # Call the API
479
+ response = dlp .deidentify_content (
480
+ parent ,
481
+ inspect_config = inspect_config ,
482
+ deidentify_config = deidentify_config ,
483
+ item = {"value" : item },
484
+ )
485
+
486
+ # Print out the results.
487
+ print (response .item .value )
488
+
489
+
490
+ # [END dlp_redact_sensitive_data]
491
+
492
+
438
493
if __name__ == "__main__" :
439
494
parser = argparse .ArgumentParser (description = __doc__ )
440
495
subparsers = parser .add_subparsers (
@@ -626,6 +681,30 @@ def write_data(data):
626
681
"key_name." ,
627
682
)
628
683
684
+ redact_parser = subparsers .add_parser (
685
+ "redact" ,
686
+ help = "Redact sensitive data in a string by replacing it with the "
687
+ "info type of the data." ,
688
+ )
689
+ redact_parser .add_argument (
690
+ "--info_types" ,
691
+ action = "append" ,
692
+ help = "Strings representing info types to look for. A full list of "
693
+ "info categories and types is available from the API. Examples "
694
+ 'include "FIRST_NAME", "LAST_NAME", "EMAIL_ADDRESS". '
695
+ "If unspecified, the three above examples will be used." ,
696
+ default = ["FIRST_NAME" , "LAST_NAME" , "EMAIL_ADDRESS" ],
697
+ )
698
+ redact_parser .add_argument (
699
+ "project" ,
700
+ help = "The Google Cloud project id to use as a parent resource." ,
701
+ )
702
+ redact_parser .add_argument (
703
+ "item" ,
704
+ help = "The string to redact."
705
+ "Example: 'My credit card is 4242 4242 4242 4242'" ,
706
+ )
707
+
629
708
args = parser .parse_args ()
630
709
631
710
if args .content == "deid_mask" :
@@ -667,3 +746,9 @@ def write_data(data):
667
746
wrapped_key = args .wrapped_key ,
668
747
key_name = args .key_name ,
669
748
)
749
+ elif args .content == "redact" :
750
+ redact_sensitive_data (
751
+ args .project ,
752
+ item = args .item ,
753
+ info_types = args .info_types ,
754
+ )
0 commit comments