无法从 Google Cloud Function 访问存储在 Secrets Manager 中的密钥

bha*_*k90 3 google-cloud-platform google-cloud-functions google-secret-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();
Run Code Online (Sandbox Code Playgroud)

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

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

有什么我在这里想念的吗?

bha*_*k90 7

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

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