如何通过 gcloud shell 在 Cloud 运行 部署中正确使用来自 Secret Manager 的机密

How to properly use a secret from the Secret Manager in a Cloud Run deployment via gcloud shell

我正在尝试构建一个 gcloud 脚本来部署我的云 运行 服务。此服务可以访问托管 MongoDB,因此我必须通过环境变量提供密码凭据。

我想通过秘密管理器传递此密码。我通过 GCP 控制台这样做:

因此秘密名称为 mongodb-password,值为 mypassword。现在,我按照 gcloud 运行 文档 (https://cloud.google.com/sdk/gcloud/reference/run/deploy) 了解如何将密钥添加到 运行 服务部署。

在设置密码部分解释如下:

Specify secrets to mount or provide as environment variables. Keys starting with a forward slash '/' are mount paths. All other keys correspond to environment variables. The values associated with each of these should be in the form SECRET_NAME:KEY_IN_SECRET; you may omit the key within the secret to specify a mount of all keys within the secret. For example: '--update-secrets=/my/path=mysecret,ENV=othersecret:key.json' will create a volume with secret 'mysecret' and mount that volume at '/my/path'. Because no secret key was specified, all keys in 'mysecret' will be included. An environment variable named ENV will also be created whose value is the value of 'key.json' in 'othersecret'. At most one of these may be specified

这让我有些困惑。我不知道 KEY_IN_SECRET 是什么意思。 秘密名称显然是 mongodb-password 但我不知道现在如何引用秘密的值。

我试图弄清楚是否有关于如何定义秘密值的约定。也许像 key=value。但没有说明我必须那样做。

所以现在我想知道我应该为 gcloud run deploy 命令的 --set-secrets=[KEY=VALUE,…] 标志做些什么。

根据给定的信息,我只能这样做:

--set-secrets=[mongodb-password=*]

我知道这是错误的,但我想解释一下目前让我感到困惑的是什么

你糊涂了,这很正常。秘密管理器集成目前仅适用于 Cloud 运行 on Anthos(例如,部署在 GKE 集群上)。

如果您使用完全管理版本,Cloud 运行 尚不支持秘密管理器集成(Alpha 版本应该很快开始,您可以期待 public 今年晚些时候推出)。

所以你需要直接在你的代码中获取你的秘密。或者,我创建了一个包装器来帮助您 load the secret from secret manager and to set them in environment variable.


在 GKE 中,秘密在 Kubernetes secrets documentation 中描述。您可以在 linux 目录树 /path/to/mysecret 的路径中安装秘密。这是一个最佳实践,它应该与完全管理版本相同(以确保可移植性和 Knative 合规性)。

这就是为什么您可以在文档中添加类似内容的原因 --update-secrets=/my/path=mysecret