S3 Bucket Policy to Allow access to specific users

2020-03-03 08:47发布

问题:

I searched through existing questions and couldnt find an answer. Hence posting here.

I want to restrict access to a S3 bucket to all users except select few users using S3 Bucket policy. I understand IAM policy is easy to manage and administer, i dont like to create roles and groups for this specific case and want S3 bucket policy created.

Here is what i have tried so far and it is not restricting access to users as expected.

{
  "Version": "2012-10-17",
  "Id": "bucketPolicy",
  "Statement": [
    {

      "Effect": "Allow",
      "Principal": {
        "AWS": ["arn:aws:iam::1234567890:user/allowedusername"]
      },
      "Action": "s3:*",
      "Resource": ["arn:aws:s3:::examplebucket",
                   "arn:aws:s3:::examplebucket/*"]
    },
    {

      "Effect": "Deny",
      "Principal": {
        "AWS": ["arn:aws:iam::1234567890:user/denieduser"]
      },
      "Action": "s3:*",
      "Resource": ["arn:aws:s3:::examplebucket",
                   "arn:aws:s3:::examplebucket/*"]
    }

  ]
}

I tried to deny all like below but that explicit deny took precedence over allow and i myself am not able to access the bucket now ;-( Thats another issue i have

{

          "Effect": "Deny",
          "Principal": {
            "AWS": ["*"]
          },
          "Action": "s3:*",
          "Resource": ["arn:aws:s3:::examplebucket",
                       "arn:aws:s3:::examplebucket/*"]
        }

回答1:

To achieve what you want, use an explicit deny with a "NotPrincipal" policy element. The policy below will ensure no other user can access the buckets other than the users listed in the "NotPrincipal" element.

    {
            "Id": "bucketPolicy",
            "Statement": [
                    {
                            "Action": "s3:*",
                            "Effect": "Deny",
                            "NotPrincipal": {
                                    "AWS": [
                                            "arn:aws:iam::1234567890:user/alloweduser"
                                    ]
                            },
                            "Resource": [
                                    "arn:aws:s3:::examplebucket",
                                    "arn:aws:s3:::examplebucket/*"
                            ]
                    }
            ],
            "Version": "2012-10-17"
    }