LoginSignup
0
0

More than 1 year has passed since last update.

AWS ValutによるMFA認証メモ

Last updated at Posted at 2022-04-20

概要

  • セキュアにAWSにログインまたスイッチロールしたい

前提

  • マルチアカウントである
  • スイッチロールが設定されている

詳細

  • aws-vaultでMFA認証およびスイッチロールをする

インストール

brew install --cask aws-vault

設定

  • アクセスキーを確認
cat ~/.aws/credentials
  • 既存の利用するIAMユーザを登録する
  • "hoge-user"はprofileに追加される識別子なので任意の名称で良い
$ aws-vault add "hoge-user"
Enter Access Key ID: AKI************
Enter Secret Access Key: 
Added credentials to profile "profile company" in vault
  • 画面に今後利用したいパスワードの入力を促されるので適当なパスワードを入力

image.png

  • ~/.aws/configにprofile設定が追加されている
$ tail ~/.aws/config
[profile hoge-user]
$ aws-vault ls
Profile                  Credentials              Sessions                 
=======                  ===========              ========                                    
default                  -                        -                        
hoge-user                hoge-user                -                    
  • テスト:aws-vaultコマンドでsts経由でアカウント情報を取得して見る
$ aws-vault exec <hoge-user> -- aws sts get-caller-identity
{
    "UserId": "AI***************L7",
    "Account": "09********82",
    "Arn": "arn:aws:iam::09********82:user/company-hoge-iamuser"
}
  • 認証がパスし、Sessins欄に有効期限が表示される
$ aws-vault ls
Profile                  Credentials              Sessions                 
=======                  ===========              ========                                    
default                  -                        -                        
hoge-user                hoge-user                sts.AssumeRole:59m46s             
  • テスト:aws-vaultコマンドで環境変数を取得してみる
$ aws-vault exec <hoge-user> -- env | grep AWS
AWS_VAULT=<hoge-user>
AWS_DEFAULT_REGION=ap-northeast-1
AWS_SECURITY_TOKEN=*******
AWS_REGION=ap-northeast-1
AWS_ACCESS_KEY_ID=ASIA*******DB6ME
AWS_SECRET_ACCESS_KEY=*******************
AWS_SESSION_TOKEN=**********
AWS_SESSION_EXPIRATION=2022-04-19T06:51:04Z
  • CloudTrailでget-caller-identityを実行したことの詳細を確認

image.png

スイッチロールする場合

  • 以下のような~.aws/configを作成
  • mfa_serialはMFAのarnを入力する事。iam-userのarnと間違えると認証できない。
[profile hogeuser]  #aws-vaultで追加したprofile


[profile hogeuser-common] #スイッチロール前アカウント
source_profile = hogeuser
region=ap-northeast-1
output=json
mfa_serial=arn:aws:iam::09********82:mfa/company-hoge-iamuser
role_session_name=company-hoge-iamuser

[profile hogeuser-admin] #スイッチロール先アカウント
include_profile=hogeuser-common
role_arn=arn:aws:iam::39********81:role/fullaccess_admin_company-hoge-iamuser

テスト:aws-vaultコマンドでsts経由でアカウント情報を取得して見る

$ aws-vault exec hogeuser-admin -- aws sts get-caller-identity
Enter MFA code for arn:aws:iam::09********82:mfa/company-hoge-iamuser: <MFA:6桁の数字を入力>
{
    "UserId": "AR**********5E:company-hoge-iamuser",
    "Account": "39********81",
    "Arn": "arn:aws:sts::39********81:assumed-role/fullaccess_admin_company-hoge-iamuser/company-hoge-iamuser"
}

テスト:aws-vaultコマンドでAWSコンソールにログイン

$ aws-vault login hogeuser-admin

profile削除する場合

  • 不要になったprofileは以下のようにして削除
$ aws-vault remove "profile <profile_name>"   
Delete credentials for profile "profile <profile_name>"? (y|N) y
Deleted credentials.

$ vi ~/aws/config
:
[profile <profile_name>]   ⇐※1末尾にあるaws vault add した際追加されたprofileを削除

※1を対応しないと以下エラー

$ aws-vault add <profile_name>
aws-vault: error: add: Your profile has a source_profile of <profile_name>, adding credentials to <profile_name> won't have any effect
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