You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GCP CloudBuild使用KMS密钥解密时报404错误求助

GCP CloudBuild KMS密钥404错误排查

问题

使用GCP CloudBuild搭建Rails应用时,执行构建命令gcloud builds submit --config cloudbuild.yaml出现404错误,提示找不到指定的KMS密钥:

ERROR: build step 2 "gcr.io/cloud-builders/docker" failed: Failed to decrypt "DB_PWD" using key "projects/onlydrams/locations/us-central1/keyRings/onlydrams/cryptoKeys/db_pwd_key/cryptoKeyVersions/1": googleapi: got HTTP response code 404 with body: <!DOCTYPE html>

已为Cloud Build服务账号分配以下IAM角色:

Cloud Build Service Account
Cloud KMS Admin
Cloud KMS CryptoKey Decrypter

cloudbuild.yaml配置如下:

steps:
# Build image with tag 'latest' and pass decrypted Rails DB password as argument
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '--tag', 'gcr.io/onlydrams/onlydrams:latest', 
         '--build-arg', 'DB_PWD', '.']
  secretEnv: ['DB_PWD']

# Push new image to Google Cloud Registry       
- name: 'gcr.io/cloud-builders/docker'
  args: ['push', 'gcr.io/onlydrams/onlydrams:latest']

availableSecrets:
  inline:
  - kmsKeyName: projects/onlydrams/locations/us-central1/keyRings/onlydrams/cryptoKeys/db_pwd_key/cryptoKeyVersions/1
    envMap: 
      DB_PWD: "CiQAYGWAVuMg5wxnkgWjKH07iWxR+GBD/wYE1YAcgYDa5nAPADwSOQDtVRn4Aj5LAMl5V0YiEnwJ48cd3RqG3lk4MN4IzhUyPIvKIZUtj5uKOVA86VbnzOaPxKNDPFUGIw=="

疑惑点:权限缺失通常返回403/401错误,而非404,且密钥路径是直接从Cloud Console复制的,为何仍出现找不到密钥的情况?

可能的原因及解决办法

  • 密钥版本号无效或已删除:指定了具体密钥版本cryptoKeyVersions/1,如果该版本已被销毁或从未创建,就会返回404。直接去掉版本号,使用密钥的基础路径:projects/onlydrams/locations/us-central1/keyRings/onlydrams/cryptoKeys/db_pwd_key,Cloud KMS会自动使用最新的有效版本。

  • 资源路径存在拼写错误:即使是复制的路径,也可能存在隐性错误(比如大小写、多余空格)。逐一核对路径各部分:

    • 项目ID onlydrams是否正确
    • 区域us-central1是否和密钥实际所在区域一致
    • 密钥环onlydrams、密钥db_pwd_key的名称是否完全匹配
  • 跨项目引用路径错误:如果Cloud Build服务账号和密钥不在同一个GCP项目,哪怕权限配置正确,也可能因跨项目路径问题触发404。确认两者是否同属一个项目;若跨项目,要确保路径完全正确,且目标项目已授权服务账号访问该密钥。

  • 密钥同步延迟:刚创建的KMS密钥可能需要几分钟同步到GCP全局服务,要是你刚创建密钥,等待几分钟后再重新执行构建。

内容的提问来源于stack exchange,提问作者Anon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 00:25:06