![](/img/trans.png)
[英]Is there an equivalent to `refFromUrl` in google-cloud-storage for Python?
[英]Authenticate Google-Cloud-Storage in Python using Workload Identity Federation
是否可以使用 Google Workload Identity Federation 的 OIDC 方法對我的 Python 應用程序的 Google Cloud Storage package 進行身份驗證。
我想做這樣的事情:
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = str(os.path.dirname(os.path.dirname(__file__))) + "/keys/credentials.json"
from google.cloud import storage
import google.oauth2.id_token
import google.auth.transport.requests
request = google.auth.transport.requests.Request()
target_audience = "https://pubsub.googleapis.com"
obtained_id_token = google.oauth2.id_token.fetch_id_token(request, target_audience)
storage_client = storage.Client(credentials=obtained_id_token)
這給出了以下錯誤:
ValueError: This library only supports credentials from google-auth-library-python. See https://google-auth.readthedocs.io/en/latest/ for help on authentication with this library.
查看自動在 Sal 的Exchange Generic OIDC Credentials for GCP Credentials repo 上。
IIUC 這將您從聯合憑據獲取到 Google 應用程序默認憑據(見下文),您可以將其與任何Google SDK 一起使用。
對於@john-hanley 的評論,您錯誤地使用了 auth 庫。
請參閱google.auth ,特別是(在export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/key.json
),您可以:
import google.auth
credentials, project_id = google.auth.default()
不能直接回復評論但是
作為參考,這里有一些描述更多的鏈接
federation 會將您的環境aws|oidc|azure
憑據交換為 GCP 理解的憑據(不涉及 svc 帳戶密鑰),請參閱gcp 工作負載身份聯合
google 發布的 oidc ( id_tokens
) 通常在這里用於訪問您在雲運行上部署的服務等,請參見google id tokens
最后,只是為了參考,谷歌有非常具體的方式使用 svc 帳戶 jwt (它不是 oidc 也不是 oauth 而是谷歌特定的東西)對某些服務進行身份驗證......它一點也不常見,但它在此處描述,請參見ZCFD61B8A7397FA7C10B2AE58FBAFABAE
這並不能解決問題的 Workload Identity Federation 部分,但確實使問題代碼正常工作:
import google.oauth2.id_token
import google.auth.transport.requests
request = google.auth.transport.requests.Request()
target_audience = "https://storage.cloud.google.com/"
# Create ID token credentials.
credentials = google.oauth2.id_token.fetch_id_token_credentials(target_audience, request=request)
# Refresh the credential to obtain an ID token.
credentials.refresh(request)
id_token = credentials.token
id_token_expiry = credentials.expiry
storage_client = storage.Client(credentials=credentials)
bucket = storage_client.get_bucket(settings.GOOGLE_CLOUD_BUCKET_NAME)
仍然需要查看是否可以在不需要 repo 中的 Credentials json 文件的情況下進行身份驗證。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.