kakakakakku blog

Weekly Tech Blog: Keep on Learning!

GitHub Actions と AWS を OIDC で連携するときに自動的に証明書の検証をしてくれるようになった

今まで GitHub Actions から AWS を OIDC (OpenID Connect) で連携する場合にサムプリントを取得して ID プロバイダを作る必要があった💡しかし,2023年6月27日に GitHub Changelog でサムプリントを2種類設定するという記事が公開されて対応することになったけど,2023年7月6日から AWS 側で自動的に証明書の検証をしてくれるようになって,特に気にする必要がなくなった.結果的に適当なサムプリントを指定しておけば良く楽になった👀

動作確認をする機会があったので簡単にまとめておこうと思う.

github.blog

ちなみに「2023年7月6日」という日付は AWS から送られてきたメールに載っていた📩

[NOTIFICATION] OpenIDConnect (OIDC) errors when using GitHub OIDC IdP to access AWS resources [AWS Account: 000000000000]

AWS CloudFormation テンプレート

AWS CloudFormation の AWS::IAM::OIDCProvider でリソースを作る場合 ThumbprintList は引き続き必須項目なので,例えば aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa のように適当な文字列「40文字(0-9 or A-F)」を指定すれば良くなった👌

docs.aws.amazon.com

以下にサンプルとして oidc-provider-github-actions.yaml を載せておく❗️

AWSTemplateFormatVersion: 2010-09-09

Resources:
  GitHubActionsOidcProvider:
    Type: AWS::IAM::OIDCProvider
    Properties:
      Url: https://token.actions.githubusercontent.com
      ClientIdList:
        - sts.amazonaws.com
      ThumbprintList:
        - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  RoleGitHubActions:
    Type: AWS::IAM::Role
    Properties:
      RoleName: github-actions-role
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Action: sts:AssumeRoleWithWebIdentity
            Principal:
              Federated: !Sub arn:aws:iam::${AWS::AccountId}:oidc-provider/token.actions.githubusercontent.com
            Condition:
              StringEquals:
                token.actions.githubusercontent.com:aud: sts.amazonaws.com
              StringLike:
                token.actions.githubusercontent.com:sub: repo:kakakakakku/xxxxx:*
      Policies:
        - PolicyName: StsGetCallerIdentity
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - sts:GetCallerIdentity
                Resource: '*'

Outputs:
  Role:
    Value: !GetAtt RoleGitHubActions.Arn

GitHub の aws-actions/configure-aws-credentials リポジトリに載っているサンプルを確認したところ,ThumbprintListffffffffffffffffffffffffffffffffffffffff を指定していた📝

Resources:
  GithubOidc:
    Type: AWS::IAM::OIDCProvider
    Condition: CreateOIDCProvider
    Properties:
      Url: https://token.actions.githubusercontent.com
      ClientIdList: 
        - sts.amazonaws.com
      ThumbprintList:
        - ffffffffffffffffffffffffffffffffffffffff

github.com

動作確認

GitHub Actions ワークフローで aws sts get-caller-identity コマンドを実行して,期待通りの動きだった👏

name: get-caller-identity

on: workflow_dispatch

permissions:
  id-token: write
  contents: read

jobs:
  get-caller-identity:
    runs-on: ubuntu-latest
    steps:
      - uses: aws-actions/configure-aws-credentials@v2
        with:
          role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
          aws-region: ap-northeast-1
      - run: aws sts get-caller-identity

aws sts get-caller-identity コマンドを実行できた!

ドキュメント

以下のドキュメントと API リファレンスを読んだところ,GitHub ではサムプリントを検証のために使わないという仕様に更新されていた📝 また GitHub 以外に Auth0 や Google でも同じ挙動になることも載っていた.

AWS secures communication with some OIDC identity providers (IdPs) through our library of trusted root certificate authorities (CAs) instead of using a certificate thumbprint to verify your IdP server certificate. These OIDC IdPs include Auth0, GitHub, Google, and those that use an Amazon S3 bucket to host a JSON Web Key Set (JWKS) endpoint. In these cases, your legacy thumbprint remains in your configuration, but is no longer used for validation.

docs.aws.amazon.com

docs.aws.amazon.com

マネジメントコンソールで ID プロバイダを作るときにもメッセージが表示されるようになっている💡

GitHub: メッセージが表示されるようになっていた!

Google: メッセージが表示されるようになっていた!

その他

個人的には Terraform Cloud でも ID プロバイダを使っているから同じ仕組みになると良いなぁ〜と思ったりした🙏

kakakakakku.hatenablog.com