AWS Parameter Store IAM 策略无法正常工作

Cha*_*der 4 parameters amazon-ec2 amazon-web-services terraform aws-ssm

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

我需要一个链接到 EC2 实例的策略,以便只能为get-parameters-by-pathAWS SSM 参数存储中的特定参数提供 a,而无法更改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"
                }
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

使用AWS 策略模拟器,View它会通知您,当尝试使用拒绝消息时,会通知您参数,而其他项目则Create可以修改、查看等ModifyDelete"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
Run Code Online (Sandbox Code Playgroud)

/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
Run Code Online (Sandbox Code Playgroud)

使用的标签:

键=值

/development-1/project-1

环境=开发-1

/development-2/project-2

环境=开发-2

我究竟做错了什么?

yda*_*coR 7

您无法根据任何 IAM 操作的参数标签设置条件键,ssm:GetParameter*因为 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
Run Code Online (Sandbox Code Playgroud)

然后,对于在生产中运行的 foo-service,您为其授予具有以下权限的 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/*"]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

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

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

例外情况是,如果您正在做非常复杂的事情,您希望授予对广泛事物的大部分访问权限,但随后阻止一小部分事物。这篇博文讨论了默认ReadOnlyAccess策略对于他们的组织来说过于宽松,因此他们通过拒绝声明来限制对他们不希望授予此类开放访问权限的事物的访问。他们也可以采取相反的方式,永远不使用 AWS 托管策略,而是必须在许多服务本身中维护非常广泛的操作集,这可能被认为更安全,但也可能需要大量工作。


Mar*_*cin 6

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

但就您而言,由于其实例配置文件,您的策略不必那么复杂。默认情况下,一切都是隐式拒绝的,因此您只需要显式允许:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": ["ssm:GetParametersByPath"],
            "Resource": ["*"]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

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