LoginSignup
5
2

More than 1 year has passed since last update.

[小ネタ] Github Actions用のOIDC ProviderをAWSコンソールから作ったときのトラップ

Posted at

これは「「はじめに」の Advent Calendar 2021」24日目の記事です。

Github ActionsからAWSのリソースを操作する

GitHub ActionsでOpenID Connect経由の各種Cloud Providerの認証取得がGAになったし、年末だしAccessTokenを大掃除したかっただけでした。
検証中に1時間もハマったのでメモ。

OIDC Provier作成

GUIからProviderを作成。

プロバイダのタイプ プロバイダのURL 対象者
OpenID Connect https://token.actions.githubusercontent.com sts.amazonaws.com

スクリーンショット 2021-12-24 20.22.15.png

プロバイダの「ロールの割当」→「新しいロールを作成」
スクリーンショット 2021-12-24 20.22.42.png

「ウェブID」 → Audienceに「 sts.amazonaws.com 」を選択
スクリーンショット 2021-12-24 20.23.01.png

ポリシーならなんやらをつけて作成後はこうなります。
スクリーンショット 2021-12-24 20.24.04.png

1つ目のトラップ

GithubActionsに適当なWorkflowを作成して動かしてみます。

name: "sample"

on: [ workflow_dispatch ]

env:
  AWS_ROLE_ARN: ${{ secrets.AWS_ROLE_ARN }}

jobs:
  test:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: write
    steps:
      - uses: actions/checkout@v2
      - uses: aws-actions/configure-aws-credentials@master
        with:
          role-to-assume: ${{ env.AWS_ROLE_ARN }}
          role-session-name: sample
          aws-region: ap-northeast-1
      - run: aws s3 ls

エラーになります。

Run aws-actions/configure-aws-credentials@master
Error: Not authorized to perform sts:AssumeRoleWithWebIdentity

信頼関係のポリシーはこうなっていますが、コレでは動きません。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Federated": "arn:aws:iam::${aws_account_id}:oidc-provider/token.actions.githubusercontent.com"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
        }
      }
    }
  ]
}

この記事を参考に、

          "token.actions.githubusercontent.com:aud": "sts.amazonaws.com"

を↓に変更します

          "token.actions.githubusercontent.com:sub": "repo:${github_org}/${github_repo}:*"

再実行してもエラー。しかも内容変わらず。

Run aws-actions/configure-aws-credentials@master
Error: Not authorized to perform sts:AssumeRoleWithWebIdentity

2つ目のトラップ

理由がわからず、Cloud Trailまで見に来ました。

{
    "eventVersion": "1.08",
    "userIdentity": {
        "type": "WebIdentityUser",
        "principalId": "arn:aws:iam::${aws_account_id}:oidc-provider/token.actions.githubusercontent.com:sts.amazonaws.com:repo:${github_org}/${github_repo}:ref:refs/heads/test/oidc-test",
        "userName": "repo:${github_org}/${github_repo}:ref:refs/heads/test/oidc-test",
        "identityProvider": "arn:aws:iam::${aws_account_id}:oidc-provider/token.actions.githubusercontent.com"
    },
    "eventTime": "2021-12-24T07:55:02Z",
    "eventSource": "sts.amazonaws.com",
    "eventName": "AssumeRoleWithWebIdentity",
    "awsRegion": "ap-northeast-1",
    "sourceIPAddress": "xxx.xxx.xxx.xxx",
    "userAgent": "aws-sdk-nodejs/2.1047.0 linux/v12.13.1 configure-aws-credentials-for-github-actions promise",
    "errorCode": "AccessDenied",
    "errorMessage": "An unknown error occurred",
    "requestParameters": {
        "durationSeconds": 3600,
        "roleArn": "arn:aws:iam::${aws_account_id}:role/GithubS3ReadOnlyAccess",
        "roleSessionName": "sample"
    },
 ・・・(略)

堂々の "An unknown error occurred"

GithubActionsの設定やSecretsの方を散々確認しましたが、実は答えはすぐ近くにありました。

間違い

        "StringEquals": {
          "token.actions.githubusercontent.com:sub": "repo:${github_org}/${github_repo}:*"
        }


正解

        "StringLike": {
          "token.actions.githubusercontent.com:sub": "repo:${github_org}/${github_repo}:*"
        }

* 入っていますからね、 StringEquals じゃなくて StringLike ですよね。

昨日に引き続き文字列違いのネタ。
クリスマスイブだけど(クリスマスイブなので?)軽めにしてます。

(こんなんで1時間ハマるとか・・・)

5
2
1

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
2