【问题标题】:Why is this AWS S3 deny-user policy not working?为什么此 AWS S3 拒绝用户策略不起作用?
【发布时间】:2020-08-16 11:05:56
【问题描述】:

我正在使用 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/*"
    }
  ]
}

这是 stanley 的个人资料:

$ 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 政策应用于 my-bucket 的问题吗?

(向斯坦利道歉)


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

$ 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"
}

更新
更新政策以移除“评论”,如下:

$ 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 的访问尝试:结果与最初描述的相同。

【问题讨论】:

  • 能否检查一下桶策略是否附加到my-bucket
  • @jellycsc - 完成:更新帖子
  • 对我来说一切都很好。这似乎是一个现有的 localstack 错误。 github.com/localstack/localstack/issues/2238
  • @jellycsc - 谢谢;我对github不熟悉;在该页面的底部添加“缺少功能”标签是否意味着它已被确认为错误? (出于学习目的,我主要只是对我尝试的操作是否正确感兴趣——我可以接受这是 LocalStack 中的一个错误)
  • 让我在 aws 中快速尝试并回复您◡̈ 我希望 100% 正确,尤其是不要误导您。

标签: amazon-web-services amazon-s3 amazon-iam aws-cli localstack


【解决方案1】:

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

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

所以我认为这是一个 localstack 错误。

【讨论】:

    猜你喜欢
    • 2019-05-22
    • 2017-02-14
    • 2023-03-29
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 2019-10-05
    相关资源
    最近更新 更多