Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command to make a bucket public #87

Open
simonw opened this issue Apr 5, 2024 · 4 comments
Open

Command to make a bucket public #87

simonw opened this issue Apr 5, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@simonw
Copy link
Owner

simonw commented Apr 5, 2024

Related to:

@simonw simonw added the enhancement New feature or request label Apr 5, 2024
@simonw
Copy link
Owner Author

simonw commented Apr 5, 2024

Here's my prototype:

@cli.command()
@click.argument("bucket")
@common_boto3_options
def make_bucket_public(bucket, **boto_options):
    """
    Make a bucket public. All files will be visible to anyone who knows the URL.

       s3-credentials make-bucket-public my-bucket
    """
    s3 = make_client("s3", **boto_options)
    try:
        print("Putting public access block")
        response1 = s3.put_public_access_block(
            Bucket=bucket,
            PublicAccessBlockConfiguration={
                "BlockPublicAcls": False,
                "IgnorePublicAcls": False,
                "BlockPublicPolicy": False,
                "RestrictPublicBuckets": False,
            },
        )
        print("Putting bucket policy")
        response2 = s3.put_bucket_policy(
            Bucket=bucket,
            Policy=json.dumps(policies.bucket_policy_allow_all_get(bucket)),
        )
    except botocore.exceptions.ClientError as ex:
        click.echo(ex.__dict__, err=True)
        raise click.ClickException(ex)
    click.echo(json.dumps(response1, indent=4, default=str))
    click.echo(json.dumps(response2, indent=4, default=str))

@simonw
Copy link
Owner Author

simonw commented Apr 5, 2024

I think it's possible the s3-credentials create name-of-bucket --create-bucket --public command doesn't work correctly because it doesn't do the s3.put_public_access_block bit.

@simonw
Copy link
Owner Author

simonw commented Apr 5, 2024

I asked ChatGPT what all of those boolean flags meant and it said:

The PublicAccessBlockConfiguration settings are used to manage public access to your S3 bucket. Each boolean option in this configuration controls a specific aspect of public access:

  1. BlockPublicAcls:

    • When set to True, this setting prevents the application of any new public Access Control Lists (ACLs) to the bucket or its objects. It doesn't change the existing ACLs but blocks any attempt to put a new ACL that grants public access.
    • When set to False, it allows the application of new public ACLs to the bucket and its objects.
  2. IgnorePublicAcls:

    • When set to True, this setting ignores any public ACLs on the bucket and its objects, effectively treating them as private. Even if an object has a public ACL, it won't be publicly accessible.
    • When set to False, it respects the public ACLs on the bucket and its objects, allowing public access if a public ACL is set.
  3. BlockPublicPolicy:

    • When set to True, this setting prevents the application of any new public bucket policies to the bucket. It blocks attempts to put a new bucket policy that grants public access.
    • When set to False, it allows the application of new public bucket policies to the bucket.
  4. RestrictPublicBuckets:

    • When set to True, this setting restricts access to the bucket to only AWS services and authorized users within the bucket owner's account, even if the bucket has a public policy. It effectively blocks public access granted by bucket policies.
    • When set to False, it allows public access granted by bucket policies.

By configuring these settings, you can control the level of public access to your S3 bucket and its objects, enhancing the security of your data. For example, setting all options to True provides the strictest level of access control, ensuring that the bucket and its objects are not publicly accessible under any circumstances.

So I think they should all be False if a bucket is going to serve public files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant