步驟 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) 政策,只允許使用者存取您定義的 Session 文件。如需使用 IAM 政策來限制對工作階段中命令之存取權的詳細資訊,請參閱 互動式命令的 IAM 政策範例

只有從 AWS Command Line Interface (AWS CLI) 啟動sessionTypeInteractiveCommands的工作階段才支援具有的文件。使用者提供自訂文件名稱做為 --document-name 參數值,並使用 --parameters 選項提供任何命令參數值。若要取得有關執行互動式命令的詳細資訊,請參閱啟動工作階段 (互動和非互動式命令)

使用以下程序建立自訂 Session 類型 SSM 文件,以定義允許使用者執行的命令。

限制對工作階段中命令的存取 (主控台)

限制使用者可以在 Session Manager 工作階段中執行的命令 (主控台)
  1. 請在以下位置開啟 AWS Systems Manager 主控台。 https://console.aws.amazon.com/systems-manager/

  2. 在導覽窗格中,選擇 Documents (文件)

  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. 選擇 Create document (建立文件)

限制對工作階段中命令的存取 (命令列)

開始之前

如果您尚未安裝,請安裝並設定 AWS Command Line Interface (AWS CLI) 或 AWS Tools for PowerShell. 如需相關資訊,請參閱安裝或更新 AWS CLI的最新版本安裝 AWS Tools for PowerShell

限制使用者可以在 Session Manager 工作階段中執行的命令 (命令列)
  1. 為定義使用者可在 Session Manager 工作階段中執行之命令的文件內容建立 JSON 或 YAML 檔案,如下列範例所示。

    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. 執行下列命令,以使用定義使用者可在 Session Manager 工作階段中執行之命令的內容來建立 SSM 文件。

    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時,您可以透過多種方式提供互動式命令參數。根據您用來連線到受管理節點之用戶端機器的作業系統 (OS) AWS CLI,您為包含特殊或逸出字元的命令提供的語法可能會有所不同。下列範例顯示使用時可提供指令參數的一些不同方式 AWS CLI,以及如何處理特殊字元或逸出字元。

儲存在中的參數Parameter Store可以在中 AWS CLI 為您的命令參數參考,如下列範例所示。

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 CLIAWS CLI 骨架的詳細資訊,請參閱使用指南中的從 JSON 或 YAML 輸入檔案產生架構和輸入參數。AWS Command Line Interface

{ "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 政策範例

您可以建立 IAM 政策,只允許使用者存取您定義的 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" } } } ] }