简体   繁体   English

我应该如何创建服务帐户以使用 Google KMS 在 GCP 项目之间访问机密?

[英]How should I create service accounts for accessing secrets between GCP projects with Google KMS?

I've followed this tutorial: https://cloud.google.com/kms/docs/store-secrets我已经按照本教程进行操作: https ://cloud.google.com/kms/docs/store-secrets

My next step was to have my apps read my secrets file from the storage bucket and decrypt safely at runtime.我的下一步是让我的应用程序从存储桶中读取我的机密文件并在运行时安全地解密。 These apps run within various projects (staging, dev, production etc).这些应用程序在各种项目(暂存、开发、生产等)中运行。

I've read the service account documentation over and over but can't quite understand the correct way forward.我一遍又一遍地阅读服务帐户文档,但不太了解正确的前进方向。

What I found that worked was to simply add service accounts to MY_KMS_PROJECT and MY_STORAGE_PROJECT from the tutorial.我发现有用的是简单地将服务帐户添加到教程中的 MY_KMS_PROJECT 和 MY_STORAGE_PROJECT。 I set them up to have access to read storage buckets and to decrypt KMS keys.我将它们设置为有权读取存储桶和解密 KMS 密钥。 Just by creating those service accounts, suddenly apps in other projects could read and decrypt.只需创建这些服务帐户,突然之间其他项目中的应用程序就可以读取和解密。 Is that how it's supposed to work?它应该如何工作?

I thought that I would have had to create a service account for each project that I want to have accessing the KMS projects from the tutorial?我以为我必须为每个我想从教程中访问 KMS 项目的项目创建一个服务帐户? Or use IAM somehow to grant access?或者以某种方式使用 IAM 来授予访问权限? For example, how would I grant access to some apps within some projects and not others?例如,我将如何授予对某些项目中的某些应用程序的访问权限,而不是其他项目?

I'm attempting to now give access to apps when they are running in my local dev environment, which usually requires downloading a service account and pointing GOOGLE_APPLICATION_CREDENTIALS to the file.我现在正尝试在我的本地开发环境中运行时授予对应用程序的访问权限,这通常需要下载服务帐户并将 GOOGLE_APPLICATION_CREDENTIALS 指向该文件。 But it seems strange to me to download the service accounts from the MY_KMS_PROJECT or MY_STORAGE_PROJECT, especially since I already have a service account for accessing firebase.但是从 MY_KMS_PROJECT 或 MY_STORAGE_PROJECT 下载服务帐户对我来说似乎很奇怪,特别是因为我已经有一个用于访问 firebase 的服务帐户。 Are service accounts somehow global to all projects?服务帐户是否以某种方式对所有项目都是全局的? Can they be combined?它们可以结合吗? GOOGLE_APPLICATION_CREDENTIALS seems only good for pointing at a single service account. GOOGLE_APPLICATION_CREDENTIALS 似乎只适用于指向单个服务帐户。

Note: most of my apps are running on google app engine standard or flexible.注意:我的大部分应用程序都在 google app engine standard 或 flexible 上运行。

Here is the code from the apps within my projects that "just work" as described above:这是我的项目中应用程序的代码,如上所述“正常工作”:

client, err := google.DefaultClient(ctx, cloudkms.CloudPlatformScope)
if err != nil {
    log.Fatal(err)
}

// Create the KMS client.
kmsService, err := cloudkms.New(client)
if err != nil {
    log.Fatal(err)
}
....

And for accessing the bucket:访问存储桶:

// Create the storage clientstorage
Client, err := storage.NewClient(ctx)
if err != nil {
    log.Fatal(err)
}
....

For your first question: This could be happening due to the fact that those applications use the same services accounts that you created for the respective projects.对于您的第一个问题:发生这种情况的原因可能是这些应用程序使用您为各自项目创建的相同服务帐户。 In this case you would need to ensure that those applications do not use the aforementioned service accounts.在这种情况下,您需要确保这些应用程序不使用上述服务帐户。

For your second question: You might try to use one service account at organization level.对于第二个问题:您可能会尝试在组织级别使用一个服务帐户。 These service accounts would be inherited by all the projects in the organization.这些服务帐户将由组织中的所有项目继承。 You can grant the service account those roles or permissions that meet your requirements您可以授予服务帐户那些满足您要求的角色或权限

For your third question: They are “Global” to all projects in case you use organization-level service accounts: you can take a look at this documentation where you might find valuable information about service accounts and organization对于您的第三个问题:如果您使用组织级服务帐户,它们对所有项目都是“全局的”:您可以查看此文档,您可能会在其中找到有关服务帐户组织的有价值信息

Please let me know your thoughts and how it goes.请让我知道您的想法以及进展情况。

This is what I discovered works.这就是我发现的作品。 Until I hear that it's not correct or ideal, it's how I'm moving forward:直到我听说它不正确或不理想,这就是我前进的方式:

  1. Follow the "storing secrets" tutorial ( https://cloud.google.com/kms/docs/store-secrets ).遵循“存储秘密”教程 ( https://cloud.google.com/kms/docs/store-secrets )。

Note that when creating the two accounts from the tutorial, there will be no service accounts automatically created since there is no compute or app engine instances by default.请注意,在创建教程中的两个帐户时,不会自动创建服务帐户,因为默认情况下没有计算或应用引擎实例。

  1. Go to IAM for the kms storage project and add any other projects with apps that you want to have access to download the encrypted secrets.转到 kms 存储项目的 IAM,并添加任何其他项目以及您希望有权访问以下载加密机密的应用程序。 Add them by their service account emails, with the role for viewing buckets.通过他们的服务帐户电子邮件添加他们,具有查看存储桶的角色。

  2. Go to IAM for the kms management project and do the same for any projects with apps that you want to be decrypting the shared secrets, via their service account emails.转到 IAM 的 kms 管理项目,并通过他们的服务帐户电子邮件对包含要解密共享机密的应用程序的任何项目执行相同的操作。

For Localhost development, use "gcloud auth application-default login" before starting your services.对于 Localhost 开发,在启动服务之前使用“gcloud auth application-default login”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 访问 Google 表格以创建表格 (GCP) - Accessing Google Sheet to Create a sheet(GCP) 如何确保 GCP 服务帐户没有管理员权限? - How can I ensure that GCP service accounts do not have Admin privileges? 如何使用 Terraform 将多个角色分配给 GCP 中的多个服务帐户? - How to assign multiple roles to multiple service accounts in GCP using Terraform? 如何使用 gcloud sdk 创建没有 default.network 的 gcp 项目 - how to create gcp projects without default network using gcloud sdk 在 GCP 文件夹中创建项目需要哪些权限? - Which permissions do I need to create projects in GCP folders? 当 SSL 卸载到 GCP Cloud KMS 时,如何在 NGINX 中进行解密? - How decryption is done in NGINX when SSL offloaded to GCP Cloud KMS? GCP Terraform 服务帐户密钥导出 - GCP terraform service accounts keys export 如何允许用户成为 Google Cloud Platform (GCP) 中服务帐户的参与者? - How can I allow a user to become an actor of a service account in Google Cloud Platform (GCP)? 如何检查 GCP 服务帐户是否具有对 Google Drive 的完全访问权限? - How do I check to see if a GCP service account has full access to Google Drive? 如何授予谷歌服务帐户使用 terraform 列出所有项目的权限? - How do I grant permission to a google service account to list all projects using terraform?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM