diff --git a/dlp/snippets/inspect_content.py b/dlp/snippets/inspect_content.py index b8a7d5599fe..1123fe89335 100644 --- a/dlp/snippets/inspect_content.py +++ b/dlp/snippets/inspect_content.py @@ -930,6 +930,59 @@ def callback(message): # [END dlp_inspect_bigquery] +# [START dlp_inspect_image_all_infotypes] +def inspect_image_file_all_infotypes( + project, + filename, + include_quote=True, +): + """Uses the Data Loss Prevention API to analyze strings for protected data in image file. + Args: + project: The Google Cloud project id to use as a parent resource. + filename: The path to the file to inspect. + include_quote: Boolean for whether to display a quote of the detected + information in the results. + + Returns: + None; the response from the API is printed to the terminal. + """ + + # Import the client library + import google.cloud.dlp + + # Instantiate a client. + dlp = google.cloud.dlp_v2.DlpServiceClient() + + # Construct the byte_item, containing the image file's byte data. + with open(filename, mode="rb") as f: + byte_item = {"type_": 'IMAGE', "data": f.read()} + + # Convert the project id into a full resource id. + parent = f"projects/{project}" + + # Call the API. + response = dlp.inspect_content( + request={ + "parent": parent, + "inspect_config": {"include_quote": include_quote}, + "item": {"byte_item": byte_item}, + } + ) + + # Print out the results. + print("Findings: ", response.result.findings.count) + if response.result.findings: + for finding in response.result.findings: + print("Quote: {}".format(finding.quote)) + print("Info type: {}".format(finding.info_type.name)) + print("Likelihood: {}".format(finding.likelihood)) + else: + print("No findings.") + + +# [END dlp_inspect_image_all_infotypes] + + if __name__ == "__main__": default_project = os.environ.get("GOOGLE_CLOUD_PROJECT") @@ -1356,6 +1409,26 @@ def callback(message): default=300, ) + parser_image_file = subparsers.add_parser( + "image_all_infotypes", + help="Inspect a local file with all info types." + ) + parser_image_file.add_argument( + "--project", + help="The Google Cloud project id to use as a parent resource.", + default=default_project, + ) + parser_image_file.add_argument( + "filename", + help="The path to the file to inspect." + ) + parser_image_file.add_argument( + "--include_quote", + help="A Boolean for whether to display a quote of the detected" + "information in the results.", + default=True, + ) + args = parser.parse_args() if args.content == "string": @@ -1436,3 +1509,10 @@ def callback(message): max_findings=args.max_findings, timeout=args.timeout, ) + + elif args.content == "image_all_infotypes": + inspect_image_file_all_infotypes( + args.project, + args.filename, + include_quote=args.include_quote, + ) diff --git a/dlp/snippets/inspect_content_test.py b/dlp/snippets/inspect_content_test.py index 5697439e8b1..039e986c540 100644 --- a/dlp/snippets/inspect_content_test.py +++ b/dlp/snippets/inspect_content_test.py @@ -315,6 +315,19 @@ def test_inspect_image_file(capsys): assert "Info type: PHONE_NUMBER" in out +def test_inspect_image_file_all_infotypes(capsys): + test_filepath = os.path.join(RESOURCE_DIRECTORY, "test.png") + + inspect_content.inspect_image_file_all_infotypes( + GCLOUD_PROJECT, + test_filepath + ) + + out, _ = capsys.readouterr() + assert "Info type: PHONE_NUMBER" in out + assert "Info type: EMAIL_ADDRESS" in out + + def delete_dlp_job(out): for line in str(out).split("\n"): if "Job name" in line: