2020def create_source (organization_id ):
2121 """Create a new findings source."""
2222 # [START securitycenter_create_source]
23- from google .cloud import securitycenter
23+ from google .cloud import securitycenter_v1
2424
25- client = securitycenter .SecurityCenterClient ()
25+ client = securitycenter_v1 .SecurityCenterClient ()
2626 # organization_id is the numeric ID of the organization. e.g.:
2727 # organization_id = "111122222444"
2828 org_name = f"organizations/{ organization_id } "
@@ -43,9 +43,9 @@ def create_source(organization_id):
4343def get_source (source_name ):
4444 """Gets an existing source."""
4545 # [START securitycenter_get_source]
46- from google .cloud import securitycenter
46+ from google .cloud import securitycenter_v1
4747
48- client = securitycenter .SecurityCenterClient ()
48+ client = securitycenter_v1 .SecurityCenterClient ()
4949
5050 # 'source_name' is the resource path for a source that has been
5151 # created previously (you can use list_sources to find a specific one).
@@ -63,10 +63,10 @@ def get_source(source_name):
6363def update_source (source_name ):
6464 """Updates a source's display name."""
6565 # [START securitycenter_update_source]
66- from google .cloud import securitycenter
66+ from google .cloud import securitycenter_v1
6767 from google .protobuf import field_mask_pb2
6868
69- client = securitycenter .SecurityCenterClient ()
69+ client = securitycenter_v1 .SecurityCenterClient ()
7070
7171 # Field mask to only update the display name.
7272 field_mask = field_mask_pb2 .FieldMask (paths = ["display_name" ])
@@ -91,11 +91,12 @@ def update_source(source_name):
9191def add_user_to_source (source_name ):
9292 """Gives a user findingsEditor permission to the source."""
939394+
9495 # [START securitycenter_set_source_iam]
95- from google .cloud import securitycenter
96+ from google .cloud import securitycenter_v1
9697 from google .iam .v1 import policy_pb2
9798
98- client = securitycenter .SecurityCenterClient ()
99+ client = securitycenter_v1 .SecurityCenterClient ()
99100
100101 # 'source_name' is the resource path for a source that has been
101102 # created previously (you can use list_sources to find a specific one).
@@ -123,19 +124,19 @@ def add_user_to_source(source_name):
123124 )
124125
125126 print (f"Updated Policy: { updated } " )
126-
127127 # [END securitycenter_set_source_iam]
128+
128129 return binding , updated
129130
130131
131132def list_source (organization_id ):
132133 """Lists finding sources."""
133134 i = - 1
134135 # [START securitycenter_list_sources]
135- from google .cloud import securitycenter
136+ from google .cloud import securitycenter_v1
136137
137138 # Create a new client.
138- client = securitycenter .SecurityCenterClient ()
139+ client = securitycenter_v1 .SecurityCenterClient ()
139140 # 'parent' must be in one of the following formats:
140141 # "organizations/{organization_id}"
141142 # "projects/{project_id}"
@@ -152,16 +153,16 @@ def list_source(organization_id):
152153def create_finding (source_name , finding_id ):
153154 """Creates a new finding."""
154155 # [START securitycenter_create_finding]
155- import datetime
156+ from datetime import datetime , timezone
156157
157- from google .cloud import securitycenter
158+ from google .cloud import securitycenter_v1
158159 from google .cloud .securitycenter_v1 import Finding
159160
160161 # Create a new client.
161- client = securitycenter .SecurityCenterClient ()
162+ client = securitycenter_v1 .SecurityCenterClient ()
162163
163164 # 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 )
165166
166167 # 'source_name' is the resource path for a source that has been
167168 # created previously (you can use list_sources to find a specific one).
@@ -194,14 +195,14 @@ def create_finding(source_name, finding_id):
194195def create_finding_with_source_properties (source_name ):
195196 """Demonstrate creating a new finding with source properties."""
196197 # [START securitycenter_create_finding_with_source_properties]
197- import datetime
198+ from datetime import datetime , timezone
198199
199- from google .cloud import securitycenter
200+ from google .cloud import securitycenter_v1
200201 from google .cloud .securitycenter_v1 import Finding
201202 from google .protobuf .struct_pb2 import Value
202203
203204 # Create a new client.
204- client = securitycenter .SecurityCenterClient ()
205+ client = securitycenter_v1 .SecurityCenterClient ()
205206
206207 # 'source_name' is the resource path for a source that has been
207208 # created previously (you can use list_sources to find a specific one).
@@ -225,7 +226,7 @@ def create_finding_with_source_properties(source_name):
225226 num_value .number_value = 1234
226227
227228 # 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 )
229230
230231 finding = Finding (
231232 state = Finding .State .ACTIVE ,
@@ -244,13 +245,13 @@ def create_finding_with_source_properties(source_name):
244245
245246def update_finding (source_name ):
246247 # [START securitycenter_update_finding_source_properties]
247- import datetime
248+ from datetime import datetime , timezone
248249
249- from google .cloud import securitycenter
250+ from google .cloud import securitycenter_v1
250251 from google .cloud .securitycenter_v1 import Finding
251252 from google .protobuf import field_mask_pb2
252253
253- client = securitycenter .SecurityCenterClient ()
254+ client = securitycenter_v1 .SecurityCenterClient ()
254255 # Only update the specific source property and event_time. event_time
255256 # is required for updates.
256257 field_mask = field_mask_pb2 .FieldMask (
@@ -259,7 +260,7 @@ def update_finding(source_name):
259260
260261 # Set the update time to Now. This must be some time greater then the
261262 # event_time on the original finding.
262- event_time = datetime .datetime . now (tz = datetime . timezone .utc )
263+ event_time = datetime .now (tz = timezone .utc )
263264
264265 # 'source_name' is the resource path for a source that has been
265266 # created previously (you can use list_sources to find a specific one).
@@ -288,13 +289,13 @@ def update_finding(source_name):
288289def update_finding_state (source_name ):
289290 """Demonstrate updating only a finding state."""
290291 # [START securitycenter_update_finding_state]
291- import datetime
292+ from datetime import datetime , timezone
292293
293- from google .cloud import securitycenter
294+ from google .cloud import securitycenter_v1
294295 from google .cloud .securitycenter_v1 import Finding
295296
296297 # Create a client.
297- client = securitycenter .SecurityCenterClient ()
298+ client = securitycenter_v1 .SecurityCenterClient ()
298299 # 'source_name' is the resource path for a source that has been
299300 # created previously (you can use list_sources to find a specific one).
300301 # Its format is:
@@ -308,7 +309,7 @@ def update_finding_state(source_name):
308309 request = {
309310 "name" : finding_name ,
310311 "state" : Finding .State .INACTIVE ,
311- "start_time" : datetime .datetime . now (tz = datetime . timezone .utc ),
312+ "start_time" : datetime .now (timezone .utc ),
312313 }
313314 )
314315 print (f"New state: { new_finding .state } " )
@@ -319,10 +320,10 @@ def trouble_shoot(source_name):
319320 """Demonstrate calling test_iam_permissions to determine if the
320321 service account has the correct permisions."""
321322 # [START securitycenter_test_iam]
322- from google .cloud import securitycenter
323+ from google .cloud import securitycenter_v1
323324
324325 # Create a client.
325- client = securitycenter .SecurityCenterClient ()
326+ client = securitycenter_v1 .SecurityCenterClient ()
326327 # 'source_name' is the resource path for a source that has been
327328 # created previously (you can use list_sources to find a specific one).
328329 # Its format is:
@@ -356,15 +357,14 @@ def trouble_shoot(source_name):
356357 print (f"Permision to update state? { len (permission_response .permissions ) > 0 } " )
357358 # [END securitycenter_test_iam]
358359 return permission_response
359- assert len (permission_response .permissions ) > 0
360360
361361
362362def list_all_findings (organization_id ):
363363 # [START securitycenter_list_all_findings]
364- from google .cloud import securitycenter
364+ from google .cloud import securitycenter_v1
365365
366366 # Create a client.
367- client = securitycenter .SecurityCenterClient ()
367+ client = securitycenter_v1 .SecurityCenterClient ()
368368
369369 # 'parent' must be in one of the following formats:
370370 # "organizations/{organization_id}"
@@ -387,10 +387,10 @@ def list_all_findings(organization_id):
387387
388388def list_filtered_findings (source_name ):
389389 # [START securitycenter_list_filtered_findings]
390- from google .cloud import securitycenter
390+ from google .cloud import securitycenter_v1
391391
392392 # Create a new client.
393- client = securitycenter .SecurityCenterClient ()
393+ client = securitycenter_v1 .SecurityCenterClient ()
394394
395395 # 'source_name' is the resource path for a source that has been
396396 # created previously (you can use list_sources to find a specific one).
@@ -419,12 +419,14 @@ def list_filtered_findings(source_name):
419419
420420def list_findings_at_time (source_name ):
421421 # [START securitycenter_list_findings_at_time]
422- from datetime import datetime , timedelta
422+ from datetime import datetime , timedelta , timezone
423423
424- from google .cloud import securitycenter
424+ from google .cloud import securitycenter_v1
425425
426426 # 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 ()
428430
429431 # 'source_name' is the resource path for a source that has been
430432 # created previously (you can use list_sources to find a specific one).
@@ -436,30 +438,39 @@ def list_findings_at_time(source_name):
436438 # "folders/{folder_id}"
437439 # You an also use a wild-card "-" for all sources:
438440 # 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 )
440444 # [END securitycenter_list_findings_at_time]
441445 i = - 1
442446 # [START securitycenter_list_findings_at_time]
443447
448+ # More details about the request syntax:
449+ # https://cloud.google.com/security-command-center/docs/reference/rest/v1/folders.sources.findings/list
444450 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+ }
446455 )
456+
447457 for i , finding_result in enumerate (finding_result_iterator ):
448458 print (
449459 "{}: name: {} resource: {}" .format (
450460 i , finding_result .finding .name , finding_result .finding .resource_name
451461 )
452462 )
453463 # [END securitycenter_list_findings_at_time]
464+
454465 return i
455466
456467
457468def get_iam_policy (source_name ):
458469 """Gives a user findingsEditor permission to the source."""
459470 # [START securitycenter_get_source_iam]
460- from google .cloud import securitycenter
471+ from google .cloud import securitycenter_v1
461472
462- client = securitycenter .SecurityCenterClient ()
473+ client = securitycenter_v1 .SecurityCenterClient ()
463474
464475 # 'source_name' is the resource path for a source that has been
465476 # created previously (you can use list_sources to find a specific one).
@@ -477,10 +488,10 @@ def group_all_findings(organization_id):
477488 """Demonstrates grouping all findings across an organization."""
478489 i = 0
479490 # [START securitycenter_group_all_findings]
480- from google .cloud import securitycenter
491+ from google .cloud import securitycenter_v1
481492
482493 # Create a client.
483- client = securitycenter .SecurityCenterClient ()
494+ client = securitycenter_v1 .SecurityCenterClient ()
484495
485496 # 'parent' must be in one of the following formats:
486497 # "organizations/{organization_id}"
@@ -503,10 +514,10 @@ def group_filtered_findings(source_name):
503514 """Demonstrates grouping all findings across an organization."""
504515 i = 0
505516 # [START securitycenter_group_filtered_findings]
506- from google .cloud import securitycenter
517+ from google .cloud import securitycenter_v1
507518
508519 # Create a client.
509- client = securitycenter .SecurityCenterClient ()
520+ client = securitycenter_v1 .SecurityCenterClient ()
510521
511522 # 'source_name' is the resource path for a source that has been
512523 # created previously (you can use list_sources to find a specific one).
@@ -529,75 +540,3 @@ def group_filtered_findings(source_name):
529540 print ((i + 1 ), group_result )
530541 # [END securitycenter_group_filtered_findings]
531542 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