LoginSignup
2
1

More than 3 years have passed since last update.

IAMロールの信頼ポリシーでロールセッション名を制限する

Posted at

はじめに

2020/4/21 に IAMロールの信頼ポリシーで利用可能な AWS STS の条件キーに
sts:RoleSessionName が追加されました。

IAM ロールを使用して実行されたアクションを担当する ID を簡単に特定
https://aws.amazon.com/jp/about-aws/whats-new/2020/04/now-easily-identify-the-identity-responsible-for-the-actions-performed-using-iam-roles/

この条件キーを 信頼ポリシーの Condition に記述することで
AssumeRole でロールを引き受ける際に設定する必要のあるロールセッション名指定できます。

何が嬉しいか

ロールセッション名とは AssumeRole 時にオプションで指定可能な文字列です。
この値は CloudTrail にも記載されるため、セッションの識別に使用できます。

条件キー sts:RoleSessionName によりロールセッション名を強制できるため
例えば以下のような信頼ポリシーを記述することで、AssumeRole を行ったIAM ユーザー名を
確実に CloudTrail の証跡に残すことができます。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "sts:AssumeRole",
            "Principal": {
                "AWS": "arn:aws:iam::0123456789012:root"
            },
            "Condition": {
                "StringLike": {
                    "sts:RoleSessionName": "${aws:username}"
                }
            }
        }
    ]
}

やってみる

test_user という IAM ユーザーが 同一アカウント内で IAMTEST_ROLE という
ロールに切り替えるというケースで考えます。

IAMTEST_ROLE の信頼ポリシーを前述のように書き換えて保存します。
Principal のアカウントID は 実際のものに変更してください。

image.png

AWSCLI で検証してみます。
default プロファイルに test_user のアクセスキー/シークレットアクセスキーを設定します。

$ aws configure
AWS Access Key ID [None]: AKIAXXXXXXXXXEXAMPLE
AWS Secret Access Key [None]: xxxxxxxxxxxxx/xxxxxxx/xxxxxxxxEXAMPLEKEY
Default region name [None]: ap-northeast-1
Default output format [None]: json

~/.aws/config に テスト用のプロファイルを iamtest と iamtest2 という名前で追加します。

[profile iamtest]
region = ap-northeast-1
role_arn = arn:aws:iam::0123456789012:role/IAMTEST_ROLE
role_session_name = hogehoge
source_profile = default

[profile iamtest2]
region = ap-northeast-1
role_arn = arn:aws:iam::0123456789012:role/IAMTEST_ROLE
role_session_name = test_user
source_profile = default

--profile iamtest では ロールセッション名が呼び出し元の IAM ユーザーと
一致しない (role_session_name = hogehoge) ため、AccessDenied になります。

$ aws sts get-caller-identity --profile iamtest
An error occurred (AccessDenied) when calling the AssumeRole operation: User:
arn:aws:iam::0123456789012:user/test_users is not authorized to perform: 
sts:AssumeRole on resource: arn:aws:iam::0123456789012:role/IAMTEST_ROLE

--profile iamtest2 ではロールセッション名が呼び出し元の IAM ユーザーと
一致する (role_session_name = test_user) ため、get-caller-identity の結果が返ります。

$ aws sts get-caller-identity --profile iamtest2
{
    "UserId": "AROAXXXXXXXXXXXXXXXXX:test_user",
    "Account": "0123456789012",
    "Arn": "arn:aws:sts::0123456789012:assumed-role/IAMTEST_ROLE/test_user"
}

ちなみに マネージドコンソールからのスイッチロールの場合、ロールセッション名は自動的に
IAM ユーザー名に設定されるため、今回の例では問題なくスイッチできます。

参考

IAM and AWS STS Condition Context Keys
https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_iam-condition-keys.html#condition-keys-sts

簡単ですが以上です。
参考になれば幸いです。

2
1
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
2
1