1616
1717import boto3
1818import common
19+ from botocore .exceptions import ClientError
1920
2021if TYPE_CHECKING :
2122 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
2824 from mypy_boto3_organizations import OrganizationsClient
2925 from mypy_boto3_route53 import Route53Client
3026 from mypy_boto3_route53 .type_defs import ListHostedZonesResponseTypeDef
3430 AssociateDRTLogBucketRequestRequestTypeDef ,
3531 AssociateProactiveEngagementDetailsRequestRequestTypeDef ,
3632 CreateProtectionGroupRequestRequestTypeDef ,
37- CreateProtectionGroupResponseTypeDef ,
3833 CreateProtectionResponseTypeDef ,
39- CreateSubscriptionRequestRequestTypeDef ,
40- CreateSubscriptionResponseTypeDef ,
4134 DeleteProtectionGroupRequestRequestTypeDef ,
42- DeleteProtectionGroupResponseTypeDef ,
4335 DeleteProtectionRequestRequestTypeDef ,
4436 DescribeEmergencyContactSettingsResponseTypeDef ,
4537 DescribeProtectionResponseTypeDef ,
4840 DisassociateDRTLogBucketRequestRequestTypeDef ,
4941 EmergencyContactTypeDef ,
5042 ProtectionTypeDef ,
51- UpdateEmergencyContactSettingsRequestRequestTypeDef ,
52- UpdateEmergencyContactSettingsResponseTypeDef ,
5343 UpdateProtectionGroupRequestRequestTypeDef ,
5444 )
5545
@@ -137,7 +127,7 @@ def get_route_53_hosted_zones(account_session: boto3.Session) -> list:
137127 """
138128 route53_client : Route53Client = account_session .client ("route53" )
139129 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" )
141131 marker : bool = True
142132 hosted_zone_arns : list = []
143133 while marker :
@@ -218,7 +208,7 @@ def update_emergency_contacts(shield_client: ShieldClient, params: dict, is_dele
218208 """
219209 emergency_contacts : Sequence [EmergencyContactTypeDef ] = []
220210 if not is_delete :
221- emergency_contacts : Sequence [ EmergencyContactTypeDef ] = build_emergency_contacts (params )
211+ emergency_contacts = build_emergency_contacts (params )
222212 LOGGER .info (f"Updating emergency contacts to { emergency_contacts } " )
223213 shield_client .update_emergency_contact_settings (EmergencyContactList = emergency_contacts )
224214 else :
@@ -304,7 +294,7 @@ def create_subscription(shield_client: ShieldClient) -> None:
304294 if subscription_enabled :
305295 LOGGER .info ("Shield Advanced Subscription is already enabled" )
306296 else :
307- enable_shield_response : CreateSubscriptionResponseTypeDef = shield_client .create_subscription ()
297+ enable_shield_response = shield_client .create_subscription ()
308298 api_call_details = {"API_Call" : "shield:CreateSubscription" , "API_Response" : enable_shield_response }
309299 LOGGER .info (api_call_details )
310300
@@ -410,13 +400,13 @@ def create_drt_role(account: str, role_name: str, account_session: boto3.Session
410400 ]
411401 }""" ,
412402 )
413- attach_policy_response : AttachRolePolicyResponseTypeDef = iam_client .attach_role_policy (
403+ attach_policy_response = iam_client .attach_role_policy (
414404 PolicyArn = "arn:aws:iam::aws:policy/service-role/AWSShieldDRTAccessPolicy" , RoleName = role_name
415405 )
416406 role_arn : str = create_role_response ["Role" ]["Arn" ]
417407 else :
418408 role_arn = role_exists
419- attach_policy_response : AttachRolePolicyResponseTypeDef = iam_client .attach_role_policy (
409+ attach_policy_response = iam_client .attach_role_policy (
420410 PolicyArn = "arn:aws:iam::aws:policy/service-role/AWSShieldDRTAccessPolicy" , RoleName = role_name
421411 )
422412
@@ -549,8 +539,7 @@ def check_proactive_engagement_enabled(shield_client: ShieldClient, params: dict
549539 time .sleep (5 )
550540 check_proactive_engagement_enabled (shield_client , params , retry + 1 )
551541 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" )
554543
555544
556545def 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
596585 pg_id : str = params [f"PROTECTION_GROUP_{ i } _ID" ]
597586 if account_id == params [f"PROTECTION_GROUP_{ i } _ACCOUNT_ID" ]:
598587 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 (
600589 ProtectionGroupId = pg_id
601590 )
602591 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
651640
652641 Args:
653642 shield_client: shield client
654- params: environment variablrd
643+ params: environment variables
655644 account_id: AWS account id
656645 """
657646 for i in range (0 , 5 ):
647+ print (i )
658648 pg_id : str = params [f"PROTECTION_GROUP_{ i } _ID" ]
659649 pg_account_id : str = params [f"PROTECTION_GROUP_{ i } _ACCOUNT_ID" ]
660650 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
667657 "APPLICATION_LOAD_BALANCER" ,
668658 "GLOBAL_ACCELERATOR" ,
669659 ] = params [f"PROTECTION_GROUP_{ i } _RESOURCE_TYPE" ]
670-
660+ print ( f" pg_account_id { pg_account_id } " )
671661 pg_members : list = params [f"PROTECTION_GROUP_{ i } _MEMBERS" ]
662+ print (f"pg_members{ pg_members } " )
663+ print (f"i { i } " )
672664 if pg_id != "" and pg_account_id == account_id :
673665 if check_if_protection_group_exists (shield_client , pg_id ):
674666 LOGGER .info (f"Protection_Group_{ i } already exists in { account_id } " )
675667 update_protection_group (shield_client , pg_id , pg_aggregation , pg_pattern , pg_resource_type , pg_members )
676668 break
677669 LOGGER .info (f"Creating Protection_Group_{ i } in { account_id } " )
678670 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 (
680672 ProtectionGroupId = pg_id , Aggregation = pg_aggregation , Pattern = pg_pattern , ResourceType = pg_resource_type
681673 )
682674 elif pg_pattern == "ARBITRARY" :
683- protection_group_response = shield_client .create_protection_group (
675+ protection_group_response : CreateProtectionGroupRequestRequestTypeDef = shield_client .create_protection_group (
684676 ProtectionGroupId = pg_id , Aggregation = pg_aggregation , Pattern = pg_pattern , Members = pg_members .split ("," )
685677 )
686678 else :
687- protection_group_response = shield_client .create_protection_group (
679+ protection_group_response : CreateProtectionGroupRequestRequestTypeDef = shield_client .create_protection_group (
688680 ProtectionGroupId = pg_id , Aggregation = pg_aggregation , Pattern = pg_pattern
689681 )
690682 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
715707 shield_client: shield client
716708 params: environment variables
717709 """
710+ print (f"Before IF SHIELD_ENABLE_PROACTIVE_ENGAGEMENT is set to { params ['SHIELD_ENABLE_PROACTIVE_ENGAGEMENT' ]} " )
718711 if params ["SHIELD_ENABLE_PROACTIVE_ENGAGEMENT" ] == "true" :
719712 if check_proactive_engagement_enabled (shield_client , params ):
720713 update_emergency_contacts (shield_client , params )
@@ -757,8 +750,14 @@ def disable_proactive_engagement(shield_client: ShieldClient) -> None:
757750 Args:
758751 shield_client: shield client
759752 """
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