Kanji
・雲端架構工程師 / 自由職業 ・1993年出生 ・愛媛縣出身 / 現居東京都澀谷區 ・5年 AWS 實戰經驗 個人檔案詳情
目錄
google-github-actions/auth@v3
attribute.condition
使用工作負載身份聯合 (WIF) 替換 GitHub Secrets 中的長期服務帳戶 JSON 金鑰已成為部署到 Cloud Run 和 Artifact Registry 的標準做法。然而,當您開啟“google-github-actions/auth@v3”自述檔案時,您就遇到了架構分叉:您應該使用 直接工作負載身份聯合 (Google 現在將其標記為 首選 )還是傳統的 服務帳戶模擬 ?
當我使用“gcloud”(“594.0.1”)檢查我的個人 Google Cloud 專案中現有的工作負載身份池時,我注意到我的舊設定仍然依賴於基於字串的“attribute.repository”條件。仔細檢視“auth@v3”文件和實時 Google Cloud Discovery 架構還可以發現硬邊界(例如 10 分鐘令牌限制和簽名 URL 不相容),如果您盲目選擇 Direct WIF,這些邊界可能會破壞 CI/CD 管道。
在本指南中,我將並排比較 Direct WIF、服務帳戶模擬和服務帳戶 JSON 金鑰,檢查“attribute.condition”和“auth@v3”中的安全陷阱,並演練 ArtifactRegistry(Direct WIF)和 Cloud Run(服務帳戶模擬)配置。請注意,我尚未執行實時破壞性 CI 測試來觸發生產中的 10 分鐘單令牌過期邊界(未在生產中進行驗證);下面的分析結合了實時本地“gcloud 594.0.1”檢查、Google Cloud Discovery API 模式(“sts.googleapis.com”、“iam.googleapis.com”、“iamcredentials.googleapis.com”)、活動池的只讀檢查和官方文件。
principalSet://
principal://
service_account
iam.googleapis.com
sts.googleapis.com
iamcredentials.googleapis.com
signBlob
在兩種聯合模式中,GitHub Actions 執行程式首先從 GitHub 請求籤名的 OIDC JWT(“id-token:write”),然後在 Google Cloud 安全令牌服務(“sts.googleapis.com”)上將其交換為聯合令牌。分歧在交換後立即發生。
藉助 Direct WIF,您可以將 IAM 角色直接分配給 Google Cloud 專案或資源上工作負載身份池的“principalSet://”(或每個主題的“principal://”)URI。對於 Google Cloud 的 Identity Federation:產品和限制 矩陣中列出的服務(例如將容器映像推送到 Artifact Registry 或將構建工件上傳到 Cloud Storage),刪除中間服務帳號可以簡化 IAM 稽核。
但是,如果您的管道達到以下五個邊界中的任何一個,則必須使用服務帳戶模擬:
roles/iam.serviceAccountUser
iam.serviceAccounts.signBlob
token_format: "access_token"
token_format: "id_token"
為了確認當前的 CLI 標誌和 API 合同,我在本地檢查了“gcloud”(“Google Cloud SDK 594.0.1”)以及 STS(“修訂版:20261106”)和 IAM(“修訂版:20261126”)的實時 Discovery 文件。
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 v1 中, GoogleIdentityStsV1ExchangeTokenRequest 定義了 audience 、 grantType 、 requestedTokenType 、 scope 、 subjectToken 和 subjectTokenType ,而 iam.googleapis.com v1 中的 WorkloadIdentityPoolProvider 公開了 attributeMapping 和 attributeCondition 以及 oidc 、 aws 、“saml”和“x509”。
GoogleIdentityStsV1ExchangeTokenRequest
audience
grantType
requestedTokenType
scope
subjectToken
subjectTokenType
WorkloadIdentityPoolProvider
attributeMapping
attributeCondition
oidc
aws
回讀我之前在個人 GCP 專案中設定的提供程式顯示了以下配置(專案編號和儲存庫識別符號用佔位符遮蔽):
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
乍一看,固定“attribute.repository”看起來被鎖定,但依賴可變儲存庫名稱會引入微妙的安全漏洞,以及“auth@v3”中的其他三個常見陷阱。
auth@v3
repository
repository_owner
所有 GitHub Actions 工作流程共享單個全域性 OIDC 發行者“https://token.actions.githubusercontent.com”。如果沒有“attributeCondition”,世界上任何公共 GitHub 儲存庫都可以進入您的工作負載身份池。
即使新增條件,如果組織或儲存庫被重新命名或刪除並隨後被外部參與者宣告(域名搶注/誤植),僅匹配諸如“assertion.repository ==“my-org/my-repo””或“assertion.repository_owner ==“my-org””之類的字串也會留下一個視窗開啟。請改用 GitHub 的不可變數字 ID:
# GitHub Actions Google Cloud 上的工作負載身份聯合:直接 WIF 與服務帳戶模擬和安全陷阱 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'
對於生產部署池,請將這些 ID 與 assertion.ref == 'refs/heads/main' 組合起來,以便功能分支無法建立部署令牌。請注意,單獨檢查 assertion.environment == 'product' 不會限制源分支,除非您還將其與 CEL 中的 assertion.ref 配對或配置 GitHub 環境部署分支保護規則。另請記住,GitHub 儲存庫名稱不區分大小寫,而 Google Cloud CEL 和 IAM 字串比較嚴格區分大小寫。
assertion.ref == 'refs/heads/main'
assertion.environment == 'product'
assertion.ref
雖然“--attribute-condition”可以直接從傳入的 JWT 評估“assertion.*”宣告,但您在 IAM 繫結 (“principalSet://…/attribute. / ”) 中引用的任何屬性 必須 在“--attribute-mapping”中顯式對映。缺少該對映會導致“無法生成 OAuth 2.0 訪問令牌”。
google-github-actions/auth@v3 將其執行時升級到 Node 24 。GitHub 託管的執行器會自動處理此問題,但較舊的自託管執行器必須先升級,然後才能從“v2”升級到“v3”。
兩個工作流程訂購細節也會阻礙團隊的工作: - 由於生成的憑證檔案 ( gha-creds-*.json ) 預設位於 $GITHUB_WORKSPACE 內 ( create_credentials_file: true ),因此 actions/checkout 必須在 google-github-actions/auth@v3 之前執行,並且 gha-creds-*.json 應列在 .gitignore 和 .dockerignore 中。 - 如“auth@v3”中所述,舊版“gsutil”命令不會讀取此操作匯出的憑據;將指令碼遷移到“gcloud storage”。
gha-creds-*.json
$GITHUB_WORKSPACE
create_credentials_file: true
actions/checkout
.gitignore
.dockerignore
google.subject
Google Cloud IAM 對對映的 google.subject 主體識別符號強制執行嚴格的 127 位元組限制 。當在具有長組織、儲存庫或分支名稱 ( repo:my-org/long-repo-name:ref:refs/heads/feature/long-branch ) 的儲存庫上使用“google.subject=assertion.sub”時,身份驗證會立即失敗,並顯示:
repo:my-org/long-repo-name:ref:refs/heads/feature/long-branch
The size of mapped attribute exceeds the 127 bytes limit.
保持儲存庫和分支名稱簡潔可以避免這種情況,或者當長分支名稱不可避免時,您可以圍繞固定長度數字 ID ( assertion.repository_id + ":" + assertion.run_id ) 構建一個緊湊的 google.subject 對映。
assertion.repository_id + ":" + assertion.run_id
在生產 CI/CD 管道中,一種乾淨的模式是使用 Direct WIF 進行 Artifact Registry 映像推送,並使用服務帳戶模擬進行 Cloud Run 部署。以下是使用“gcloud”和“google-github-actions/auth@v3”的端到端設定。
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. Create the pool gcloud iam workload-identity-pools create "${POOL_ID}" \ --project="${PROJECT_ID}" \ --location="global" \ --display-name="GitHub Actions Pool" # 2. Create the OIDC provider guarded by immutable IDs and main branch 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'"
對於 ArtifactRegistry,將“roles/artifactregistry.writer”直接繫結到“principalSet://”,無需中間服務帳戶。對於 Cloud Run 部署,請在部署服務帳戶 (“DEPLOY_SA”) 上授予“roles/iam.workloadIdentityUser”,並將“roles/run.developer”繫結到該服務帳戶。
MEMBER="principalSet://iam.googleapis.com/projects/${PROJECT_NUMBER}/locations/global/workloadIdentityPools/${POOL_ID}/attribute.repository_id/${REPO_ID}" # Pattern A: Direct WIF for Artifact Registry gcloud projects add-iam-policy-binding "${PROJECT_ID}" \ --member="${MEMBER}" \ --role="roles/artifactregistry.writer" # Pattern B: Service Account Impersonation for Cloud Run deployment 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 }} # Omit service_account for Direct WIF # Format: 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,幷包含完整的“/providers/ ”字尾。另請注意,“gcloud auth list”僅列印本地配置的帳戶,不測試遠端授權 - 根據實際目標 API(例如“gcloud artifacts repositories list”)驗證訪問許可權。
當“google-github-actions/auth@v3”失敗時,錯誤的措辭會指出哪個層拒絕了請求:
permissions.id-token: write
workload_identity_provider
/providers/...
--attribute-condition
無法生成 OAuth 2.0 訪問令牌
對映屬性的大小超過 127 位元組限制
設定到 Google Cloud 的新 GitHub Actions 部署時,首先對 Artifact Registry 或 Cloud Storage 等受支援的服務使用 Direct WIF ,並在部署到 Cloud Run、簽署 Cloud Storage URL 或建立 ID 令牌時將其與 服務帳戶模擬 配對。無論哪種方式,將“attribute.condition”鎖定為不可變的數字“repository_owner_id”和“repository_id”值在第一天就值得做。
對於我自己的仍使用可變“attribute.repository”字串的舊池,我仍然需要決定將它們與 Terraform 狀態管理一起遷移到不可變 ID 的最乾淨時機。
Real-time AI quant analysis of corporate earnings reports.
提供逼真 AI 模擬通話與多維反饋,結合 16 講實戰社交學院與工作室級 AI 肖像生成,全面提升社交互動品質。
拍發票即可完成高精度智慧拆單,支援 LINE 登入無縫結算團隊聚會支出,告別繁雜帳單分攤。
占卜師、管家、調酒師等 AI 角色即時引導話題與聚會遊戲,讓線下與線上聚餐氛圍瞬間升溫。
人工甘味料や添加物のNG除外と有効成分あたりの実質単価比較データベース。