@@ -127,27 +127,18 @@ def lookup_associated_accounts(inspector2_client: Inspector2Client, account_id:
127127 Raises:
128128 Exception: raises exception as e
129129 """
130- max_retries = 3
131- for attempt in range (max_retries ):
132- try :
133- response = inspector2_client .get_member (accountId = account_id )
134- if response ["member" ]["accountId" ] == account_id :
135- LOGGER .info (f"{ account_id } relationship status: { response ['member' ]['relationshipStatus' ]} " )
136- if response ["member" ]["relationshipStatus" ] != "ENABLED" :
137- associate_account (inspector2_client , account_id , inspector2_client .meta .region_name )
138- return True
139- return False
140- except inspector2_client .exceptions .ResourceNotFoundException :
141- return False
142- except inspector2_client .exceptions .InternalServerException as e :
143- LOGGER .warning (f"InternalServerException for account { account_id } , attempt { attempt + 1 } /{ max_retries } : { e } " )
144- if attempt == max_retries - 1 :
145- LOGGER .error (f"Failed to get member after { max_retries } attempts for account { account_id } " )
146- return False
147- sleep (2 ** attempt ) # Exponential backoff
148- except Exception as e :
149- LOGGER .error (f"Failed to get inspector members for account { account_id } : { e } " )
150- raise
130+ try :
131+ response = inspector2_client .get_member (accountId = account_id )
132+ except inspector2_client .exceptions .ResourceNotFoundException :
133+ return False
134+ except Exception as e :
135+ LOGGER .error (f"Failed to get inspector members. { e } " )
136+ raise
137+ if response ["member" ]["accountId" ] == account_id :
138+ LOGGER .info (f"{ account_id } relationship status: { response ['member' ]['relationshipStatus' ]} " )
139+ if response ["member" ]["relationshipStatus" ] != "ENABLED" :
140+ associate_account (inspector2_client , account_id )
141+ return True
151142 return False
152143
153144
@@ -529,19 +520,16 @@ def set_auto_enable_inspector_in_org(
529520 LOGGER .info (f"inspector organization already auto-enabled properly in { region } " )
530521
531522
532- def associate_account (inspector2_client : Inspector2Client , account_id : str , region : str = None ) -> AssociateMemberResponseTypeDef :
523+ def associate_account (inspector2_client : Inspector2Client , account_id : str ) -> AssociateMemberResponseTypeDef :
533524 """Associate member accounts (which also enables inspector) to the delegated admin account.
534525
535526 Args:
536527 inspector2_client (Inspector2Client): inspector SDK client
537528 account_id (str): account ID
538- region (str): AWS region for logging
539529
540530 Returns:
541531 AssociateMemberResponseTypeDef: API call response
542532 """
543- region_info = f" in { region } " if region else ""
544- LOGGER .info (f"Associating account { account_id } { region_info } " )
545533 associate_response = inspector2_client .associate_member (accountId = account_id )
546534 api_call_details = {
547535 "API_Call" : "inspector2:AssociateMember" ,
@@ -570,7 +558,7 @@ def associate_inspector_member_accounts(configuration_role_name: str, delegated_
570558 LOGGER .info (f"Account ({ account ['AccountId' ]} ) is a member" )
571559 else :
572560 LOGGER .info (f"Account ({ account ['AccountId' ]} ) is NOT a member yet" )
573- LOGGER .info (associate_account (inspector_delegated_admin_region_client , account ["AccountId" ], region ))
561+ LOGGER .info (associate_account (inspector_delegated_admin_region_client , account ["AccountId" ]))
574562
575563
576564def create_service_linked_role (account_id : str , configuration_role_name : str ) -> None :
@@ -588,4 +576,4 @@ def create_service_linked_role(account_id: str, configuration_role_name: str) ->
588576 "inspector2.amazonaws.com" ,
589577 "A service-linked role required for AWS Inspector to access your resources." ,
590578 iam_client ,
591- )
579+ )
0 commit comments