【问题标题】:Authenticate Google-Cloud-Storage in Python using Workload Identity Federation使用 Workload Identity Federation 在 Python 中对 Google-Cloud-Storage 进行身份验证
【发布时间】:2022-01-28 03:31:29
【问题描述】:

是否可以使用 Google Workload Identity Federation 的 OIDC 方法对我的 Python 应用程序的 Google Cloud Storage 包进行身份验证。

我想做这样的事情:

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.

【问题讨论】:

  • 1) 云存储需要 OAuth 访问令牌而不是身份令牌。 2) 您的代码尝试使用一个库创建凭据类,然后在不兼容的库方法中使用该变量。 3)您的问题提到了 Workload Identity Federation,但我在您的代码中没有看到。
  • @JohnHanley 感谢您的澄清评论。我想使用 Workload Identity Federation 来避免使用和传递敏感的 json 文件。我可以使用 WIF 对这些 Google SDK 进行身份验证吗?

标签: python google-cloud-platform google-cloud-storage openid-connect


【解决方案1】:

在 Sal 的 Exchange Generic OIDC Credentials for GCP Credentials 存储库中查看 Automatic

IIUC 这会将您从联合凭据获取到您可以与 any Google SDK 一起使用的 Google 应用程序默认凭据(见下文)。

对于@john-hanley 的评论,您错误地使用了 auth 库。

请参阅google.auth,特别是(在export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/key.json 之后),您可以:

import google.auth

credentials, project_id = google.auth.default()

【讨论】:

  • 感谢这个有用的答案,我正在尝试使用 key.json 文件。为了安全起见,我不希望处理和传递服务帐户 json 文件。我目前有可能吗?
【解决方案2】:

这并不能解决问题的 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 文件的情况下进行身份验证。

【讨论】:

  • 如上所述,您不能直接使用 OIDC 令牌(无论由谁颁发)来访问像存储桶这样的 GCP 资源。如果您有任何提供商颁发的任意 OIDC 令牌,那么您需要工作负载联合,它将将该令牌交换为谷歌理解的令牌(即,将其交换为访问令牌),这是正确的。您只需要创建一个 Google SDK 客户端可以理解的 aJSON 文件,并使用相同的 GOOGLE_APPLICATION_CREDENTIALS 指向具有该第 3 方 id_token 的文件。您实际上并没有分发服务帐户的密钥。
  • @DazWilkin 我想我现在明白了这一点,有没有一种方便的方法可以在开发时从本地环境中检索 OIDC 令牌?
【解决方案3】:

不能直接回复cmets但是

供参考,这里有一些描述更多的链接

  • federation 会将您的环境 aws|oidc|azure 凭据交换为 GCP 理解的凭据(不涉及 svc 帐户密钥)请参阅 gcp workload identity federation

  • google 发布的 oidc (id_tokens) 通常在这里用于访问您在云运行上部署的服务等,请参阅 google id tokens

  • 最后,只是为了参考,谷歌有非常具体的方式,某些服务使用 svc 帐户 jwt 进行身份验证(它不是 oidc 或 oauth,而是谷歌特定的东西)......它一点也不常见,但它在这里描述见jwt access tokens

【讨论】:

  • 谢谢,这个链接对了解发生的事情也很有帮助:cloud.google.com/docs/authentication/…
  • @sal 是我在回答中引用的 repo 的作者。他非常了解安全性,尤其是 Google|GCP 安全性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-03
  • 1970-01-01
  • 2019-11-06
  • 1970-01-01
  • 2014-10-30
  • 2020-08-06
  • 2020-03-20
相关资源
最近更新 更多