简体   繁体   English

如何将我的 gcloud 用户凭据安全地放入容器中,并在本地测试时使用它们来模拟服务帐户?

[英]how can I get my gcloud user creds into a container securely and use them to impersonate a service account when testing locally?

Starting with this:从此开始:

  • users have their own google user accounts that are setup locally via gcloud login用户拥有自己的 google 用户帐户,这些帐户是通过 gcloud 登录在本地设置的
  • the application is using the gcp APIs in the usual way- by default it will look for GOOGLE_APPLICATION_CREDENTIALS, GCE roles, service accounts, or use the local users gcloud configured credentials应用程序以通常的方式使用 gcp API - 默认情况下,它将查找 GOOGLE_APPLICATION_CREDENTIALS、GCE 角色、服务帐户,或使用本地用户 gcloud 配置的凭据
  • when users run it locally it will use their own user account, when run in gcp it will use a service account当用户在本地运行时,它将使用他们自己的用户帐户,当在 gcp 中运行时,它将使用服务帐户
  • The user's account also has access to impersonate the service account.用户的帐户也有权模拟服务帐户。 So when running the app locally users first do gcloud config set auth/impersonate_service_account [SA_FULL_EMAIL] and it can be run with the same creds as what will run in the dev environment- without them having to download any keys因此,当在本地运行应用程序时,用户首先执行gcloud config set auth/impersonate_service_account [SA_FULL_EMAIL]并且它可以使用与在开发环境中运行的相同的凭据运行 - 无需下载任何密钥

Now that works.现在可以了。 BUT I also want to make it possible to run the applications locally in containers too.但我也希望能够在容器中本地运行应用程序。 Using docker/docker-compose/minikube/etc how can I make it possible to impersonate a service account?使用 docker/docker-compose/minikube/etc 如何使模拟服务帐户成为可能?

the container would need access to the gcloud creds and it would need to set impersonation in the session too before the app starts somehow.在应用程序以某种方式启动之前,容器需要访问gcloud 凭据,并且还需要在 session 中设置模拟。 This must not be done in code- the app should just use the APIs as normal without having to do anything differently.这不能在代码中完成——应用程序应该像往常一样使用 API,而不必做任何不同的事情。

EDIT: when applications run in dev or prod GCP accounts/projects they run in the context of a service account that has correctly scoped permissions for that specific application.编辑:当应用程序在 dev 或 prod GCP 帐户/项目中运行时,它们在具有该特定应用程序正确范围权限的服务帐户的上下文中运行。 Developer's own user accounts have broad permissions to the dev environment.开发人员自己的用户帐户对开发环境具有广泛的权限。 When running locally its useful to run with the same service account that application runs with in the dev environment instead of the developer's own user account在本地运行时,使用与在开发环境中运行应用程序相同的服务帐户而不是开发人员自己的用户帐户运行很有用

The correct way to achieve this is the Secret Manager that is supplied by Google Cloud.实现此目的的正确方法是 Google Cloud 提供的 Secret Manager。

  • Activate Secret Manager API in your Google Cloud account.在您的 Google Cloud 帐户中激活 Secret Manager API。
  • Create Secret data and relevant payload either using GC UI or sdk.使用 GC UI 或 sdk 创建 Secret 数据和相关负载。
  • Use the following function in your settings.py with your Project ID.在您的 settings.py 中使用以下 function 和您的项目 ID。
  • Give permissions for your service account to access Secrets (If it does not already have access)授予您的服务帐户访问 Secrets 的权限(如果它还没有访问权限)
  • Use the Secret Manager to access the payloads in your code without explicitly exposing the payload.使用 Secret Manager 访问代码中的有效负载,而无需显式公开有效负载。
 def access_secret_version(secret_id): """ Access the payload for the given secret version if one exists. The version can be a version number as a string (eg "5") or an alias (eg "latest"). """ project_id = PROJECT_ID version_id = 1 # Import the Secret Manager client library. from google.cloud import secretmanager_v1beta1 as secretmanager # Create the Secret Manager client. client = secretmanager.SecretManagerServiceClient() # Build the resource name of the secret version. name = client.secret_version_path(project_id, secret_id, version_id) # Access the secret version. response = client.access_secret_version(name) # Print the secret payload. # # WARNING: Do not print the secret in a production environment - this # snippet is showing how to access the secret material. payload = response.payload.data.decode('UTF-8') # logging.info('Plaintext: {}'.format(payload)) logging.info('Secret accessed for:' + secret_id) return payload

This what I wanted这就是我想要的

GSA_TOKEN=$(gcloud auth print-access-token --impersonate-service-account mygsa)
docker run --env GOOGLE_APPLICATION_CREDENTIALS=$GSA_TOKEN my-image

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将gcloud与服务帐户一起使用? - How do I use gcloud with a service account? 如何让一个服务帐户模拟另一个服务帐户? (节点.js) - How can I have a service account impersonate another service account? (Node.js) GCP - 以用户身份模拟服务帐号 - GCP - Impersonate service account as a user GCP:使用来自本地信用的 python 模拟服务帐户。 IAM 服务帐户凭据 API 已禁用 - GCP: Impersonate Service Account with python from local creds. IAM Service Account Credentials API disabled 如何使用 impersonate_service_account 进行流式光束管道 - How to use impersonate_service_account for streaming beam pipeline 我可以使用 gcloud activate-service-account 进行模拟(不是 static 密钥)吗? - Can I use gcloud activate-service-account with impersonation (not static keys)? 为什么 GCP 服务帐号不能冒充自己? - Why can a GCP service account not impersonate itself? 如何从 Android 应用程序安全地使用 GCP 服务帐户? - How to securely use GCP service account from an Android app? 服务帐户如何在 gcloud 中进行身份验证而不更改环境中的默认 gcloud 帐户? - How can a service account authenticate in gcloud without changing the default gcloud account in the environment? 如何安全地访问包含服务帐户凭据的 json 文件? - How can I securely access json file containing service account credentials?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM