authentication - 如何管理用于本地开发的 Google Cloud 凭据

标签 authentication google-cloud-platform google-authentication impersonation service-accounts

我搜索了很多如何验证/授权 Google 的客户端库,但似乎没有人同意如何去做。

有些人说我应该创建一个服务帐户,从中创建一个 key ,然后将该 key 提供给每个想要充当此服务帐户的开发人员。我讨厌这个解决方案,因为它会将服务帐户的身份泄露给多个人。

其他人提到您只需通过以下操作使用 Cloud SDK 和 ADC(应用程序默认凭据)登录:

$ gcloud auth application-default login

然后,google-cloud-storage 等库将从 ADC 加载与我的用户相关联的凭据。 它更好,但对我来说仍然不好,因为这需要转到 IAM 并为每个开发人员(或组)提供应用程序运行所需的权限。此外,如果开发人员出于测试目的在本地运行许多应用程序(例如微服务),则所需的权限列表可能会很长。也很难理解为什么我们会在一段时间后授予此类权限。

我遇到的最后一种方法是服务帐户模拟。这解决了将私钥暴露给开发人员的问题,让我们定义应用程序所需的权限,比如说一次A,将它们关联到一个服务帐户并说:

Hey, let Julien act as the service account used for application A.

以下是如何模拟委托(delegate)人的片段:

from google.auth import impersonated_credentials
from google.auth import default

from google.cloud import storage

target_scopes = ['https://www.googleapis.com/auth/devstorage.read_only']

credentials, project = default(scopes=target_scopes)

final_credentials = impersonated_credentials.Credentials(
    source_credentials=credentials,
    target_principal="foo@bar-271618.iam.gserviceaccount.com",
    target_scopes=target_scopes
)

client = storage.Client(credentials=final_credentials)

print(next(client.list_buckets()))

If you want to try this yourself, you need to create the service account you want to impersonate (here foo@bar-271618.iam.gserviceaccount.com) and grant your user the role Service Account Token Creator from the service account permission tab.

我唯一担心的是,它需要我包装我想使用的所有 Google Cloud 客户端库,并检查我是否在本地运行我的应用程序:

from google.auth import impersonated_credentials
from google.auth import default

from google.cloud import storage

target_scopes = ['https://www.googleapis.com/auth/devstorage.read_only']

credentials, project = default(scopes=target_scopes)
if env := os.getenv("RUNNING_ENVIRONMENT") == "local":
    credentials = impersonated_credentials.Credentials(
        source_credentials=credentials,
        target_principal=os.environ["TARGET_PRINCIPAL"],
        target_scopes=target_scopes
    )

client = storage.Client(credentials=credentials)
print(next(client.list_buckets()))

另外,我必须定义我正在使用的范围(我认为是 oauth2 访问范围?),这很烦人。

我的问题是:我的方向是否正确?我是否想得太多了?有没有更简单的方法来实现所有这些目标?

这是我使用的一些来源:

更新1

讨论了这个话题here .

我提出了第一个命题here以支持此增强功能。

更新2

该功能已实现!参见 here了解详情。

最佳答案

您可以使用新的 gcloud 功能并像这样模拟您的本地凭据:

gcloud auth application-default login --impersonate-service-account=<SA email>

这是一项新功能。作为一名Java和Golang开发者,我查看并测试了Java客户端库,它已经支持这种认证模式。然而,在 Go 中还不是这样。我提交了一份 pull request将其添加到 go 客户端库中。

我快速检查了 Python,它 seems implemented .试用其中一个最新版本(2021 年 8 月 3 日之后发布)并告诉我!!

注意:一些人知道您的用例。在这种情况下,我很高兴不孤单:)

关于authentication - 如何管理用于本地开发的 Google Cloud 凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69412702/

相关文章:

php - 来自更多位置的 "Remember me"

javascript - 类型错误 : Cannot call a class as a function - ES6 - Angular1. x - Webpack

mysql - SSL 证书上的 Google 云 (GCP) SQL 限制

c# - 使用 Owin + OAuth + Google 在 ExternalLogin 上从 HTTP 重定向到 HTTPS

ruby-on-rails - Rails 错误 : certificate verify failed. ..我已经尝试了一切

authentication - 为什么API网关和认证服务应该不同?

Swift:反斜杠点 "\."是什么意思?

amazon-web-services - 在 AWS、Azure 或 Google Cloud 等主要参与者托管时,我可以相信我的代码和表结构不会被复制吗?

google-cloud-platform - 如何查看 GCP 中特定 Dataflow 流作业的成本?

firebase - 如何在flutter中实现oauth登录?