Skip to content

Commit f1c2044

Browse files
author
Scott Stout
committed
warn on buckets with website hosting enabled
1 parent 919ef22 commit f1c2044

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

s3-block-public-access/enable-s3-block-public-access.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def main(args, logger):
5656
def fix_bucket(s3_client, bucket, args, f=None):
5757
'''Determine if the Bucket is safe to fix. Do the fix or write the AWS CLI or just notify based on args '''
5858
if not is_safe_to_fix_bucket(s3_client, bucket):
59-
logger.warning(f"Bucket {bucket} has a bucket policy or ACLs that could conflict with Block Public Access. Not Enabling it.")
59+
logger.warning(f"Bucket {bucket} has a bucket policy, conflicting ACLs or Website Hosting enabled which could conflict with Block Public Access. Not Enabling it.")
6060
return(False)
6161
elif args.actually_do_it is True:
6262
logger.info(f"Enabling Block Public Access on {bucket}")
@@ -77,7 +77,7 @@ def fix_bucket(s3_client, bucket, args, f=None):
7777

7878
def is_safe_to_fix_bucket(s3_client, bucket_name):
7979
'''Check ACLS and Policy to see if Bucket is safe to fix'''
80-
return(is_safe_to_fix_by_acl(s3_client, bucket_name) and is_safe_to_fix_by_policy(s3_client, bucket_name))
80+
return(is_safe_to_fix_by_acl(s3_client, bucket_name) and is_safe_to_fix_by_policy(s3_client, bucket_name) and is_safe_to_fix_by_bucket_website(s3_client, bucket_name))
8181

8282

8383
def is_safe_to_fix_by_acl(s3_client, bucket_name):
@@ -121,6 +121,19 @@ def is_safe_to_fix_by_policy(s3_client, bucket_name):
121121
else:
122122
raise
123123

124+
def is_safe_to_fix_by_bucket_website(s3_client, bucket_name):
125+
'''Inspect Bucket Website and determine if this bucket is safe to fix'''
126+
127+
try:
128+
s3_client.get_bucket_website(Bucket=bucket_name)
129+
logger.warning(f"Bucket {bucket_name} is Hosting a Website!")
130+
return(False) # Not Safe, Bucket Website Hosting enabled
131+
except ClientError as e:
132+
if e.response['Error']['Code'] == 'NoSuchWebsiteConfiguration':
133+
# No Bucket Website Hosting
134+
return(True)
135+
else:
136+
raise
124137

125138
def enable_block_public_access(s3_client, bucket_name):
126139
'''Actually perform the enabling of block public access and checking of the status code'''

0 commit comments

Comments
 (0)