ステップ 5: (オプション) セッションでのコマンドへのアクセスを制限する
カスタム Session
タイプの AWS Systems Manager (SSM) ドキュメントを使用すると、AWS Systems Manager Session Manager セッションでユーザーが実行できるコマンドを制限できます。ドキュメントでは、ユーザーがセッションを開始したときに実行されるコマンド、およびユーザーがコマンドに指定できるパラメータを定義します。Session
ドキュメントの schemaVersion
は 1.0 であり、sessionType
は InteractiveCommands
であることが必要です。その後、お客様が定義した Session
ドキュメントのみへのアクセスをユーザーに許可する AWS Identity and Access Management (IAM) ポリシーを作成できます。IAM ポリシーを使用してセッションでのコマンドへのアクセスを制限する方法の詳細については、「対話型コマンドの IAM ポリシーの例」を参照してください。
InteractiveCommands
の sessionType
を持つドキュメントは、AWS Command Line Interface (AWS CLI) から開始されたセッションでのみサポートされます。ユーザーはカスタムドキュメント名を --document-name
パラメータ値として指定し、--parameters
オプションを使用して任意のコマンドパラメータ値を指定します。対話型コマンドの実行の詳細については、「セッションの開始 (対話形式と非対話形式のコマンド)」を参照してください。
ユーザーに実行を許可するコマンドを定義するカスタムの Session
タイプの SSM ドキュメントを作成するには以下の手順を使用します。
セッションでコマンドへのアクセスを制限する (コンソール)
Session Manager セッションでユーザーが実行できるコマンドを制限するには (コンソール)
AWS Systems Manager コンソール (https://console.aws.amazon.com/systems-manager/) を開きます。
-
ナビゲーションペインで、[ドキュメント] を選択します。
-
[Create command or session (コマンドまたはセッションの作成)] を選択します。
-
[名前] に、ドキュメントのわかりやすい名前を入力します。
-
[Document type (ドキュメントタイプ)] で、[Session document (セッションドキュメント)] を選択します。
-
以下の例に示すように、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
}
}
}
-
[Create document] を選択します。
セッションでコマンドへのアクセスを制限する (コマンドライン)
開始する前に
まだ AWS Command Line Interface (AWS CLI) または AWS Tools for PowerShell をインストールして設定していない場合は、インストールして設定します。詳細については、「AWS CLI の最新バージョンをインストールまたは更新します。」および「AWS Tools for PowerShell のインストール」を参照してください。
Session Manager セッションでユーザーが実行できるコマンドを制限するには (コマンドライン)
-
以下の例に示すように、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
}
}
}
-
以下のコマンドを実行して、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 を使用する際、対話型のコマンドパラメータを指定する方法はさまざまあります。AWS CLI でマネージドノードに接続する際に使用するクライアントマシンのオペレーティングシステム (OS) によっては、特殊またはエスケープ記号を含むコマンドの構文が異なる場合があります。次の例は、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 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 ポリシーの例
お客様が定義した Session
ドキュメントのみへのアクセスをユーザーに許可する IAM ポリシーを作成できます。これにより、Session Manager セッションでユーザーが実行できるコマンドは、カスタムの Session
タイプの SSM ドキュメントで定義されたコマンドのみに制限されます。
- ユーザーに対して 1 つのマネージドノードに 1 つの対話コマンドの実行を許可
-
- 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}-*"]
}
]
}
- ユーザーに対してすべてのマネージドノードに 1 つの対話コマンドの実行を許可
-
- 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}-*"]
}
]
}