0
0

More than 1 year has passed since last update.

AWS System Manager経由でWinSCPを利用するバッチ(MFA認証あり・Windows)

Last updated at Posted at 2023-05-17

MFA認証ありの環境で、SSM (AWS System Manager)経由でWinSCPを使いたいという要望があり、試行錯誤してバッチ化できたのでメモ。

前提となる知識

設定

セッショントークンを取得するためのIAM認証情報をcredentialsに記載する

~/.aws/credentials
[my_account]
aws_access_key_id = xxx
aws_secret_access_key = xxx

接続ロールのIAM設定をconfigに記載する

~/.aws/config
[profile my_role]
region = xxxxxxxxxxxxx
role_arn = arn:aws:iam::xxxxxxxxxxxx:role/xxx
source_profile = my_session

バッチ

基本はJSONの取り回しをしやすいpowershellで作成し、AWS CLI実行時のみbatを呼び出す。(もっとスマートな方法があるかもしれません。)

本体powershell

my_dir/save-session-token.ps1
$mfaSerialNumber = Get-Content var/mfa-serial.txt
"[MFAシリアル]$mfaSerialNumber"

$sessionTokenObj = 0

while (!$sessionTokenObj) {
    $mfaToken = 0
    while(!$mfaToken){
        $mfaToken =(Read-Host MFAトークンを入力してください)
    }
    Start-Process get-session-token.bat -ArgumentList "$mfaSerialNumber $mfaToken" -NoNewWindow -Wait -RedirectStandardError var/standard-error-get-session-token.txt -RedirectStandardOutput var/standard-output-get-session-token.txt
    $error = Get-Content var/standard-error-get-session-token.txt
    if (!$error) {
        $sessionTokenObj = Get-Content var/standard-output-get-session-token.txt | ConvertFrom-Json
        Copy-Item var/standard-output-get-session-token.txt var/session-token.json
        Start-Process set-aws-credential.bat -NoNewWindow -ArgumentList "aws_access_key_id $($sessionTokenObj.Credentials.AccessKeyId)"
        Start-Process set-aws-credential.bat -NoNewWindow -ArgumentList "aws_secret_access_key $($sessionTokenObj.Credentials.SecretAccessKey)"
        Start-Process set-aws-credential.bat -NoNewWindow -ArgumentList "aws_session_token $($sessionTokenObj.Credentials.SessionToken)"
        Start-Sleep -Seconds 3
        "新しいセッショントークンをcredentialsに設定しました"
    } else {
        $error
    }
}

"10秒後に自動でウィンドウを閉じます"
Start-Sleep -Seconds 10
exit

MFA用シリアル設定

my_dir/var/mfa-serial.txt
arn:aws:iam::xxxxxxxxxxxx:mfa/Xxxxxxxxx

AWSコマンド実行bat

my_dir/set-aws-credential.bat
aws configure --profile my_session set %1 %2
my_dir/get-session-token.bat
@echo off
aws sts get-session-token --profile my_account --serial-number %1 --token-code %2 --duration-seconds 86400

オプションで有効期限を24時間(86400秒)としていますが、15分~36時間の間で指定できます。デフォルトは12時間。

 --duration-seconds 86400

WinSCP設定

プロキシコマンドでmy_roleのプロファイルを指定します。
my_roleで接続するときに一時的な認証情報であるセッショントークンを使用します。

aws ssm start-session --target %host --document-name AWS-StartSSHSession --parameters "portNumber=%port" --profile "my_role"

接続

本体powershellを起動するとMFA認証コードを聞かれるので入力します。
image.png

セッショントークン取得に成功したらcredentialsに登録されます。

~/.aws/credentials
[my_account]
aws_access_key_id = xxx
aws_secret_access_key = xxx
[my_session]
aws_access_key_id = get-session-tokenで取得した値
aws_secret_access_key = get-session-tokenで取得した値
aws_session_token = get-session-tokenで取得した値

これでWinSCPで接続できます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0