将环境变量从 Google 的 Secret Manager 加载到 Docker 在 Google Cloud 运行 上运行但未通过 Cloud Build 部署的容器中?

Load env variables from Google's Secret Manager into Docker container that runs on Google Cloud Run, but not deployed via Cloud Build?

我目前正在使用 Google 的 Cloud 运行.

作为 docker 容器交付节点 + nestjs 应用程序

我正在使用机密管理器来管理机密,并使用项目来管理 dev/staging/prod,并且我正在尝试让我在云中的容器可以使用机密。

当前进程由“yarn docker:auth”触发,它会触发一系列 bash 脚本:

docker build -t gcr.io/my_project_id_dev/auth-service -f .docker/auth.DockerFile . &&
gcloud auth activate-service-account abc@my_project_id_dev.iam.gserviceaccount.com --key-file=gcloud-sa.json &&
gcloud auth configure-docker &&
docker push gcr.io/my_project_id_dev/auth-service &&
gcloud beta run services replace .gcp/cloud_run/auth.yaml &&
gcloud run deploy auth ... --allow-unauthenticated  --platform managed --region europe-west2

最后一个命令中的 arguments/flags 也不起作用,每次我 运行 时都会提示我选择一个平台和区域。

我已经尝试将标志添加到我的 auth.yaml 文件以及 Google Cloud Build 文档中举例说明的秘密,但由于语法错误每次都失败。

在yaml文件中,我在没有嵌套的yaml文件底部添加了以下内容 属性:

availableSecrets:
  secretManager:
    - versionName: projects/my_project_id/secrets/mongo_uri/versions/latest
    env: 'mongo_uri'

我的问题是:

我还在我的 nodejs 应用程序中添加了一个启动功能,它尝试使用来自 npmjs 的 @google-cloud/secret-manager 将秘密加载到环境中。我在本地使用默认凭据执行此操作没有任何问题,但是:

我要解决的根本问题是将这些秘密放入容器环境中。

谢谢。

编辑:

想要添加 YAML 部分,我将服务帐户分配给云 运行 容器:

spec:
  template:
    metadata:
      annotations:
        autoscaling.knative.dev/maxScale: '2'
        run.googleapis.com/client-name: cloud-console
        run.googleapis.com/sandbox: gvisor
    spec:
      containerConcurrency: 2
      containers:
      - image:  gcr.io/my_project_id/auth-service
        ports:
        - containerPort: 3000
        resources:
          limits:
            cpu: 1000m
            memory: 512Mi
      serviceAccountName: abc@my_project_id.iam.gserviceaccount.com
      timeoutSeconds: 300

但遗憾的是,这仍然会导致此一般性错误:

(gcloud.beta.run.services.replace) Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable.

日志本身绝对没有更多详细信息,空白!由于没有注入服务帐户,容器不会在本地启动,但是如果没有处理秘密加载的那段代码,容器就可以在本地启动。

这里有一点要解包。简而言之,我认为您可能会混淆 build time运行time 秘密以及它们的访问方式。

如果您不需要在编译或测试阶段访问秘密,您可以从 cloudbuild.yaml 中省略 availableSecrets 节。这在 构建时间 中引入了秘密。例如,假设您想要 运行 Cloud Build 中的测试,并且您需要 API 密钥或数据库密码来执行测试。那时你会使用 Cloud Build + Secret Manager integration.


gcloud auth activate-service-account abc@my_project_id_dev.iam.gserviceaccount.com --key-file=gcloud-sa.json

如果您在本地使用 gcloud,则不需要服务帐户。您可以通过以下方式以自己的身份向 gcloud 进行身份验证:

gcloud auth login && gcloud auth application-default login

您通常会在安装 gcloud 的过程中执行一次这些步骤。 configure-docker也是如此。你一般只运行这个命令一次


Cloud 运行 + Secret Manager 集成目前处于预览阶段。你可以request access using this Google Form。 (对于未来的读者,如果该表格不再存在,则表示集成 public,无需注册。)


Will the docker container inside Cloud run have any kind of "Default" credentials? and If not, what would be the best way to inject it? It seems bad practice to either build or ship the container with a service account key-file.

是的,Cloud 运行 容器将默认作为默认的 Compute Engine 服务帐户执行。此服务帐户具有广泛的权限,但没有访问机密的权限。

我们建议您创建一个特定于此工作负载和 grant access to only the specific secrets it needs 的新服务帐户。然后,您可以使用带有 --service-account 标志的自定义服务帐户 运行 Cloud 运行 容器:

gcloud run deploy auth --service-account "...@...iam.gserviceaccount.com"

The arguments/flags on the last command also doesn't work and I'm prompted to pick a platform & region every time I run it.

我们需要查看完整的命令以了解失败的原因,特别是 ....


With absolutely no further details within the logs themselves, blank! The container doesn't start locally due to no service account injected into it, but without that piece of code that handles the secrets-loading, the container starts locally just fine.

您可以从侧边栏查看 Cloud Logging 中容器的日志。