-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
91 additions
and
174 deletions.
There are no files selected for viewing
65 changes: 25 additions & 40 deletions
65
local-app/python-tools/cross-organization/check_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,66 +1,51 @@ | ||
| import boto3 | ||
| from botocore.exceptions import ClientError | ||
|
|
||
| def account_task(account_session, account_id, account_name, region): | ||
| """ | ||
| Task: Audits AWS Config across ALL enabled regions in the account. | ||
| Captures: Status, Retention, S3 Bucket, SNS Topic, and Delivery Frequency. | ||
| Task: Deep Audit of AWS Config across all regions. | ||
| Returns: Data structured by region for JSON/CSV processing. | ||
| """ | ||
| res = {"alias": "None", "status": "PENDING", "details": ""} | ||
| results = {"alias": "None", "status": "SUCCESS", "data": {}} | ||
|
|
||
| try: | ||
| # 1. Get IAM Alias | ||
| iam = account_session.client('iam') | ||
| aliases = iam.list_account_aliases().get('AccountAliases', []) | ||
| res["alias"] = aliases[0] if aliases else "None" | ||
| results["alias"] = aliases[0] if aliases else "None" | ||
|
|
||
| # 2. Determine all active regions for this account | ||
| # We use EC2 to describe regions as it's the most reliable way to find 'opt-in' status | ||
| ec2 = account_session.client('ec2', region_name=region) | ||
| regions = [r['RegionName'] for r in ec2.describe_regions()['Regions']] | ||
|
|
||
| report_lines = [] | ||
| global_recorder_count = 0 | ||
|
|
||
| for reg in regions: | ||
| reg_status = "OFF" | ||
| config = account_session.client('config', region_name=reg) | ||
|
|
||
| # Get Recorder Status | ||
| # Fetch Config state | ||
| recorders = config.describe_configuration_recorders().get('ConfigurationRecorders', []) | ||
| delivery = config.describe_delivery_channels().get('DeliveryChannels', []) | ||
| retention = config.describe_retention_configurations().get('RetentionConfigurations', []) | ||
|
|
||
| # Default values | ||
| s3 = "N/A" | ||
| sns = "N/A" | ||
| freq = "N/A" | ||
| ret_days = "Default(7yrs)" | ||
|
|
||
| # Store regional metrics | ||
| reg_data = { | ||
| "recorder_enabled": "False", | ||
| "global_resources": "False", | ||
| "s3_bucket": "N/A", | ||
| "sns_topic": "N/A", | ||
| "delivery_freq": "N/A", | ||
| "retention_days": "2555 (7yr)" | ||
| } | ||
|
|
||
| if recorders: | ||
| reg_status = "ON" | ||
| if recorders[0].get('recordingGroup', {}).get('includeGlobalResourceTypes'): | ||
| global_recorder_count += 1 | ||
| reg_status = "ON+GLOBAL" | ||
| reg_data["recorder_enabled"] = "True" | ||
| reg_data["global_resources"] = str(recorders[0].get('recordingGroup', {}).get('includeGlobalResourceTypes', False)) | ||
|
|
||
| if delivery: | ||
| s3 = delivery[0].get('s3BucketName', 'N/A') | ||
| sns = delivery[0].get('snsTopicARN', 'N/A').split(':')[-1] # Shorten ARN | ||
| freq = delivery[0].get('configSnapshotDeliveryProperties', {}).get('deliveryFrequency', '24h') | ||
| reg_data["s3_bucket"] = delivery[0].get('s3BucketName', 'N/A') | ||
| reg_data["sns_topic"] = delivery[0].get('snsTopicARN', 'N/A').split(':')[-1] | ||
| reg_data["delivery_freq"] = delivery[0].get('configSnapshotDeliveryProperties', {}).get('deliveryFrequency', '24h') | ||
|
|
||
| if retention: | ||
| ret_days = f"{retention[0].get('RetentionPeriodInDays')} days" | ||
| reg_data["retention_days"] = str(retention[0].get('RetentionPeriodInDays')) | ||
|
|
||
| report_lines.append(f"[{reg}: {reg_status} | S3:{s3} | Ret:{ret_days} | Freq:{freq}]") | ||
|
|
||
| # Summarize for the main table | ||
| res["status"] = f"OK({len(regions)}reg)" if global_recorder_count == 1 else f"WARN({global_recorder_count} Globals)" | ||
| res["details"] = " | ".join(report_lines) | ||
|
|
||
| except ClientError as e: | ||
| res["status"] = f"FAIL({e.response['Error']['Code']})" | ||
| results["data"][reg] = reg_data | ||
|
|
||
| except Exception as e: | ||
| res["status"] = "ERROR" | ||
| results["status"] = f"ERROR: {str(e)}" | ||
|
|
||
| return res | ||
|
|
||
| return results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters