Created
October 15, 2023 08:33
-
-
Save shaposhnikoff/4fdba8b56d14d7e6f50663a8f82c3c22 to your computer and use it in GitHub Desktop.
AWS S3 bucket policy script
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
import boto3 | |
import json,re | |
bucket_name_regexp = '.*-data-.*' | |
def list_all_s3_buckets_and_acls(): | |
s3_client = boto3.client('s3') | |
response = s3_client.list_buckets() | |
for bucket in response['Buckets']: | |
if re.match(bucket_name_regexp , bucket['Name']): | |
bucket_name = bucket['Name'] | |
print(bucket_name) | |
# Print Bucket Policy | |
try: | |
bucket_policy = s3_client.get_bucket_policy(Bucket=bucket_name) | |
if 'Policy' in bucket_policy: | |
policy = json.loads(bucket_policy['Policy']) | |
print('Bucket Policy:', policy) | |
except: | |
print('No Bucket Policy') | |
# Print ACLs | |
acl_response = s3_client.get_bucket_acl(Bucket=bucket_name) | |
for acl in acl_response['Grants']: | |
permission = acl['Permission'] | |
grantee = acl['Grantee'] | |
print('Permission:', permission, 'Grantee:', grantee) | |
print('Owner:', acl_response['Owner']) | |
print('------------------') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment