【问题标题】:Access secret manager from cloud function in GCP. What IAM settings to use?从 GCP 中的云功能访问秘密管理器。要使用哪些 IAM 设置?
【发布时间】:2021-07-02 20:33:04
【问题描述】:

我不知道在这里设置什么。恕我直言,整个政策、约束力和成员的东西非常令人困惑。有这些角色吗?总之……

尝试从云功能访问机密管理器。云功能是使用 Terraform 设置的:

module "mds_reporting_cloud_function" {
  source                         = "terraform-google-modules/scheduled-function/google"
  version                        = "2.0.0"
  project_id                     = var.function_gcp_project
  job_name                       = var.function_name
  job_description                = var.function_description
  job_schedule                   = var.function_cron_schedule
  function_entry_point           = "main"
  function_source_directory      = "${path.module}/../../../../src"
  function_name                  = var.function_name
  region                         = var.function_gcp_region
  bucket_name                    = var.function_name
  function_description           = var.function_description
  function_environment_variables = var.function_environment_variables
  function_runtime               = "python38"
  topic_name                     = var.function_name
}

resource "google_cloudfunctions_function_iam_binding" "binding" {
  project        = var.function_gcp_project
  region         = var.function_gcp_region
  cloud_function = var.function_name
  role           = "roles/secretmanager.secretAccessor"
  members = [
    "serviceAccount:${var.function_gcp_project}@appspot.gserviceaccount.com"
  ]
}

我的理解是,如果没有为云功能指定服务帐户,它将使用默认的 App Engine 服务帐户。

绑定应将角色“绑定”到 App Engine 服务帐户的现有 IAM 政策。

但是,它会抛出这个错误:

Error: 
Error applying IAM policy for cloudfunctions cloudfunction "projects/alpine-proton-280612/locations/europe-west3/functions/mds-reporting-cloud-function":
Error setting IAM policy for cloudfunctions cloudfunction "projects/alpine-proton-280612/locations/europe-west3/functions/mds-reporting-cloud-function": 
googleapi: Error 400: Role roles/secretmanager.secretAccessor is not supported for this resource.

不知道该怎么做。

【问题讨论】:

  • 您的角色分配是向后的。您正在尝试将服务帐户 secretAccessor 授予该功能。函数不提供秘密服务。如果您尝试修改服务帐号并为其授予额外角色,请将权限授予项目资源,而不是功能资源。
  • 提示:使用 Terraform 修改项目资源时要小心。可以通过覆盖(删除)现有角色将自己锁定在项目之外。创建一次性测试项目。
  • 查看@guillaume 的回答。他的方法是更好地在 Secret Manager 资源上授予 secretAccessor
  • @John Hanley,非常感谢您的解释!

标签: google-cloud-platform google-cloud-functions terraform google-iam google-secret-manager


【解决方案1】:

最好的解决方案是仅在密钥上授予 Cloud Functions 服务帐户访问密钥的权限。为此,请使用 Secret Manager IAM terraform 资源

resource "google_secret_manager_secret_iam_binding" "binding" {
  project = var.function_gcp_project
  secret_id = google_secret_manager_secret.your-secret.secret_id
# If your secret is not created by terraform, use this format for the id projects/{{project}}/secrets/{{secret_id}}
  role = "roles/secretmanager.secretAccessor"
  members = [
    "serviceAccount:${var.function_gcp_project}@appspot.gserviceaccount.com"
  ]
}

重要提示:

  • 您也可以在 project level 上授予此角色,但安全性较低,因为该函数可以访问项目的所有机密
  • 您使用 App Engine(和云功能)默认服务帐户。它也不是那么安全。事实上,任何 App Engine 服务和具有自定义服务帐户的 Cloud Functions 都将默认使用此服务帐户,因此将能够访问这些机密。首选custom service account for your Cloud Functions
  • John 的第二条评论非常重要。 Terraform 具有多个级别的 IAM 角色的写入和替换。请记住(适用于所有 IAM_XXX terraform 模块)
    • Policy 替换整个资源的所有可能角色的所有帐户(在一个项目中,它可能是戏剧性的!)
    • 绑定替换特定角色的所有帐号
    • 会员仅添加特定角色的帐户。什么都不删除。

【讨论】:

    猜你喜欢
    • 2022-06-24
    • 1970-01-01
    • 2022-06-30
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 2020-11-13
    相关资源
    最近更新 更多