GCP-如何在没有密钥文件的情况下运行Python应用程序作为服务帐户

发布于 2025-02-08 09:25:54 字数 241 浏览 3 评论 0原文

gcloudgsutil具有- 模仿service-account,我们可以假冒服务帐户。

对于Python程序,是否有一种方法可以将程序作为服务帐户运行,而无需使用Service帐户秘密密钥文件作为密钥文件,而不是出于安全原因。

服务帐户密钥可能会造成安全风险。我们建议您避免下载服务帐户键

gcloud and gsutil have --impersonate-service-account by which we can impersonate a service account.

For Python program, is there a way to run the program as a service account without using the service account secret key file as key file is not recommended for security reason.

Service account keys could pose a security risk if compromised. We recommend that you avoid downloading service account keys

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤星 2025-02-15 09:25:55

是的,有一种方法可以在Python程序中使用无钥匙的身份验证。它称为 workload noreload Identity vederation Federation(wif)

使用wif,您可以使用外部提供商生成 openID noreid connect (oidc)<强>身份令牌。此外部提供商可以是IDAA(身份作为服务),例如 okta auth0 authlete>这可以是您自己建立的东西。更准确地说,您需要创建一个OAuth 2.0 /a>,例如,使用 authlib 。我从来没有自己做过,但是我敢肯定这是很多工作。

设置了外部身份提供商后,您将需要配置工作负载身份池

要允许使用这些令牌,您必须配置工作负载身份池以信任您的外部身份提供商。然后,由外部身份提供商发出的代币将通过工作负载身份联合会确认,您可以使用令牌获得短暂的服务帐户凭据。

就个人而言,我会三思而后行地自己实施OAuth 2.0授权服务器。我不想担心发出正确的令牌类型,当然我不想担心管理令牌数据库(将其黑名单,使其无效等)。我很可能会下载服务帐户的JSON密钥,并在我的Python程序中使用它,或者支付IDAA来发行ID令牌(即Workload Identity Federation中使用的外部身份提供商)。

Yes, there is a way to use a keyless authentication in your python program. It's called Workload Identity Federation (WIF).

With WIF you use an external provider to generate a OpenID Connect (OIDC) identity token. This external provider can be an IDaaS (IDentity as a Service) like Okta, Auth0 or Authlete, or it can be something you build yourself. More precisely, you would need to create an OAuth 2.0 authorization server, for example using a library like Authlib. I've never done it myself, but I'm sure it's a lot of work.

Once you have set up the external identity provider, you will need to configure a workload identity pool:

To allow the use of these tokens, you must configure the workload identity pool to trust your external identity provider. Tokens issued by the external identity provider are then recognized by workload identity federation, and you can use the tokens to obtain short-lived service account credentials.

Personally, I would think twice about implementing an OAuth 2.0 authorization server myself. I don't want to worry about issuing the correct type of token, and certainly I do not want to worry about managing the database of tokens (blacklisting them, invalidating them, etc). Most likely I would either download the JSON key of a service account and use it in my python program, or pay an IDaaS to use to issue ID tokens (i.e. the external identity provider used in Workload Identity Federation).

枫林﹌晚霞¤ 2025-02-15 09:25:54

对于Python程序,是否可以将程序作为服务运行
不建议不使用密钥文件作为密钥文件的帐户
安全原因。

如果您的Python程序在Google Cloud之外运行,则不,您必须使用凭据。

你有一个捕获22。您需要使用凭证授权,以模仿另一个凭据。

您有三个选择:

  • 用户帐户凭据
  • 另一个服务帐户凭据
  • 联合令牌

的每种方法都需要秘密。

对于计算服务,例如计算引擎,云功能,云运行等,您可以使用元数据服务进行授权。但是,那么您就不需要模仿凭据,就可以使用凭据,因为它们是安全的(机器上没有秘密)。

我写了一篇有关此主题的文章以及如何使用用户帐户凭据设置模仿的文章:

Google Cloud - 通过假冒的安全性提高安全性

如果正确设置了模仿,则不需要Flag - 不需要模拟服务

For Python program, is there a way to run the program as a service
account without using the key file as key file is not recommended for
security reason.

If your Python program is running outside Google Cloud, then no, you must use credentials.

You have a catch22. You need to be authorized using credentials to impersonate another credential.

You have three choices:

  • user account credentials
  • another service account credentials
  • federated tokens

Each of those methods requires secrets.

For compute services, such as Compute Engine, Cloud Functions, Cloud Run, etc you can use the metadata service for authorization. However, then you do not need to impersonate credentials, you can just use the credentials as they are safe (no secrets stored on the machine).

I wrote an article on this topic and how to setup impersonation using user account credentials:

Google Cloud – Improving Security with Impersonation

If impersonation is set up correctly, the flag --impersonate-service-account is not required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文