Repository navigation
Expand file tree
/
Copy pathdeployer.tf
More file actions
106 lines (95 loc) · 5.07 KB
/
Copy pathdeployer.tf
File metadata and controls
106 lines (95 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
###################################
# Terraform apply service account #
###################################
# The apply account (var.terraformer) lives in the sac-prod-sa project. Its
# access to the state bucket (sac-prod-tf--sentry-watchdog) is managed in
# security-as-code; everything it needs inside this project is granted here.
# These grants are applied by a human with Owner on the project. Because the
# projectIamAdmin grant below is conditioned to a short list of roles, the
# account cannot change its own roles, the plan account's (planner.tf), or the
# legacy deploy SA's (infrastructure/permissions.tf): edits to those need a
# human to apply them, after which the CI apply is a no-op.
locals {
deploy_sa_member = "serviceAccount:${var.terraformer}"
staging_bucket = "${var.project}-cloud-function-staging"
# Buckets this configuration manages, from the same terraform.yaml files the
# gcs and pubsubs modules read. A new bucket must be created by a human once
# (the apply account has no project-wide storage.buckets.create).
managed_buckets = concat(
[local.staging_bucket],
[for f in fileset(path.module, "gcs/*/terraform.yaml") : yamldecode(file("${path.module}/${f}")).gcs.bucket_name if can(yamldecode(file("${path.module}/${f}")).gcs)],
[for f in fileset(path.module, "pubsubs/*/terraform.yaml") : yamldecode(file("${path.module}/${f}")).sink.sink_name if can(yamldecode(file("${path.module}/${f}")).sink)],
)
# Narrowest predefined role per resource type this configuration manages.
# The *_iam_member resources need setIamPolicy, which only the admin roles
# carry. iam.serviceAccountAdmin cannot create keys or impersonate.
deploy_sa_project_roles = [
"roles/cloudfunctions.admin", # google_cloudfunctions2_function + its IAM bindings
"roles/run.admin", # google_cloud_run_service_iam_member on the gen2 services
"roles/eventarc.developer", # Eventarc triggers created for event-driven gen2 functions
"roles/cloudscheduler.admin", # google_cloud_scheduler_job
"roles/pubsub.admin", # google_pubsub_topic/subscription + their IAM bindings
"roles/iam.serviceAccountAdmin", # google_service_account + google_service_account_iam_member
"roles/iam.serviceAccountUser", # act as the function, cron and build SAs when deploying
"roles/iam.roleViewer", # refresh google_project_iam_custom_role (create/update stay human-applied)
"roles/serviceusage.serviceUsageAdmin", # google_project_service
]
# Project-level roles the deployer may grant or revoke: the function,
# cronjob and pubsub SAs' bindings created by the modules.
deploy_sa_grantable_roles = [
"roles/logging.logWriter",
"roles/eventarc.eventReceiver",
"roles/monitoring.viewer",
]
}
resource "google_project_iam_member" "deploy_sa" {
for_each = toset(local.deploy_sa_project_roles)
project = var.project
role = each.value
member = local.deploy_sa_member
}
# Secret Manager secrets and their IAM, without secretmanager.versions.access:
# roles/secretmanager.admin would let the deployer read every secret value.
resource "google_project_iam_custom_role" "tf_secret_manager" {
project = var.project
role_id = "sentryWatchdogTfSecretManager"
title = "Sentry Watchdog TF Secret Manager"
description = "Manage Secret Manager secrets and IAM for Terraform, without payload access."
permissions = [
"secretmanager.secrets.create",
"secretmanager.secrets.get",
"secretmanager.secrets.list",
"secretmanager.secrets.update",
"secretmanager.secrets.delete",
"secretmanager.secrets.getIamPolicy",
"secretmanager.secrets.setIamPolicy",
]
}
resource "google_project_iam_member" "deploy_sa_secret_manager" {
project = var.project
role = google_project_iam_custom_role.tf_secret_manager.id
member = local.deploy_sa_member
}
# Unconditioned, projectIamAdmin could grant Owner to anyone, which would make
# every other role on this list irrelevant. The condition restricts setIamPolicy
# calls to the listed roles; getIamPolicy is unaffected so plans still work.
resource "google_project_iam_member" "deploy_sa_project_iam_admin" {
project = var.project
role = "roles/resourcemanager.projectIamAdmin"
member = local.deploy_sa_member
condition {
title = "deployer-restricted-role-grants"
description = "Deployer may only grant or revoke: ${join(", ", local.deploy_sa_grantable_roles)}"
# NOTE: edits to this condition must be applied by a human; the deployer
# cannot modify its own projectIamAdmin binding.
expression = "api.getAttribute('iam.googleapis.com/modifiedGrantsByRole', []).hasOnly(${jsonencode(local.deploy_sa_grantable_roles)})"
}
}
# Bucket settings, bucket IAM and function source uploads. Scoped to the
# buckets this configuration manages rather than project-wide storage.admin.
resource "google_storage_bucket_iam_member" "deploy_sa_buckets" {
for_each = toset(local.managed_buckets)
bucket = each.value
role = "roles/storage.admin"
member = local.deploy_sa_member
}