为什么此 AWS S3 拒绝用户策略不起作用?

Why is this AWS S3 deny-user policy not working?

我正在使用 LocalStack 学习 AWS,目前在 IAM 用户和 S3 存储桶的上下文中学习 IAM 策略,并且我专注于使用 AWS CLI。

我试图创建一个策略来拒绝特定用户的所有 S3 访问:

{
  "comment": "s3-deny-stanley.json",
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Deny",
      "Action": "s3:*",
      "Principal": { "AWS": "arn:aws:iam::000000000000:user/stanley" },
      "Resource": "arn:aws:s3:::my-bucket/*"
    }
  ]
}

这是斯坦利的个人资料:

$ aws --profile=admin_cred --endpoint-url=http://localhost:4593 iam get-user --user-name stanley
{
    "User": {
        "Path": "/",
        "UserName": "stanley",
        "UserId": "mnxybnqil4uugewvlq7x",
        "Arn": "arn:aws:iam::000000000000:user/stanley",
        "CreateDate": "2020-05-01T20:01:30.932000+00:00"
    }
}

我是这样应用政策的:

$ aws --profile=admin_cred --endpoint-url=http://localhost:4572 s3api put-bucket-policy --bucket my-bucket --policy file://s3-deny-stanley.json
$

我像这样将文件添加到我的存储桶中:

$ cat << EOF > foo.txt
> hello, world!
> EOF
$
$ aws --profile=admin_cred --endpoint-url=http://localhost:4572 s3 cp ./foo.txt s3://my-bucket/foo.txt
upload: ./foo.txt to s3://my-bucket/foo.txt

现在我尝试以 stanley 的身份访问该文件,但出乎我的意料,他似乎能够:

$ aws --profile=stanley --endpoint-url=http://localhost:4572 s3 ls s3://my-bucket/
2020-05-01 14:33:03         14 foo.txt
$ aws --profile=stanley --endpoint-url=http://localhost:4572 s3 cp s3://my-bucket/foo.txt ./foo2.txt
download: s3://my-bucket/foo.txt to ./foo2.txt
$ 

有人可以指出我上面尝试将 s3-deny-stanley 策略应用于我的存储桶的尝试有什么问题吗?

(向史丹利致歉)


更新:
我检查了政策是否像这样附加:

$ aws --profile=admin_cred --endpoint-url=http://localhost:4572 s3api get-bucket-policy --bucket my-bucket
{
    "Policy": "{\n  \"comment\": \"s3-deny-stanley.json\",\n  \"Version\": \"2012-10-17\",\n  \"Statement\": [\n    {\n      \"Effect\": \"Deny\",\n      \"Action\": \"s3:*\",\n      \"Principal\": { \"AWS\": \"arn:aws:iam::000000000000:user/stanley\" },\n      \"Resource\": \"arn:aws:s3:::my-bucket/*\"\n    }\n  ]\n}\n\n"
}

更新:
更新了删除 "comment" 的政策,如下所示:

$ cat ./s3-deny-stanley.json
{
"Version": "2012-10-17",
"Statement": [
    {
      "Effect": "Deny",
      "Action": "s3:*",
      "Principal": { "AWS": "arn:aws:iam::000000000000:user/stanley" },
      "Resource": "arn:aws:s3:::my-bucket/*"
    }
  ]
}

出于谨慎考虑,我删除了原来的存储桶,重新创建了它,对其应用了更新后的策略,并重复了 stanley 的访问尝试:结果与最初描述的相同。

无法重现 OP 在 AWS 上描述的行为。我授予了 stanley 管理员权限并获得了以下内容

$ aws s3 cp s3://bucketname/key . --profile stanley
fatal error: An error occurred (403) when calling the HeadObject operation: Forbidden

所以我认为这是一个本地堆栈错误。