分配 GCP 功能服务帐户角色以使用 Terraform 与 Firebase 互动

Assign GCP functions service account roles to engage with Firebase using Terraform

我想在我的 GCP 云功能中使用 Firebase Admin SDK,专门用于创建自定义身份验证令牌。

我在部署后收到 auth/insufficient-permission 个错误,并到达 this thread。请注意,它讨论的是 Firebase 函数,而我使用的是纯 GCP Cloud Functions。

据我了解,GCP Cloud Functions 使用默认的 App Engine 服务帐户,该帐户缺少 Firebase Admin SDK admin service agent 角色。

我通过 GCP 控制台手动添加它,它似乎解决了问题,但现在我想通过我管理基础设施的 terraform 自动化它。

  1. 如何访问默认的 App Engine 服务帐户?我认为它是在创建 GCP 项目时自动创建的。
  2. 如何在不更改使用该角色的其他服务帐户的情况下向其添加相关角色?
  3. 这是正确的方法吗,还是我遗漏了更好的方法?

我看的相关文档是here。请注意,我使用的 initializeApp() 没有参数,即让库隐式发现服务帐户。

  1. 如何通过 Terraform 获取默认的 App Engine 服务帐户:google_app_engine_default_service_account

  2. 如何使用分配给服务帐户的 'additional' IAM 角色: IAM policy for service account

  3. 对于一般性建议 - 我更愿意使用专门创建的服务帐户并完全删除(或禁用)默认的 App Engine 服务帐户。

编辑 ==> 根据要求提供更多详细信息

这里是Cloud Function service account in runtime的描述:

The App Engine service account has the Editor role, which allows it broad access to many Google Cloud services. While this is the fastest way to develop functions, Google recommends using this default service account for testing and development only. For production, you should grant the service account only the minimum set of permissions required to achieve its goal.

因此,delete/disable App Engine 服务帐户可能会有用,为给定的云功能创建一个特定的服务帐户,为其分配所有相关的最少 IAM 角色,然后使用它。

作为旁注,我还建议 delete/disable 默认 Compute Engine 服务帐户,删除具有所有防火墙规则和子网的默认网络...但这是一个单独的故事。