forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
triggers.py
290 lines (244 loc) · 9.69 KB
/
triggers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# Copyright 2017 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.
"""Sample app that sets up Data Loss Prevention API automation triggers."""
from __future__ import print_function
import argparse
import os
import time
# [START dlp_create_trigger]
def create_trigger(
project,
bucket,
scan_period_days,
info_types,
trigger_id=None,
display_name=None,
description=None,
min_likelihood=None,
max_findings=None,
auto_populate_timespan=False,
):
"""Creates a scheduled Data Loss Prevention API inspect_content trigger.
Args:
project: The Google Cloud project id to use as a parent resource.
bucket: The name of the GCS bucket to scan. This sample scans all
files in the bucket using a wildcard.
scan_period_days: How often to repeat the scan, in days.
The minimum is 1 day.
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.
trigger_id: The id of the trigger. If omitted, an id will be randomly
generated.
display_name: The optional display name of the trigger.
description: The optional description of the trigger.
min_likelihood: A string representing the minimum likelihood threshold
that constitutes a match. One of: 'LIKELIHOOD_UNSPECIFIED',
'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE', 'LIKELY', 'VERY_LIKELY'.
max_findings: The maximum number of findings to report; 0 = no maximum.
auto_populate_timespan: Automatically populates time span config start
and end times in order to scan new content only.
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()
# 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]
# Construct the configuration dictionary. Keys which are None may
# optionally be omitted entirely.
inspect_config = {
"info_types": info_types,
"min_likelihood": min_likelihood,
"limits": {"max_findings_per_request": max_findings},
}
# Construct a cloud_storage_options dictionary with the bucket's URL.
url = "gs://{}/*".format(bucket)
storage_config = {
"cloud_storage_options": {"file_set": {"url": url}},
# Time-based configuration for each storage object.
"timespan_config": {
# Auto-populate start and end times in order to scan new objects
# only.
"enable_auto_population_of_timespan_config": auto_populate_timespan
},
}
# Construct the job definition.
job = {"inspect_config": inspect_config, "storage_config": storage_config}
# Construct the schedule definition:
schedule = {
"recurrence_period_duration": {"seconds": scan_period_days * 60 * 60 * 24}
}
# Construct the trigger definition.
job_trigger = {
"inspect_job": job,
"display_name": display_name,
"description": description,
"triggers": [{"schedule": schedule}],
"status": "HEALTHY",
}
# Convert the project id into a full resource id.
parent = dlp.project_path(project)
# Call the API.
response = dlp.create_job_trigger(
parent, job_trigger=job_trigger, trigger_id=trigger_id
)
print("Successfully created trigger {}".format(response.name))
# [END dlp_create_trigger]
# [START dlp_list_triggers]
def list_triggers(project):
"""Lists all Data Loss Prevention API triggers.
Args:
project: The Google Cloud project id to use as a parent resource.
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()
# Convert the project id into a full resource id.
parent = dlp.project_path(project)
# Call the API.
response = dlp.list_job_triggers(parent)
# Define a helper function to convert the API's "seconds since the epoch"
# time format into a human-readable string.
def human_readable_time(timestamp):
return str(time.localtime(timestamp.seconds))
for trigger in response:
print("Trigger {}:".format(trigger.name))
print(" Created: {}".format(human_readable_time(trigger.create_time)))
print(" Updated: {}".format(human_readable_time(trigger.update_time)))
if trigger.display_name:
print(" Display Name: {}".format(trigger.display_name))
if trigger.description:
print(" Description: {}".format(trigger.discription))
print(" Status: {}".format(trigger.status))
print(" Error count: {}".format(len(trigger.errors)))
# [END dlp_list_triggers]
# [START dlp_delete_trigger]
def delete_trigger(project, trigger_id):
"""Deletes a Data Loss Prevention API trigger.
Args:
project: The id of the Google Cloud project which owns the trigger.
trigger_id: The id of the trigger to delete.
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()
# Convert the project id into a full resource id.
parent = dlp.project_path(project)
# Combine the trigger id with the parent id.
trigger_resource = "{}/jobTriggers/{}".format(parent, trigger_id)
# Call the API.
dlp.delete_job_trigger(trigger_resource)
print("Trigger {} successfully deleted.".format(trigger_resource))
# [END dlp_delete_triggers]
if __name__ == "__main__":
default_project = os.environ.get("GCLOUD_PROJECT")
parser = argparse.ArgumentParser(description=__doc__)
subparsers = parser.add_subparsers(
dest="action", help="Select which action to perform."
)
subparsers.required = True
parser_create = subparsers.add_parser("create", help="Create a trigger.")
parser_create.add_argument(
"bucket", help="The name of the GCS bucket containing the file."
)
parser_create.add_argument(
"scan_period_days",
type=int,
help="How often to repeat the scan, in days. The minimum is 1 day.",
)
parser_create.add_argument(
"--trigger_id",
help="The id of the trigger. If omitted, an id will be randomly " "generated",
)
parser_create.add_argument(
"--display_name", help="The optional display name of the trigger."
)
parser_create.add_argument(
"--description", help="The optional description of the trigger."
)
parser_create.add_argument(
"--project",
help="The Google Cloud project id to use as a parent resource.",
default=default_project,
)
parser_create.add_argument(
"--info_types",
nargs="+",
help="Strings representing info types to look for. A full list of "
"info categories and types is available from the API. Examples "
'include "FIRST_NAME", "LAST_NAME", "EMAIL_ADDRESS". '
"If unspecified, the three above examples will be used.",
default=["FIRST_NAME", "LAST_NAME", "EMAIL_ADDRESS"],
)
parser_create.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_create.add_argument(
"--max_findings",
type=int,
help="The maximum number of findings to report; 0 = no maximum.",
)
parser_create.add_argument(
"--auto_populate_timespan", type=bool, help="Limit scan to new content only."
)
parser_list = subparsers.add_parser("list", help="List all triggers.")
parser_list.add_argument(
"--project",
help="The Google Cloud project id to use as a parent resource.",
default=default_project,
)
parser_delete = subparsers.add_parser("delete", help="Delete a trigger.")
parser_delete.add_argument("trigger_id", help="The id of the trigger to delete.")
parser_delete.add_argument(
"--project",
help="The Google Cloud project id to use as a parent resource.",
default=default_project,
)
args = parser.parse_args()
if args.action == "create":
create_trigger(
args.project,
args.bucket,
args.scan_period_days,
args.info_types,
trigger_id=args.trigger_id,
display_name=args.display_name,
description=args.description,
min_likelihood=args.min_likelihood,
max_findings=args.max_findings,
auto_populate_timespan=args.auto_populate_timespan,
)
elif args.action == "list":
list_triggers(args.project)
elif args.action == "delete":
delete_trigger(args.project, args.trigger_id)