Skip to content

Commit ac428b7

Browse files
committed
get-bucket-policy and set-bucket-policy, closes #91
1 parent e896f46 commit ac428b7

3 files changed

Lines changed: 125 additions & 0 deletions

File tree

docs/help.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Commands:
3737
debug-bucket Run a bunch of diagnostics to help debug a bucket
3838
delete-objects Delete one or more object from an S3 bucket
3939
delete-user Delete specified users, their access keys and their...
40+
get-bucket-policy Get bucket policy for a bucket
4041
get-cors-policy Get CORS policy for a bucket
4142
get-object Download an object from an S3 bucket
4243
get-objects Download multiple objects from an S3 bucket
@@ -48,6 +49,7 @@ Commands:
4849
policy Output generated JSON policy for one or more buckets
4950
put-object Upload an object to an S3 bucket
5051
put-objects Upload multiple objects to an S3 bucket
52+
set-bucket-policy Set bucket policy for a bucket
5153
set-cors-policy Set CORS policy for a bucket
5254
whoami Identify currently authenticated user
5355
```
@@ -156,6 +158,25 @@ Usage: s3-credentials delete-user [OPTIONS] USERNAMES...
156158
157159
s3-credentials delete-user username1 username2
158160
161+
Options:
162+
--access-key TEXT AWS access key ID
163+
--secret-key TEXT AWS secret access key
164+
--session-token TEXT AWS session token
165+
--endpoint-url TEXT Custom endpoint URL
166+
-a, --auth FILENAME Path to JSON/INI file containing credentials
167+
--help Show this message and exit.
168+
```
169+
## s3-credentials get-bucket-policy --help
170+
171+
```
172+
Usage: s3-credentials get-bucket-policy [OPTIONS] BUCKET
173+
174+
Get bucket policy for a bucket
175+
176+
s3-credentials get-bucket-policy my-bucket
177+
178+
Returns the bucket policy for this bucket, if set, as JSON
179+
159180
Options:
160181
--access-key TEXT AWS access key ID
161182
--secret-key TEXT AWS secret access key
@@ -464,6 +485,29 @@ Options:
464485
-a, --auth FILENAME Path to JSON/INI file containing credentials
465486
--help Show this message and exit.
466487
```
488+
## s3-credentials set-bucket-policy --help
489+
490+
```
491+
Usage: s3-credentials set-bucket-policy [OPTIONS] BUCKET
492+
493+
Set bucket policy for a bucket
494+
495+
s3-credentials set-bucket-policy my-bucket --policy-file policy.json
496+
497+
Or to set a policy that allows GET requests from all:
498+
499+
s3-credentials set-bucket-policy my-bucket --allow-all-get
500+
501+
Options:
502+
--policy-file FILENAME
503+
--allow-all-get Allow GET requests from all
504+
--access-key TEXT AWS access key ID
505+
--secret-key TEXT AWS secret access key
506+
--session-token TEXT AWS session token
507+
--endpoint-url TEXT Custom endpoint URL
508+
-a, --auth FILENAME Path to JSON/INI file containing credentials
509+
--help Show this message and exit.
510+
```
467511
## s3-credentials set-cors-policy --help
468512

469513
```

docs/other-commands.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,3 +570,36 @@ Bucket public access block:
570570
}
571571
}
572572
```
573+
## get-bucket-policy
574+
575+
The `get-bucket-policy` command displays the current bucket policy for a bucket:
576+
```bash
577+
s3-credentials get-bucket-policy my-bucket
578+
```
579+
Example output:
580+
581+
```json
582+
{
583+
"Version": "2012-10-17",
584+
"Statement": [
585+
{
586+
"Sid": "AllowAllGetObject",
587+
"Effect": "Allow",
588+
"Principal": "*",
589+
"Action": "s3:GetObject",
590+
"Resource": "arn:aws:s3:::my-bucket/*"
591+
}
592+
]
593+
}
594+
```
595+
596+
## set-bucket-policy
597+
598+
The `set-bucket-policy` command can be used to set a bucket policy for a bucket:
599+
```bash
600+
s3-credentials set-bucket-policy my-bucket --policy-file policy.json
601+
```
602+
Or for the common case of setting a policy to allow GET access to all buckets:
603+
```bash
604+
s3-credentials set-bucket-policy my-bucket --allow-all-get
605+
```

s3_credentials/cli.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,54 @@ def get_cors_policy(bucket, **boto_options):
13651365
click.echo(json.dumps(response["CORSRules"], indent=4, default=str))
13661366

13671367

1368+
@cli.command()
1369+
@click.argument("bucket")
1370+
@common_boto3_options
1371+
def get_bucket_policy(bucket, **boto_options):
1372+
"""
1373+
Get bucket policy for a bucket
1374+
1375+
s3-credentials get-bucket-policy my-bucket
1376+
1377+
Returns the bucket policy for this bucket, if set, as JSON
1378+
"""
1379+
s3 = make_client("s3", **boto_options)
1380+
try:
1381+
response = s3.get_bucket_policy(Bucket=bucket)
1382+
except botocore.exceptions.ClientError as e:
1383+
raise click.ClickException(e)
1384+
click.echo(json.dumps(json.loads(response["Policy"]), indent=4, default=str))
1385+
1386+
1387+
@cli.command()
1388+
@click.argument("bucket")
1389+
@click.option("--policy-file", type=click.File("r"))
1390+
@click.option("--allow-all-get", is_flag=True, help="Allow GET requests from all")
1391+
@common_boto3_options
1392+
def set_bucket_policy(bucket, policy_file, allow_all_get, **boto_options):
1393+
"""
1394+
Set bucket policy for a bucket
1395+
1396+
s3-credentials set-bucket-policy my-bucket --policy-file policy.json
1397+
1398+
Or to set a policy that allows GET requests from all:
1399+
1400+
s3-credentials set-bucket-policy my-bucket --allow-all-get
1401+
"""
1402+
s3 = make_client("s3", **boto_options)
1403+
if allow_all_get and policy_file:
1404+
raise click.ClickException("Cannot pass both --allow-all-get and --policy-file")
1405+
if allow_all_get:
1406+
policy = policies.bucket_policy_allow_all_get(bucket)
1407+
else:
1408+
policy = json.load(policy_file)
1409+
try:
1410+
s3.put_bucket_policy(Bucket=bucket, Policy=json.dumps(policy))
1411+
except botocore.exceptions.ClientError as e:
1412+
raise click.ClickException(e)
1413+
click.echo("Policy set:\n" + json.dumps(policy, indent=4), err=True)
1414+
1415+
13681416
def without_response_metadata(data):
13691417
return dict(
13701418
(key, value) for key, value in data.items() if key != "ResponseMetadata"

0 commit comments

Comments
 (0)