Kanji
・クラウドエンジニア / フリーランス ・1993年生まれ ・愛媛県出身 / 東京都渋谷区在住 ・AWS歴5年 プロフィールの詳細
目次
google-github-actions/auth@v3
attribute.condition
GitHub Actions から Google Cloud(Cloud Run や Artifact Registry)へデプロイするとき、長寿命のサービスアカウントキー JSON を GitHub Secrets に置かないために Workload Identity Federation(WIF)を採用するのは定番になった。
ただ、いざ google-github-actions/auth@v3 の README を開いて設定しようとすると、最初に手が止まる分岐がある。従来の「サービスアカウント借用(Service Account Impersonation)」にするのか、Google が推奨(Preferred)に掲げている「Direct Workload Identity Federation(Direct WIF)」にするのか、という選択だ。僕自身、手元の検証用 GCP プロジェクトで稼働させている既存の Workload Identity Pool を gcloud ( 594.0.1 )で読み出して見直してみたところ、過去に設定したままの attribute.repository 文字列条件や、Direct WIF では動かない SDK 境界など、設計時に見落としがちなポイントがいくつも見つかった。
gcloud
594.0.1
attribute.repository
本記事では、Direct WIF・サービスアカウント借用・サービスアカウントキー JSON の 3 方式を仕様と制約の両面から比較し、 google-github-actions/auth@v3 での設定例(Artifact Registry 向けの Direct WIF と Cloud Run 向けのサービスアカウント借用)、 attribute.condition の乗っ取り対策、よくあるエラーの切り分け手順をまとめる。なお、Direct WIF の単一トークン 10 分上限到達時の失効挙動や新規プール作成のライブ CI 疎通は僕自身まだ本番パイプラインでは検証しておらず(本番未検証)、手元の gcloud 594.0.1 ・Discovery API(STS v1 / IAM v1 / IAMCredentials v1)・既存プール参照結果および公式仕様に基づく整理として記載する。
gcloud 594.0.1
principalSet://
roles/iam.workloadIdentityUser
service_account
credentials_json
iam.googleapis.com
sts.googleapis.com
iamcredentials.googleapis.com
auth
signBlob
roles/iam.serviceAccountTokenCreator
どちらの方式でも、GitHub Actions のランナーが最初に OIDC トークン(JWT)を取得し、Google Cloud Security Token Service( sts.googleapis.com )へ投げてフェデレーション済みトークンを受け取る流れまでは同じだ。違いはその先にある。
Direct WIF は、Workload Identity Pool の principalSet:// (または特定ジョブ単位の principal:// )を Google Cloud の各リソースやプロジェクトの IAM メンバーとして直接登録する。Artifact Registry へのイメージプッシュや Cloud Storage へのビルド成果物アップロードなど、Google Cloud の公式対応表( Identity Federation: products and limitations )に含まれるサービスであれば、中間サービスアカウントを省いて権限設計を軽くできる。
principal://
ただし、以下の 5 つのうち 1 つでも当てはまるなら Direct WIF ではなくサービスアカウント借用を選ぶ必要がある。
roles/iam.serviceAccountUser
iam.serviceAccounts.signBlob
token_format: "id_token"
token_format: "access_token"
記事執筆にあたり、ローカル環境の gcloud CLI( 594.0.1 )と Google Cloud の live Discovery API を叩いて、現行のパラメータ仕様と既存プールの設定状態を確認した。
gcloud --version | head -n 5 gcloud iam workload-identity-pools providers create-oidc --help | grep -E "attribute|issuer|allowed|discovery"
Google Cloud SDK 594.0.1 alpha 2026.11.21 beta 2026.11.21 bq 2.1.42 bundled-python3-unix 3.14.9 --attribute-mapping=[KEY=VALUE,...] --issuer-uri=ISSUER_URI [--allowed-audiences=[ALLOWED_AUDIENCES,...]] [--attribute-condition=ATTRIBUTE_CONDITION] [--description=DESCRIPTION] [--disabled] [--discovery-uri=DISCOVERY_URI]
あわせて、 sts.googleapis.com ( revision: 20261106 )と iam.googleapis.com ( revision: 20261126 )の Discovery スキーマも取得した。STS 側の GoogleIdentityStsV1ExchangeTokenRequest には audience ・ grantType ・ requestedTokenType ・ scope ・ subjectToken ・ subjectTokenType 等が定義されており、IAM 側の WorkloadIdentityPoolProvider は oidc ・ aws ・ saml ・ x509 の各プロバイダに加えて attributeMapping と attributeCondition をトップレベル属性として保持している。
revision: 20261106
revision: 20261126
GoogleIdentityStsV1ExchangeTokenRequest
audience
grantType
requestedTokenType
scope
subjectToken
subjectTokenType
WorkloadIdentityPoolProvider
oidc
aws
saml
x509
attributeMapping
attributeCondition
手元の検証用プロジェクトで以前作成した GitHub Actions 用プロバイダを読み出してみると、次のような状態になっていた(固有のプロジェクト番号とアカウント名はプレースホルダに置換している)。
attributeCondition: attribute.repository == "my-org/my-repo" attributeMapping: attribute.actor: assertion.actor attribute.repository: assertion.repository google.subject: assertion.sub oidc: issuerUri: https://token.actions.githubusercontent.com state: ACTIVE
一見すると特定リポジトリに絞れていて安全そうに見えるが、実はここにも設計上の落とし穴が潜んでいる。次のセクションで、よく踏む 4 つの罠を整理する。
repository
repository_owner
GitHub の OIDC トークン発行元( https://token.actions.githubusercontent.com )は世界中の全リポジトリで共通だ。 --attribute-condition を未設定で作成すると、理論上は他人のパブリックリポジトリのワークフローでも Pool への入場(認証フェーズ)自体は通過できてしまうため、必ず条件式で制限する必要がある。
https://token.actions.githubusercontent.com
--attribute-condition
さらに厄介なのが、先ほどの僕自身の古い設定のように assertion.repository == "my-org/my-repo" や assertion.repository_owner == "my-org" といった「名前」だけで絞るパターンだ。組織名やリポジトリ名を変更したり削除したりすると、第三者が同じ名前を再取得(Cybersquatting / Typosquatting)して条件を突破する余地が残る。GitHub が発行する不変の数値 ID を使うのが現在のベストプラクティスだ。
assertion.repository == "my-org/my-repo"
assertion.repository_owner == "my-org"
# 変更されない不変の数値 ID を GitHub REST API で取得する curl -sfL -H "Accept: application/json" "https://api.github.com/orgs/my-org" | jq .id curl -sfL -H "Accept: application/json" "https://api.github.com/repos/my-org/my-repo" | jq .id
assertion.repository_owner_id == '12345678' && assertion.repository_id == '987654321'
本番デプロイ用の Pool では、ここにブランチ条件( assertion.ref == 'refs/heads/main' )を組み合わせると PR ブランチからのトークン取得を防げる。なお、 assertion.environment == 'production' だけではブランチを限定できないため、GitHub Environment 側の Deployment branches 保護ルールを設定するか、CEL 式側で ref 条件を明示的に併用することが必須だ。また、GitHub のリポジトリ名は大小文字を区別しないが、Google Cloud の CEL と IAM 文字列比較は 大文字・小文字を厳密に区別する 点もハマりやすい。
assertion.ref == 'refs/heads/main'
assertion.environment == 'production'
ref
attribute-mapping
--attribute-condition 側では assertion.repository_id のように JWT クレームの assertion.* を直接評価できるが、IAM バインディングの principalSet://.../attribute.<KEY>/<VALUE> で参照したい属性は、必ず --attribute-mapping で attribute.<KEY>=assertion.<CLAIM> としてマッピングしておく必要がある。マッピング漏れがあると、 Failed to generate OAuth 2.0 Access Token で弾かれる。
assertion.repository_id
assertion.*
principalSet://.../attribute.<KEY>/<VALUE>
--attribute-mapping
attribute.<KEY>=assertion.<CLAIM>
Failed to generate OAuth 2.0 Access Token
gsutil
google-github-actions/auth@v3 (v3.0.0 以降)では、ランタイムが Node 24 へ引き上げられた。GitHub-hosted ランナー( ubuntu-latest 等)はそのまま動くが、古い Self-hosted ランナーを運用している場合はランナー自体のアップデートが必須になる。
ubuntu-latest
また、Workflow YAML の記述順にも定番の罠がある。デフォルト( create_credentials_file: true )で生成される一時クレデンシャルファイルは $GITHUB_WORKSPACE 配下に書き出されるため、 actions/checkout を google-github-actions/auth@v3 より前に置かないと後続ステップが認証を引き継げない 。コンテナイメージをビルド・公開するパイプラインでは、 .gitignore と .dockerignore に gha-creds-*.json を必ず追記しておくべきだ。さらに v3 のドキュメントでも強調されているとおり、レガシーな gsutil コマンドはこのアクションが書き出す認証情報を自動認識しないため、 gcloud storage コマンドへ統一する必要がある。
create_credentials_file: true
$GITHUB_WORKSPACE
actions/checkout
.gitignore
.dockerignore
gha-creds-*.json
v3
gcloud storage
google.subject
Google Cloud IAM では、Workload Identity Federation の属性マッピング先である google.subject (主体識別子)に 127 バイト上限 がある。 google.subject=assertion.sub としている場合、GitHub 側の sub クレーム( repo:my-org/very-long-repository-name:ref:refs/heads/feature/very-long-branch-name のような文字列)が 127 文字を超えると、次のエラーで認証が即死する。
google.subject=assertion.sub
sub
repo:my-org/very-long-repository-name:ref:refs/heads/feature/very-long-branch-name
The size of mapped attribute exceeds the 127 bytes limit.
長い組織名やブランチ名を運用するリポジトリでは、リポジトリ名・ブランチ名を短く保つ以外にも、 google.subject のマッピングを assertion.repository_id + ":" + assertion.run_id のような固定長の短い構成へ寄せる設計を検討しておくと安全だ。
assertion.repository_id + ":" + assertion.run_id
実際の CI/CD パイプラインでは、Direct WIF で完結できる Artifact Registry 操作と、サービスアカウント借用が必要な Cloud Run デプロイを組み合わせて設計するのが現実的だ。ここでは gcloud でのプール作成から、 principalSet:// の直接付与、Cloud Run 向けの SA 借用設定までを示す。
PROJECT_ID="my-gcp-project" PROJECT_NUMBER="123456789012" POOL_ID="github-actions-pool" PROVIDER_ID="github-actions-provider" REPO_OWNER_ID="12345678" REPO_ID="987654321" DEPLOY_SA="github-deploy@${PROJECT_ID}.iam.gserviceaccount.com" # 1. Pool の作成 gcloud iam workload-identity-pools create "${POOL_ID}" \ --project="${PROJECT_ID}" \ --location="global" \ --display-name="GitHub Actions Pool" # 2. 不変 ID による attribute-condition 付きの OIDC Provider 作成 gcloud iam workload-identity-pools providers create-oidc "${PROVIDER_ID}" \ --project="${PROJECT_ID}" \ --location="global" \ --workload-identity-pool="${POOL_ID}" \ --display-name="GitHub Actions Provider" \ --issuer-uri="https://token.actions.githubusercontent.com" \ --attribute-mapping="google.subject=assertion.sub,attribute.actor=assertion.actor,attribute.repository=assertion.repository,attribute.repository_id=assertion.repository_id,attribute.ref=assertion.ref" \ --attribute-condition="assertion.repository_owner_id == '${REPO_OWNER_ID}' && assertion.repository_id == '${REPO_ID}' && assertion.ref == 'refs/heads/main'"
Artifact Registry のイメージプッシュだけであれば、中間サービスアカウントを作らず principalSet:// へ直接権限を付与できる。一方、Cloud Run へデプロイする場合は公式対応表に従いデプロイ用サービスアカウント( DEPLOY_SA )へ roles/iam.workloadIdentityUser を付与して借用する。
DEPLOY_SA
MEMBER="principalSet://iam.googleapis.com/projects/${PROJECT_NUMBER}/locations/global/workloadIdentityPools/${POOL_ID}/attribute.repository_id/${REPO_ID}" # パターン A: Artifact Registry のみを Direct WIF で利用する場合 gcloud projects add-iam-policy-binding "${PROJECT_ID}" \ --member="${MEMBER}" \ --role="roles/artifactregistry.writer" # パターン B: Cloud Run デプロイ等も行う場合は SA 借用を設定 gcloud iam service-accounts add-iam-policy-binding "${DEPLOY_SA}" \ --project="${PROJECT_ID}" \ --role="roles/iam.workloadIdentityUser" \ --member="${MEMBER}" gcloud projects add-iam-policy-binding "${PROJECT_ID}" \ --member="serviceAccount:${DEPLOY_SA}" \ --role="roles/run.developer"
name: Build and Deploy (WIF) on: push: branches: - main permissions: contents: read id-token: write jobs: push-image-direct-wif: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v7 - name: Authenticate via Direct WIF (Artifact Registry) uses: google-github-actions/auth@v3 with: project_id: ${{ vars.GCP_PROJECT_ID }} workload_identity_provider: ${{ vars.GCP_WIF_PROVIDER }} # Direct WIF では service_account を指定しない # 形式: projects/123456789012/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider - name: Verify Artifact Registry access with federated token run: gcloud artifacts repositories list --project="${{ vars.GCP_PROJECT_ID }}" --location=asia-northeast1 deploy-cloud-run-impersonation: needs: push-image-direct-wif runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v7 - name: Authenticate via Service Account Impersonation (Cloud Run) uses: google-github-actions/auth@v3 with: workload_identity_provider: ${{ vars.GCP_WIF_PROVIDER }} service_account: ${{ vars.GCP_DEPLOY_SA }}
なお、 workload_identity_provider にはプロジェクト ID ではなく プロジェクト番号(数字) を渡す必要がある。また、 gcloud auth list はローカルの認証設定を列挙するだけで対象 API への疎通検証にはならないため、上記のように実際に利用する gcloud artifacts repositories list 等のコマンドで到達性を確認するのが確実だ。
workload_identity_provider
gcloud auth list
gcloud artifacts repositories list
google-github-actions/auth@v3 で失敗したときは、エラーメッセージに含まれる段階名を見れば原因をほぼ一発で絞れる。
Failed to generate Google Cloud federated token
permissions.id-token: write
/providers/...
The size of mapped attribute exceeds the 127 bytes limit
assertion.sub
repository_id
GitHub Actions と Google Cloud を繋ぐ構成を今から新しく組むなら、Artifact Registry や Cloud Storage 等の対応サービスだけを扱うジョブでは中間サービスアカウントを持たない Direct WIF を選び、Cloud Run デプロイ・Signed URL 生成・ID トークン出力などが必要なジョブでは サービスアカウント借用 を選ぶのが整理しやすい。どちらを選んでも、共有 OIDC イシューアに対する attribute.condition の不変 ID( repository_owner_id / repository_id )化だけは最初に必ず入れておきたいところだ。
repository_owner_id
僕自身も、既存プロジェクトに残っている文字列条件の Provider をいつ不変 ID ベース+ブランチ制限へ寄せ直すか、Terraform 管理への切り出しタイミングとあわせて見極めたいと思っている。
日本株・グローバル企業の適時開示・決算速報をAIクオンツ解析。市場モメンタムや注目銘柄をリアルタイムに可視化するデータポータルです。
本番さながらのAI電話で初通話もデート誘いも余裕に。通話中の対話をリアルタイム解析し、最適な切り返しや好感度アップの助言を即座にサジェスト。24時間いつでも通話練習が可能です(無料体験あり・1レッスン 約150円〜)。
レシートを撮影するだけでGemini AIが品目・金額を高精度OCR解析。LINEログインで友人との面倒な立替・割り勘計算を美しく完結します。
占い師・執事・バーテンダーなどのAIキャラクターが進行役となり、お題やゲームを出題。集まる時間を最高にエキサイティングにするiOSアプリ。
人工甘味料や添加物のNG成分除外フィルターと、有効成分あたりの実質単価を徹底比較・可視化するヘルスケア比較データベースです。