如何在 Cloud 运行 容器中配置服务帐户密钥?
How do I configure the Service Account keys in a Cloud Run container?
我在 Go 中创建了一个使用 Secrets Manager 的后端,并将其部署到 Cloud 运行。问题是 Secret Manager api 需要一个 Service Account credential json file 指向并且它在我的本地机器上工作,因为我只是在 GOOGLE_APPLICATION_CREDENTIALS
环境变量中指定文件路径,但我没有在云 运行 环境中具有相同的便利性。我在云 运行 上的后端将如何指定其 GOOGLE_APPLICATION_CREDENTIALS
环境变量可以这么说?
根据官方文档
Setting Up Authentication for Server to Server Production Applications
If the environment variable isn't set, ADC uses the default service
account that Compute Engine, Google Kubernetes Engine, Cloud Run, App
Engine, and Cloud Functions provide, for applications that run on
those services.
因此,要从 Cloud 运行 访问 Secret Manager,应用程序默认凭据 (ADC) 将使用 Cloud 运行 的默认服务帐户。
编辑
默认情况下,云 运行 使用 compure engine service account PROJECT_NUMBER-compute@developer.gserviceaccount.com
which has the EDITOR
role. You have 2 choices, either use default service account or deploy cloud run with a non default service account that you created 和 Secret Manager Admin
角色。
除了 @marian.vladoi 的出色答案外,简而言之,要访问 GCP API(在您的案例中是 Secret Manager API),您需要做两个事情:
使用 --service-account
选项(或 UI 等效项)使用特定服务帐户部署您的 Cloud 运行 应用程序。
授予此服务帐户执行某些操作的权限(在本例中,访问机密)。
在 Cloud 运行 容器(或 GKE 应用、Cloud 运行 应用、Cloud Functions 应用等)中,您无需使用 GOOGLE_APPLICATION_CREDENTIALS
指定密钥。当您 运行 在任何 GCP 客户端库中的 Google Cloud 上时,会自动获取必要的凭据。
我在 Go 中创建了一个使用 Secrets Manager 的后端,并将其部署到 Cloud 运行。问题是 Secret Manager api 需要一个 Service Account credential json file 指向并且它在我的本地机器上工作,因为我只是在 GOOGLE_APPLICATION_CREDENTIALS
环境变量中指定文件路径,但我没有在云 运行 环境中具有相同的便利性。我在云 运行 上的后端将如何指定其 GOOGLE_APPLICATION_CREDENTIALS
环境变量可以这么说?
根据官方文档
Setting Up Authentication for Server to Server Production Applications
If the environment variable isn't set, ADC uses the default service account that Compute Engine, Google Kubernetes Engine, Cloud Run, App Engine, and Cloud Functions provide, for applications that run on those services.
因此,要从 Cloud 运行 访问 Secret Manager,应用程序默认凭据 (ADC) 将使用 Cloud 运行 的默认服务帐户。
编辑
默认情况下,云 运行 使用 compure engine service account PROJECT_NUMBER-compute@developer.gserviceaccount.com
which has the EDITOR
role. You have 2 choices, either use default service account or deploy cloud run with a non default service account that you created 和 Secret Manager Admin
角色。
除了 @marian.vladoi 的出色答案外,简而言之,要访问 GCP API(在您的案例中是 Secret Manager API),您需要做两个事情:
使用
--service-account
选项(或 UI 等效项)使用特定服务帐户部署您的 Cloud 运行 应用程序。授予此服务帐户执行某些操作的权限(在本例中,访问机密)。
在 Cloud 运行 容器(或 GKE 应用、Cloud 运行 应用、Cloud Functions 应用等)中,您无需使用 GOOGLE_APPLICATION_CREDENTIALS
指定密钥。当您 运行 在任何 GCP 客户端库中的 Google Cloud 上时,会自动获取必要的凭据。