Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Mar 18, 2026
1 parent e4eb07d commit e7570b3
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions local-app/python-tools/cross-organization/check_security_groups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import boto3

# --- VERSIONING ---
__version__ = "1.0.0"

def account_task(account_session, account_id, account_name, region):
"""
Scans each region for Security Groups and collects metadata.
"""
results = {"alias": "N/A", "data": {}}
try:
# Get all enabled regions for the account
ec2_global = account_session.client('ec2', region_name=region)
regions = [r['RegionName'] for r in ec2_global.describe_regions()['Regions']]

for reg in regions:
ec2 = account_session.client('ec2', region_name=reg)
try:
# Describe security groups for the current region
paginator = ec2.get_paginator('describe_security_groups')
for page in paginator.paginate():
for sg in page['SecurityGroups']:
group_id = sg['GroupId']

# Extract all tags into a dictionary
tags = {t['Key']: t['Value'] for t in sg.get('Tags', [])}

# Keyed by region:group_id for the global aggregator
results["data"][f"{reg}:{group_id}"] = {
"resource": f"arn:aws:ec2:{reg}:{account_id}:security-group/{group_id}",
"vpc_id": sg.get('VpcId', 'N/A'),
"owner_id": sg.get('OwnerId', 'N/A'),
"group_name": sg.get('GroupName', 'N/A'),
"description": sg.get('Description', 'N/A'),
"group_id": group_id,
"region": reg,
"tags": tags
}
except Exception:
# Skip regions that may have restricted access
continue

results["data"]["account_summary"] = {"_summary": f"SG_COUNT:{len(results['data'])}"}

except Exception as e:
results["error"] = str(e)

return results

0 comments on commit e7570b3

Please sign in to comment.