From d056f97ef452ecdb80aa6d11e5b600e50ccdb983 Mon Sep 17 00:00:00 2001 From: badra001 Date: Tue, 30 Dec 2025 16:18:53 -0500 Subject: [PATCH] v1.0.10 --- .../inf-org-crossaccount/test-cross-org.py | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/infrastructure/global/stacksets/inf-org-crossaccount/test-cross-org.py b/infrastructure/global/stacksets/inf-org-crossaccount/test-cross-org.py index 419f3060..bc7f912a 100755 --- a/infrastructure/global/stacksets/inf-org-crossaccount/test-cross-org.py +++ b/infrastructure/global/stacksets/inf-org-crossaccount/test-cross-org.py @@ -6,7 +6,7 @@ from botocore.exceptions import ClientError, ProfileNotFound # --- VERSIONING --- -__version__ = "1.0.9" +__version__ = "1.0.10" def get_args(): parser = argparse.ArgumentParser(description=f"Multi-Partition AWS Org Connectivity Test v{__version__}") @@ -34,33 +34,32 @@ def test_connectivity(args): boto3.set_stream_logger(name='botocore', level=logging.DEBUG) try: - # Initialize session + # Initialize management session session = boto3.Session(profile_name=args.profile, region_name=args.region) sts_client = session.client('sts') - # 1. Identify Partition and Identity + # Identify Partition and Identity caller = sts_client.get_caller_identity() source_arn = caller['Arn'] - - # Dynamically extract partition (e.g., 'aws', 'aws-us-gov', 'aws-cn') partition = source_arn.split(':')[1] - print("-" * 65) + print("-" * 100) print(f"AWS ORG CONNECTIVITY TEST - Version {__version__}") - print("-" * 65) + print("-" * 100) print(f"CONFIGURATION:") print(f" Partition: {partition}") print(f" Profile: {args.profile or 'Environment/Default'}") print(f" Region: {session.region_name}") print(f" Source ARN: {source_arn}") print(f" Target Role: {args.role_name}") - print("-" * 65) + print("-" * 100) org_client = session.client('organizations') paginator = org_client.get_paginator('list_accounts') - print(f"{'Account Name':<25} | {'Account ID':<15} | {'Status'}") - print("-" * 65) + # Table Header + print(f"{'Account Name':<25} | {'Alias':<25} | {'Account ID':<15} | {'Status'}") + print("-" * 100) for page in paginator.paginate(): for account in page['Accounts']: @@ -69,33 +68,36 @@ def test_connectivity(args): acc_id = account['Id'] acc_name = account['Name'] - - # 2. Construct ARN using the detected partition role_arn = f"arn:{partition}:iam::{acc_id}:role/{args.role_name}" + alias = "N/A" try: - # Attempt AssumeRole + # Assume Role assumed = sts_client.assume_role( RoleArn=role_arn, RoleSessionName=f"ConnectivityTest-v{__version__.replace('.', '-')}" ) - # Verify by creating a client inside the member account + # Create temporary session for member account creds = assumed['Credentials'] - member_sts = boto3.client( - 'sts', + member_session = boto3.Session( aws_access_key_id=creds['AccessKeyId'], aws_secret_access_key=creds['SecretAccessKey'], aws_session_token=creds['SessionToken'], region_name=session.region_name ) - member_sts.get_caller_identity() - print(f"{acc_name[:25]:<25} | {acc_id:<15} | SUCCESS") + # Fetch Account Alias + iam_client = member_session.client('iam') + aliases = iam_client.list_account_aliases().get('AccountAliases', []) + if aliases: + alias = aliases[0] + + print(f"{acc_name[:25]:<25} | {alias[:25]:<25} | {acc_id:<15} | SUCCESS") except ClientError as e: error_code = e.response['Error']['Code'] - print(f"{acc_name[:25]:<25} | {acc_id:<15} | FAILED ({error_code})") + print(f"{acc_name[:25]:<25} | {'ERROR':<25} | {acc_id:<15} | FAILED ({error_code})") except ProfileNotFound: print(f"ERROR: Profile '{args.profile}' not found.")