【发布时间】:2021-05-16 18:07:03
【问题描述】:
我想让我的nodeJS 应用程序使用我的IAM 凭据在本地进行身份验证,但是当我运行下面的基本secrets manager 函数时:
// config/secret_manager.js
const { SecretManagerServiceClient } = require('@google-cloud/secret-manager')
const client = new SecretManagerServiceClient()
async function getSecret(name) {
const [version] = await client.accessSecretVersion({ name })
const secretValue = JSON.parse(version.payload.data.toString())
return secretValue
}
module.exports = getSecret
我收到错误:
无法加载默认凭据。浏览至https://cloud.google.com/docs/authentication/getting-started 了解更多信息。 在 GoogleAuth.getApplicationDefaultAsync
我似乎找不到任何关于如何在本地使用我的 CLI 登录凭据的文档,有什么想法?
(我不想使用 JSON 服务帐户配置)
【问题讨论】:
-
由于您标记了
Node.js,您可能应该使用服务帐户而不是用户凭据。对于调试,可以使用用户凭据,但不建议用于生产场景。在 Google 计算服务上运行代码时,应使用元数据中的凭据。使用有关环境的更多详细信息更新您的问题。 -
@JohnHanley in CloudRun 会不会同样的功能不能获取任何可用的服务帐户?
-
您问题中的代码将从元数据中获取凭据。您的答案在 Cloud Run 中不起作用。一个关键点是不要在应该使用服务帐户的地方使用用户凭据,这对于其中一项计算服务几乎总是如此。 SDK 将打印警告,您将受到配额限制等。
标签: node.js google-cloud-platform service-accounts google-secret-manager gcloud-cli