1

我需要一些与创建 AWS 策略相关的帮助。

我需要一个链接到 EC2 实例的策略,以便能够仅将 a 提供给get-parameters-by-pathAWS SSM 参数存储中的特定参数,而不能更改 、 等任何内容DeleteCreate并且应该只能获取这些值。

此策略特异性将通过标签给出。

这是我正在尝试使用的政策:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": ["ssm:*"],
            "Resource": ["*"]
        },
        {
            "Effect": "Deny",
            "Action": [
               "ssm:PutParameter",
               "ssm:GetParameter",
               "ssm:GetParameters",
               "ssm:DeleteParameter",
               "ssm:GetParameterHistory",
               "ssm:DeleteParameters",
               "ssm:GetParametersByPath"
             ],
            "Resource": ["*"],
            "Condition": {
                "StringNotEquals": {
                    "ssm:resourceTag/env": "development-1"
                }
            }
        }
    ]
}

使用AWS 策略模拟器,它会通知您,当尝试View, Create,时Modify,会通知Delete带有拒绝消息的参数"ssm:resourceTag/env": "development-2",而其他项目"ssm:resourceTag/env": "development-1"可以修改、查看等。

但是,当将同一策略绑定到 EC2 实例时,该策略会阻止在拒绝中添加的任何操作。

EC2 通知消息

/development-1/project-1

aws --region us-east-2 ssm get-parameters-by-path --path /development-1/project-1/ --recursive --with-decryption --output text --query "Parameters[].[Value]"

An error occurred (AccessDeniedException) when calling the GetParametersByPath operation: User: arn:aws:sts::111111111:assumed-role/rule-ec2/i-11111111111 is not authorized to perform: ssm:GetParametersByPath on resource: arn:aws:ssm:us-east-2:11111111111:parameter/development-1/project-1/ with an explicit deny

/development-2/project-2

aws --region us-east-2 ssm get-parameters-by-path --path /development-2/project-2/ --recursive --with-decryption --output text --query "Parameters[].[Value]"
    
An error occurred (AccessDeniedException) when calling the GetParametersByPath operation: User: arn:aws:sts::11111111111:assumed-role/rule-ec2/i-11111111111 is not authorized to perform: ssm:GetParametersByPath on resource: arn:aws:ssm:us-east-2:11111111111:parameter/development-2/project-2/ with an explicit deny

使用的标签:

键=值

/development-1/project-1

环境=发展-1

/development-2/project-2

环境=发展-2

我究竟做错了什么?

4

2 回答 2

2

你习惯Denyssm:GetParametersByPath,所以总是会被拒绝。拒绝总是优先于任何允许。

但是在您的情况下,由于它的实例配置文件,您的策略不必那么复杂。默认情况下,所有内容都被隐式拒绝,因此您只需要显式允许:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": ["ssm:GetParametersByPath"],
            "Resource": ["*"]
        }
    ]
}

因为Resource您只能添加您希望允许访问的 ssm 参数,而不是全部。

于 2021-08-26T22:50:09.440 回答
1

您无法根据任何ssm:GetParameter*IAM 操作的参数标签设置条件键,因为 API(当前)不支持条件键

相反,您可以通过参数的 ARN 进行限制,一般而言,使用 SSM 参数存储的做法是使用参数的分层路径以允许您通过 IAM 限制访问,然后可以选择通配符,让您乐于拥有任何东西在那条路径下。

所以一个常见的模式可能是有一些看起来像这样的结构:

/production/foo-service/database/password
/production/foo-service/bar-service/api-key
/production/bar-service/database/password
/production/bar-service/foo-service/api-key
/development/foo-service/database/password
/development/foo-service/bar-service/api-key
/development/bar-service/database/password
/development/bar-service/foo-service/api-key

然后,对于在生产中运行的 foo 服务,您可以为其赋予具有以下权限的 IAM 角色:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": ["ssm:GetParameter*"],
            "Resource": ["arn:aws:ssm:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:parameter/production/foo-service/*"]
        }
    ]
}

这将允许生产中的 foo-service 访问这两个参数/production/foo-service/database/password/production/foo-service/bar-service/api-key但不允许访问任何其他参数或修改或删除参数的能力。

正如@Marcin回答中所述,您不需要在此处向您的 IAM 策略添加拒绝语句,因为 IAM 的默认设置是拒绝,除非已明确给出允许语句。

例外情况是,如果您正在做非常复杂的事情,您希望主要访问范围广泛的事物,然后阻止一小部分事物。这篇博客文章讨论了默认ReadOnlyAccess策略如何对其组织过于宽容,因此他们通过拒绝声明来限制他们不想给予如此开放访问的事物的访问权限。他们也可以反其道而行之,从不使用 AWS 托管策略,而必须在许多服务本身上维护一套非常广泛的操作,这可能被认为更安全,但也可能需要大量工作。

于 2021-08-27T11:08:20.753 回答