【问题标题】:IAM policy attached to role not working附加到角色的 IAM 策略不起作用
【发布时间】:2018-05-15 00:03:38
【问题描述】:

我有一个节点应用程序正在以下列方式调用assumeRoleWithWebIdentity:

var params = {
   DurationSeconds: 3600,   
   RoleArn: "arn:aws:iam::role/my_test_role",
   RoleSessionName: "session_name",
   WebIdentityToken: req.body.id_token
};



sts.assumeRoleWithWebIdentity(params, function(err, data) {

    //create s3 client with data.Credentials.SecretAccessKey, AccessKeyId, sessionToken
    //call s3.listObjectsV2({Bucket: 'my-bucket'}).

});

不,我在 IAM 调用 my_test_role 中有一个角色。附加到该角色的是一个名为 my_test_policy 的策略,如下所示:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:ListAllMyBuckets",
        "s3:GetBucketLocation"
      ],
      "Resource": "arn:aws:s3:::*"
    },
    {
      "Effect": "Allow",
      "Action": "s3:ListBucket",
      "Resource": "arn:aws:s3:::my_bucket",
      "Condition": {"StringLike": {"s3:prefix": [
        "",
        "home/",
        "home/BOB/*"
      ]}}
    },
    {
      "Effect": "Allow",
      "Action": "s3:*",
      "Resource": [
        "arn:aws:s3:::my_bucket/home/BOB",
        "arn:aws:s3:::my_bucket/home/BOB/*"
      ]
    }
  ]
}

在 s3 中,我有一个名为 my_bucket 的存储桶,该存储桶中是 home 文件夹。家里有一堆用户文件夹:

my_bucket/home/ALICE
my_bucket/home/BOB
my_bucket/home/MARY

当我的节点应用程序列出对象时,它会列出 home 中的所有对象。我的策略的目的是将列表限制为已担任该角色的用户。因此,如果 BOB 已担任该角色,他应该只会看到 my_bucket/home/BOB 而不会看到其他任何内容。我最终将用 ${my_oidc_url:sub} 替换策略中硬编码的“BOB”。但在我开始这一步之前,我想我会硬编码“BOB”,看看它是否有效。它不是。假定的角色可以看到所有文件夹。有什么建议么?

【问题讨论】:

标签: node.js amazon-web-services amazon-s3 amazon-iam


【解决方案1】:

在您的s3:ListBucket 策略中,您允许列出home/ 文件夹,所以它当然会列出其中的所有内容。

如果你只有home/BOB/* 目录,那么我想你会得到想要的行为。

【讨论】:

  • 我刚刚尝试按照您的建议删除home/,但仍然得到相同的结果。出于某种原因,我觉得无论我在那里放什么,它都没有效果。会不会是政策没有问题,只是用错了地方?
  • 您也可以尝试删除空白前缀(""),以便显示的唯一前缀是"home/BOB/"(没有*)。
  • 是这样做的吗?
【解决方案2】:

为了测试这种情况,我做了以下操作:

  • 创建了一个 Amazon S3 存储桶并上传了文件。

内容是:

2018-05-11 08:57:55      10096 foo
2018-05-11 08:57:38      10096 home/alice/foo
2018-05-11 08:57:32      10096 home/bob/foo
2018-05-11 08:57:51      10096 home/foo
2018-05-11 08:57:45      10096 home/mary/foo
  • 创建了一个名为 bob 的 IAM 角色。

权限是:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "ListObjects",
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::my-bucket",
            "Condition": {
                "StringLike": {
                    "s3:prefix": [
                        "home/bob/*"
                    ]
                }
            }
        },
        {
            "Sid": "AccessObjects",
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::my-bucket/home/bob/*"
        }
    ]
}

ListObjects 等价于ListBucket

  • 担任bob角色

通过 CLI 命令:

aws sts assume-role --role-arn arn:aws:iam::123456789012:role/bob --role-session-name bob
  • 将生成的凭据保存到 bob 个人资料

然后我可以在home/bob 路径中做任何事情,但在其他路径中什么也做不了:

$ aws --profile bob s3 ls s3://my-bucket/home/bob/
    2018-05-11 09:16:23      10096 foo

$ aws --profile bob s3 cp foo s3://my-bucket/home/bob/foo
    upload: ./foo to s3://my-bucket/home/bob/foo                 

$ aws --profile bob s3 cp s3://my-bucket/home/bob/foo .
    download: s3://my-bucket/home/bob/foo to ./foo                  

$ aws --profile bob s3 ls s3://my-bucket/home/

    An error occurred (AccessDenied) when calling the ListObjects operation: Access Denied

$ aws --profile bob s3 ls s3://my-bucket/

    An error occurred (AccessDenied) when calling the ListObjects operation: Access Denied

政策变量

虽然可以轻松地将 IAM 用户替换为 policy variable,但使用假定角色似乎并不容易。这是因为变量将被设置为:

  • aws:username 将未定义
  • aws:userid 将设置为 role id:caller-specified-role-name

这并不像简单地引用 'bob' 的值那么简单。您实际上需要将 S3 路径命名为:AIDAJQABLZS4A3QDU576Q:bob

【讨论】:

    【解决方案3】:

    好的,最后是一些事情。

    1. 我的 node.js 应用程序没有使用我的临时凭据,而是使用静态凭据。这是因为我的 s3 客户端在担任该角色后初始化不正确。
    2. 我的 node.js 应用在实例中发送了一个空前缀以及一个不正确的前缀
    3. 以下政策对我有用

      {
      "Version": "2012-10-17",
      "Statement": [
          {
              "Sid": "AllowUserToSeeBucketListInTheConsole",
              "Action": [
                  "s3:ListAllMyBuckets",
                  "s3:GetBucketLocation"
              ],
              "Effect": "Allow",
              "Resource": "arn:aws:s3:::*"
          },
          {
              "Sid": "AllowRootAndHomeListingOfCompanyBucket",
              "Action": "s3:ListBucket",
              "Effect": "Allow",
              "Resource": "arn:aws:s3:::my-bucket2"
          },
          {
              "Sid": "DenyAllListingExpectForHomeAndUserFolders",
              "Effect": "Deny",
              "Action": "s3:ListBucket",
              "Resource": "arn:aws:s3:::my-bucket2",
              "Condition": {
                  "Null": {
                      "s3:prefix": "false"
                  },
                  "StringNotLike": {
                      "s3:prefix": [
                          "",
                          "home/",
                          "home/${MY_OIDC_URL:sub}/*"
                      ]
                  }
              }
          },
          {
              "Sid": "AllowAllS3ActionsInUserFolder",
              "Effect": "Allow",
              "Action": "s3:*",
              "Resource": "arn:aws:s3:::my-bucket2/home/${MY_OIDC_URL:sub}/*"
          }
      ]
      }
      

    【讨论】:

      猜你喜欢
      • 2017-05-17
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 2020-09-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-22
      • 2021-11-07
      相关资源
      最近更新 更多