步骤 5:(可选)限制对会话中命令的访问 - Amazon Systems Manager
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

步骤 5:(可选)限制对会话中命令的访问

您可以使用自定义SessionAmazon Systems Manager (SSM) 文档来限制用户可以在 SAmazon Systems ManagerSession Manager ession 中运行的命令。在文档中,您可以定义用户启动会话时运行的命令以及用户可以为该命令提供的参数。Session 文档的 schemaVersion 必须为 1.0,文档的 sessionType 必须为 InteractiveCommands。然后,您可以创建Amazon Identity and Access Management (IAM) 策略以仅允许用户访问您定义的。Session有关使用 IAM policy 限制对会话中命令的访问的更多信息,请参阅 交互式命令的 IAM policy 示例

只有从 () 启动sessionType的会话才支持带InteractiveCommands有 oAmazon Command Line InterfaceAmazon CLI f 的文档。用户提供自定义文档名称作为--document-name参数值,并使用--parameters选项提供任何命令参数值。有关运行交互式命令的更多信息,请参阅 启动会话(交互式和非交互式命令)

使用以下过程创建自Session定义 SSM 文档来定义允许用户运行的命令。

限制对会话中命令的访问(控制台)

限制用户可以在 Session Manager 会话中运行的命令(控制台)
  1. 访问 https://console.aws.amazon.com/systems-manager/,打开 Amazon Systems Manager 控制台。

  2. 在导航窗格中,选择 文档

  3. 选择 Create command or session (创建命令或会话)

  4. 对于 Name (名称),为文档输入一个描述性名称。

  5. 对于 Document type (文档类型),选择 Session document (会话文档)

  6. 输入文档内容,用来定义用户可以使用 JSON 或 YAML 在 Session Manager 会话中运行的命令,如以下示例所示。

    YAML
    --- schemaVersion: '1.0' description: Document to view a log file on a Linux instance sessionType: InteractiveCommands parameters: logpath: type: String description: The log file path to read. default: "/var/log/amazon/ssm/amazon-ssm-agent.log" allowedPattern: "^[a-zA-Z0-9-_/]+(.log)$" properties: linux: commands: "tail -f {{ logpath }}" runAsElevated: true
    JSON
    { "schemaVersion": "1.0", "description": "Document to view a log file on a Linux instance", "sessionType": "InteractiveCommands", "parameters": { "logpath": { "type": "String", "description": "The log file path to read.", "default": "/var/log/amazon/ssm/amazon-ssm-agent.log", "allowedPattern": "^[a-zA-Z0-9-_/]+(.log)$" } }, "properties": { "linux": { "commands": "tail -f {{ logpath }}", "runAsElevated": true } } }
  7. 选择创建文档

限制对会话中命令的访问(命令行)

开始前的准备工作

安装并配置 Amazon Command Line Interface (Amazon CLI) 或 Amazon Tools for PowerShell(如果尚未这样做)。有关信息,请参阅安装或更新 Amazon CLI 的最新版本以及安装 Amazon Tools for PowerShell

限制用户可以在 Session Manager 会话中运行的命令(命令行)
  1. 为文档内容创建 JSON 或 YAML 文件,用来定义用户可在 Session Manager 会话中运行的命令,如以下示例所示。

    YAML
    --- schemaVersion: '1.0' description: Document to view a log file on a Linux instance sessionType: InteractiveCommands parameters: logpath: type: String description: The log file path to read. default: "/var/log/amazon/ssm/amazon-ssm-agent.log" allowedPattern: "^[a-zA-Z0-9-_/]+(.log)$" properties: linux: commands: "tail -f {{ logpath }}" runAsElevated: true
    JSON
    { "schemaVersion": "1.0", "description": "Document to view a log file on a Linux instance", "sessionType": "InteractiveCommands", "parameters": { "logpath": { "type": "String", "description": "The log file path to read.", "default": "/var/log/amazon/ssm/amazon-ssm-agent.log", "allowedPattern": "^[a-zA-Z0-9-_/]+(.log)$" } }, "properties": { "linux": { "commands": "tail -f {{ logpath }}", "runAsElevated": true } } }
  2. 运行以下命令,使用您的内容创建 SSM 文档,该内容定义了用户可以在 Session Manager 会话中运行的命令。

    Linux & macOS
    aws ssm create-document \ --content file://path/to/file/documentContent.json \ --name "exampleAllowedSessionDocument" \ --document-type "Session"
    Windows
    aws ssm create-document ^ --content file://C:\path\to\file\documentContent.json ^ --name "exampleAllowedSessionDocument" ^ --document-type "Session"
    PowerShell
    $json = Get-Content -Path "C:\path\to\file\documentContent.json" | Out-String New-SSMDocument ` -Content $json ` -Name "exampleAllowedSessionDocument" ` -DocumentType "Session"

交互式命令参数和 Amazon CLI

在使用 Amazon CLI 时,可以通过多种方式提供交互式命令参数。根据您用于通过 Amazon CLI 连接到托管式节点的客户端计算机的操作系统 (OS),您为包含特殊字符或转义字符的命令提供的语法可能不同。以下示例显示了在使用 Amazon CLI 时提供命令参数的一些不同方法,以及如何处理特殊字符或转义字符。

您的命令参数可在 Amazon CLI 中引用存储在 Parameter Store 中的参数,如以下示例所示。

Linux & macOS
aws ssm start-session \ --target instance-id \ --document-name MyInteractiveCommandDocument \ --parameters '{"command":["{{ssm:mycommand}}"]}'
Windows
aws ssm start-session ^ --target instance-id ^ --document-name MyInteractiveCommandDocument ^ --parameters '{"command":["{{ssm:mycommand}}"]}'

以下示例显示了如何使用 Amazon CLI 的简写语法来传递参数。

Linux & macOS
aws ssm start-session \ --target instance-id \ --document-name MyInteractiveCommandDocument \ --parameters command="ifconfig"
Windows
aws ssm start-session ^ --target instance-id ^ --document-name MyInteractiveCommandDocument ^ --parameters command="ipconfig"

您也可以提供 JSON 格式的参数,如以下示例所示。

Linux & macOS
aws ssm start-session \ --target instance-id \ --document-name MyInteractiveCommandDocument \ --parameters '{"command":["ifconfig"]}'
Windows
aws ssm start-session ^ --target instance-id ^ --document-name MyInteractiveCommandDocument ^ --parameters '{"command":["ipconfig"]}'

参数也可以存储在 JSON 文件中并提供给 Amazon CLI,如以下示例所示。有关从文件中使用 Amazon CLI 参数的更多信息,请参阅《Amazon Command Line Interface 用户指南》中的从文件中加载 Amazon CLI 参数

{ "command": [ "my command" ] }
Linux & macOS
aws ssm start-session \ --target instance-id \ --document-name MyInteractiveCommandDocument \ --parameters file://complete/path/to/file/parameters.json
Windows
aws ssm start-session ^ --target instance-id ^ --document-name MyInteractiveCommandDocument ^ --parameters file://complete/path/to/file/parameters.json

您还可以从 JSON 输入文件生成 Amazon CLI 骨架,如以下示例所示。有关从 JSON 输入文件生成 Amazon CLI 骨架的更多信息,请参阅《Amazon Command Line Interface 用户指南》中的从 JSON 或 YAML 输入文件生成 Amazon CLI 骨架和输入参数

{ "Target": "instance-id", "DocumentName": "MyInteractiveCommandDocument", "Parameters": { "command": [ "my command" ] } }
Linux & macOS
aws ssm start-session \ --cli-input-json file://complete/path/to/file/parameters.json
Windows
aws ssm start-session ^ --cli-input-json file://complete/path/to/file/parameters.json

要对引号内的字符进行转义,必须在转义字符中添加额外的反斜杠,如以下示例所示。

Linux & macOS
aws ssm start-session \ --target instance-id \ --document-name MyInteractiveCommandDocument \ --parameters '{"command":["printf \"abc\\\\tdef\""]}'
Windows
aws ssm start-session ^ --target instance-id ^ --document-name MyInteractiveCommandDocument ^ --parameters '{"command":["printf \"abc\\\\tdef\""]}'

有关在 Amazon CLI 中将引号和命令参数结合使用的信息,请参阅《Amazon Command Line Interface 用户指南》中的在 Amazon CLI 中将引号和字符串结合使用

交互式命令的 IAM policy 示例

您可以创建 IAM policy 以仅允许用户访问您定义的 Session 文档。这将用户可以在 Session Manager 会话中运行的命令限制为仅在自定义 Session 类型 SSM 文档中定义的命令。

允许用户在单个托管式节点上运行交互式命令
{ "Version":"2012-10-17", "Statement":[ { "Effect":"Allow", "Action":"ssm:StartSession", "Resource":[ "arn:aws:ec2:region:987654321098:instance/i-02573cafcfEXAMPLE", "arn:aws:ssm:region:987654321098:document/exampleAllowedSessionDocument" ], "Condition":{ "BoolIfExists":{ "ssm:SessionDocumentAccessCheck":"true" } } } ] }
允许用户在所有托管式节点上运行交互式命令
{ "Version":"2012-10-17", "Statement":[ { "Effect":"Allow", "Action":"ssm:StartSession", "Resource":[ "arn:aws:ec2:us-west-2:987654321098:instance/*", "arn:aws:ssm:us-west-2:987654321098:document/exampleAllowedSessionDocument" ], "Condition":{ "BoolIfExists":{ "ssm:SessionDocumentAccessCheck":"true" } } } ] }
允许用户在所有托管式节点上运行多个交互式命令
{ "Version":"2012-10-17", "Statement":[ { "Effect":"Allow", "Action":"ssm:StartSession", "Resource":[ "arn:aws:ec2:us-west-2:987654321098:instance/*", "arn:aws:ssm:us-west-2:987654321098:document/exampleAllowedSessionDocument", "arn:aws:ssm:us-west-2:987654321098:document/exampleAllowedSessionDocument2" ], "Condition":{ "BoolIfExists":{ "ssm:SessionDocumentAccessCheck":"true" } } } ] }