首页 > 解决方案 > 无法从 Google Cloud Function 访问存储在 Secrets Manager 中的密钥

问题描述

在测试我编写的尝试访问存储在 Secret Manager 中的密钥的 Google Cloud 函数时,我收到此错误:Error: 7 PERMISSION_DENIED: Permission 'secretmanager.versions.access' denied for resource '<resource-name>' (or it may not exist).

我的代码:

const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
const secretClient = new SecretManagerServiceClient();

...

const [version] = await secretClient.accessSecretVersion({
  name: secretName
});
const secret = version.payload.data.toString();

我已按照文档中的步骤操作,在对服务的调用中指定了密钥的全名(projects/<project-id>/secrets/<secret-name>/versions/latest,因此无法在 GCP 密钥管理器中访问密钥中的问题在这里不适用)并为服务帐户提供以“Secret Manager Secret Accessor”角色运行我的云功能(这应该排除为什么我的 Firebase 应用程序没有连接到 Google Secret Manager 中的根本问题?)。

我在尝试使用 curl 在本地触发函数时以及在 UI 中测试它时(GCF > 函数详细信息 > 测试)都看到了这个问题。

我在这里有什么遗漏吗?

标签: google-cloud-platformgoogle-cloud-functionsgoogle-secret-manager

解决方案


事实证明,我将“Secret Manager Secret Accessor”角色赋予了错误的服务帐户 - 我将其赋予了GCF 管理服务帐户,该帐户用于创建/更新/删除功能(service-<project-id>@gcf-admin-robot.iam.gserviceaccount.com)而不是运行时服务帐户,这是实际用于运行函数 ( <project-id>@appspot.gserviceaccount.com) 的内容。

一旦我将上述角色(以及所需的功能)添加到运行时服务帐户,该功能就成功完成了。


推荐阅读