agent: |
gxVzUwi6V7rXwSQnJMWwEnforce S3 Bucket Write Protection using Public Access Block Settings
Enforce S3 Bucket Write Protection using Public Access Block Settings
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
This task programmatically tightens security on a specified AWS S3 bucket by disabling public write access. It modifies the bucket's Block Public Access settings, ensuring compliance with data security standards. This preventive measure is critical in safeguarding sensitive data from unauthorized modifications.
inputs
outputs
import boto3
from botocore.exceptions import ClientError, NoCredentialsError, BotoCoreError
creds = _get_creds(cred_label)['creds']
access_key = creds['username']
secret_key = creds['password']
def disable_public_write_access(bucket_name):
"""
Disables public write access for a specified S3 bucket by updating Block Public Access settings and ACL.
"""
s3 = boto3.client('s3',aws_access_key_id=access_key,aws_secret_access_key=secret_key)
# Update Block Public Access settings to block public ACLs
try:
s3.put_public_access_block(
Bucket=bucket_name,
PublicAccessBlockConfiguration={
'BlockPublicAcls': True,
'IgnorePublicAcls': True,
'BlockPublicPolicy': True,
'RestrictPublicBuckets': True
}
)
print(f"Updated Block Public Access settings for '{bucket_name}'.")
except ClientError as e:
print(f"Failed to update Block Public Access settings for '{bucket_name}': {e}")
raise
try:
if bucket_name:
#bucket_name = 'your-bucket-name'
disable_public_write_access(bucket_name)
else:
print("Please provide a bucket name to restrict public access")
except NoCredentialsError:
print("No AWS credentials found. Please configure your credentials.")
except BotoCoreError as e:
print(f"An error occurred accessing AWS S3 service: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
copied