タダです.
AWS CLI の SSO Login を使う中で疑問を持つ挙動があり,調べた内容をこの記事でまとめます.
疑問を持つ挙動の概要
~/.aws/config に下記の SSO の設定をしていたとします.この状態で aws sso login --profile dev としたらどういった挙動ができるようになるでしょうか.
[profile dev] sso_start_url = https://hoge.awsapps.com/start sso_region = us-east-1 sso_account_id = 1234567891011 sso_role_name = AdministratorAccess region = ap-northeast-1 output = json [profile stg] sso_start_url = https://hoge.awsapps.com/start sso_region = us-east-1 sso_account_id = 1234567891012 sso_role_name = AdministratorAccess region = ap-northeast-1 output = json
自分が試してみた動作はログイン後に aws sts get-caller-identity --profile dev と aws sts get-caller-identity --profile stg を叩いてみました.すると,stg もセッション情報が取れてしまいました.dev にログインしたのに期待した動作と違い,気になるので調査をすることにしました.
$ aws sts get-caller-identity --profile dev Error loading SSO Token: Token for https://hoge.awsapps.com/start does not exist $aws sts get-caller-identity --profile stg Error loading SSO Token: Token for https://hoge.awsapps.com/start does not exist $ aws sso login --profile dev Attempting to automatically open the SSO authorization page in your default browser. If the browser does not open or you wish to use a different device to authorize this request, open the following URL: https://device.sso.us-east-1.amazonaws.com/ Then enter the code: $ aws sts get-caller-identity --profile dev { "UserId": "xxx:hoge@hoge.com", "Account": "1234567891011", "Arn": "arn:aws:sts::1234567891011:assumed-role/AWSReservedSSO_AdministratorAccess_xxx/hoge@hoge.com" } ※↓がstgのクレデンシャルを取得できている状況 $ aws sts get-caller-identity --profile stg { "UserId": "yyy:hoge@hoge.com", "Account": "1234567891012", "Arn": "arn:aws:sts::1234567891012:assumed-role/AWSReservedSSO_AdministratorAccess_yyy/hoge@hoge.com" }
調査した結果
調べた結果,ドキュメントに記載がありますが,SSO の設定として2つの設定があります.更新不可のレガシー設定と自動認証更新によるトークンプロバイダーの設定があります.今回の例では,更新不可のレガシーを設定を使っていることになります.
AWS サポートにも問い合わせさせていただき調査いただいたところ,更新不可のレガシー設定で aws sso login --profile プロファイル名 にてアクセスポータルにサインインを行うとサインインを行ったユーザーに割り当てられているすべての許可セットに関連付くIAMロールの認証情報を取得可能となるそうです.そのため,dev でログインしても stg の認証情報が取れていたということになっていた模様です.
自動更新によるトークンプロバイダーの設定に変更した場合の挙動確認
今回,ドキュメントを読んで自動更新によるトークンプロバイダーの設定を適用すると,認証情報を自動的に更新するっていうのが最高だなと思いました.これまでは認証が切れたら自分で都度ログインしなきゃいけなかったのが自動更新してくれるならこの手間がなくなって嬉しい限りです.
SSO トークンプロバイダー設定を使用すると、アプリケーションの必要に応じて認証トークンを自動的に更新し、セッション期間の延長オプションを使用できます
それでは設定を変更をしてみます.手動変更してもうまくいくと思ったのですが,なかなか自分の環境ではうまくいかず aws configure sso --profie プロファイル名 で再度設定をすればいい感じになりました.これで明示的に指定したプロファイルでログインした環境でしか認証情報を取れなくなりました.
$ aws configure sso --profile dev SSO session name (Recommended): sso-dev There are 2 AWS accounts available to you. Using the account ID 1234567891011 There are 2 roles available to you. Using the role name "AdministratorAccess" CLI default client Region [ap-northeast-1]: CLI default output format [json]: $ aws sts get-caller-identity --profile dev { "UserId": "xxx:hoge@hoge.com", "Account": "1234567891011", "Arn": "arn:aws:sts::1234567891011:assumed-role/AWSReservedSSO_AdministratorAccess_xxx/hoge@hoge.com" } $ aws sts get-caller-identity --profile stg Error loading SSO Token: Token for https://hoge.awsapps.com/start does not exist $ cat ~/.aws/config [profile dev] sso_sssion = hoge-dev sso_account_id = 1234567891011 sso_role_name = AdministratorAccess region = ap-northeast-1 output = json [sso-session hoge-dev] sso_region = us-east-1 sso_start_url = https://hoge.awsapps.com/start sso_registration_scopes = sso:account:access [profile stg] sso_account_id = 1234567891012 sso_role_name = AdministratorAccess region = ap-northeast-1 output = json sso_session = hoge-stg [sso-session hoge-stg] sso_start_url = https://hoge.awsapps.com/start sso_region = us-east-1 sso_registration_scopes = sso:account:access
なお,次回からのセッション取得はこれまで通り aws sso login --profile プロファイル名 で実行可能です.
$aws sso login --profile dev Attempting to automatically open the SSO authorization page in your default browser. If the browser does not open or you wish to use a different device to authorize this request, open the following URL: https://device.sso.us-east-1.amazonaws.com/ Then enter the code: xxxx-xxx Successfully logged into Start URL: https://hoge.awsapps.com/start $ aws sts get-caller-identity --profile dev { "UserId": "xxx:hoge@hoge.com", "Account": "1234567891011", "Arn": "arn:aws:sts::1234567891011:assumed-role/AWSReservedSSO_AdministratorAccess_xxx/hoge@hoge.com" } $ aws sts get-caller-identity --profile stg Error loading SSO Token: Token for hoge-stg does not exist
まとめ
AWS CLI の SSO Login の挙動で調べたことをまとめました.ちょっとした config の設定の差異で他の環境といえど クレデンシャルを取れてしまうのは怖いので,適切な設定管理を行いたいです.