おじさん。AWSを触る。その8
ハンズオンネタを書くといってそのまま一年放置していました。
今回はIAMネタです。
ハンズオン用IAMユーザに「AdministratorAccess」なんて安易につけたくないんです。
「AdministratorAccess」つけたIAMユーザで
「AWS Access Key ID 」や「AWS Secret Access Key」を払い出すなんて。
Key漏れたらどうするんですか?
何か良い方法がないかなぁと。
「AssumeRole」とか「aws sts assume-role」使えば何とかなるかなと。
前提:
環境はWindows10Homeを利用しています。
Windows での AWS CLI バージョン 2 のインストール済みとします。
1.ハンズオン用IAMユーザを作る
今回ハンズオン用IAMユーザとして「handson-cli-usr」を作ります。
何の権限もつけません。
アクセスキーは作ります。
MFAはつけます。
新規ユーザを作ります。
「プログラムによるアクセス」のみ許可します。
グループは何も指定しません。
タグもつけません
「このユーザにはアクセス権限がありません」と言われますが、何もつけないのです。
「アクセスキー」だけはメモするなりダウンロードします。
「MFAデバイスの割り当て」をします。
「仮想MFAデバイス」を選択します。
好きな仮想MFAを利用します。
これで「アクセスキー」で「MFA」で「何の権限もない」IAMユーザができました。
仮想デバイスは「arn:aws:iam::XXXXXXXXXXXX:mfa/handson-cli-usr」メモしておいてください。
2.ハンズオン用IAMロールを作る
今回は「AdministratorAccess」を持ったIAMロールを作ります。
「別のAWSアカウント」を選択して「アカウントID」に別ではない自分のアカウントIDを入力します。
「MFAが必要」にチェックを忘れずに
「AdministratorAccess」を選択します。
特に何もせずに次へ
「ロール名」に「Myhandson-cli-role」として作成します。
「信頼関係の編集」を押して修正します。
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::XXXXXXXXX:root"
},
"Action": "sts:AssumeRole",
"Condition": {
"Bool": {
"aws:MultiFactorAuthPresent": "true"
}
}
}
]
}
"AWS": "arn:aws:iam::XXXXXXXXX:root" となっているところを
"arn:aws:iam::XXXXXXXXXXX:user/handson-cli-usr" に変更します。
MFAを利用していてるIAMユーザ「handson-cli-usr」はこのロールに変身してよろしいとなります。
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::XXXXXXXXXXXX:user/handson-cli-usr"
},
"Action": "sts:AssumeRole",
"Condition": {
"Bool": {
"aws:MultiFactorAuthPresent": "true"
}
}
}
]
}
3.ローカルPCから接続してみる。
aws configure
AWS Access Key ID [None]: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
AWS Secret Access Key [None]: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Default region name [ap-northeast-1]:
Default output format [json]:
ダウンロードしたアクセスキーをセットしておきます。
コマンドを実行してみましょう。
D:\jawsug>aws iam get-user
An error occurred (ValidationError) when calling the GetUser operation: Must specify userName when calling with non-User credentials
D:\jawsug>aws ec2 describe-instances
An error occurred (UnauthorizedOperation) when calling the DescribeInstances operation: You are not authorized to perform this operation.
D:\jawsug>aws s3 ls
An error occurred (AccessDenied) when calling the ListBuckets operation: Access Denied
IAMユーザ「handson-usr」は何の権限もないので権限エラーになります。
では、assume-roleで権限のあるロールに変身します。
一時的な認証情報を取得します。
--role-arn 変身するロール
--serial-number MFAデバイス
--role-session-name お好きな名前
--token-code MFAデバイスから取得した番号
--duration-seconds 必要ならば有効期限
aws sts assume-role --role-arn arn:aws:iam::XXXXXXXXXXXXX:role/Myhandson-cli-role \
--serial-number arn:aws:iam::XXXXXXXXXXXXX:mfa/handson-cli-usr \
--role-session-name Myhandson-role \
--token-code xxxxxxx
デフォルトでは3600 seconds.の有効期限です。
1時間有効期限切れたらハンズオン終わらない。もっと長くしたいという人はduration-secondsオプションを指定することにより有効期限を既定から変更できます。
{
"Credentials": {
"AccessKeyId": "XXXXXXXXXXXXXXXXXXXXXXXX",
"SecretAccessKey": "XXXXXXXXXXXXXXXXXXXXXXXX",
"SessionToken": "XXXXXXXXXXXXXXXXXXXXXXXX",
"Expiration": "2021-05-26T13:29:10+00:00"
},
"AssumedRoleUser": {
"AssumedRoleId": "XXXXXXXXXXXXXXXXXXXXXXXX:Myhandson-cli-role",
"Arn": "arn:aws:sts::XXXXXXXXXXXXXXXXXXXXXXXX:assumed-role/Myhandson-cli-role/Myhandson-role"
}
}
一時的な認証情報が取得できました。
.aws/credentials に取得した一時的な認証情報を記述しておきます。
[default]
aws_access_key_id = XXXXXXXXXXXXXXXXX
aws_secret_access_key = XXXXXXXXXXXXXXXXX
[session-token]
aws_access_key_id = XXXXXXXXXXXXXXXXX
aws_secret_access_key = XXXXXXXXXXXXXXXXX
aws_session_token = XXXXXXXXXXXXXXXXX
4.プロファイルを切り替えて試してみる
まずは何も権限もないプロファイルでコマンドを試します。
aws configure list
Name Value Type Location
---- ----- ---- --------
profile <not set> None None
access_key ****************5GPL shared-credentials-file
secret_key ****************41Qn shared-credentials-file
region <not set> None None
D:\jawsug>aws iam get-user
An error occurred (ValidationError) when calling the GetUser operation: Must specify userName when calling with non-User credentials
D:\jawsug>aws ec2 describe-instances
An error occurred (UnauthorizedOperation) when calling the DescribeInstances operation: You are not authorized to perform this operation.
D:\jawsug>aws s3 ls
An error occurred (AccessDenied) when calling the ListBuckets operation: Access Denied
デフォルトでは何も権限がないので権限エラーになります。
先ほど取得したプロファイルに切り替えます
D:\jawsug>set AWS_DEFAULT_PROFILE=session-token
D:\jawsug>aws configure list
Name Value Type Location
---- ----- ---- --------
profile session-token env ['AWS_PROFILE', 'AWS_DEFAULT_PROFILE']
access_key ****************GBMG shared-credentials-file
secret_key ****************np7W shared-credentials-file
region <not set> None None
コマンドを実行してみましょう
D:\jawsug>aws s3 ls
D:\jawsug>aws iam get-user --user-name handson-usr
{
"User": {
"Path": "/",
"UserName": "handson-usr",
"UserId": "XXXXXXXXXXXXXXXX",
"Arn": "arn:aws:iam::XXXXXXXXXXXX:user/handson-cli-usr",
"CreateDate": "2021-05-26T09:28:47+00:00"
}
}
「AdministratorAccess」が権限が付いたことを確認できます。
これでハンズオンやった後とかアクセスキーの心配をしなくていいですね。有効期限が切れると
D:\jawsug>aws iam get-user
An error occurred (ValidationError) when calling the GetUser operation: Must specify userName when calling with non-User credentials
D:\jawsug>aws ec2 describe-instances
An error occurred (UnauthorizedOperation) when calling the DescribeInstances operation: You are not authorized to perform this operation.
D:\jawsug>aws s3 ls
An error occurred (AccessDenied) when calling the ListBuckets operation: Access Denie
となってこのアクセスキーでは何もできなくなっています。
以上、何の権限もないIAMユーザを変身させるでした。
毎回、アクセスーを取得してcredentialsファイル書き換えるの煩雑なのでラップする何かを作らないといけないなぁ。
[謎のコマンド] 引数:--token-code で切り替えれるようにしないと。
参考にしたURL
https://dev.classmethod.jp/articles/iam-role-passrole-assumerole/
https://blog.serverworks.co.jp/tech/2018/07/25/cliassumerole/
https://aws.amazon.com/jp/premiumsupport/knowledge-center/iam-assume-role-cli/
サポートして頂くと頑張るかもしれません。