Skip to content

Commit

Permalink
Ensure public bucket policies are allowed before trying to write one (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
RealOrangeOne authored Jul 12, 2023
1 parent efea390 commit 0d323c7
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions buckup/bucket_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ def set_bucket_policy(self, bucket, user, allow_public_acls, public_get_object_p
policy_statement = []
public_access = bool(public_get_object_paths)

# NB: This API doesn't exist on a `Bucket`
self.s3_client.put_public_access_block(
Bucket=bucket.name,
PublicAccessBlockConfiguration={
"BlockPublicAcls": not allow_public_acls,
"IgnorePublicAcls": not allow_public_acls,
"BlockPublicPolicy": not public_access,
"RestrictPublicBuckets": not public_access
}
)
if public_access or allow_public_acls:
print('Configured public access to bucket.')

if public_access:
policy_statement.append(
self.get_bucket_policy_statement_for_get_object(
Expand Down Expand Up @@ -126,20 +139,6 @@ def set_bucket_policy(self, bucket, user, allow_public_acls, public_get_object_p
break
print('Bucket policy set.')

# NB: This API doesn't exist on a `Bucket`
self.s3_client.put_public_access_block(
Bucket=bucket.name,
PublicAccessBlockConfiguration={
"BlockPublicAcls": not allow_public_acls,
"IgnorePublicAcls": not allow_public_acls,
"BlockPublicPolicy": not public_access,
"RestrictPublicBuckets": not public_access
}
)

if public_access or allow_public_acls:
print('Configured public access to bucket.')

def create_bucket(self, name, region):
"""
Create bucket of name in the given region.
Expand Down

0 comments on commit 0d323c7

Please sign in to comment.