[英]how can I get my gcloud user creds into a container securely and use them to impersonate a service account when testing locally?
從此開始:
gcloud config set auth/impersonate_service_account [SA_FULL_EMAIL]
並且它可以使用與在開發環境中運行的相同的憑據運行 - 無需下載任何密鑰現在可以了。 但我也希望能夠在容器中本地運行應用程序。 使用 docker/docker-compose/minikube/etc 如何使模擬服務帳戶成為可能?
在應用程序以某種方式啟動之前,容器需要訪問gcloud 憑據,並且還需要在 session 中設置模擬。 這不能在代碼中完成——應用程序應該像往常一樣使用 API,而不必做任何不同的事情。
編輯:當應用程序在 dev 或 prod GCP 帳戶/項目中運行時,它們在具有該特定應用程序正確范圍權限的服務帳戶的上下文中運行。 開發人員自己的用戶帳戶對開發環境具有廣泛的權限。 在本地運行時,使用與在開發環境中運行應用程序相同的服務帳戶而不是開發人員自己的用戶帳戶運行很有用
實現此目的的正確方法是 Google Cloud 提供的 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
這就是我想要的
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.