20
20
def create_source (organization_id ):
21
21
"""Create a new findings source."""
22
22
# [START securitycenter_create_source]
23
- from google .cloud import securitycenter
23
+ from google .cloud import securitycenter_v1
24
24
25
- client = securitycenter .SecurityCenterClient ()
25
+ client = securitycenter_v1 .SecurityCenterClient ()
26
26
# organization_id is the numeric ID of the organization. e.g.:
27
27
# organization_id = "111122222444"
28
28
org_name = f"organizations/{ organization_id } "
@@ -43,9 +43,9 @@ def create_source(organization_id):
43
43
def get_source (source_name ):
44
44
"""Gets an existing source."""
45
45
# [START securitycenter_get_source]
46
- from google .cloud import securitycenter
46
+ from google .cloud import securitycenter_v1
47
47
48
- client = securitycenter .SecurityCenterClient ()
48
+ client = securitycenter_v1 .SecurityCenterClient ()
49
49
50
50
# 'source_name' is the resource path for a source that has been
51
51
# created previously (you can use list_sources to find a specific one).
@@ -63,10 +63,10 @@ def get_source(source_name):
63
63
def update_source (source_name ):
64
64
"""Updates a source's display name."""
65
65
# [START securitycenter_update_source]
66
- from google .cloud import securitycenter
66
+ from google .cloud import securitycenter_v1
67
67
from google .protobuf import field_mask_pb2
68
68
69
- client = securitycenter .SecurityCenterClient ()
69
+ client = securitycenter_v1 .SecurityCenterClient ()
70
70
71
71
# Field mask to only update the display name.
72
72
field_mask = field_mask_pb2 .FieldMask (paths = ["display_name" ])
@@ -91,11 +91,12 @@ def update_source(source_name):
91
91
def add_user_to_source (source_name ):
92
92
"""Gives a user findingsEditor permission to the source."""
93
93
user_email = "csccclienttest@gmail.com"
94
+
94
95
# [START securitycenter_set_source_iam]
95
- from google .cloud import securitycenter
96
+ from google .cloud import securitycenter_v1
96
97
from google .iam .v1 import policy_pb2
97
98
98
- client = securitycenter .SecurityCenterClient ()
99
+ client = securitycenter_v1 .SecurityCenterClient ()
99
100
100
101
# 'source_name' is the resource path for a source that has been
101
102
# created previously (you can use list_sources to find a specific one).
@@ -123,19 +124,19 @@ def add_user_to_source(source_name):
123
124
)
124
125
125
126
print (f"Updated Policy: { updated } " )
126
-
127
127
# [END securitycenter_set_source_iam]
128
+
128
129
return binding , updated
129
130
130
131
131
132
def list_source (organization_id ):
132
133
"""Lists finding sources."""
133
134
i = - 1
134
135
# [START securitycenter_list_sources]
135
- from google .cloud import securitycenter
136
+ from google .cloud import securitycenter_v1
136
137
137
138
# Create a new client.
138
- client = securitycenter .SecurityCenterClient ()
139
+ client = securitycenter_v1 .SecurityCenterClient ()
139
140
# 'parent' must be in one of the following formats:
140
141
# "organizations/{organization_id}"
141
142
# "projects/{project_id}"
@@ -152,16 +153,16 @@ def list_source(organization_id):
152
153
def create_finding (source_name , finding_id ):
153
154
"""Creates a new finding."""
154
155
# [START securitycenter_create_finding]
155
- import datetime
156
+ from datetime import datetime , timezone
156
157
157
- from google .cloud import securitycenter
158
+ from google .cloud import securitycenter_v1
158
159
from google .cloud .securitycenter_v1 import Finding
159
160
160
161
# Create a new client.
161
- client = securitycenter .SecurityCenterClient ()
162
+ client = securitycenter_v1 .SecurityCenterClient ()
162
163
163
164
# Use the current time as the finding "event time".
164
- event_time = datetime .datetime . now (tz = datetime . timezone .utc )
165
+ event_time = datetime .now (tz = timezone .utc )
165
166
166
167
# 'source_name' is the resource path for a source that has been
167
168
# created previously (you can use list_sources to find a specific one).
@@ -194,14 +195,14 @@ def create_finding(source_name, finding_id):
194
195
def create_finding_with_source_properties (source_name ):
195
196
"""Demonstrate creating a new finding with source properties."""
196
197
# [START securitycenter_create_finding_with_source_properties]
197
- import datetime
198
+ from datetime import datetime , timezone
198
199
199
- from google .cloud import securitycenter
200
+ from google .cloud import securitycenter_v1
200
201
from google .cloud .securitycenter_v1 import Finding
201
202
from google .protobuf .struct_pb2 import Value
202
203
203
204
# Create a new client.
204
- client = securitycenter .SecurityCenterClient ()
205
+ client = securitycenter_v1 .SecurityCenterClient ()
205
206
206
207
# 'source_name' is the resource path for a source that has been
207
208
# created previously (you can use list_sources to find a specific one).
@@ -225,7 +226,7 @@ def create_finding_with_source_properties(source_name):
225
226
num_value .number_value = 1234
226
227
227
228
# Use the current time as the finding "event time".
228
- event_time = datetime .datetime . now (tz = datetime . timezone .utc )
229
+ event_time = datetime .now (tz = timezone .utc )
229
230
230
231
finding = Finding (
231
232
state = Finding .State .ACTIVE ,
@@ -244,13 +245,13 @@ def create_finding_with_source_properties(source_name):
244
245
245
246
def update_finding (source_name ):
246
247
# [START securitycenter_update_finding_source_properties]
247
- import datetime
248
+ from datetime import datetime , timezone
248
249
249
- from google .cloud import securitycenter
250
+ from google .cloud import securitycenter_v1
250
251
from google .cloud .securitycenter_v1 import Finding
251
252
from google .protobuf import field_mask_pb2
252
253
253
- client = securitycenter .SecurityCenterClient ()
254
+ client = securitycenter_v1 .SecurityCenterClient ()
254
255
# Only update the specific source property and event_time. event_time
255
256
# is required for updates.
256
257
field_mask = field_mask_pb2 .FieldMask (
@@ -259,7 +260,7 @@ def update_finding(source_name):
259
260
260
261
# Set the update time to Now. This must be some time greater then the
261
262
# event_time on the original finding.
262
- event_time = datetime .datetime . now (tz = datetime . timezone .utc )
263
+ event_time = datetime .now (tz = timezone .utc )
263
264
264
265
# 'source_name' is the resource path for a source that has been
265
266
# created previously (you can use list_sources to find a specific one).
@@ -288,13 +289,13 @@ def update_finding(source_name):
288
289
def update_finding_state (source_name ):
289
290
"""Demonstrate updating only a finding state."""
290
291
# [START securitycenter_update_finding_state]
291
- import datetime
292
+ from datetime import datetime , timezone
292
293
293
- from google .cloud import securitycenter
294
+ from google .cloud import securitycenter_v1
294
295
from google .cloud .securitycenter_v1 import Finding
295
296
296
297
# Create a client.
297
- client = securitycenter .SecurityCenterClient ()
298
+ client = securitycenter_v1 .SecurityCenterClient ()
298
299
# 'source_name' is the resource path for a source that has been
299
300
# created previously (you can use list_sources to find a specific one).
300
301
# Its format is:
@@ -308,7 +309,7 @@ def update_finding_state(source_name):
308
309
request = {
309
310
"name" : finding_name ,
310
311
"state" : Finding .State .INACTIVE ,
311
- "start_time" : datetime .datetime . now (tz = datetime . timezone .utc ),
312
+ "start_time" : datetime .now (timezone .utc ),
312
313
}
313
314
)
314
315
print (f"New state: { new_finding .state } " )
@@ -319,10 +320,10 @@ def trouble_shoot(source_name):
319
320
"""Demonstrate calling test_iam_permissions to determine if the
320
321
service account has the correct permisions."""
321
322
# [START securitycenter_test_iam]
322
- from google .cloud import securitycenter
323
+ from google .cloud import securitycenter_v1
323
324
324
325
# Create a client.
325
- client = securitycenter .SecurityCenterClient ()
326
+ client = securitycenter_v1 .SecurityCenterClient ()
326
327
# 'source_name' is the resource path for a source that has been
327
328
# created previously (you can use list_sources to find a specific one).
328
329
# Its format is:
@@ -356,15 +357,14 @@ def trouble_shoot(source_name):
356
357
print (f"Permision to update state? { len (permission_response .permissions ) > 0 } " )
357
358
# [END securitycenter_test_iam]
358
359
return permission_response
359
- assert len (permission_response .permissions ) > 0
360
360
361
361
362
362
def list_all_findings (organization_id ):
363
363
# [START securitycenter_list_all_findings]
364
- from google .cloud import securitycenter
364
+ from google .cloud import securitycenter_v1
365
365
366
366
# Create a client.
367
- client = securitycenter .SecurityCenterClient ()
367
+ client = securitycenter_v1 .SecurityCenterClient ()
368
368
369
369
# 'parent' must be in one of the following formats:
370
370
# "organizations/{organization_id}"
@@ -387,10 +387,10 @@ def list_all_findings(organization_id):
387
387
388
388
def list_filtered_findings (source_name ):
389
389
# [START securitycenter_list_filtered_findings]
390
- from google .cloud import securitycenter
390
+ from google .cloud import securitycenter_v1
391
391
392
392
# Create a new client.
393
- client = securitycenter .SecurityCenterClient ()
393
+ client = securitycenter_v1 .SecurityCenterClient ()
394
394
395
395
# 'source_name' is the resource path for a source that has been
396
396
# created previously (you can use list_sources to find a specific one).
@@ -419,12 +419,14 @@ def list_filtered_findings(source_name):
419
419
420
420
def list_findings_at_time (source_name ):
421
421
# [START securitycenter_list_findings_at_time]
422
- from datetime import datetime , timedelta
422
+ from datetime import datetime , timedelta , timezone
423
423
424
- from google .cloud import securitycenter
424
+ from google .cloud import securitycenter_v1
425
425
426
426
# Create a new client.
427
- client = securitycenter .SecurityCenterClient ()
427
+ # More info about SecurityCenterClient:
428
+ # https://cloud.google.com/python/docs/reference/securitycenter/latest/google.cloud.securitycenter_v1.services.security_center.SecurityCenterClient
429
+ client = securitycenter_v1 .SecurityCenterClient ()
428
430
429
431
# 'source_name' is the resource path for a source that has been
430
432
# created previously (you can use list_sources to find a specific one).
@@ -436,30 +438,39 @@ def list_findings_at_time(source_name):
436
438
# "folders/{folder_id}"
437
439
# You an also use a wild-card "-" for all sources:
438
440
# source_name = "organizations/111122222444/sources/-"
439
- five_days_ago = str (datetime .now () - timedelta (days = 5 ))
441
+
442
+ five_days_ago = datetime .now (timezone .utc ) - timedelta (days = 5 )
443
+ timestamp_milliseconds = int (five_days_ago .timestamp () * 1000 )
440
444
# [END securitycenter_list_findings_at_time]
441
445
i = - 1
442
446
# [START securitycenter_list_findings_at_time]
443
447
448
+ # More details about the request syntax:
449
+ # https://cloud.google.com/security-command-center/docs/reference/rest/v1/folders.sources.findings/list
444
450
finding_result_iterator = client .list_findings (
445
- request = {"parent" : source_name , "filter" : five_days_ago }
451
+ request = {
452
+ "parent" : source_name ,
453
+ "filter" : f"event_time < { timestamp_milliseconds } " ,
454
+ }
446
455
)
456
+
447
457
for i , finding_result in enumerate (finding_result_iterator ):
448
458
print (
449
459
"{}: name: {} resource: {}" .format (
450
460
i , finding_result .finding .name , finding_result .finding .resource_name
451
461
)
452
462
)
453
463
# [END securitycenter_list_findings_at_time]
464
+
454
465
return i
455
466
456
467
457
468
def get_iam_policy (source_name ):
458
469
"""Gives a user findingsEditor permission to the source."""
459
470
# [START securitycenter_get_source_iam]
460
- from google .cloud import securitycenter
471
+ from google .cloud import securitycenter_v1
461
472
462
- client = securitycenter .SecurityCenterClient ()
473
+ client = securitycenter_v1 .SecurityCenterClient ()
463
474
464
475
# 'source_name' is the resource path for a source that has been
465
476
# created previously (you can use list_sources to find a specific one).
@@ -477,10 +488,10 @@ def group_all_findings(organization_id):
477
488
"""Demonstrates grouping all findings across an organization."""
478
489
i = 0
479
490
# [START securitycenter_group_all_findings]
480
- from google .cloud import securitycenter
491
+ from google .cloud import securitycenter_v1
481
492
482
493
# Create a client.
483
- client = securitycenter .SecurityCenterClient ()
494
+ client = securitycenter_v1 .SecurityCenterClient ()
484
495
485
496
# 'parent' must be in one of the following formats:
486
497
# "organizations/{organization_id}"
@@ -503,10 +514,10 @@ def group_filtered_findings(source_name):
503
514
"""Demonstrates grouping all findings across an organization."""
504
515
i = 0
505
516
# [START securitycenter_group_filtered_findings]
506
- from google .cloud import securitycenter
517
+ from google .cloud import securitycenter_v1
507
518
508
519
# Create a client.
509
- client = securitycenter .SecurityCenterClient ()
520
+ client = securitycenter_v1 .SecurityCenterClient ()
510
521
511
522
# 'source_name' is the resource path for a source that has been
512
523
# created previously (you can use list_sources to find a specific one).
@@ -529,75 +540,3 @@ def group_filtered_findings(source_name):
529
540
print ((i + 1 ), group_result )
530
541
# [END securitycenter_group_filtered_findings]
531
542
return i
532
-
533
-
534
- def group_findings_at_time (source_name ):
535
- """Demonstrates grouping all findings across an organization as of
536
- a specific time."""
537
- i = - 1
538
- # [START securitycenter_group_findings_at_time]
539
- from datetime import datetime , timedelta
540
-
541
- from google .cloud import securitycenter
542
-
543
- # Create a client.
544
- client = securitycenter .SecurityCenterClient ()
545
-
546
- # 'source_name' is the resource path for a source that has been
547
- # created previously (you can use list_sources to find a specific one).
548
- # Its format is:
549
- # source_name = "{parent}/sources/{source_id}"
550
- # 'parent' must be in one of the following formats:
551
- # "organizations/{organization_id}"
552
- # "projects/{project_id}"
553
- # "folders/{folder_id}"
554
- # source_name = "organizations/111122222444/sources/1234"
555
-
556
- # Group findings as of yesterday.
557
- read_time = datetime .utcnow () - timedelta (days = 1 )
558
-
559
- group_result_iterator = client .group_findings (
560
- request = {"parent" : source_name , "group_by" : "category" , "read_time" : read_time }
561
- )
562
- for i , group_result in enumerate (group_result_iterator ):
563
- print ((i + 1 ), group_result )
564
- # [END securitycenter_group_findings_at_time]
565
- return i
566
-
567
-
568
- def group_findings_and_changes (source_name ):
569
- """Demonstrates grouping all findings across an organization and
570
- associated changes."""
571
- i = 0
572
- # [START securitycenter_group_findings_with_changes]
573
- from datetime import timedelta
574
-
575
- from google .cloud import securitycenter
576
-
577
- # Create a client.
578
- client = securitycenter .SecurityCenterClient ()
579
-
580
- # 'source_name' is the resource path for a source that has been
581
- # created previously (you can use list_sources to find a specific one).
582
- # Its format is:
583
- # source_name = "{parent}/sources/{source_id}"
584
- # 'parent' must be in one of the following formats:
585
- # "organizations/{organization_id}"
586
- # "projects/{project_id}"
587
- # "folders/{folder_id}"
588
- # source_name = "organizations/111122222444/sources/1234"
589
-
590
- # List assets and their state change the last 30 days
591
- compare_delta = timedelta (days = 30 )
592
-
593
- group_result_iterator = client .group_findings (
594
- request = {
595
- "parent" : source_name ,
596
- "group_by" : "state_change" ,
597
- "compare_duration" : compare_delta ,
598
- }
599
- )
600
- for i , group_result in enumerate (group_result_iterator ):
601
- print ((i + 1 ), group_result )
602
- # [END securitycenter_group_findings_with_changes]]
603
- return i
0 commit comments