5
6

More than 5 years have passed since last update.

【AWS】IAM Roleの権限を実際に確認する【tips】

Last updated at Posted at 2018-06-07

はじめに

あるIAM Roleの権限が適切かどうか/必要なAWS APIを実行できるか、意図したとおりになっているか確認するためには、通常はPolicy Simulatorを利用することになるかと思います。

IAMPolicy Simulator を使用した IAM ポリシーのテスト
https://docs.aws.amazon.com/ja_jp/IAM/latest/UserGuide/access_policies_testing-policies.html

ただ、一部のリソースベースのPolicyに対応していなかったり、実際に動作させてみないと分からないことがあります。

特に最近困ったのは、ECRのdockerリポジトリに対して、pull/push出来るかどうか直ぐに確認できなかったことです。
このような場合、自分のIAM Userアカウントから対象のIAM RoleをAssumeRoleすることにより、少し簡単に確認することが可能です。

手順概要

  1. 対象となるIAM RoleのTrust Relationshipsに、自分自身のIAM Entity(User/Role)を追加する
  2. CLIからAssumeRoleし、temporary credentialsを環境変数にセットする
  3. 実行できるか確認したいAWS APIを実行する
  4. Trust Relationshipsから、自分自身のIAM Entity(User/Role)をはずす

動作確認環境・構成

  • AWSアカウントID: 999999999999
  • Region: ap-northeast-1
  • IAM User: test-user(AssumeRoleする権限はあるものとします。)
  • IAM Role: test-role (CodeBuild用のRoleとします)

IAM RoleのTrush Relationships

TrustRelationships
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "codebuild.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

手順詳細

1. 対象となるIAM RoleのTrust Relationshipsに、自分自身のIAM Entity(User/Role)を追加する

AWS Management Consoleから、対象のRoleのTrust Relationshipsを編集します。
https://console.aws.amazon.com/iam/home?region=ap-northeast-1#/roles/test-role?section=trust

TrustRelationships(変更後)
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "codebuild.amazonaws.com",
        "AWS": "arn:aws:iam::999999999999:user/test-user"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

2. CLIからAssumeRoleし、temporary credentialsを環境変数にセットする

aws configureを実行するか環境変数等で、test-userのAccessKey/SecretAccessKeyが利用されるように設定します。

自分自身のIAM User権限でAWS APIが実行されることを確認します。

get-caller-identity
$ aws sts get-caller-identity
{
    "Account": "999999999999", 
    "UserId": "AIDAXXXXXXXXXXXXXXXXX", 
    "Arn": "arn:aws:iam::999999999999:user/test-user"
}

AWS CLIからAssumeRoelを実行します。

assume-role
$ aws sts assume-role --role-arn "arn:aws:iam::999999999999:role/test-role" --role-session-name "you-can-put-any-string"
{
    "AssumedRoleUser": {
        "AssumedRoleId": "AROAXXXXXXXXXXXXXXXXX:you-can-put-any-string",
        "Arn": "arn:aws:sts::999999999999:assumed-role/test-role/you-can-put-any-string"
    },
    "Credentials": {
        "SecretAccessKey": "0hq4**********************************",
        "SessionToken": "FQoD************************************************************************************************************************************************************************************************************",
        "Expiration": "2018-06-08T12:34:56Z",
        "AccessKeyId": "ASIAXXXXXXXXXXXXXXXX"
    }
}

取得したtemporary credentialsを、下記のように環境変数にセットします。

$ export AWS_ACCESS_KEY_ID="ASIAXXXXXXXXXXXXXXXX"
$ export AWS_SECRET_ACCESS_KEY="0hq4**********************************"
$ export AWS_SESSION_TOKEN="FQoD************************************************************************************************************************************************************************************************************"

3. 実行できるか確認したいAWS APIを実行する

AWS CLIから、確認したいAWS APIを実行します。
今回は、ECR上のDocker Imageをpull出来るかどうかを確認します。

### Docker loginする
$ $(aws ecr get-login --no-include-email --region ap-northeast-1)
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Login Succeeded

### Docker pullする
$ docker pull 999999999999.dkr.ecr.ap-northeast-1.amazonaws.com/test-docker:latest

4. Trust Relationshipsから、自分自身のIAM Entity(User/Role)をはずす

AWS Management Consoleから、対象のRoleのTrust Relationshipsを編集し、変更前の状態に戻します。
https://console.aws.amazon.com/iam/home?region=ap-northeast-1#/roles/test-role?section=trust

5
6
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
5
6