Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

5分で分かる Workload Identity Federation

先日、LT で「5分で分かる Workload Identity Federation」という話をしました。この記事はそのスライドを文章に書き起こしたものです。

きっかけ: lumos-web の CD パイプライン

私は今年の9月から、横浜国立大学プログラミングサークル Lumos のサークルサイト兼メンバーポータルサイトである lumos-web の開発に参加しています。lumos-web は Lumos System Team (Lusy) のメンバーで共同開発しており、Contributors は現在約20名になりました。

これだけの人数で開発していると、CI/CD(デプロイの自動化)無しの開発は考えられません。直近30日だけで、GitHub Actions のジョブが 530回、合計830分 実行されていました。main ブランチに push されると、ワークフローの YAML ファイルに従って Docker イメージがビルドされ、Google Cloud の Cloud Run に自動でデプロイされます。

開発者がコードを GitHub に push すると、Google Cloud へ自動でデプロイされる

lumos-web のデプロイの流れ

ここで、ふと疑問が湧きました。

GitHub Actions で発火するデプロイジョブは、どうやって Google Cloud のリソースへのアクセス権限を得ているのだろうか? そして、よりセキュアにこれを達成する方法はないだろうか?

調べてみると、その答えが Workload Identity Federation (WIF) でした。大げさに言えば、この分野の歴史は「WIF より前」と「WIF 以後」に分けられます。

WIF より前(〜2021年後期)

Google Cloud の場合、WIF が登場する前は次の手順で GitHub Actions にリソースへのアクセス権限(write 権限)を与えていました。

  1. Google Cloud 上でサービスアカウントを作る

  2. サービスアカウントに write 権限を付与する

  3. サービスアカウントとしてアクセスできる鍵(サービスアカウントキー)を作成する

  4. 鍵を GitHub に Secrets として登録し、ワークフローから使えるようにする

一見シンプルですが、大きな問題があります。この鍵には 有効期限がありません。つまり、鍵が一度でも漏洩したら、誰かがずっと Google Cloud にアクセスし続けられてしまいます。

鍵を安全に保管し、定期的にローテーションし、漏洩していないか気を配る……という運用の負担も、すべて開発者側にのしかかります。

WIF 以後(2021年後期〜)

WIF を使うと、長期間有効な鍵を GitHub に置く必要がなくなります。

GitHub Actions が Security Token Service で認証して一時的なアクセストークンを受け取り、それを使って Google Cloud のリソースを操作する

Workload Identity Federation の仕組み

登場人物は次の3つです。

流れは次のとおりです。

  1. GitHub Actions は、実行中のジョブを証明する ID トークンを GitHub から受け取る

  2. GitHub Actions はその ID トークンを STS に提示し、STS はそれが本当に GitHub の発行したトークンかを検証する。この ID トークンは OpenID Connect (OIDC) というプロトコルに準拠している

  3. 認証に成功すると、STS は 有効期限つきのアクセストークン(federated token)を発行し、GitHub Actions に渡す

  4. GitHub Actions はその一時的なアクセストークンを使って、Google Cloud のリソースを操作する

ポイントは、やり取りされるのが 短時間で失効するトークン だということです。仮に漏洩したとしても、使える時間はごく短く、鍵の保管やローテーションに悩まされることもありません。

実際、lumos-web のワークフローでは次のように書いています(値は伏せています)。

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: "read"
      id-token: "write" # GitHub から OIDC の ID トークンを受け取るために必要
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Login to Google Cloud
        id: auth
        uses: google-github-actions/auth@v2
        with:
          token_format: access_token
          workload_identity_provider: "projects/<PROJECT_NUMBER>/locations/global/workloadIdentityPools/<POOL>/providers/<PROVIDER>"
          service_account: "<SERVICE_ACCOUNT>@<PROJECT_ID>.iam.gserviceaccount.com"
      # 以降、Docker イメージのビルド・push、Cloud Run へのデプロイ

Secrets に鍵を登録する代わりに、permissions で id-token: "write" を指定し、workload_identity_provider を渡すだけです。

Google Cloud の公式ドキュメントでも、Google Cloud の外部で実行されるワークロードからリソースにアクセスする場合は、サービスアカウントキーではなく Workload Identity 連携を使うよう推奨されています(サービスアカウントキーの作成と削除)。

まとめ

Workload Identity Federation のおかげで、lumos-web ではセキュアに CD パイプラインを作れています。

ただし、万能ではありません。悪意のあるコードがリポジトリに加えられた場合、そのコードが GitHub Actions 経由で Google Cloud のリソースにアクセスする可能性は残ります。WIF は「鍵の漏洩」というリスクを取り除いてくれますが、「リポジトリそのものの安全性」は引き続き自分たちで守る必要があります。

LTで伝えられなかったことについて

LT では時間の都合で触れられなかったのが、「結局、認証はどうやって行われているのか」という話です。

GitHub Actions は認証を経て、一時的なアクセストークンを受け取ります。では、このトークンはどのような仕組みで発行されているのでしょうか。実は、ここには2つの段階があります。

  1. ID トークンの発行と検証: GitHub が、実行中のワークフローの情報(リポジトリ名やブランチなど)を含む ID トークン(JWT)を発行し、Google Cloud がそれを検証します。ここで使われているのが OpenID Connect (OIDC) というプロトコルです。OIDC は、もともと認可(authorization)のためのプロトコルである OAuth 2.0 の仕組みを活用して、認証(authentication)を実現しています。

  2. トークンの交換: 検証に通った ID トークンを、STS が Google Cloud のアクセストークンに交換します。この交換は OAuth 2.0 Token Exchange(RFC 8693)という仕様に沿って行われます。

アクセストークンを、GitHub Actions の特定のワークフロー(ワークロード)にだけ、どうやって正しく与えているのか。正直なところ、ここを説明しなければ Workload Identity Federation を説明したことにはなりません。

とはいえ、GitHub Actions を使うときに、私たちがこのあたりを意識することはほとんどありません。Google が提供している google-github-actions/auth という Action が、これらの処理を代わりに引き受けてくれているからです。

この google-github-actions/auth が実際に何をしているのかについては、別の記事で具体的に見ていこうと思います ![:w

English version

I recently gave a lightning talk called “Workload Identity Federation in 5 Minutes.” This post is a written version of those slides.

Background: the CD pipeline for lumos-web

Since September, I have been working on lumos-web, the website and member portal for Lumos, a programming club at Yokohama National University. It is built by members of the Lumos System Team (Lusy), and about 20 people have contributed so far.

With this many people working on it, developing without CI/CD (automated deployment) is unthinkable. In the last 30 days alone, GitHub Actions ran 530 jobs, for a total of 830 minutes. When someone pushes to the main branch, a workflow YAML file builds a Docker image and deploys it to Cloud Run on Google Cloud automatically.

A developer pushes code to GitHub, and it is deployed to Google Cloud automatically

How lumos-web is deployed

That made me wonder:

How does a deploy job triggered by GitHub Actions get permission to access resources on Google Cloud? And is there a more secure way to do it?

The answer turned out to be Workload Identity Federation (WIF). To put it dramatically, the history of this field can be divided into “before WIF” and “after WIF.”

Before WIF (until late 2021)

On Google Cloud, before WIF, you gave GitHub Actions access to your resources (write access) like this:

  1. Create a service account on Google Cloud

  2. Grant the service account write access

  3. Create a key that lets you act as the service account (a service account key)

  4. Store the key in GitHub as a secret so that workflows can use it

It looks simple, but there is a big problem: the key never expires. If it leaks even once, whoever has it can keep accessing your Google Cloud resources indefinitely.

On top of that, developers carry the whole burden of keeping the key safe, rotating it regularly, and watching for leaks.

After WIF (late 2021 onward)

With WIF, you no longer need to keep a long-lived key in GitHub.

GitHub Actions authenticates with the Security Token Service, receives a temporary access token, and uses it to work with Google Cloud resources

How Workload Identity Federation works

There are three players:

The flow goes like this:

  1. GitHub Actions receives an ID token from GitHub that proves which job is running

  2. GitHub Actions presents the ID token to STS, and STS checks that GitHub really issued it. The ID token follows a protocol called OpenID Connect (OIDC)

  3. If authentication succeeds, STS issues an access token with an expiry time (a federated token) and hands it to GitHub Actions

  4. GitHub Actions uses that temporary access token to work with Google Cloud resources

The key point is that only short-lived tokens change hands. Even if one leaks, it can only be used for a short time, and you no longer have to worry about storing or rotating keys.

Here is what the lumos-web workflow actually looks like (with the values hidden):

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: "read"
      id-token: "write" # Needed to receive an OIDC ID token from GitHub
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Login to Google Cloud
        id: auth
        uses: google-github-actions/auth@v2
        with:
          token_format: access_token
          workload_identity_provider: "projects/<PROJECT_NUMBER>/locations/global/workloadIdentityPools/<POOL>/providers/<PROVIDER>"
          service_account: "<SERVICE_ACCOUNT>@<PROJECT_ID>.iam.gserviceaccount.com"
      # Then build and push the Docker image, and deploy to Cloud Run

Instead of storing a key in Secrets, all you do is set id-token: "write" under permissions and pass a workload_identity_provider.

Google Cloud’s official documentation also recommends using Workload Identity Federation instead of service account keys when workloads running outside Google Cloud need to access resources (Create and delete service account keys).

Summary

Thanks to Workload Identity Federation, we can run a secure CD pipeline for lumos-web.

It is not a silver bullet, though. If someone adds malicious code to the repository, that code could still reach Google Cloud resources through GitHub Actions. WIF removes the risk of a leaked key, but we still have to protect the repository itself.

What I couldn’t cover in the talk

One thing I didn’t have time for in the talk is how authentication actually works.

GitHub Actions authenticates and then receives a temporary access token. So how is that token issued? It actually happens in two stages:

  1. Issuing and verifying the ID token: GitHub issues an ID token (a JWT) containing information about the running workflow, such as the repository name and branch, and Google Cloud verifies it. This is where OpenID Connect (OIDC) comes in. OIDC builds on OAuth 2.0, a protocol originally designed for authorization, to provide authentication.

  2. Exchanging the token: STS exchanges the verified ID token for a Google Cloud access token. This exchange follows a specification called OAuth 2.0 Token Exchange (RFC 8693).

How does it make sure the access token goes only to a specific GitHub Actions workflow (workload)? To be honest, without explaining that, I haven’t really explained Workload Identity Federation.

That said, when we use GitHub Actions, we rarely have to think about any of this. google-github-actions/auth, an Action provided by Google, takes care of all of it for us.

I’ll take a closer look at what google-github-actions/auth actually does in another post.