Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do an OR condition in an S3 bucket policy?

Tags:

I'm working on an S3 bucket policy. The idea is to explicitly deny access to all IAM users within the account, except for those explicitly granted.

I found a blog post that explains how to restrict access to a specific user. It works well. However, I want to extend the syntax to include a second IAM user that will be allowed access. This is, in effect, an OR condition.

But, I've very new to JSON, and I'm not sure how to go about that.

Here is the policy that works for restricting access to a single user:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Deny",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::my-bucket",
                "arn:aws:s3:::my-bucket/*"
            ],
            "Condition": {
                "StringNotLike": {
                    "aws:userId": [
                        "AIDA<obfuscated id>:*",
                        "AIDA<obfuscated id>",
                        "111111111111"
                    ]
                }
            }
        }
    ]
}

Can anyone help me edit the above JSON to allow for an OR condition where I could specify an additional userid that would be allowed access?

AdvThanksance!

like image 439
Mark J. Bobak Avatar asked Feb 28 '17 16:02

Mark J. Bobak


People also ask

How do you modify a bucket policy?

To create or edit a bucket policyIn the left navigation pane, choose Outposts buckets. Choose the Outposts bucket whose bucket policy you want to edit. Choose the Permissions tab. In the Outposts bucket policy section, to create or edit new policy, choose Edit.

How do I check my S3 bucket policy?

Open the Amazon S3 console at https://console.aws.amazon.com/s3/ . Select the bucket that you want AWS Config to use to deliver configuration items, and then choose Properties. Choose Permissions. Choose Edit Bucket Policy.

What is the difference between S3 ACL and bucket policy?

The biggest advantage of using ACL is that you can control the access level of not only buckets but also of an object using it. Whereas IAM or Bucket Policies can only be attached to buckets but not to objects in the bucket, Bucket ACLs can be assigned to buckets as well as objects in it.


1 Answers

Ok, I figured this out.

First, I tried adding a second StringgNotLike clause to the Condition, but that didn't work.

After doing as bit more reading, I realized the Condition clause accepts multiple key/value pairs. In fact, the original policy I showed in my question already did that. I just needed to add more values to the array that was already there.

The policy that works, looks like this:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Deny",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::my-private-bucket",
                "arn:aws:s3:::my-private-bucket/*"
            ],
            "Condition": {
                "StringNotLike": {
                    "aws:userId": [
                        "AIDA<obfuscated-id-1>:*",
                        "AIDA<obfuscated-id-1>",
                        "AIDA<obfuscated-id-2>:*",
                        "AIDA<obfuscated-id-2>",
                        "111111111111"
                    ]
                }
            }
        }
    ]
}

When I realized that the key had already specified an array of values, I just added the second user id to the array, and it worked great.

like image 153
Mark J. Bobak Avatar answered Sep 23 '22 10:09

Mark J. Bobak