19
19
20
20
if TYPE_CHECKING :
21
21
from mypy_boto3_iam import IAMClient
22
- from mypy_boto3_iam .type_defs import CreateRoleResponseTypeDef , DeleteRoleRequestRequestTypeDef , DetachRolePolicyRequestPolicyDetachRoleTypeDef
22
+ from mypy_boto3_iam .type_defs import CreateRoleResponseTypeDef
23
23
from mypy_boto3_organizations import OrganizationsClient
24
24
from mypy_boto3_route53 import Route53Client
25
25
from mypy_boto3_route53 .type_defs import ListHostedZonesResponseTypeDef
26
26
from mypy_boto3_s3 import S3Client
27
27
from mypy_boto3_shield import ShieldClient
28
28
from mypy_boto3_shield .type_defs import (
29
- AssociateDRTLogBucketRequestRequestTypeDef ,
30
29
AssociateProactiveEngagementDetailsRequestRequestTypeDef ,
31
- CreateProtectionGroupRequestRequestTypeDef ,
32
30
CreateProtectionResponseTypeDef ,
33
- DeleteProtectionGroupRequestRequestTypeDef ,
34
- DeleteProtectionRequestRequestTypeDef ,
35
31
DescribeEmergencyContactSettingsResponseTypeDef ,
36
32
DescribeProtectionResponseTypeDef ,
37
33
DescribeSubscriptionResponseTypeDef ,
38
34
DisableApplicationLayerAutomaticResponseRequestRequestTypeDef ,
39
- DisassociateDRTLogBucketRequestRequestTypeDef ,
40
35
EmergencyContactTypeDef ,
41
36
ProtectionTypeDef ,
42
- UpdateProtectionGroupRequestRequestTypeDef ,
43
37
)
44
38
45
39
51
45
52
46
UNEXPECTED = "Unexpected!"
53
47
RESOURCES_BY_ACCOUNT : dict = {}
48
+ SHIELD_DRT_POLICY = "arn:aws:iam::aws:policy/service-role/AWSShieldDRTAccessPolicy"
54
49
55
50
try :
56
51
MANAGEMENT_ACCOUNT_SESSION = boto3 .Session ()
@@ -86,10 +81,10 @@ def build_resources_by_account(account_session: boto3.Session, params: dict, acc
86
81
"""
87
82
buckets : list = get_buckets_to_protect (account_session , params ["SHIELD_DRT_LOG_BUCKETS" ].split ("," ))
88
83
check_if_key_in_object ("buckets" , RESOURCES_BY_ACCOUNT [account_id ], "list" )
89
- RESOURCES_BY_ACCOUNT [account_id ]["buckets" ]: list = buckets
84
+ RESOURCES_BY_ACCOUNT [account_id ]["buckets" ] = buckets
90
85
check_if_key_in_object ("resources_to_protect" , RESOURCES_BY_ACCOUNT [account_id ], "list" )
91
86
hosted_zones : list = get_route_53_hosted_zones (account_session )
92
- RESOURCES_BY_ACCOUNT [account_id ]["resources_to_protect" ]: list = hosted_zones
87
+ RESOURCES_BY_ACCOUNT [account_id ]["resources_to_protect" ] = hosted_zones
93
88
resources_to_protect : list = get_resources_to_protect_in_account (account_id , params ["RESOURCES_TO_PROTECT" ].split ("," ))
94
89
RESOURCES_BY_ACCOUNT [account_id ]["resources_to_protect" ].extend (resources_to_protect )
95
90
@@ -321,14 +316,12 @@ def detach_drt_role_policy(account_session: boto3.Session, role_name: str) -> No
321
316
try :
322
317
LOGGER .info ("detaching DRT role policy" )
323
318
iam_client : IAMClient = account_session .client ("iam" )
324
- detach_policy_response : DetachRolePolicyRequestPolicyDetachRoleTypeDef = iam_client .detach_role_policy (
325
- RoleName = role_name , PolicyArn = "arn:aws:iam::aws:policy/service-role/AWSShieldDRTAccessPolicy"
326
- )
319
+ detach_policy_response = iam_client .detach_role_policy (RoleName = role_name , PolicyArn = SHIELD_DRT_POLICY )
327
320
api_call_details = {"API_Call" : "iam:DetachRolePolicy" , "API_Response" : detach_policy_response }
328
321
LOGGER .info (api_call_details )
329
322
except iam_client .exceptions .NoSuchEntityException as nse :
330
323
LOGGER .info (f"NoSuchEntityException { nse } " )
331
- LOGGER .info (f "Continuing..." )
324
+ LOGGER .info ("Continuing..." )
332
325
333
326
334
327
def delete_drt_role (account_session : boto3 .Session , role_name : str ) -> None :
@@ -342,7 +335,7 @@ def delete_drt_role(account_session: boto3.Session, role_name: str) -> None:
342
335
LOGGER .info ("deleting DRT role" )
343
336
iam_client : IAMClient = account_session .client ("iam" )
344
337
detach_drt_role_policy (account_session , role_name )
345
- delete_role_response : DeleteRoleRequestRequestTypeDef = iam_client .delete_role (RoleName = role_name )
338
+ delete_role_response = iam_client .delete_role (RoleName = role_name )
346
339
api_call_details = {"API_Call" : "iam:DeleteRole" , "API_Response" : delete_role_response }
347
340
LOGGER .info (api_call_details )
348
341
except iam_client .exceptions .NoSuchEntityException as nse :
@@ -379,9 +372,9 @@ def create_drt_role(account: str, role_name: str, account_session: boto3.Session
379
372
380
373
iam_client : IAMClient = account_session .client ("iam" )
381
374
role_exists = check_if_role_exists (iam_client , role_name )
382
- role_arn = ""
375
+ role_arn : str = ""
383
376
if role_exists == "" :
384
- create_role_response : CreateRoleResponseTypeDef = iam_client .create_role (
377
+ create_role_response = iam_client .create_role (
385
378
RoleName = role_name ,
386
379
AssumeRolePolicyDocument = """{
387
380
"Version": "2012-10-17",
@@ -396,15 +389,11 @@ def create_drt_role(account: str, role_name: str, account_session: boto3.Session
396
389
]
397
390
}""" ,
398
391
)
399
- attach_policy_response = iam_client .attach_role_policy (
400
- PolicyArn = "arn:aws:iam::aws:policy/service-role/AWSShieldDRTAccessPolicy" , RoleName = role_name
401
- )
402
- role_arn : str = create_role_response ["Role" ]["Arn" ]
392
+ attach_policy_response = iam_client .attach_role_policy (PolicyArn = SHIELD_DRT_POLICY , RoleName = role_name )
393
+ role_arn = create_role_response ["Role" ]["Arn" ]
403
394
else :
404
395
role_arn = role_exists
405
- attach_policy_response = iam_client .attach_role_policy (
406
- PolicyArn = "arn:aws:iam::aws:policy/service-role/AWSShieldDRTAccessPolicy" , RoleName = role_name
407
- )
396
+ attach_policy_response = iam_client .attach_role_policy (PolicyArn = SHIELD_DRT_POLICY , RoleName = role_name )
408
397
409
398
api_call_details = {"API_Call" : "iam:AttachRolePolicy" , "API_Response" : attach_policy_response }
410
399
LOGGER .info (api_call_details )
@@ -453,7 +442,7 @@ def delete_protection(shield_client: ShieldClient, resource_arn: str) -> None:
453
442
protection_id : str = get_protection_id (shield_client , resource_arn )
454
443
if protection_id != "" :
455
444
LOGGER .info (f"Deleting protection for { resource_arn } and protectionId { protection_id } " )
456
- delete_protection_response : DeleteProtectionRequestRequestTypeDef = shield_client .delete_protection (ProtectionId = protection_id )
445
+ delete_protection_response = shield_client .delete_protection (ProtectionId = protection_id )
457
446
api_call_details = {"API_Call" : "shield:DeleteProtection" , "API_Response" : delete_protection_response }
458
447
LOGGER .info (api_call_details )
459
448
else :
@@ -468,7 +457,7 @@ def associate_drt_log_bucket(shield_client: ShieldClient, log_bucket: str) -> No
468
457
shield_client: shield client
469
458
log_bucket: bucket to grant access via bucket policy
470
459
"""
471
- associate_drt_log_response : AssociateDRTLogBucketRequestRequestTypeDef = shield_client .associate_drt_log_bucket (LogBucket = log_bucket )
460
+ associate_drt_log_response = shield_client .associate_drt_log_bucket (LogBucket = log_bucket )
472
461
api_call_details = {"API_Call" : "shield:AssociateDRTLogBucket" , "API_Response" : associate_drt_log_response }
473
462
LOGGER .info (api_call_details )
474
463
@@ -480,7 +469,7 @@ def disassociate_drt_log_bucket(shield_client: ShieldClient, log_bucket: str) ->
480
469
shield_client: shield client
481
470
log_bucket: bucket to update the policy
482
471
"""
483
- disassociate_drt_log_response : DisassociateDRTLogBucketRequestRequestTypeDef = shield_client .disassociate_drt_log_bucket (LogBucket = log_bucket )
472
+ disassociate_drt_log_response = shield_client .disassociate_drt_log_bucket (LogBucket = log_bucket )
484
473
api_call_details = {"API_Call" : "shield:DisassociateDRTLogBucket" , "API_Response" : disassociate_drt_log_response }
485
474
LOGGER .info (api_call_details )
486
475
@@ -535,7 +524,7 @@ def check_proactive_engagement_enabled(shield_client: ShieldClient, params: dict
535
524
time .sleep (5 )
536
525
check_proactive_engagement_enabled (shield_client , params , retry + 1 )
537
526
else :
538
- raise ValueError ( "Proactive engagement status not found" )
527
+ return False
539
528
540
529
541
530
def check_if_protection_group_exists (shield_client : ShieldClient , protection_group_id : str ) -> bool :
@@ -581,9 +570,7 @@ def delete_protection_group(shield_client: ShieldClient, params: dict, account_i
581
570
pg_id : str = params [f"PROTECTION_GROUP_{ i } _ID" ]
582
571
if account_id == params [f"PROTECTION_GROUP_{ i } _ACCOUNT_ID" ]:
583
572
if pg_id != "" :
584
- delete_protection_group_response : DeleteProtectionGroupRequestRequestTypeDef = shield_client .delete_protection_group (
585
- ProtectionGroupId = pg_id
586
- )
573
+ delete_protection_group_response = shield_client .delete_protection_group (ProtectionGroupId = pg_id )
587
574
api_call_details = {"API_Call" : "shield:DeleteProtectionGroup" , "API_Response" : delete_protection_group_response }
588
575
LOGGER .info (api_call_details )
589
576
else :
@@ -603,7 +590,7 @@ def update_protection_group(
603
590
"APPLICATION_LOAD_BALANCER" ,
604
591
"GLOBAL_ACCELERATOR" ,
605
592
],
606
- pg_members : list ,
593
+ pg_members : list [ str ] ,
607
594
) -> None :
608
595
"""Updates an existing protection group
609
596
@@ -616,17 +603,15 @@ def update_protection_group(
616
603
pg_members: protection group members
617
604
"""
618
605
if pg_pattern == "BY_RESOURCE_TYPE" :
619
- protection_group_response : UpdateProtectionGroupRequestRequestTypeDef = shield_client .update_protection_group (
606
+ protection_group_response = shield_client .update_protection_group (
620
607
ProtectionGroupId = pg_id , Aggregation = pg_aggregation , Pattern = pg_pattern , ResourceType = pg_resource_type
621
608
)
622
609
elif pg_pattern == "ARBITRARY" :
623
- protection_group_response : UpdateProtectionGroupRequestRequestTypeDef = shield_client .update_protection_group (
610
+ protection_group_response = shield_client .update_protection_group (
624
611
ProtectionGroupId = pg_id , Aggregation = pg_aggregation , Pattern = pg_pattern , Members = pg_members .split ("," )
625
612
)
626
613
else :
627
- protection_group_response : UpdateProtectionGroupRequestRequestTypeDef = shield_client .update_protection_group (
628
- ProtectionGroupId = pg_id , Aggregation = pg_aggregation , Pattern = pg_pattern
629
- )
614
+ protection_group_response = shield_client .update_protection_group (ProtectionGroupId = pg_id , Aggregation = pg_aggregation , Pattern = pg_pattern )
630
615
api_call_details = {"API_Call" : "shield:UpdateProtectionGroup" , "API_Response" : protection_group_response }
631
616
LOGGER .info (api_call_details )
632
617
@@ -660,15 +645,15 @@ def create_protection_group(shield_client: ShieldClient, params: dict, account_i
660
645
break
661
646
LOGGER .info (f"Creating Protection_Group_{ i } in { account_id } " )
662
647
if pg_pattern == "BY_RESOURCE_TYPE" :
663
- protection_group_response : CreateProtectionGroupRequestRequestTypeDef = shield_client .create_protection_group (
648
+ protection_group_response = shield_client .create_protection_group (
664
649
ProtectionGroupId = pg_id , Aggregation = pg_aggregation , Pattern = pg_pattern , ResourceType = pg_resource_type
665
650
)
666
651
elif pg_pattern == "ARBITRARY" :
667
- protection_group_response : CreateProtectionGroupRequestRequestTypeDef = shield_client .create_protection_group (
652
+ protection_group_response = shield_client .create_protection_group (
668
653
ProtectionGroupId = pg_id , Aggregation = pg_aggregation , Pattern = pg_pattern , Members = pg_members .split ("," )
669
654
)
670
655
else :
671
- protection_group_response : CreateProtectionGroupRequestRequestTypeDef = shield_client .create_protection_group (
656
+ protection_group_response = shield_client .create_protection_group (
672
657
ProtectionGroupId = pg_id , Aggregation = pg_aggregation , Pattern = pg_pattern
673
658
)
674
659
api_call_details = {"API_Call" : "shield:CreateProtectionGroup" , "API_Response" : protection_group_response }
@@ -687,7 +672,7 @@ def check_emergency_contacts(shield_client: ShieldClient) -> bool:
687
672
emergency_contacts_response : DescribeEmergencyContactSettingsResponseTypeDef = shield_client .describe_emergency_contact_settings ()
688
673
api_call_details = {"API_Call" : "shield:DescribeEmergencyContactSettings" , "API_Response" : emergency_contacts_response }
689
674
LOGGER .info (api_call_details )
690
- if len (emergency_contacts_response ) > 0 :
675
+ if "EmergencyContactList" in emergency_contacts_response and len (emergency_contacts_response [ "EmergencyContactList" ] ) > 0 :
691
676
return True
692
677
else :
693
678
return False
@@ -716,23 +701,21 @@ def enable_proactive_engagement(shield_client: ShieldClient, params: dict) -> No
716
701
LOGGER .info (f"SHIELD_ENABLE_PROACTIVE_ENGAGEMENT is set to { params ['SHIELD_ENABLE_PROACTIVE_ENGAGEMENT' ]} " )
717
702
718
703
719
- def associate_proactive_engagement_details (shield_client : ShieldClient , params : dict ):
704
+ def associate_proactive_engagement_details (shield_client : ShieldClient , params : dict ) -> None :
720
705
"""Allow the DRT to use the contact information to reach out to the contacts
721
706
722
707
Args:
723
708
shield_client: shield client
724
709
params: environment variables
725
710
"""
726
- associate_proactive_engagement_response : AssociateProactiveEngagementDetailsRequestRequestTypeDef = (
727
- shield_client .associate_proactive_engagement_details (
728
- EmergencyContactList = [
729
- {
730
- "EmailAddress" : params ["SHIELD_PROACTIVE_ENGAGEMENT_EMAIL" ],
731
- "PhoneNumber" : params ["SHIELD_PROACTIVE_ENGAGEMENT_PHONE_NUMBER" ],
732
- "ContactNotes" : params ["SHIELD_PROACTIVE_ENGAGEMENT_NOTES" ],
733
- },
734
- ]
735
- )
711
+ associate_proactive_engagement_response = shield_client .associate_proactive_engagement_details (
712
+ EmergencyContactList = [
713
+ {
714
+ "EmailAddress" : params ["SHIELD_PROACTIVE_ENGAGEMENT_EMAIL" ],
715
+ "PhoneNumber" : params ["SHIELD_PROACTIVE_ENGAGEMENT_PHONE_NUMBER" ],
716
+ "ContactNotes" : params ["SHIELD_PROACTIVE_ENGAGEMENT_NOTES" ],
717
+ },
718
+ ]
736
719
)
737
720
api_call_details = {"API_Call" : "shield:AssociateProactiveEngagementDetails" , "API_Response" : associate_proactive_engagement_response }
738
721
LOGGER .info (api_call_details )
0 commit comments