16
16
17
17
import boto3
18
18
import common
19
+ from botocore .exceptions import ClientError
19
20
20
21
if TYPE_CHECKING :
21
22
from mypy_boto3_iam import IAMClient
22
- from mypy_boto3_iam .type_defs import (
23
- AttachRolePolicyResponseTypeDef ,
24
- CreateRoleResponseTypeDef ,
25
- DeleteRoleRequestRequestTypeDef ,
26
- DetachRolePolicyRequestPolicyDetachRoleTypeDef ,
27
- )
23
+ from mypy_boto3_iam .type_defs import CreateRoleResponseTypeDef , DeleteRoleRequestRequestTypeDef , DetachRolePolicyRequestPolicyDetachRoleTypeDef
28
24
from mypy_boto3_organizations import OrganizationsClient
29
25
from mypy_boto3_route53 import Route53Client
30
26
from mypy_boto3_route53 .type_defs import ListHostedZonesResponseTypeDef
34
30
AssociateDRTLogBucketRequestRequestTypeDef ,
35
31
AssociateProactiveEngagementDetailsRequestRequestTypeDef ,
36
32
CreateProtectionGroupRequestRequestTypeDef ,
37
- CreateProtectionGroupResponseTypeDef ,
38
33
CreateProtectionResponseTypeDef ,
39
- CreateSubscriptionRequestRequestTypeDef ,
40
- CreateSubscriptionResponseTypeDef ,
41
34
DeleteProtectionGroupRequestRequestTypeDef ,
42
- DeleteProtectionGroupResponseTypeDef ,
43
35
DeleteProtectionRequestRequestTypeDef ,
44
36
DescribeEmergencyContactSettingsResponseTypeDef ,
45
37
DescribeProtectionResponseTypeDef ,
48
40
DisassociateDRTLogBucketRequestRequestTypeDef ,
49
41
EmergencyContactTypeDef ,
50
42
ProtectionTypeDef ,
51
- UpdateEmergencyContactSettingsRequestRequestTypeDef ,
52
- UpdateEmergencyContactSettingsResponseTypeDef ,
53
43
UpdateProtectionGroupRequestRequestTypeDef ,
54
44
)
55
45
@@ -137,7 +127,7 @@ def get_route_53_hosted_zones(account_session: boto3.Session) -> list:
137
127
"""
138
128
route53_client : Route53Client = account_session .client ("route53" )
139
129
hosted_zones : ListHostedZonesResponseTypeDef = route53_client .list_hosted_zones ()
140
- LOGGER .info ("[INFO] Listing hosted zones from the Route53\n \n " )
130
+ LOGGER .info ("[INFO] Listing hosted zones from the Route53" )
141
131
marker : bool = True
142
132
hosted_zone_arns : list = []
143
133
while marker :
@@ -218,7 +208,7 @@ def update_emergency_contacts(shield_client: ShieldClient, params: dict, is_dele
218
208
"""
219
209
emergency_contacts : Sequence [EmergencyContactTypeDef ] = []
220
210
if not is_delete :
221
- emergency_contacts : Sequence [ EmergencyContactTypeDef ] = build_emergency_contacts (params )
211
+ emergency_contacts = build_emergency_contacts (params )
222
212
LOGGER .info (f"Updating emergency contacts to { emergency_contacts } " )
223
213
shield_client .update_emergency_contact_settings (EmergencyContactList = emergency_contacts )
224
214
else :
@@ -304,7 +294,7 @@ def create_subscription(shield_client: ShieldClient) -> None:
304
294
if subscription_enabled :
305
295
LOGGER .info ("Shield Advanced Subscription is already enabled" )
306
296
else :
307
- enable_shield_response : CreateSubscriptionResponseTypeDef = shield_client .create_subscription ()
297
+ enable_shield_response = shield_client .create_subscription ()
308
298
api_call_details = {"API_Call" : "shield:CreateSubscription" , "API_Response" : enable_shield_response }
309
299
LOGGER .info (api_call_details )
310
300
@@ -410,13 +400,13 @@ def create_drt_role(account: str, role_name: str, account_session: boto3.Session
410
400
]
411
401
}""" ,
412
402
)
413
- attach_policy_response : AttachRolePolicyResponseTypeDef = iam_client .attach_role_policy (
403
+ attach_policy_response = iam_client .attach_role_policy (
414
404
PolicyArn = "arn:aws:iam::aws:policy/service-role/AWSShieldDRTAccessPolicy" , RoleName = role_name
415
405
)
416
406
role_arn : str = create_role_response ["Role" ]["Arn" ]
417
407
else :
418
408
role_arn = role_exists
419
- attach_policy_response : AttachRolePolicyResponseTypeDef = iam_client .attach_role_policy (
409
+ attach_policy_response = iam_client .attach_role_policy (
420
410
PolicyArn = "arn:aws:iam::aws:policy/service-role/AWSShieldDRTAccessPolicy" , RoleName = role_name
421
411
)
422
412
@@ -549,8 +539,7 @@ def check_proactive_engagement_enabled(shield_client: ShieldClient, params: dict
549
539
time .sleep (5 )
550
540
check_proactive_engagement_enabled (shield_client , params , retry + 1 )
551
541
else :
552
- # TODO take a look at this and see if I should raise an error instead
553
- return True
542
+ raise ValueError ("Proactive engagement status not found" )
554
543
555
544
556
545
def check_if_protection_group_exists (shield_client : ShieldClient , protection_group_id : str ) -> bool :
@@ -596,7 +585,7 @@ def delete_protection_group(shield_client: ShieldClient, params: dict, account_i
596
585
pg_id : str = params [f"PROTECTION_GROUP_{ i } _ID" ]
597
586
if account_id == params [f"PROTECTION_GROUP_{ i } _ACCOUNT_ID" ]:
598
587
if pg_id != "" :
599
- delete_protection_group_response : DeleteProtectionGroupResponseTypeDef = shield_client .delete_protection_group (
588
+ delete_protection_group_response : DeleteProtectionGroupRequestRequestTypeDef = shield_client .delete_protection_group (
600
589
ProtectionGroupId = pg_id
601
590
)
602
591
api_call_details = {"API_Call" : "shield:DeleteProtectionGroup" , "API_Response" : delete_protection_group_response }
@@ -651,10 +640,11 @@ def create_protection_group(shield_client: ShieldClient, params: dict, account_i
651
640
652
641
Args:
653
642
shield_client: shield client
654
- params: environment variablrd
643
+ params: environment variables
655
644
account_id: AWS account id
656
645
"""
657
646
for i in range (0 , 5 ):
647
+ print (i )
658
648
pg_id : str = params [f"PROTECTION_GROUP_{ i } _ID" ]
659
649
pg_account_id : str = params [f"PROTECTION_GROUP_{ i } _ACCOUNT_ID" ]
660
650
pg_aggregation : Literal ["SUM" , "MEAN" , "MAX" ] = params [f"PROTECTION_GROUP_{ i } _AGGREGATION" ]
@@ -667,24 +657,26 @@ def create_protection_group(shield_client: ShieldClient, params: dict, account_i
667
657
"APPLICATION_LOAD_BALANCER" ,
668
658
"GLOBAL_ACCELERATOR" ,
669
659
] = params [f"PROTECTION_GROUP_{ i } _RESOURCE_TYPE" ]
670
-
660
+ print ( f" pg_account_id { pg_account_id } " )
671
661
pg_members : list = params [f"PROTECTION_GROUP_{ i } _MEMBERS" ]
662
+ print (f"pg_members{ pg_members } " )
663
+ print (f"i { i } " )
672
664
if pg_id != "" and pg_account_id == account_id :
673
665
if check_if_protection_group_exists (shield_client , pg_id ):
674
666
LOGGER .info (f"Protection_Group_{ i } already exists in { account_id } " )
675
667
update_protection_group (shield_client , pg_id , pg_aggregation , pg_pattern , pg_resource_type , pg_members )
676
668
break
677
669
LOGGER .info (f"Creating Protection_Group_{ i } in { account_id } " )
678
670
if pg_pattern == "BY_RESOURCE_TYPE" :
679
- protection_group_response = shield_client .create_protection_group (
671
+ protection_group_response : CreateProtectionGroupRequestRequestTypeDef = shield_client .create_protection_group (
680
672
ProtectionGroupId = pg_id , Aggregation = pg_aggregation , Pattern = pg_pattern , ResourceType = pg_resource_type
681
673
)
682
674
elif pg_pattern == "ARBITRARY" :
683
- protection_group_response = shield_client .create_protection_group (
675
+ protection_group_response : CreateProtectionGroupRequestRequestTypeDef = shield_client .create_protection_group (
684
676
ProtectionGroupId = pg_id , Aggregation = pg_aggregation , Pattern = pg_pattern , Members = pg_members .split ("," )
685
677
)
686
678
else :
687
- protection_group_response = shield_client .create_protection_group (
679
+ protection_group_response : CreateProtectionGroupRequestRequestTypeDef = shield_client .create_protection_group (
688
680
ProtectionGroupId = pg_id , Aggregation = pg_aggregation , Pattern = pg_pattern
689
681
)
690
682
api_call_details = {"API_Call" : "shield:CreateProtectionGroup" , "API_Response" : protection_group_response }
@@ -715,6 +707,7 @@ def enable_proactive_engagement(shield_client: ShieldClient, params: dict) -> No
715
707
shield_client: shield client
716
708
params: environment variables
717
709
"""
710
+ print (f"Before IF SHIELD_ENABLE_PROACTIVE_ENGAGEMENT is set to { params ['SHIELD_ENABLE_PROACTIVE_ENGAGEMENT' ]} " )
718
711
if params ["SHIELD_ENABLE_PROACTIVE_ENGAGEMENT" ] == "true" :
719
712
if check_proactive_engagement_enabled (shield_client , params ):
720
713
update_emergency_contacts (shield_client , params )
@@ -757,8 +750,14 @@ def disable_proactive_engagement(shield_client: ShieldClient) -> None:
757
750
Args:
758
751
shield_client: shield client
759
752
"""
760
- disable_proactive_engagement_response : DisableApplicationLayerAutomaticResponseRequestRequestTypeDef = (
761
- shield_client .disable_proactive_engagement ()
762
- )
763
- api_call_details = {"API_Call" : "shield:DisableProactiveEngagement" , "API_Response" : disable_proactive_engagement_response }
764
- LOGGER .info (api_call_details )
753
+ try :
754
+ disable_proactive_engagement_response : DisableApplicationLayerAutomaticResponseRequestRequestTypeDef = (
755
+ shield_client .disable_proactive_engagement ()
756
+ )
757
+ api_call_details = {"API_Call" : "shield:DisableProactiveEngagement" , "API_Response" : disable_proactive_engagement_response }
758
+ LOGGER .info (api_call_details )
759
+ except ClientError as e :
760
+ if e .response ["Error" ]["Code" ] == "InvalidOperationException" :
761
+ LOGGER .exception (e )
762
+ else :
763
+ raise e
0 commit comments