This repository has been archived by the owner on Sep 5, 2023. It is now read-only.
/
snippets_findings.py
635 lines (533 loc) · 21.8 KB
/
snippets_findings.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#!/usr/bin/env 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
#
# https://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.
"""Examples of working with source and findings in Cloud Security Command Center."""
def create_source(organization_id):
"""Create a new findings source. """
# [START securitycenter_create_source]
# [START create_source]
from google.cloud import securitycenter
client = securitycenter.SecurityCenterClient()
# organization_id is the numeric ID of the organization. e.g.:
# organization_id = "111122222444"
org_name = "organizations/{org_id}".format(org_id=organization_id)
created = client.create_source(
request={
"parent": org_name,
"source": {
"display_name": "Customized Display Name",
"description": "A new custom source that does X",
},
}
)
print("Created Source: {}".format(created.name))
# [END create_source]
# [END securitycenter_create_source]
def get_source(source_name):
"""Gets an existing source."""
# [START securitycenter_get_source]
# [START get_source]
from google.cloud import securitycenter
client = securitycenter.SecurityCenterClient()
# source_name is the resource path for a source that has been
# created previously (you can use list_sources to find a specific one).
# Its format is:
# source_name = "organizations/{organization_id}/sources/{source_id}"
# e.g.:
# source_name = "organizations/111122222444/sources/1234"
source = client.get_source(request={"name": source_name})
print("Source: {}".format(source))
# [END get_source]
# [END securitycenter_get_source]
return source
def update_source(source_name):
"""Updates a source's display name."""
# [START securitycenter_update_source]
# [START update_source]
from google.cloud import securitycenter
from google.protobuf import field_mask_pb2
client = securitycenter.SecurityCenterClient()
# Field mask to only update the display name.
field_mask = field_mask_pb2.FieldMask(paths=["display_name"])
# source_name is the resource path for a source that has been
# created previously (you can use list_sources to find a specific one).
# Its format is:
# source_name = "organizations/{organization_id}/sources/{source_id}"
# e.g.:
# source_name = "organizations/111122222444/sources/1234"
updated = client.update_source(
request={
"source": {"name": source_name, "display_name": "Updated Display Name"},
"update_mask": field_mask,
}
)
print("Updated Source: {}".format(updated))
# [END update_source]
# [END securitycenter_update_source]
return updated
def add_user_to_source(source_name):
"""Gives a user findingsEditor permission to the source."""
user_email = "csccclienttest@gmail.com"
# [START securitycenter_set_source_iam]
# [START update_source_iam]
from google.cloud import securitycenter
from google.iam.v1 import policy_pb2
client = securitycenter.SecurityCenterClient()
# source_name is the resource path for a source that has been
# created previously (you can use list_sources to find a specific one).
# Its format is:
# source_name = "organizations/{organization_id}/sources/{source_id}"
# e.g.:
# source_name = "organizations/111122222444/sources/1234"
# Get the old policy so we can do an incremental update.
old_policy = client.get_iam_policy(request={"resource": source_name})
print("Old Policy: {}".format(old_policy))
# Setup a new IAM binding.
binding = policy_pb2.Binding()
binding.role = "roles/securitycenter.findingsEditor"
# user_email is an e-mail address known to Cloud IAM (e.g. a gmail address).
# user_mail = user@somedomain.com
binding.members.append("user:{}".format(user_email))
# Setting the e-tag avoids over-write existing policy
updated = client.set_iam_policy(
request={
"resource": source_name,
"policy": {"etag": old_policy.etag, "bindings": [binding]},
}
)
print("Updated Policy: {}".format(updated))
# [END update_source_iam]
# [END securitycenter_set_source_iam]
return binding, updated
def list_source(organization_id):
"""Lists finding sources."""
i = -1
# [START securitycenter_list_sources]
# [START list_sources]
from google.cloud import securitycenter
# Create a new client.
client = securitycenter.SecurityCenterClient()
# organization_id is the numeric ID of the organization. e.g.:
# organization_id = "111122222444"
org_name = "organizations/{org_id}".format(org_id=organization_id)
# Call the API and print out each existing source.
for i, source in enumerate(client.list_sources(request={"parent": org_name})):
print(i, source)
# [END list_sources]
# [END securitycenter_list_sources]
return i
def create_finding(source_name):
"""Creates a new finding."""
# [START securitycenter_create_finding]
# [START create_finding]
from google.cloud import securitycenter
from google.cloud.securitycenter_v1 import CreateFindingRequest, Finding
import datetime
# Create a new client.
client = securitycenter.SecurityCenterClient()
# Use the current time as the finding "event time".
event_time = datetime.datetime.now()
# source_name is the resource path for a source that has been
# created previously (you can use list_sources to find a specific one).
# Its format is:
# source_name = "organizations/{organization_id}/sources/{source_id}"
# e.g.:
# source_name = "organizations/111122222444/sources/1234"
# Controlled by caller.
finding_id = "samplefindingid"
# The resource this finding applies to. The CSCC UI can link
# the findings for a resource to the corresponding Asset of a resource
# if there are matches.
resource_name = "//cloudresourcemanager.googleapis.com/organizations/11232"
finding = Finding(
state=Finding.State.ACTIVE,
resource_name=resource_name,
category="MEDIUM_RISK_ONE",
event_time=event_time,
)
request = CreateFindingRequest(
parent=source_name,
finding_id=finding_id,
finding=finding,
)
# Call The API.
created_finding = client.create_finding(
request=request
)
print(created_finding)
# [END create_finding]
# [END securitycenter_create_finding]
return created_finding
def create_finding_with_source_properties(source_name):
"""Demonstrate creating a new finding with source properties. """
# [START securitycenter_create_finding_with_source_properties]
# [START create_finding_with_properties]
import datetime
from google.cloud import securitycenter
from google.cloud.securitycenter_v1 import Finding
from google.protobuf.struct_pb2 import Value
# Create a new client.
client = securitycenter.SecurityCenterClient()
# source_name is the resource path for a source that has been
# created previously (you can use list_sources to find a specific one).
# Its format is:
# source_name = "organizations/{organization_id}/sources/{source_id}"
# e.g.:
# source_name = "organizations/111122222444/sources/1234"
# Controlled by caller.
finding_id = "samplefindingid2"
# The resource this finding applies to. The CSCC UI can link
# the findings for a resource to the corresponding Asset of a resource
# if there are matches.
resource_name = "//cloudresourcemanager.googleapis.com/organizations/11232"
# Define source properties values as protobuf "Value" objects.
str_value = Value()
str_value.string_value = "string_example"
num_value = Value()
num_value.number_value = 1234
# Use the current time as the finding "event time".
event_time = datetime.datetime.now()
finding = Finding(
state=Finding.State.ACTIVE,
resource_name=resource_name,
category="MEDIUM_RISK_ONE",
source_properties={"s_value": "string_example", "n_value": 1234},
event_time=event_time,
)
created_finding = client.create_finding(
request={
"parent": source_name,
"finding_id": finding_id,
"finding": finding
}
)
print(created_finding)
# [END create_finding_with_properties]
# [END securitycenter_create_finding_with_source_properties]
def update_finding(source_name):
# [START securitycenter_update_finding_source_properties]
# [START update_finding]
import datetime
from google.cloud import securitycenter
from google.cloud.securitycenter_v1 import Finding
from google.protobuf import field_mask_pb2
client = securitycenter.SecurityCenterClient()
# Only update the specific source property and event_time. event_time
# is required for updates.
field_mask = field_mask_pb2.FieldMask(
paths=["source_properties.s_value", "event_time"]
)
# Set the update time to Now. This must be some time greater then the
# event_time on the original finding.
event_time = datetime.datetime.now()
# source_name is the resource path for a source that has been
# created previously (you can use list_sources to find a specific one).
# Its format is:
# source_name = "organizations/{organization_id}/sources/{source_id}"
# e.g.:
# source_name = "organizations/111122222444/sources/1234"
finding_name = "{}/findings/samplefindingid2".format(source_name)
finding = Finding(
name=finding_name,
source_properties={"s_value": "new_string"},
event_time=event_time,
)
updated_finding = client.update_finding(
request={
"finding": finding,
"update_mask": field_mask,
}
)
print(
"New Source properties: {}, Event Time {}".format(
updated_finding.source_properties, updated_finding.event_time
)
)
# [END update_finding]
# [END securitycenter_update_finding_source_properties]
def update_finding_state(source_name):
"""Demonstrate updating only a finding state."""
# [START securitycenter_update_finding_state]
# [START update_finding_state]
import datetime
from google.cloud import securitycenter
from google.cloud.securitycenter_v1 import Finding
# Create a client.
client = securitycenter.SecurityCenterClient()
# source_name is the resource path for a source that has been
# created previously (you can use list_sources to find a specific one).
# Its format is:
# source_name = "organizations/{organization_id}/sources/{source_id}"
# e.g.:
# source_name = "organizations/111122222444/sources/1234"
finding_name = "{}/findings/samplefindingid2".format(source_name)
# Call the API to change the finding state to inactive as of now.
new_finding = client.set_finding_state(
request={
"name": finding_name,
"state": Finding.State.INACTIVE,
"start_time": datetime.datetime.now(),
}
)
print(f"New state: {new_finding.state}")
# [END update_finding_state]
# [END securitycenter_update_finding_state]
def trouble_shoot(source_name):
"""Demonstrate calling test_iam_permissions to determine if the
service account has the correct permisions."""
# [START securitycenter_test_iam]
# [START test_iam_permissions]
from google.cloud import securitycenter
# Create a client.
client = securitycenter.SecurityCenterClient()
# source_name is the resource path for a source that has been
# created previously (you can use list_sources to find a specific one).
# Its format is:
# source_name = "organizations/{organization_id}/sources/{source_id}"
# e.g.:
# source_name = "organizations/111122222444/sources/1234"
# Check for permssions to call create_finding or update_finding.
permission_response = client.test_iam_permissions(
request={
"resource": source_name,
"permissions": ["securitycenter.findings.update"],
}
)
print(
"Permision to create or update findings? {}".format(
len(permission_response.permissions) > 0
)
)
# [END test_iam_permissions]
# [END securitycenter_test_iam]
assert len(permission_response.permissions) > 0
# [START securitycenter_test_iam]
# [START test_iam_permissions]
# Check for permissions necessary to call set_finding_state.
permission_response = client.test_iam_permissions(
request={
"resource": source_name,
"permissions": ["securitycenter.findings.setState"],
}
)
print(
"Permision to update state? {}".format(len(permission_response.permissions) > 0)
)
# [END test_iam_permissions]
# [END securitycenter_test_iam]
return permission_response
assert len(permission_response.permissions) > 0
def list_all_findings(organization_id):
# [START securitycenter_list_all_findings]
# [START list_all_findings]
from google.cloud import securitycenter
# Create a client.
client = securitycenter.SecurityCenterClient()
# organization_id is the numeric ID of the organization. e.g.:
# organization_id = "111122222444"
org_name = "organizations/{org_id}".format(org_id=organization_id)
# The "sources/-" suffix lists findings across all sources. You
# also use a specific source_name instead.
all_sources = "{org_name}/sources/-".format(org_name=org_name)
finding_result_iterator = client.list_findings(request={"parent": all_sources})
for i, finding_result in enumerate(finding_result_iterator):
print(
"{}: name: {} resource: {}".format(
i, finding_result.finding.name, finding_result.finding.resource_name
)
)
# [END list_all_findings]
# [END securitycenter_list_all_findings]
return i
def list_filtered_findings(source_name):
# [START securitycenter_list_filtered_findings]
# [START list_filtered_findings]
from google.cloud import securitycenter
# Create a new client.
client = securitycenter.SecurityCenterClient()
# source_name is the resource path for a source that has been
# created previously (you can use list_sources to find a specific one).
# Its format is:
# source_name = "organizations/{organization_id}/sources/{source_id}"
# e.g.:
# source_name = "organizations/111122222444/sources/1234"
# You an also use a wild-card "-" for all sources:
# source_name = "organizations/111122222444/sources/-"
finding_result_iterator = client.list_findings(
request={"parent": source_name, "filter": 'category="MEDIUM_RISK_ONE"'}
)
# Iterate an print all finding names and the resource they are
# in reference to.
for i, finding_result in enumerate(finding_result_iterator):
print(
"{}: name: {} resource: {}".format(
i, finding_result.finding.name, finding_result.finding.resource_name
)
)
# [END list_filtered_findings]
# [END securitycenter_list_filtered_findings]
return i
def list_findings_at_time(source_name):
# [START securitycenter_list_findings_at_time]
# [START list_findings_at_a_time]
from google.cloud import securitycenter
from datetime import timedelta, datetime
# Create a new client.
client = securitycenter.SecurityCenterClient()
# source_name is the resource path for a source that has been
# created previously (you can use list_sources to find a specific one).
# Its format is:
# source_name = "organizations/{organization_id}/sources/{source_id}"
# e.g.:
# source_name = "organizations/111122222444/sources/1234"
# You an also use a wild-card "-" for all sources:
# source_name = "organizations/111122222444/sources/-"
five_days_ago = str(datetime.now() - timedelta(days=5))
# [END list_findings_at_a_time]
# [END securitycenter_list_findings_at_time]
i = -1
# [START securitycenter_list_findings_at_time]
# [START list_findings_at_a_time]
finding_result_iterator = client.list_findings(
request={"parent": source_name, "filter": five_days_ago}
)
for i, finding_result in enumerate(finding_result_iterator):
print(
"{}: name: {} resource: {}".format(
i, finding_result.finding.name, finding_result.finding.resource_name
)
)
# [END list_findings_at_a_time]
# [END securitycenter_list_findings_at_time]
return i
def get_iam_policy(source_name):
"""Gives a user findingsEditor permission to the source."""
# [START securitycenter_get_source_iam]
# [START get_source_iam]
from google.cloud import securitycenter
client = securitycenter.SecurityCenterClient()
# source_name is the resource path for a source that has been
# created previously (you can use list_sources to find a specific one).
# Its format is:
# source_name = "organizations/{organization_id}/sources/{source_id}"
# e.g.:
# source_name = "organizations/111122222444/sources/1234"
# Get the old policy so we can do an incremental update.
policy = client.get_iam_policy(request={"resource": source_name})
print("Policy: {}".format(policy))
# [END get_source_iam]
# [END securitycenter_get_source_iam]
def group_all_findings(organization_id):
"""Demonstrates grouping all findings across an organization."""
i = 0
# [START securitycenter_group_all_findings]
# [START group_all_findings]
from google.cloud import securitycenter
# Create a client.
client = securitycenter.SecurityCenterClient()
# organization_id is the numeric ID of the organization. e.g.:
# organization_id = "111122222444"
org_name = "organizations/{org_id}".format(org_id=organization_id)
# The "sources/-" suffix lists findings across all sources. You
# also use a specific source_name instead.
all_sources = "{org_name}/sources/-".format(org_name=org_name)
group_result_iterator = client.group_findings(
request={"parent": all_sources, "group_by": "category"}
)
for i, group_result in enumerate(group_result_iterator):
print((i + 1), group_result)
# [END group_all_findings]
# [END securitycenter_group_all_findings]
return i
def group_filtered_findings(source_name):
"""Demonstrates grouping all findings across an organization."""
i = 0
# [START securitycenter_group_filtered_findings]
from google.cloud import securitycenter
# Create a client.
client = securitycenter.SecurityCenterClient()
# source_name is the resource path for a source that has been
# created previously (you can use list_sources to find a specific one).
# Its format is:
# source_name = "organizations/{organization_id}/sources/{source_id}"
# e.g.:
# source_name = "organizations/111122222444/sources/1234"
group_result_iterator = client.group_findings(
request={
"parent": source_name,
"group_by": "category",
"filter": 'state="ACTIVE"',
}
)
for i, group_result in enumerate(group_result_iterator):
print((i + 1), group_result)
# [END securitycenter_group_filtered_findings]
return i
def group_findings_at_time(source_name):
"""Demonstrates grouping all findings across an organization as of
a specific time."""
i = -1
# [START securitycenter_group_findings_at_time]
from datetime import datetime, timedelta
from google.cloud import securitycenter
# Create a client.
client = securitycenter.SecurityCenterClient()
# source_name is the resource path for a source that has been
# created previously (you can use list_sources to find a specific one).
# Its format is:
# source_name = "organizations/{organization_id}/sources/{source_id}"
# e.g.:
# source_name = "organizations/111122222444/sources/1234"
# Group findings as of yesterday.
read_time = datetime.utcnow() - timedelta(days=1)
group_result_iterator = client.group_findings(
request={
"parent": source_name,
"group_by": "category",
"read_time": read_time,
}
)
for i, group_result in enumerate(group_result_iterator):
print((i + 1), group_result)
# [END securitycenter_group_findings_at_time]
return i
def group_findings_and_changes(source_name):
"""Demonstrates grouping all findings across an organization and
associated changes."""
i = 0
# [START securitycenter_group_findings_with_changes]
from datetime import timedelta
from google.cloud import securitycenter
# Create a client.
client = securitycenter.SecurityCenterClient()
# source_name is the resource path for a source that has been
# created previously (you can use list_sources to find a specific one).
# Its format is:
# source_name = "organizations/{organization_id}/sources/{source_id}"
# e.g.:
# source_name = "organizations/111122222444/sources/1234"
# List assets and their state change the last 30 days
compare_delta = timedelta(days=30)
group_result_iterator = client.group_findings(
request={
"parent": source_name,
"group_by": "state_change",
"compare_duration": compare_delta,
}
)
for i, group_result in enumerate(group_result_iterator):
print((i + 1), group_result)
# [END securitycenter_group_findings_with_changes]]
return i