Skip to content

Commit 6286564

Browse files
[DLP] Implemented dlp_inspect_image_all_infotypes with unit test cases (#9529)
* [dlp_inspect_image_all_infotypes] Implemented with unit test cases * Updated as per review comments * [dlp_inspect_image_all_infotypes] Implemented with unit test cases * Updated as per review comments * Uncommented the commented imports (that was commented by me, to check a few things). --------- Co-authored-by: Karl Weinmeister <[email protected]>
1 parent b05180f commit 6286564

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

dlp/snippets/inspect_content.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,59 @@ def callback(message):
930930
# [END dlp_inspect_bigquery]
931931

932932

933+
# [START dlp_inspect_image_all_infotypes]
934+
def inspect_image_file_all_infotypes(
935+
project,
936+
filename,
937+
include_quote=True,
938+
):
939+
"""Uses the Data Loss Prevention API to analyze strings for protected data in image file.
940+
Args:
941+
project: The Google Cloud project id to use as a parent resource.
942+
filename: The path to the file to inspect.
943+
include_quote: Boolean for whether to display a quote of the detected
944+
information in the results.
945+
946+
Returns:
947+
None; the response from the API is printed to the terminal.
948+
"""
949+
950+
# Import the client library
951+
import google.cloud.dlp
952+
953+
# Instantiate a client.
954+
dlp = google.cloud.dlp_v2.DlpServiceClient()
955+
956+
# Construct the byte_item, containing the image file's byte data.
957+
with open(filename, mode="rb") as f:
958+
byte_item = {"type_": 'IMAGE', "data": f.read()}
959+
960+
# Convert the project id into a full resource id.
961+
parent = f"projects/{project}"
962+
963+
# Call the API.
964+
response = dlp.inspect_content(
965+
request={
966+
"parent": parent,
967+
"inspect_config": {"include_quote": include_quote},
968+
"item": {"byte_item": byte_item},
969+
}
970+
)
971+
972+
# Print out the results.
973+
print("Findings: ", response.result.findings.count)
974+
if response.result.findings:
975+
for finding in response.result.findings:
976+
print("Quote: {}".format(finding.quote))
977+
print("Info type: {}".format(finding.info_type.name))
978+
print("Likelihood: {}".format(finding.likelihood))
979+
else:
980+
print("No findings.")
981+
982+
983+
# [END dlp_inspect_image_all_infotypes]
984+
985+
933986
if __name__ == "__main__":
934987
default_project = os.environ.get("GOOGLE_CLOUD_PROJECT")
935988

@@ -1356,6 +1409,26 @@ def callback(message):
13561409
default=300,
13571410
)
13581411

1412+
parser_image_file = subparsers.add_parser(
1413+
"image_all_infotypes",
1414+
help="Inspect a local file with all info types."
1415+
)
1416+
parser_image_file.add_argument(
1417+
"--project",
1418+
help="The Google Cloud project id to use as a parent resource.",
1419+
default=default_project,
1420+
)
1421+
parser_image_file.add_argument(
1422+
"filename",
1423+
help="The path to the file to inspect."
1424+
)
1425+
parser_image_file.add_argument(
1426+
"--include_quote",
1427+
help="A Boolean for whether to display a quote of the detected"
1428+
"information in the results.",
1429+
default=True,
1430+
)
1431+
13591432
args = parser.parse_args()
13601433

13611434
if args.content == "string":
@@ -1436,3 +1509,10 @@ def callback(message):
14361509
max_findings=args.max_findings,
14371510
timeout=args.timeout,
14381511
)
1512+
1513+
elif args.content == "image_all_infotypes":
1514+
inspect_image_file_all_infotypes(
1515+
args.project,
1516+
args.filename,
1517+
include_quote=args.include_quote,
1518+
)

dlp/snippets/inspect_content_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,19 @@ def test_inspect_image_file(capsys):
315315
assert "Info type: PHONE_NUMBER" in out
316316

317317

318+
def test_inspect_image_file_all_infotypes(capsys):
319+
test_filepath = os.path.join(RESOURCE_DIRECTORY, "test.png")
320+
321+
inspect_content.inspect_image_file_all_infotypes(
322+
GCLOUD_PROJECT,
323+
test_filepath
324+
)
325+
326+
out, _ = capsys.readouterr()
327+
assert "Info type: PHONE_NUMBER" in out
328+
assert "Info type: EMAIL_ADDRESS" in out
329+
330+
318331
def delete_dlp_job(out):
319332
for line in str(out).split("\n"):
320333
if "Job name" in line:

0 commit comments

Comments
 (0)