-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
samples.py
373 lines (303 loc) · 14 KB
/
samples.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/bin/python
# Copyright 2019 Google LLC
#
# 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.
# [START containeranalysis_create_note]
def create_note(note_id, project_id):
"""Creates and returns a new vulnerability note."""
# note_id = 'my-note'
# project_id = 'my-gcp-project'
from grafeas.grafeas_v1 import Version
from google.cloud.devtools import containeranalysis_v1
client = containeranalysis_v1.ContainerAnalysisClient()
grafeas_client = client.get_grafeas_client()
project_name = f"projects/{project_id}"
note = {
'vulnerability': {
'details': [
{
'affected_cpe_uri': 'your-uri-here',
'affected_package': 'your-package-here',
'affected_version_start': {
'kind': Version.VersionKind.MINIMUM
},
'fixed_version': {
'kind': Version.VersionKind.MAXIMUM
}
}
]
}
}
response = grafeas_client.create_note(parent=project_name, note_id=note_id, note=note)
return response
# [END containeranalysis_create_note]
# [START containeranalysis_delete_note]
def delete_note(note_id, project_id):
"""Removes an existing note from the server."""
# note_id = 'my-note'
# project_id = 'my-gcp-project'
from google.cloud.devtools import containeranalysis_v1
client = containeranalysis_v1.ContainerAnalysisClient()
grafeas_client = client.get_grafeas_client()
note_name = f"projects/{project_id}/notes/{note_id}"
grafeas_client.delete_note(name=note_name)
# [END containeranalysis_delete_note]
# [START containeranalysis_create_occurrence]
def create_occurrence(resource_url, note_id, occurrence_project, note_project):
""" Creates and returns a new occurrence of a previously
created vulnerability note."""
# resource_url = 'https://gcr.io/my-project/my-image@sha256:123'
# note_id = 'my-note'
# occurrence_project = 'my-gcp-project'
# note_project = 'my-gcp-project'
from grafeas.grafeas_v1 import Version
from google.cloud.devtools import containeranalysis_v1
client = containeranalysis_v1.ContainerAnalysisClient()
grafeas_client = client.get_grafeas_client()
formatted_note = f"projects/{note_project}/notes/{note_id}"
formatted_project = f"projects/{occurrence_project}"
occurrence = {
'note_name': formatted_note,
'resource_uri': resource_url,
'vulnerability': {
'package_issue': [
{
'affected_cpe_uri': 'your-uri-here',
'affected_package': 'your-package-here',
'affected_version': {
'kind': Version.VersionKind.MINIMUM
},
'fixed_version': {
'kind': Version.VersionKind.MAXIMUM
}
}
]
}
}
return grafeas_client.create_occurrence(parent=formatted_project, occurrence=occurrence)
# [END containeranalysis_create_occurrence]
# [START containeranalysis_delete_occurrence]
def delete_occurrence(occurrence_id, project_id):
"""Removes an existing occurrence from the server."""
# occurrence_id = basename(occurrence.name)
# project_id = 'my-gcp-project'
from google.cloud.devtools import containeranalysis_v1
client = containeranalysis_v1.ContainerAnalysisClient()
grafeas_client = client.get_grafeas_client()
parent = f"projects/{project_id}/occurrences/{occurrence_id}"
grafeas_client.delete_occurrence(name=parent)
# [END containeranalysis_delete_occurrence]
# [START containeranalysis_get_note]
def get_note(note_id, project_id):
"""Retrieves and prints a specified note from the server."""
# note_id = 'my-note'
# project_id = 'my-gcp-project'
from google.cloud.devtools import containeranalysis_v1
client = containeranalysis_v1.ContainerAnalysisClient()
grafeas_client = client.get_grafeas_client()
note_name = f"projects/{project_id}/notes/{note_id}"
response = grafeas_client.get_note(name=note_name)
return response
# [END containeranalysis_get_note]
# [START containeranalysis_get_occurrence]
def get_occurrence(occurrence_id, project_id):
"""retrieves and prints a specified occurrence from the server."""
# occurrence_id = basename(occurrence.name)
# project_id = 'my-gcp-project'
from google.cloud.devtools import containeranalysis_v1
client = containeranalysis_v1.ContainerAnalysisClient()
grafeas_client = client.get_grafeas_client()
parent = f"projects/{project_id}/occurrences/{occurrence_id}"
return grafeas_client.get_occurrence(name=parent)
# [END containeranalysis_get_occurrence]
# [START containeranalysis_discovery_info]
def get_discovery_info(resource_url, project_id):
"""Retrieves and prints the discovery occurrence created for a specified
image. The discovery occurrence contains information about the initial
scan on the image."""
# resource_url = 'https://gcr.io/my-project/my-image@sha256:123'
# project_id = 'my-gcp-project'
from google.cloud.devtools import containeranalysis_v1
filter_str = 'kind="DISCOVERY" AND resourceUrl="{}"'.format(resource_url)
client = containeranalysis_v1.ContainerAnalysisClient()
grafeas_client = client.get_grafeas_client()
project_name = f"projects/{project_id}"
response = grafeas_client.list_occurrences(parent=project_name,
filter_=filter_str)
for occ in response:
print(occ)
# [END containeranalysis_discovery_info]
# [START containeranalysis_occurrences_for_note]
def get_occurrences_for_note(note_id, project_id):
"""Retrieves all the occurrences associated with a specified Note.
Here, all occurrences are printed and counted."""
# note_id = 'my-note'
# project_id = 'my-gcp-project'
from google.cloud.devtools import containeranalysis_v1
client = containeranalysis_v1.ContainerAnalysisClient()
grafeas_client = client.get_grafeas_client()
note_name = f"projects/{project_id}/notes/{note_id}"
response = grafeas_client.list_note_occurrences(name=note_name)
count = 0
for o in response:
# do something with the retrieved occurrence
# in this sample, we will simply count each one
count += 1
return count
# [END containeranalysis_occurrences_for_note]
# [START containeranalysis_occurrences_for_image]
def get_occurrences_for_image(resource_url, project_id):
"""Retrieves all the occurrences associated with a specified image.
Here, all occurrences are simply printed and counted."""
# resource_url = 'https://gcr.io/my-project/my-image@sha256:123'
# project_id = 'my-gcp-project'
from google.cloud.devtools import containeranalysis_v1
filter_str = 'resourceUrl="{}"'.format(resource_url)
client = containeranalysis_v1.ContainerAnalysisClient()
grafeas_client = client.get_grafeas_client()
project_name = f"projects/{project_id}"
response = grafeas_client.list_occurrences(parent=project_name,
filter=filter_str)
count = 0
for o in response:
# do something with the retrieved occurrence
# in this sample, we will simply count each one
count += 1
return count
# [END containeranalysis_occurrences_for_image]
# [START containeranalysis_pubsub]
def pubsub(subscription_id, timeout_seconds, project_id):
"""Respond to incoming occurrences using a Cloud Pub/Sub subscription."""
# subscription_id := 'my-occurrences-subscription'
# timeout_seconds = 20
# project_id = 'my-gcp-project'
import time
from google.cloud.pubsub import SubscriberClient
client = SubscriberClient()
subscription_name = client.subscription_path(project_id, subscription_id)
receiver = MessageReceiver()
client.subscribe(subscription_name, receiver.pubsub_callback)
# listen for 'timeout' seconds
for _ in range(timeout_seconds):
time.sleep(1)
# print and return the number of pubsub messages received
print(receiver.msg_count)
return receiver.msg_count
class MessageReceiver:
"""Custom class to handle incoming Pub/Sub messages."""
def __init__(self):
# initialize counter to 0 on initialization
self.msg_count = 0
def pubsub_callback(self, message):
# every time a pubsub message comes in, print it and count it
self.msg_count += 1
print('Message {}: {}'.format(self.msg_count, message.data))
message.ack()
def create_occurrence_subscription(subscription_id, project_id):
"""Creates a new Pub/Sub subscription object listening to the
Container Analysis Occurrences topic."""
# subscription_id := 'my-occurrences-subscription'
# project_id = 'my-gcp-project'
from google.api_core.exceptions import AlreadyExists
from google.cloud.pubsub import SubscriberClient
topic_id = 'container-analysis-occurrences-v1'
client = SubscriberClient()
topic_name = f"projects/{project_id}/topics/{topic_id}"
subscription_name = client.subscription_path(project_id, subscription_id)
success = True
try:
client.create_subscription({"name": subscription_name, "topic": topic_name})
except AlreadyExists:
# if subscription already exists, do nothing
pass
else:
success = False
return success
# [END containeranalysis_pubsub]
# [START containeranalysis_poll_discovery_occurrence_finished]
def poll_discovery_finished(resource_url, timeout_seconds, project_id):
"""Returns the discovery occurrence for a resource once it reaches a
terminal state."""
# resource_url = 'https://gcr.io/my-project/my-image@sha256:123'
# timeout_seconds = 20
# project_id = 'my-gcp-project'
import time
from grafeas.grafeas_v1 import DiscoveryOccurrence
from google.cloud.devtools import containeranalysis_v1
deadline = time.time() + timeout_seconds
client = containeranalysis_v1.ContainerAnalysisClient()
grafeas_client = client.get_grafeas_client()
project_name = f"projects/{project_id}"
discovery_occurrence = None
while discovery_occurrence is None:
time.sleep(1)
filter_str = 'resourceUrl="{}" \
AND noteProjectId="goog-analysis" \
AND noteId="PACKAGE_VULNERABILITY"'.format(resource_url)
# [END containeranalysis_poll_discovery_occurrence_finished]
# The above filter isn't testable, since it looks for occurrences in a
# locked down project fall back to a more permissive filter for testing
filter_str = 'kind="DISCOVERY" AND resourceUrl="{}"'\
.format(resource_url)
# [START containeranalysis_poll_discovery_occurrence_finished]
result = grafeas_client.list_occurrences(parent=project_name, filter=filter_str)
# only one occurrence should ever be returned by ListOccurrences
# and the given filter
for item in result:
discovery_occurrence = item
if time.time() > deadline:
raise RuntimeError('timeout while retrieving discovery occurrence')
status = DiscoveryOccurrence.AnalysisStatus.PENDING
while status != DiscoveryOccurrence.AnalysisStatus.FINISHED_UNSUPPORTED \
and status != DiscoveryOccurrence.AnalysisStatus.FINISHED_FAILED \
and status != DiscoveryOccurrence.AnalysisStatus.FINISHED_SUCCESS:
time.sleep(1)
updated = grafeas_client.get_occurrence(name=discovery_occurrence.name)
status = updated.discovery.analysis_status
if time.time() > deadline:
raise RuntimeError('timeout while waiting for terminal state')
return discovery_occurrence
# [END containeranalysis_poll_discovery_occurrence_finished]
# [START containeranalysis_vulnerability_occurrences_for_image]
def find_vulnerabilities_for_image(resource_url, project_id):
""""Retrieves all vulnerability occurrences associated with a resource."""
# resource_url = 'https://gcr.io/my-project/my-image@sha256:123'
# project_id = 'my-gcp-project'
from google.cloud.devtools import containeranalysis_v1
client = containeranalysis_v1.ContainerAnalysisClient()
grafeas_client = client.get_grafeas_client()
project_name = f"projects/{project_id}"
filter_str = 'kind="VULNERABILITY" AND resourceUrl="{}"'\
.format(resource_url)
return list(grafeas_client.list_occurrences(parent=project_name, filter=filter_str))
# [END containeranalysis_vulnerability_occurrences_for_image]
# [START containeranalysis_filter_vulnerability_occurrences]
def find_high_severity_vulnerabilities_for_image(resource_url, project_id):
"""Retrieves a list of only high vulnerability occurrences associated
with a resource."""
# resource_url = 'https://gcr.io/my-project/my-image@sha256:123'
# project_id = 'my-gcp-project'
from grafeas.grafeas_v1 import Severity
from google.cloud.devtools import containeranalysis_v1
client = containeranalysis_v1.ContainerAnalysisClient()
grafeas_client = client.get_grafeas_client()
project_name = f"projects/{project_id}"
filter_str = 'kind="VULNERABILITY" AND resourceUrl="{}"'\
.format(resource_url)
vulnerabilities = grafeas_client.list_occurrences(parent=project_name, filter=filter_str)
filtered_list = []
for v in vulnerabilities:
if v.vulnerability.effective_severity == Severity.HIGH or v.vulnerability.effective_severity == Severity.CRITICAL:
filtered_list.append(v)
return filtered_list
# [END containeranalysis_filter_vulnerability_occurrences]