-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Add custom infoType snippets to DLP samples #3991
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Copyright 2020 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Custom infoType snippets. | ||
|
||
This file contains sample code that uses the Data Loss Prevention API to create | ||
custom infoType detectors to refine scan results. | ||
""" | ||
|
||
|
||
# [START dlp_omit_name_if_also_email] | ||
def omit_name_if_also_email( | ||
project, | ||
content_string, | ||
): | ||
"""Marches PERSON_NAME and EMAIL_ADDRESS, but not both. | ||
|
||
Uses the Data Loss Prevention API omit matches on PERSON_NAME if the | ||
EMAIL_ADDRESS detector also matches. | ||
Args: | ||
project: The Google Cloud project id to use as a parent resource. | ||
content_string: The string to inspect. | ||
|
||
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 a list of infoTypes for DLP to locate in `content_string`. See | ||
# https://cloud.google.com/dlp/docs/concepts-infotypes for more information | ||
# about supported infoTypes. | ||
info_types_to_locate = [{"name": "PERSON_NAME"}, {"name": "EMAIL_ADDRESS"}] | ||
|
||
# Construct the configuration dictionary that will only match on PERSON_NAME | ||
# if the EMAIL_ADDRESS doesn't also match. This configuration helps reduce | ||
# the total number of findings when there is a large overlap between different | ||
# infoTypes. | ||
inspect_config = { | ||
"info_types": | ||
info_types_to_locate, | ||
"rule_set": [{ | ||
"info_types": [{ | ||
"name": "PERSON_NAME" | ||
}], | ||
"rules": [{ | ||
"exclusion_rule": { | ||
"exclude_info_types": { | ||
"info_types": [{ | ||
"name": "EMAIL_ADDRESS" | ||
}] | ||
}, | ||
"matching_type": "MATCHING_TYPE_PARTIAL_MATCH" | ||
} | ||
}] | ||
}] | ||
} | ||
|
||
# Construct the `item`. | ||
item = {"value": content_string} | ||
|
||
# Convert the project id into a full resource id. | ||
parent = dlp.project_path(project) | ||
|
||
# Call the API. | ||
response = dlp.inspect_content(parent, inspect_config, item) | ||
|
||
return [f.info_type.name for f in response.result.findings] | ||
|
||
|
||
# [END dlp_omit_name_if_also_email] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Copyright 2020 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the 'License'); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an 'AS IS' BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
|
||
import custom_infotype | ||
|
||
GCLOUD_PROJECT = os.getenv("GCLOUD_PROJECT") | ||
|
||
|
||
def test_omit_name_if_also_email(capsys): | ||
info_types = custom_infotype.omit_name_if_also_email( | ||
GCLOUD_PROJECT, "alice@example.com") | ||
|
||
# Ensure we found only EMAIL_ADDRESS, and not PERSON_NAME. | ||
assert len(info_types) == 1 | ||
assert info_types[0] == "EMAIL_ADDRESS" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.