首页 > 解决方案 > 资源...密钥的权限“cloudkms.cryptoKeyVersions.useToDecrypt”被拒绝

问题描述

我正在使用 Google Cloud Functions 构建一个 http 端点。我有一个加密的秘密存储为一个文件,该文件在函数中加载和解密,以防止我的秘密存储在代码中。通常我从谷歌云存储中动态加载一些东西,但似乎 KMS 更适合这个目的。

使用 KMS 的代码如下所示:

getCredentials: async function () {
    const kms = require('@google-cloud/kms');
    const client = new kms.KeyManagementServiceClient();
    const fs = require('fs');
    let ciphertext = (fs.readFileSync('secret.enc')).toString('base64')
    const name = client.cryptoKeyPath(
      '[project]',
      'global',
      '[keyring]',
      '[key]'
    );

一切都在本地运行良好,但当使用 http 触发器调用时,我似乎无法让该功能正常工作。检查日志我看到了这个:

textPayload:  "Error: Permission 'cloudkms.cryptoKeyVersions.useToDecrypt' denied for resource 'projects/[projectname]/locations/global/keyRings/[keyring]/cryptoKeys/[key]'.
    at Http2CallStream.call.on (/srv/functions/node_modules/@grpc/grpc-js/build/src/client.js:96:45)
    at Http2CallStream.emit (events.js:194:15)
    at Http2CallStream.EventEmitter.emit (domain.js:459:23)
    at process.nextTick (/srv/functions/node_modules/@grpc/grpc-js/build/src/call-stream.js:71:22)
    at process._tickCallback (internal/process/next_tick.js:61:11)" 

我已经尝试了各种可用的 IAM 权限(包括所有者),所以看来我必须有更深的误解。

这可能与我无法让 Google Cloud Build 部署该功能的另一个问题有关。它在没有帮助的情况下出错:

starting build "b2321cdb-bd4c-4828-8d38-80a86f4fe808"

FETCHSOURCE
Initialized empty Git repository in /workspace/.git/
From https://source.developers.google.com/p/[projectname]/r/[repo]
* branch 314691d6e63199caf867c74bcd0090bc70386a0e -> FETCH_HEAD
HEAD is now at 314691d Merge pull request #2 from [repo]/tristans/update-deploy-cloudbuild
BUILD
Already have image (with digest): gcr.io/cloud-builders/gcloud
Deploying function (may take a while - up to 2 minutes)...
...............failed.
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: Build error details not available
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/gcloud" failed: exit status 1

您似乎不需要任何 KMS 权限来部署functions deploy name --trigger-http --runtime=nodejs10 --entry-point=fname --project=project,就像我说的那样,当我在gcloud deploy本地运行时它工作正常,所以我不确定为什么会失败。我们有几个云功能设置了类似的部署过程,所以似乎有一些关于 KMS 工作方式的不明显或破坏的东西对我有用,但也许这是一个红鲱鱼。

如果有更好的方法来为此目的使用 KMS,我会全力以赴!

标签: google-cloud-functionsgoogle-cloud-buildgoogle-cloud-iamgoogle-cloud-kms

解决方案


根据Google Functions 文档,函数的运行时服务帐户是PROJECT_ID@appspot.gserviceaccount.com
您需要向此帐户授予 KMS 解密权限。
这可以在谷歌云网络控制台的“IAM & Admin / IAM”部分完成

在此处输入图像描述


推荐阅读