Skip to content

Commit

Permalink
v1.0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Dec 30, 2025
1 parent f0b3629 commit d056f97
Showing 1 changed file with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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__}")
Expand Down Expand Up @@ -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']:
Expand All @@ -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.")
Expand Down

0 comments on commit d056f97

Please sign in to comment.