saml2awsでSAML認証を用いたaws cliの利用を簡単にした


はじめに

皆さんはAWS マネジメントコンソールへのログインどうされていますか?
弊社では最近GSuite SAML認証を使ってコンソールへログインしています。

GSuiteにログインしているだけでAWSにもログインできて大変便利なんですが一つだけ大きなデメリットがあり、それが… aws cliがとても使いづらい!という問題でした。
(AWS STSの一時的認証情報の期限が切れるたびにGSuiteとごにょごにょして一時的認証情報を取得し直す必要があるorz)

ですが、いろいろ探してみるとsaml2awsという便利ツールがあったので紹介です。golangで実装されています。

saml2awsが対応しているIDプロバイダー

  • One of the supported Identity Providers
    • ADFS (2.x or 3.x)
    • PingFederate + PingId
    • Okta
    • KeyCloak + (TOTP)
    • Google Apps
  • AWS SAML Provider configured

インストール方法

OSXインストール方法
brew tap versent/homebrew-taps
brew install saml2aws
Windowsインストール方法
choco install saml2aws
saml2aws --version

設定手順

まずはsaml認証の設定です。

項目名 設定値
provider GoogleApps
AWS Profile awsコマンドで利用するprofile名
URL https://accounts.google.com/o/saml2/initsso?idpid=XXXXXXX&spid=YYYYY&forceauthn=false
※ saml認証用のURL
Username GSuiteユーザー名
Password GSuiteパスワード
Confirm パスワード確認

今回は上記内容を対話式で設定します。

% saml2aws configure -a test_idp_account
? Please choose a provider: GoogleApps
? AWS Profile test_profile

? URL https://accounts.google.com/o/saml2/initsso?idpid=XXXXXXX&spid=YYYYY&forceauthn=false
? Username [email protected]

? Password ****
? Confirm ****

account {
  URL: https://accounts.google.com/o/saml2/initsso?idpid=XXXXXXX&spid=YYYYY&forceauthn=false
  Username: [email protected]
  Provider: GoogleApps
  MFA: Auto
  SkipVerify: false
  AmazonWebservicesURN: urn:amazon:webservices
  SessionDuration: 3600
  Profile: test_profile
}

Configuration saved for IDP account: test_idp_account

次に先程設定した認証情報でログインしてみます。


% saml2aws login -a test_idp_account
Using IDP Account test_idp_account to access GoogleApps https://accounts.google.com/o/saml2/initsso?idpid=XXXXXXX&spid=YYYYY
To use saved password just hit enter.
? Username [email protected]
? Password ****

Authenticating as [email protected] ...
? Security Token [000000] 000000
Selected role: arn:aws:iam::000000000000:role/GSuite
Requesting AWS credentials using SAML assertion
Logged in as: arn:aws:sts::000000000000:assumed-role/GSuite/[email protected]

Your new access key pair has been stored in the AWS configuration
Note that it will expire at 2018-07-13 19:03:14 +0900 JST
To use this credential, call the AWS CLI with the --profile option (e.g. aws --profile test_profile ec2 describe-instances).

これで無事プロファイルが作成されました!
(ちなみに2018/7/13現在、GSuiteでMFA(2段階認証)を設定してると日本環境ではエラーになってしまいます。。ですが、私の方で適当ではありますが日本環境で動くように修正してプルリクエスト送ってます。続報をまて! 2018/7/14にマージされました)

aws cliで使ってみる


aws --profile test_profile ec2 describe-instances --region ap-northeast-1                                  
{
    "Reservations": [
        {
            "Groups": [],
            "Instances": [
                {

                .
                .
                .

簡単にコマンド使えるようになりました。便利!