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

s3api get-bucket-policy does not return valid policy #849

Closed
quiver opened this issue Jul 19, 2014 · 4 comments
Closed

s3api get-bucket-policy does not return valid policy #849

quiver opened this issue Jul 19, 2014 · 4 comments
Assignees
Labels
documentation This is a problem with documentation.

Comments

@quiver
Copy link
Contributor

quiver commented Jul 19, 2014

problem

$s3api get-bucket-policy returns a JSON policy, but

  • s3api put-bucket-policy
  • s3 manage console's Bucket Policy Editor

does not accept its returned policy data.

How to reproduce

create a bucket and set bucket policy

$ aws s3 mb 13b7
$ cat policy.json
{
   "Statement": [
      {
         "Effect": "Allow",
         "Principal": {
            "AWS": "*"
         },
         "Action": "s3:GetObject",
         "Resource": "arn:aws:s3:::13b7/*"
      }
   ]
}
$ aws s3api put-bucket-policy --bucket 13b7 --policy file://policy.json

Retrieve bucket policy

$ aws s3api get-bucket-policy --bucket 13b7 --debug
...
2014-07-19 14:37:46,736 - MainThread - botocore.response - DEBUG - Response Headers:
...
content-type: application/json
2014-07-19 14:37:46,737 - MainThread - botocore.response - DEBUG - Response Body:
{"Version":"2008-10-17","Statement":[{"Sid":"","Effect":"Allow","Principal":{"AWS":"*"},"Action":"s3:GetObject","Resource":"arn:aws:s3:::13b7/*"}]}
...
2014-07-19 14:37:46,738 - MainThread - awscli.errorhandler - DEBUG - HTTP Response Code: 200
{
    "Policy": "{\"Version\":\"2008-10-17\",\"Statement\":[{\"Sid\":\"\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":\"s3:GetObject\",\"Resource\":\"arn:aws:s3:::13b7/*\"}]}"
}

As you see, API server's Response Body and aws cli's Response Body is different.
aws cli's response is { "Policy" : "api-server's-response-as-string"}

put s3's bucket policy back to the original bucket

$ aws s3api get-bucket-policy --bucket 13b7 > returned_policy.json
$ cat returned_policy.json
{
    "Policy": "{\"Version\":\"2008-10-17\",\"Statement\":[{\"Sid\":\"\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":\"s3:GetObject\",\"Resource\":\"arn:aws:s3:::13b7/*\"}]}"
}
$ aws s3api put-bucket-policy --bucket 13b7 --policy file://returned_policy.json

A client error (MalformedPolicy) occurred when calling the PutBucketPolicy operation: Invalid policy element

aws s3api get-bucket-policy should return API server's response body as is.

Ref #678

@jamesls
Copy link
Member

jamesls commented Jul 22, 2014

Yeah in this case we have the JSON policy within a JSON string so the quotes are escaped which I agree is not ideal. The only thing that makes this difficult is that this would be a breaking change to the output.

As a workaround, you can use the --output text to get just the JSON policy. Leaving this open for the time being while do a little more digging.

@alilee
Copy link

alilee commented Apr 28, 2015

get-bucket-notification and get-bucket-logging produce beautifully formatted JSON. Makes it much easier to debug..

@jamesls jamesls added documentation This is a problem with documentation. and removed bug This issue is a bug. labels Oct 19, 2015
@jamesls
Copy link
Member

jamesls commented Oct 21, 2015

I'd like to follow up on this issue. We can't really change the existing behavior because we need to stay backwards compatible, so we have a few options:

  • Add an example to this command about how you can round trip policies. Something like:
To print the JSON policy for the bucket:

$ aws s3api get-bucket-policy --query Policy --output text --bucket bucket
{"Version":"2012-10-17","Statement":[{"Sid":"AddPerm","Effect":"Allow","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::bucket/*"}]}

You can then also save this policy to a file, edit the file, and reapply the policy to your S3 Bucket:
$ aws s3api get-bucket-policy --query Policy --output text --bucket bucket > mypolicy.json
$ # edit your policy...
$ aws s3api put-bucket-policy --bucket bucket --policy file://mypolicy.json
  • Add an opt-in option for this command to just pretty print the policy:
$ aws s3api get-bucket-policy --bucket bucket --pretty-print
{
  "Statement": [
    {
      "Action": "s3:GetObject",
      "Effect": "Allow",
      "Principal": "*",
      "Resource": "arn:aws:s3:::bucket/*",
      "Sid": "AddPerm"
    }
  ],
  "Version": "2012-10-17"
}

I'm leaning towards the first option, but I'm open to either.

Thoughts? cc @kyleknap @mtdowling @rayluo @JordonPhillips

jamesls added a commit to jamesls/aws-cli that referenced this issue Oct 21, 2015
@jamesls
Copy link
Member

jamesls commented Oct 21, 2015

For now we can start with an example: #1583

If we decide to add option 2, we can always update this example as needed.

thoward-godaddy pushed a commit to thoward-godaddy/aws-cli that referenced this issue Feb 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation This is a problem with documentation.
Projects
None yet
Development

No branches or pull requests

3 participants