未评估角色,但已评估 S3 存储桶策略

发布于 2025-01-11 08:28:07 字数 2184 浏览 0 评论 0原文

最终目标

我使用认知身份池来授予用户读取 S3 存储桶的权限。这个想法是每个用户都有一个他们可以读取的“文件夹”(前缀),并且他们不应该能够读取其他人的文件夹。

总体设计

我对此进行探索的方式是:

  • 用户通过 cognito 用户池登录(这有效)
  • 他们使用 cognito 身份池来承担角色(检查 s3 访问日志后,我也认为这有效)
  • 他们点击s3 端点

现在,s3 存储桶具有策略,并且角色也具有策略(如下所示)。 问题是仅评估 s3 策略。

详细信息

角色

该角色称为“S3Stuff”

角色权限

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:ListBucket",
            "Resource": "arn:aws:s3:::<bucket>",
            "Condition": {
                "StringLike": {
                    "s3:prefix": "${cognito-identity.amazonaws.com:sub}/*"
                }
            }
        }
    ]
}

角色信任关系

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Federated": "cognito-identity.amazonaws.com"
            },
            "Action": "sts:AssumeRoleWithWebIdentity",
            "Condition": {
                "StringEquals": {
                    "cognito-identity.amazonaws.com:aud": "us-east-1:<GUID>"
                },
                "StringLike": {
                    "cognito-identity.amazonaws.com:amr": "authenticated"
                }
            }
        }
    ]
}

S3 存储桶策略

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::720911909616:role/S3Stuff"
            },
            "Action": "s3:ListBucket",
            "Resource": "arn:aws:s3:::<bucket>"
        }
    ]
}

会发生什么

使用上述 S3 配置,当我以 a 身份登录 cognito 时, 会发生什么情况?用户,我可以列出存储桶的全部内容。当我向 s3 存储桶策略添加条件时(如下所示),它可以正常工作(即我可以列出存储桶中我应该能够访问的部分,而无法访问我无法访问的部分)。另外,在我对其设置 s3 策略之前,它拒绝我访问。

条件

"Condition": {
    "StringLike": {
        "s3:prefix": "${cognito-identity.amazonaws.com:sub}/*"
    }
}

需要明确的是我知道我可以将所有内容都放入 s3 策略中,但我想知道发生了什么以及为什么。

为什么角色中的策略被绕过

End goal

I'm using cognito identity pools to give users permission to read an S3 bucket. The idea is that each user has a "folder" (prefix) that they can read and they shouldn't be able to read anyone elses folder.

General design

The way I have this plumbed up is:

  • Users sign in via a cognito user pool (this works)
  • They use the cognito identity pool to assume a role (after checking the s3 access logs, I also think this works)
  • They hit the s3 endpoint

Now, the s3 bucket has policy and the role also has policy (which is below). The problem is only the s3 policy is being evaluated.

Details

Role

The role is called "S3Stuff"

Role permissions

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:ListBucket",
            "Resource": "arn:aws:s3:::<bucket>",
            "Condition": {
                "StringLike": {
                    "s3:prefix": "${cognito-identity.amazonaws.com:sub}/*"
                }
            }
        }
    ]
}

Role trust relationship

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Federated": "cognito-identity.amazonaws.com"
            },
            "Action": "sts:AssumeRoleWithWebIdentity",
            "Condition": {
                "StringEquals": {
                    "cognito-identity.amazonaws.com:aud": "us-east-1:<GUID>"
                },
                "StringLike": {
                    "cognito-identity.amazonaws.com:amr": "authenticated"
                }
            }
        }
    ]
}

S3 Bucket Policy

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::720911909616:role/S3Stuff"
            },
            "Action": "s3:ListBucket",
            "Resource": "arn:aws:s3:::<bucket>"
        }
    ]
}

What happens

With the above S3 configuration, when I log into cognito as a user, I can list the entire contents of the bucket. When I add a condition to the s3 bucket policy (like below) it works correctly (ie I can list the part of the bucket I should be able to and can't access what I couldn't be able to). Also before I put the s3 policy on it was denying me access.

Condition

"Condition": {
    "StringLike": {
        "s3:prefix": "${cognito-identity.amazonaws.com:sub}/*"
    }
}

To be clear I know I can just throw everything into the s3 policy but I want to know what's going on and why.

Why is the policy in the role being bypassed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海夕 2025-01-18 08:28:07

这张图是关键。我是从 AWS Learning 上的“深入了解安全性:AWS 身份和访问管理”中获得的。

我是误导认为适用于请求的策略是所有策略中的“最小权限联合”。这非常接近正确,但不真实

  1. 所有策略均已解析。
  2. 如果有明确的拒绝(“效果”:“拒绝”),那么整个事情都会被拒绝。
  3. 如果没有明确的拒绝,那么它就会开始寻找隐式的拒绝(这是当策略没有“效果”:“允许”子句让我做某事时)。 除了基于资源的策略可以让您缩短对隐式拒绝的搜索。如果资源明确允许您,则您可以跳过:权限边界、会话策略和身份策略(包括角色和 IAM)。

如果您有一个基于资源的策略来批准某项操作,则角色策略只有在明确拒绝的情况下才会生效。

关于我在将 S3 策略放在上面之前所说的部分,我正在拒绝访问,可能是因为 cognito aud。我有一段时间使用了错误的 aud(被引用的认知 aud 来自认知身份池而不是认知用户池,以防万一有人好奇)并最终发现出来并使用正确的。在将 aud 更改为正确的内容之前,我必须已经开始测试 s3 策略。

This picture is key. I got it from "Deep Dive with Security: AWS Identity and Access Management" on the AWS Learning thing.

this shows in which order policy is evaluated. First is AWS Organization, then Resource based, then Permission Boundaries, then Session Policy, and finally Identity-based. Resource based policy can approve immediately

I was mislead into thinking that the policy that applies to a request is the "least-privilege union" of all the policies. This is very close to being right but it is untrue.

  1. All policies are parsed.
  2. If there is an explicit deny ("Effect": "Deny") then the whole thing is denied.
  3. If there isn't an explicit deny then it starts looking for implicit denies (this is when the policy doesn't have an "Effect": "Allow" clause that lets me do a thing). EXCEPT the resource based policies let you short circuit the search for implicit denies. If a resource explicitly allows you then you skip: permission boundaries, session policy, and identity policy (which includes roles and IAM).

If you have a resource based policy that approves an action the role policies will only have an effect if there is an explicit deny.

Concerning the part where I said that before I put the S3 policy on I was being denied access, It's probably because of the cognito aud. I was using the wrong aud for a while (the cognito aud being refereed to comes from cognito-identity pools not cognito user pools just in case anyone was curious) and eventually figured it out and used the right one. I must have started testing s3 policy before changing the aud to the right thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文