Skip to content

[DLP] Corrected Region tags for 6 DLP samples and made related code changes #10273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
31 changes: 14 additions & 17 deletions dlp/snippets/custom_infotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,11 @@ def inspect_string_custom_omit_overlap(project: str, content_string: str) -> Non

# [END dlp_inspect_string_custom_omit_overlap]

# [START dlp_omit_name_if_also_email]
# [START dlp_inspect_string_omit_overlap]
import google.cloud.dlp # noqa: F811, E402


def omit_name_if_also_email(
def inspect_string_omit_overlap(
project: str,
content_string: str,
) -> None:
Expand Down Expand Up @@ -486,7 +486,7 @@ def omit_name_if_also_email(
print("No findings.")


# [END dlp_omit_name_if_also_email]
# [END dlp_inspect_string_omit_overlap]


# [START dlp_inspect_string_without_overlap]
Expand Down Expand Up @@ -574,25 +574,22 @@ def inspect_string_without_overlap(project: str, content_string: str) -> None:
# [END dlp_inspect_string_without_overlap]


# [START dlp_inspect_with_person_name_w_custom_hotword]
# [START dlp_inspect_string_custom_hotword]
import google.cloud.dlp # noqa: F811, E402


def inspect_with_person_name_w_custom_hotword(
def inspect_string_w_custom_hotword(
project: str, content_string: str, custom_hotword: str = "patient"
) -> None:
"""Uses the Data Loss Prevention API increase likelihood for matches on
PERSON_NAME if the user specified custom hotword is present. Only
PERSON_NAME if the user specified custom hot-word is present. Only
includes findings with the increased likelihood by setting a minimum
likelihood threshold of VERY_LIKELY.

Args:
project: The Google Cloud project id to use as a parent resource.
content_string: The string to inspect.
custom_hotword: The custom hotword used for likelihood boosting.

Returns:
None; the response from the API is printed to the terminal.
custom_hotword: The custom hot-word used for likelihood boosting.
"""

# Instantiate a client.
Expand Down Expand Up @@ -644,7 +641,7 @@ def inspect_with_person_name_w_custom_hotword(
print("No findings.")


# [END dlp_inspect_with_person_name_w_custom_hotword]
# [END dlp_inspect_string_custom_hotword]


# [START dlp_inspect_string_multiple_rules]
Expand Down Expand Up @@ -736,11 +733,11 @@ def inspect_string_multiple_rules(project: str, content_string: str) -> None:
# [END dlp_inspect_string_multiple_rules]


# [START dlp_inspect_with_medical_record_number_custom_regex_detector]
# [START dlp_inspect_custom_regex]
import google.cloud.dlp # noqa: E402, F811


def inspect_with_medical_record_number_custom_regex_detector(
def inspect_data_with_custom_regex_detector(
project: str,
content_string: str,
) -> None:
Expand Down Expand Up @@ -796,14 +793,14 @@ def inspect_with_medical_record_number_custom_regex_detector(
print("No findings.")


# [END dlp_inspect_with_medical_record_number_custom_regex_detector]
# [END dlp_inspect_custom_regex]


# [START dlp_inspect_with_medical_record_number_w_custom_hotwords]
# [START dlp_inspect_hotword_rule]
import google.cloud.dlp # noqa: F811, E402


def inspect_with_medical_record_number_w_custom_hotwords(
def inspect_data_w_custom_hotwords(
project: str,
content_string: str,
) -> None:
Expand Down Expand Up @@ -876,4 +873,4 @@ def inspect_with_medical_record_number_w_custom_hotwords(
print("No findings.")


# [END dlp_inspect_with_medical_record_number_w_custom_hotwords]
# [END dlp_inspect_hotword_rule]
16 changes: 8 additions & 8 deletions dlp/snippets/custom_infotype_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def test_inspect_string_custom_omit_overlap(capsys: pytest.LogCaptureFixture) ->
assert "John Doe" in out


def test_omit_name_if_also_email(capsys: pytest.LogCaptureFixture) -> None:
custom_infotype.omit_name_if_also_email(GCLOUD_PROJECT, "[email protected]")
def test_inspect_string_omit_overlap(capsys: pytest.LogCaptureFixture) -> None:
custom_infotype.inspect_string_omit_overlap(GCLOUD_PROJECT, "[email protected]")

# Ensure we found only EMAIL_ADDRESS, and not PERSON_NAME.
out, _ = capsys.readouterr()
Expand All @@ -95,10 +95,10 @@ def test_inspect_string_without_overlap(capsys: pytest.LogCaptureFixture) -> Non
assert "example.org" not in out


def test_inspect_with_person_name_w_custom_hotword(
def test_inspect_string_w_custom_hotword(
capsys: pytest.LogCaptureFixture,
) -> None:
custom_infotype.inspect_with_person_name_w_custom_hotword(
custom_infotype.inspect_string_w_custom_hotword(
GCLOUD_PROJECT, "patient's name is John Doe.", "patient"
)

Expand Down Expand Up @@ -147,10 +147,10 @@ def test_inspect_string_multiple_rules_redacted(
assert "No findings" in out


def test_inspect_with_medical_record_number_custom_regex_detector(
def test_inspect_data_with_custom_regex_detector(
capsys: pytest.LogCaptureFixture,
) -> None:
custom_infotype.inspect_with_medical_record_number_custom_regex_detector(
custom_infotype.inspect_data_with_custom_regex_detector(
GCLOUD_PROJECT, "Patients MRN 444-5-22222"
)

Expand All @@ -161,7 +161,7 @@ def test_inspect_with_medical_record_number_custom_regex_detector(
def test_inspect_with_medical_record_number_w_custom_hotwords_no_hotwords(
capsys: pytest.LogCaptureFixture,
) -> None:
custom_infotype.inspect_with_medical_record_number_w_custom_hotwords(
custom_infotype.inspect_data_w_custom_hotwords(
GCLOUD_PROJECT, "just a number 444-5-22222"
)

Expand All @@ -173,7 +173,7 @@ def test_inspect_with_medical_record_number_w_custom_hotwords_no_hotwords(
def test_inspect_with_medical_record_number_w_custom_hotwords_has_hotwords(
capsys: pytest.LogCaptureFixture,
) -> None:
custom_infotype.inspect_with_medical_record_number_w_custom_hotwords(
custom_infotype.inspect_data_w_custom_hotwords(
GCLOUD_PROJECT, "Patients MRN 444-5-22222"
)

Expand Down
36 changes: 24 additions & 12 deletions dlp/snippets/inspect_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,26 @@
import json
import os

from typing import List

# [START dlp_inspect_string_basic]
# [START dlp_inspect_phone_number]
import google.cloud.dlp


def inspect_string_basic(
def inspect_phone_number(
project: str,
content_string: str,
info_types: List[str] = ["PHONE_NUMBER"],
) -> None:
"""Uses the Data Loss Prevention API to analyze strings for protected data.
Args:
project: The Google Cloud project id to use as a parent resource.
content_string: The string to inspect.
info_types: A list of strings representing info types to look for.
A full list of info type categories can be fetched from the API.
Returns:
None; the response from the API is printed to the terminal.
content_string: The string to inspect phone number from.
"""

# Instantiate a client.
dlp = google.cloud.dlp_v2.DlpServiceClient()

# Prepare info_types by converting the list of strings into a list of
# dictionaries (protos are also accepted).
info_types = [{"name": info_type} for info_type in info_types]
info_types = [{"name": "PHONE_NUMBER"}]

# Construct the configuration dictionary.
inspect_config = {
Expand Down Expand Up @@ -75,7 +68,7 @@ def inspect_string_basic(
print("No findings.")


# [END dlp_inspect_string_basic]
# [END dlp_inspect_phone_number]


# [START dlp_inspect_string]
Expand Down Expand Up @@ -1416,6 +1409,20 @@ def inspect_data_to_hybrid_job_trigger(
)
subparsers.required = True

parser_phone_number = subparsers.add_parser(
"phone_number",
help="Inspect phone number in a string.",
)
parser_phone_number.add_argument(
"content_string",
help="The string to inspect phone number from.",
)
parser_phone_number.add_argument(
"--project",
help="The Google Cloud project id to use as a parent resource.",
default=default_project,
)

parser_string = subparsers.add_parser("string", help="Inspect a string.")
parser_string.add_argument("item", help="The string to inspect.")
parser_string.add_argument(
Expand Down Expand Up @@ -2019,6 +2026,11 @@ def inspect_data_to_hybrid_job_trigger(
max_findings=args.max_findings,
include_quote=args.include_quote,
)
elif args.content == "phone_number":
inspect_phone_number(
args.project,
args.content_string
)
elif args.content == "table":
inspect_table(
args.project,
Expand Down
4 changes: 2 additions & 2 deletions dlp/snippets/inspect_content_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ def cleanup() -> None:
cleanup()


def test_inspect_string_basic(capsys: pytest.CaptureFixture) -> None:
def test_inspect_phone_number(capsys: pytest.CaptureFixture) -> None:
test_string = "String with a phone number: 234-555-6789"

inspect_content.inspect_string_basic(GCLOUD_PROJECT, test_string)
inspect_content.inspect_phone_number(GCLOUD_PROJECT, test_string)

out, _ = capsys.readouterr()
assert "Info type: PHONE_NUMBER" in out
Expand Down
4 changes: 2 additions & 2 deletions dlp/snippets/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def create_inspect_template(
# [END dlp_create_inspect_template]


# [START dlp_list_templates]
# [START dlp_list_inspect_templates]
import google.cloud.dlp # noqa: E402, F811


Expand Down Expand Up @@ -132,7 +132,7 @@ def list_inspect_templates(project: str) -> None:
)


# [END dlp_list_templates]
# [END dlp_list_inspect_templates]


# [START dlp_delete_inspect_template]
Expand Down