为什么通过 IAM 策略将 ssm:sendCommand 限制为特定文档显示访问被拒绝?

Why does restricting `ssm:sendCommand` to a specific document via an IAM policy show access denied?

我正在尝试让 IAM 用户只能对特定文档使用 SSM 运行 命令。

如果我将以下策略附加到用户,则该用户确实只能在 EC2 实例上成功执行 AWS-RunShellScript(这是 AWS 托管的)文档。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ssm:DescribeDocument",
                "ssm:DescribeDocumentParameters",
                "ssm:DescribeDocumentPermission",
                "ssm:GetCommandInvocation",
                "ssm:GetDocument",
                "ssm:ListCommandInvocations",
                "ssm:ListCommands",
                "ssm:ListDocumentMetadataHistory",
                "ssm:ListDocuments",
                "ssm:ListDocumentVersions"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": "ssm:SendCommand",
            "Resource": "arn:aws:ssm:us-west-2:999999999999:document/AWS-RunShellScript"
        }
    ]
}

但是,如果我将策略中的资源项替换为我创建的自定义文档 ARN(例如 arn:aws:ssm:us-west-2:999999999999:document/CustomDocument),我会收到“拒绝访问”

文档中不是很清楚,但要限制 ssm:SendCommand,您必须使用 Resource 字段来 指定允许 IAM 用户访问的文档运行 您允许命令在 运行 上 .

的实例

用户在尝试 运行 命令时会看到所有实例,但只能针对 IAM 策略中指定的 EC2 实例 ID 执行命令。

如果需要,您可以指定允许任何实例的策略,或者根据在 IAM 策略中授予 least privilege 的标准安全建议指定有限的实例 ID 列表。

至于 AWS 托管文档为何有效,这可能是一些内部魔法 ‍♂️

这应该有效:

{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect":"Allow",
         "Action":"ssm:SendCommand",
         "Resource":[
            "arn:aws:ec2:us-west-2:999999999999:instance/*",
            "arn:aws:ssm:us-west-2:999999999999:document/AWS-RunShellScript"
         ]
      }
   ]
}