Last active
July 12, 2022 05:35
-
-
Save nownabe/cd2867bbea7233e3f3aa4b049689defe to your computer and use it in GitHub Desktop.
Sample for DLP using templates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import environ | |
from google.cloud import dlp_v2 as dlp | |
client = dlp.DlpServiceClient() | |
def create_inspect_template(template_id: str): | |
""" | |
Create inspectTemplate | |
https://cloud.google.com/dlp/docs/concepts-templates | |
https://cloud.google.com/dlp/docs/creating-templates-inspect | |
API References | |
- projects.inspectTemplates.create | |
https://cloud.google.com/dlp/docs/reference/rest/v2/projects.inspectTemplates/create | |
- InspectTemplate | |
https://cloud.google.com/dlp/docs/reference/rest/v2/projects.inspectTemplates#InspectTemplate | |
- InspectConfig | |
https://cloud.google.com/dlp/docs/reference/rest/v2/InspectConfig | |
""" | |
custom_info_types = [ | |
{ | |
"info_type": {"name": "NG_WORDS_01"}, | |
"likelihood": dlp.Likelihood.VERY_LIKELY, | |
"regex": {"pattern": "NG1|NG2|NG3"}, | |
}, | |
{ | |
"info_type": {"name": "NG_WORDS_02"}, | |
"likelihood": dlp.Likelihood.VERY_LIKELY, | |
"regex": {"pattern": "NG4|NG5|NG6"} | |
} | |
] | |
inspect_config = { | |
"min_likelihood": dlp.Likelihood.LIKELIHOOD_UNSPECIFIED, | |
"custom_info_types": custom_info_types, | |
} | |
inspect_template = { | |
"display_name": "my-template-1", | |
"description": "test inspect template", | |
"inspect_config": inspect_config, | |
} | |
response = client.create_inspect_template( | |
request={ | |
"parent": f"projects/{environ['PROJECT_ID']}", | |
"inspect_template": inspect_template, | |
"template_id": template_id, | |
} | |
) | |
print() | |
print("================ create_inspect_template response ================") | |
print() | |
print(response) | |
print() | |
return response.name | |
def inspect_content(template_name: str, content: str): | |
""" | |
Inspect content (Inspect text) | |
https://cloud.google.com/dlp/docs/inspecting-text | |
API References | |
- projects.content.inspect | |
https://cloud.google.com/dlp/docs/reference/rest/v2/projects.content/inspect | |
- ContentItem | |
https://cloud.google.com/dlp/docs/reference/rest/v2/ContentItem | |
""" | |
content_item = {"value": content} | |
response = client.inspect_content( | |
request={ | |
"parent": f"projects/{environ['PROJECT_ID']}", | |
"item": content_item, | |
"inspect_template_name": template_name, | |
} | |
) | |
print() | |
print("================ inspect_content response ================") | |
print() | |
print(response) | |
print() | |
if __name__ == "__main__": | |
template_name = create_inspect_template("my-template-1") | |
inspect_content(template_name, "この文はNG1が含まれています。") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import environ | |
from google.cloud import dlp_v2 as dlp | |
client = dlp.DlpServiceClient() | |
job_name = f"projects/{environ['PROJECT_ID']}/dlpJobs/my-job" | |
# https://cloud.google.com/dlp/docs/reference/rest/v2/projects.dlpJobs/get | |
# https://googleapis.dev/python/dlp/latest/dlp_v2/dlp_service.html#google.cloud.dlp_v2.services.dlp_service.DlpServiceClient.get_dlp_job | |
job = client.get_dlp_job(request={"name": job_name}) | |
info_type_stats = job.inspect_details.result.info_type_stats | |
print(sum([stat.count for stat in info_type_stats])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment