Skip to content
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

Add samples for DLP API v2beta1 #1369

Merged
merged 11 commits into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add argparse
  • Loading branch information
andrewsg committed Jan 4, 2018
commit 6bedaa80ff926efc104adb5679956934410343ad
35 changes: 34 additions & 1 deletion dlp/inspect_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Sample app that uses the Data Loss Prevent API to inspect a file."""

from __future__ import print_function

import argparse


# [START inspect_file]
def inspect_file(filename, info_types=None, min_likelihood=None,
Expand Down Expand Up @@ -89,4 +93,33 @@ def inspect_file(filename, info_types=None, min_likelihood=None,


if __name__ == '__main__':
inspect_file("/usr/local/google/home/gorcester/Downloads/wQOVLom8Gsa.png", ["EMAIL_ADDRESS", "US_MALE_NAME", "US_FEMALE_NAME"])
parser = argparse.ArgumentParser(
description=__doc__)
parser.add_argument('filename', help='The path to the file to inspect.')
parser.add_argument('--info_types', action='append',
help='Strings representing info types to look for. A full list of info '
'categories and types is available from the API. Examples '
'include "US_MALE_NAME", "US_FEMALE_NAME", "EMAIL_ADDRESS", '
'"CANADA_SOCIAL_INSURANCE_NUMBER", "JAPAN_PASSPORT". If omitted, '
'the API will use a limited default set. Specify this flag '
'multiple times to specify multiple info types.')
parser.add_argument('--min_likelihood',
choices=['LIKELIHOOD_UNSPECIFIED', 'VERY_UNLIKELY', 'UNLIKELY',
'POSSIBLE', 'LIKELY', 'VERY_LIKELY'],
help='A string representing the minimum likelihood threshold that '
'constitutes a match.')
parser.add_argument('--max_findings', type=int,
help='The maximum number of findings to report; 0 = no maximum.')
parser.add_argument('--include_quote', type=bool,
help='A boolean for whether to display a quote of the detected '
'information in the results.')
parser.add_argument('--mime_type',
help='The MIME type of the file. If not specified, the type is '
'inferred via the Python standard library\'s mimetypes module.')

args = parser.parse_args()

inspect_file(
args.filename, info_types=args.info_types,
min_likelihood=args.min_likelihood, include_quote=args.include_quote,
mime_type=args.mime_type)
39 changes: 37 additions & 2 deletions dlp/inspect_gcs_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Sample app that uses the Data Loss Prevent API to inspect a file on Google
Cloud Storage."""


from __future__ import print_function

import argparse


# [START inspect_gcs_file]
def inspect_gcs_file(bucket, filename, info_types=None, min_likelihood=None,
Expand Down Expand Up @@ -88,6 +94,35 @@ def inspect_gcs_file(bucket, filename, info_types=None, min_likelihood=None,
print('No findings.')
# [END inspect_gcs_file]


if __name__ == '__main__':
# inspect_gcs_file('andrewsg-test', 'wQOVLom8Gsa.png', ["EMAIL_ADDRESS", "US_MALE_NAME", "US_FEMALE_NAME"])
inspect_gcs_file('nodejs-docs-samples-dlp', 'test.txt', ["EMAIL_ADDRESS", "PHONE_NUMBER"])
parser = argparse.ArgumentParser(
description=__doc__)
parser.add_argument('bucket',
help='The name of the GCS bucket containing the file.')
parser.add_argument('filename',
help='The name of the file in the bucket, including the path, e.g. '
'"images/myfile.png".')
parser.add_argument('--info_types', action='append',
help='Strings representing info types to look for. A full list of info '
'categories and types is available from the API. Examples '
'include "US_MALE_NAME", "US_FEMALE_NAME", "EMAIL_ADDRESS", '
'"CANADA_SOCIAL_INSURANCE_NUMBER", "JAPAN_PASSPORT". If omitted, '
'the API will use a limited default set. Specify this flag '
'multiple times to specify multiple info types.')
parser.add_argument('--min_likelihood',
choices=['LIKELIHOOD_UNSPECIFIED', 'VERY_UNLIKELY', 'UNLIKELY',
'POSSIBLE', 'LIKELY', 'VERY_LIKELY'],
help='A string representing the minimum likelihood threshold that '
'constitutes a match.')
parser.add_argument('--max_findings', type=int,
help='The maximum number of findings to report; 0 = no maximum.')
parser.add_argument('--include_quote', type=bool,
help='A boolean for whether to display a quote of the detected '
'information in the results.')

args = parser.parse_args()

inspect_gcs_file(
args.bucket, args.filename, info_types=args.info_types,
min_likelihood=args.min_likelihood, include_quote=args.include_quote)
1 change: 1 addition & 0 deletions dlp/inspect_gcs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def bucket(request):
# Yield the object to the test code; lines after this execute as a teardown.
yield bucket

# Delete the files.
for blob in blobs:
blob.delete()

Expand Down
32 changes: 30 additions & 2 deletions dlp/inspect_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Sample app that uses the Data Loss Prevent API to inspect a string."""

from __future__ import print_function

import argparse


# [START inspect_string]
def inspect_string(item, info_types=None, min_likelihood=None,
Expand Down Expand Up @@ -79,5 +83,29 @@ def inspect_string(item, info_types=None, min_likelihood=None,


if __name__ == '__main__':
inspect_string("I'm Gary and my email is [email protected]", ["EMAIL_ADDRESS", "US_MALE_NAME", "US_FEMALE_NAME"])
# DO NOT SUBMIT
parser = argparse.ArgumentParser(
description=__doc__)
parser.add_argument('item', help='The string to inspect.')
parser.add_argument('--info_types', action='append',
help='Strings representing info types to look for. A full list of info '
'categories and types is available from the API. Examples '
'include "US_MALE_NAME", "US_FEMALE_NAME", "EMAIL_ADDRESS", '
'"CANADA_SOCIAL_INSURANCE_NUMBER", "JAPAN_PASSPORT". If omitted, '
'the API will use a limited default set. Specify this flag '
'multiple times to specify multiple info types.')
parser.add_argument('--min_likelihood',
choices=['LIKELIHOOD_UNSPECIFIED', 'VERY_UNLIKELY', 'UNLIKELY',
'POSSIBLE', 'LIKELY', 'VERY_LIKELY'],
help='A string representing the minimum likelihood threshold that '
'constitutes a match.')
parser.add_argument('--max_findings', type=int,
help='The maximum number of findings to report; 0 = no maximum.')
parser.add_argument('--include_quote', type=bool,
help='A boolean for whether to display a quote of the detected '
'information in the results.')

args = parser.parse_args()

inspect_string(
args.item, info_types=args.info_types,
min_likelihood=args.min_likelihood, include_quote=args.include_quote)