30
30
LOGGER = logging .getLogger (__name__ )
31
31
log_level = os .environ .get ("LOG_LEVEL" , logging .INFO )
32
32
LOGGER .setLevel (log_level )
33
+ LOGGER .info (f"boto3 version: { boto3 .__version__ } " )
33
34
34
35
# Initialise the helper
35
36
helper = CfnResource (json_logging = True , log_level = "DEBUG" , boto_level = "CRITICAL" )
36
37
37
38
# Global Variables
38
39
UNEXPECTED = "Unexpected!"
39
40
BOTO3_CONFIG = Config (retries = {"max_attempts" : 10 , "mode" : "standard" })
41
+ MAX_RETRIES = 12
42
+ SLEEP_TIME = 5
40
43
41
44
42
45
def assume_role (role : str , role_session_name : str , account : str = None , session : boto3 .Session = None ) -> boto3 .Session :
@@ -70,7 +73,7 @@ def assume_role(role: str, role_session_name: str, account: str = None, session:
70
73
)
71
74
72
75
73
- def associate_admin_account (delegated_admin_account_id : str ) -> None :
76
+ def associate_admin_account (delegated_admin_account_id : str ) -> None : # noqa CCR001
74
77
"""Associate an administrator account for Firewall Manager.
75
78
76
79
Args:
@@ -79,6 +82,7 @@ def associate_admin_account(delegated_admin_account_id: str) -> None:
79
82
Raises:
80
83
ValueError: Admin account already exists.
81
84
"""
85
+ LOGGER .info (f"Admin account: { delegated_admin_account_id } " )
82
86
firewall_manager_client : FMSClient = boto3 .client ("fms" , region_name = "us-east-1" , config = BOTO3_CONFIG ) # APIs only work in us-east-1 region
83
87
84
88
try :
@@ -90,8 +94,32 @@ def associate_admin_account(delegated_admin_account_id: str) -> None:
90
94
except firewall_manager_client .exceptions .ResourceNotFoundException :
91
95
LOGGER .info ("Administrator account does not exist. Continuing..." )
92
96
93
- LOGGER .info ("Associating admin account in Firewall Manager" )
94
- firewall_manager_client .associate_admin_account (AdminAccount = delegated_admin_account_id )
97
+ LOGGER .info ("Attempting to associate the admin account in Firewall Manager" )
98
+ try :
99
+ firewall_manager_client .associate_admin_account (AdminAccount = delegated_admin_account_id )
100
+ except botocore .exceptions .ClientError as error :
101
+ LOGGER .info (f"Error associating admin account: { error .response ['Error' ]['Message' ]} " )
102
+ if error .response ["Error" ]["Code" ] == "InvalidOperationException" :
103
+ LOGGER .info (f"Invalid operation exception occurred; waiting { SLEEP_TIME } seconds before trying again..." )
104
+ i_retry = 0
105
+ while i_retry <= MAX_RETRIES :
106
+ time .sleep (SLEEP_TIME )
107
+ try :
108
+ firewall_manager_client .associate_admin_account (AdminAccount = delegated_admin_account_id )
109
+ associated = True
110
+ except botocore .exceptions .ClientError as retry_error :
111
+ LOGGER .info (f"Attempt { i_retry } - error associating admin account: { retry_error .response ['Error' ]['Message' ]} " )
112
+ associated = False
113
+ if associated is True :
114
+ break
115
+ else :
116
+ i_retry += 1
117
+ if associated is False :
118
+ LOGGER .error ("Unable to associate admin account." )
119
+ raise ValueError ("Unable to associate admin account." ) from None
120
+ else :
121
+ LOGGER .error ("Unexpected error. Unable to associate admin account due to error unrelated to an invalid operation." )
122
+ raise ValueError ("Unexpected error. Unable to associate admin account due to error unrelated to an invalid operation." ) from None
95
123
LOGGER .info ("...Waiting 5 minutes for admin account association." )
96
124
time .sleep (300 ) # use 5 minute wait
97
125
while True :
0 commit comments