Sign in
agent:
Auto Exec

Filter out Unencrypted AWS S3 Buckets

There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.

This task selectively identifies and lists all Amazon S3 buckets that lack encryption within an AWS account. In the context of security and compliance, it's crucial to pinpoint these unencrypted buckets as they are vulnerable to unauthorized data access and potential data breaches. With this list, organizations can immediately recognize and address security risks by applying necessary encryption, ensuring that data stored in these S3 buckets is protected and compliant with data privacy standards and regulations.

import boto3 from botocore.exceptions import BotoCoreError, NoCredentialsError, PartialCredentialsError creds = _get_creds(cred_label)['creds'] access_key = creds['username'] secret_key = creds['password'] def filter_unencrypted_buckets(all_buckets): unencrypted_buckets = [] try: # Creating a Boto3 S3 client s3 = boto3.client('s3',aws_access_key_id=access_key,aws_secret_access_key=secret_key) for bucket in all_buckets: try: # Attempting to get the encryption configuration of a bucket response = s3.get_bucket_encryption(Bucket=bucket) except s3.exceptions.ClientError as e: # Handling specific error when the encryption configuration is not found if e.response['Error']['Code'] == 'ServerSideEncryptionConfigurationNotFoundError': unencrypted_buckets.append(bucket) else: print(f"Unexpected error getting encryption status for bucket {bucket}: {e}") except Exception as e: # Handling general exceptions print(f"Unexpected error getting encryption status for bucket {bucket}: {e}") return unencrypted_buckets except NoCredentialsError: print("Error: AWS credentials not found") return None except PartialCredentialsError: print("Error: Incomplete AWS credentials") return None except BotoCoreError as e: print(f"Boto3 core error: {e}") return None except Exception as e: print(f"Unexpected error: {e}") return None # Main block ''' # Example list of buckets all_buckets = ['aws-cost-usage-redshift-quicksight-gzip-csv-type-file', 'backup-large-files-from-ec2-instances-filesystem', 'cost-usage-test-redshift', 'encryption-test-bucket-789', 'ssm-session-logging-123'] ''' # buckets received from parent task all_buckets = buckets unencrypted_buckets = filter_unencrypted_buckets(all_buckets) if unencrypted_buckets is not None: if unencrypted_buckets: print("Found the following unencrypted S3 buckets:") for bucket in unencrypted_buckets: print(bucket) else: print("No unencrypted S3 buckets found.") else: print("Error occurred while trying to filter unencrypted S3 buckets.") context.proceed=False
copied