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:
# Fetch immutable numeric IDs via 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'
对于生产部署池,请将这些 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除外と有効成分あたりの実質単価比較データベース。