Skip to content

Instantly share code, notes, and snippets.

@matsubo
Last active July 14, 2021 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matsubo/6f46aa2f8131e2ce5610a979e8d98361 to your computer and use it in GitHub Desktop.
Save matsubo/6f46aa2f8131e2ce5610a979e8d98361 to your computer and use it in GitHub Desktop.

Session Managerを利用するときの制限のかけかた

前提

  • Session Managerをデフォルトで使うと sudoが可能なssm-userユーザを使ってログインすることとなる。

sudoさせたくない場合

  • ssm-userとは別のユーザアカウントをログイン先のOS上で作成する

    • sudo権限は付与しない
  • RoleやGroupでログイン先のユーザ名を指定する場合は、Session Manager上の設定を有効化する必要がある

    • Enable Run As support for Linux instances
  • SSMSessionRunAs というタグ名を作って、ログインさせたいユーザ名を値に設定する。

  • 例: 以下のようなPolicyを作った上で、RoleやGroupにattachする。

    • ここでホスト名やregionなどの制限を付け加える。
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "ssm:StartSession",
            "Resource": [
                "arn:aws:ssm:*:*:document/AWS-StartSSHSession",
                "arn:aws:ec2:*:*:instance/<instance id>"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "ssm:DescribeSessions",
                "ssm:GetConnectionStatus",
                "ssm:DescribeInstanceProperties",
                "ec2:DescribeInstances"
            ],
            "Resource": "*"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": "ssm:TerminateSession",
            "Resource": "arn:aws:ssm:*:*:session/${aws:username}-*"
        }
    ]
}

ログイン方法

現時点では以下のユースケースを満たすためのアプローチは設定は違う。 1を可能にした上で、2をできるようにする必要がある。

  1. session managerを使ったshellでのログイン
  2. rsyncを使うためのsshのトンネルの作成

1はStartSession権限を与えれば使えるようになる。 2を実現するためには、ログイン先のshellアカウントの authorized_keys にログイン元のssh公開鍵を登録しておく。

AWSの公式ドキュメントなどで紹介されている以下の.ssh/configの設定はSession Managerを使ってトンネルを張ることを意味しているだけ。 sshトンネルを張った上で、sshをローカルに実行するようにして2を実現している様子。

host i-* mi-*
    ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"

※2を実現するために、RunCommand権限を付与すれば公開鍵の登録は不要になるが、今度は別のユーザでのログインもできるようになってしまうので本末転倒になる。

@matsubo
Copy link
Author

matsubo commented May 20, 2021

sample setting for "ssh over session manager"

.ssh/config

# SSH over Session Manager
host i-* mi-*
    ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"

command

matsu@dell ~ [20]> ssh baito@i-05a6d249f191b253a
open terminal failed: not a terminal
Last login: Thu May 20 16:34:09 2021 from localhost

       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/
No packages needed for security; 1 packages available
Run "sudo yum update" to apply all updates.
[baito@ip-10-0-0-141 ~]$ whoami
baito

rsync example

command

matsu@dell ~> rsync -aruzv -e ssh jpon/  baito@i-xxxxxxxxxxxxxxxxxx:/tmp/jpon
open terminal failed: not a terminal
sending incremental file list
./
all.utf8.csv
^Crsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(644) [sender=3.1.3]
rsync: [sender] write error: Broken pipe (32)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment