View a markdown version of this page

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

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

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

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

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

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

限制用户可以在 Session Manager 会话中运行的命令(控制台)
  1. 访问 https://console.aws.amazon.com/systems-manager/,打开 AWS 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. 选择创建文档。

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

开始前的准备工作

安装并配置 AWS Command Line Interface (AWS CLI) 或 AWS Tools for PowerShell(如果尚未这样做)。有关信息,请参阅安装或更新 AWS CLI 的最新版本以及安装 AWS 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"

交互式命令参数和 AWS CLI

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

您的命令参数可在 AWS 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}}"]}'

以下示例显示了如何使用 AWS 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 文件中并提供给 AWS CLI,如以下示例所示。有关从文件中使用 AWS CLI 参数的更多信息,请参阅《AWS Command Line Interface 用户指南》中的从文件中加载 AWS 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 输入文件生成 AWS CLI 骨架,如以下示例所示。有关从 JSON 输入文件生成 AWS CLI 骨架的更多信息,请参阅《AWS Command Line Interface 用户指南》中的从 JSON 或 YAML 输入文件生成 AWS 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\""]}'

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

交互式命令的 IAM policy 示例

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

允许用户在单个托管式节点上运行交互式命令
JSON
{ "Version":"2012-10-17", "Statement":[ { "Effect":"Allow", "Action":"ssm:StartSession", "Resource":[ "arn:aws:ec2:us-east-1:444455556666:instance/i-02573cafcfEXAMPLE", "arn:aws:ssm:us-east-1:444455556666:document/allowed-session-document" ] }, { "Effect": "Allow", "Action": ["ssmmessages:OpenDataChannel"], "Resource": ["arn:aws:ssm:*:*:session/${aws:userid}-*"] } ] }
允许用户在所有托管式节点上运行交互式命令
JSON
{ "Version":"2012-10-17", "Statement":[ { "Effect":"Allow", "Action":"ssm:StartSession", "Resource":[ "arn:aws:ec2:us-east-1:444455556666:instance/*", "arn:aws:ssm:us-east-1:444455556666:document/allowed-session-document" ] }, { "Effect": "Allow", "Action": ["ssmmessages:OpenDataChannel"], "Resource": ["arn:aws:ssm:*:*:session/${aws:userid}-*"] } ] }
允许用户在所有托管式节点上运行多个交互式命令
JSON
{ "Version":"2012-10-17", "Statement":[ { "Effect":"Allow", "Action":"ssm:StartSession", "Resource":[ "arn:aws:ec2:us-east-1:444455556666:instance/*", "arn:aws:ssm:us-east-1:444455556666:document/allowed-session-document", "arn:aws:ssm:us-east-1:444455556666:document/allowed-session-document-2" ] }, { "Effect": "Allow", "Action": ["ssmmessages:OpenDataChannel"], "Resource": ["arn:aws:ssm:*:*:session/${aws:userid}-*"] } ] }