3

我希望能够根据命名约定甚至更好地根据标签授予 Google Cloud 中的服务帐户对多个机密的访问权限。

到目前为止,看起来 GCP 仅提供基于组织、文件夹、项目或机密级别的访问权限,除此之外,您无法进一步了解 IAM 的设置方式。看这里

我想也许 GCP 的IAM 条件可以让我在这里更灵活,但我也没有运气。使用下面的 terraform - 我的 SA 仍然可以访问项目级别的所有机密。

resource "google_project_iam_member" "access_secrets" {
project = var.project_id
role    = "roles/secretmanager.secretAccessor"
member  = "serviceAccount:${google_service_account.service-a.email}"
provider = google-beta
  
condition {
   title       = "all-service-a-secrets"
   description = "All Service A secrets"
   expression  = "resource.name.startsWith('projects/my-project/secrets/service-a-secrets')"
  }
}

由于主要使用 AWS,我觉得权限更灵活一些。似乎答案可能是更自由地使用项目,但我无法找到很多关于使用 GCP 项目的最佳方法的意见。

4

3 回答 3

1

现在可以(不确定何时实施)使用resource.name.startsWith- 但是,这让我感到困惑,您必须使用数字项目 ID 而不是 URL 中的项目名称。

于 2020-11-10T00:26:30.420 回答
1

我正在使用 IAM 条件策略根据命名约定授予对机密的访问权限。

例如,以下条件策略仅授予用户对以“dev-myapp1-”开头的机密的访问权限

resource "google_project_iam_binding" "SecretCreator" {
  project = "my-project-id"
  role    = "projects/my-project-id/roles/SecretCreator"

  members = [
    "user:user.name@example.com",
  ]

resource "google_project_iam_binding" "SecretManagerAdmin" {
  project = "my-project-id"
  role    = "roles/secretmanager.admin"

  members = [
    "user:user.name@example.com",
  ]

  condition {
    title       = "dev-myapp1-*"
    expression  = "resource.name.startsWith(\"projects/123456789012/secrets/dev-myapp1-\")" 
    # "123456789012" is the Project Number. You need to use Project Number in expression not the Project ID
    #You can get the Project Number by running the command : gcloud projects describe my-project-id
  }
}

resource "google_project_iam_binding" "SecretListViewer" {
  project = "my-project-id"
  role    = "projects/my-project-id/roles/SecretListViewer"

  members = [
    "user:user.name@example.com",
  ]

}

resource "google_project_iam_binding" "SecretManagerViewer" {
  project = "my-project-id"
  role    = "roles/secretmanager.viewer"

  members = [
    "user:user.name@example.com",
  ]

}

您可能需要在项目中创建两个自定义角色才能使用此条件策略。您可以使用以下 terraform 模板在项目中创建自定义角色。

resource "google_project_iam_custom_role" "SecretListViewer" {
  role_id     = "SecretListViewer"
  title       = "SecretListViewer"
  description = "SecretListViewer"
  permissions = ["secretmanager.secrets.list"]
}

resource "google_project_iam_custom_role" "SecretCreator" {
  role_id     = "SecretCreator"
  title       = "SecretCreator"
  description = "SecretCreator"
  permissions = ["secretmanager.secrets.create"]
}

有了这个,用户应该能够创建任何名称的秘密,但如果名称不以“dev-myapp1-”开头,则不能向其添加内容或查看或使用它。他将拥有使用“dev-myapp1-”管理机密的完全权限。但他将无法查看内容或管理其他机密。

于 2021-07-02T12:07:19.390 回答
-1

目前这是不可能的,但它在 2020 年的路线图上。

于 2020-07-10T13:14:27.563 回答