首页 > 解决方案 > IAM CodeBuild 策略条件 - 委托人标签等于资源标签

问题描述

目标:

创建一个 IAM 角色策略,该策略仅在角色标签等于资源标签时才允许角色对 aws 资源执行定义的操作。

例如:

IAM 标签:

富=酒吧

CodeBuild 项目标签:

富=酒吧

IAM 角色策略:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codebuild:List*",
                "codebuild:DescribeTestCases",
                "codebuild:DescribeCodeCoverages",
                "codebuild:BatchGet*"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "aws:ResourceTag/foo": "${aws:PrincipalTag/foo}"
                }
            }
        }
    ]
}

当用户担任该角色时,该用户将被拒绝访问与 AWS 控制台和 AWS CLI 中的角色codebuild:ListProjects具有相同密钥对的项目。foo=bar

AWS 控制台错误:

User: arn:aws:sts::123456789:assumed-role/example-role/example-user is not authorized to perform: codebuild:ListProjects

使用以下命令的 AWS CLI 错误:aws codebuild list-projects --profile test

An error occurred (AccessDeniedException) when calling the ListProjects operation: User: arn:aws:sts::123456789:assumed-role/example-role/botocore-session-59458209 is not authorized to perform: codebuild:ListProjects

尝试:

#1

在策略条件中使用实际的密钥对值:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codebuild:List*",
                "codebuild:DescribeTestCases",
                "codebuild:DescribeCodeCoverages",
                "codebuild:BatchGet*"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "aws:ResourceTag/foo": "bar"
                }
            }
        }
    ]
}

codebuild:ListProjects在 aws 控制台上访问被拒绝错误的结果

#2

aws:PrincipalTag/foo从(此处从臀部射击)移除 aws 键支架

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codebuild:List*",
                "codebuild:DescribeTestCases",
                "codebuild:DescribeCodeCoverages",
                "codebuild:BatchGet*"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "aws:ResourceTag/foo": "aws:PrincipalTag/foo"
                }
            }
        }
    ]
}

#3

使用资源标签条件中的资源类型codebuild:ResourceTag/foo

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codebuild:List*",
                "codebuild:DescribeTestCases",
                "codebuild:DescribeCodeCoverages",
                "codebuild:BatchGet*"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "codebuild:ResourceTag/foo": "${aws:PrincipalTag/foo}"
                }
            }
        }
    ]
}

aws控制台上的相同codebuild:ListProjects访问被拒绝错误

标签: amazon-web-servicesamazon-iam

解决方案


来自文档

CodeBuild 支持基于标签的基于项目的操作的授权

我认为ListProjects行动不是基于项目的。它适用于整个代码构建,而不是特定项目。


推荐阅读