forked from GoogleCloudPlatform/cloud-foundation-fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-accounts.tf
162 lines (153 loc) · 7.13 KB
/
service-accounts.tf
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
# tfdoc:file:description Service identities and supporting resources.
locals {
_service_accounts_cmek_service_dependencies = {
"composer" : [
"composer",
"artifactregistry", "container-engine", "compute", "pubsub", "storage"
]
"dataflow" : ["dataflow", "compute"]
}
_service_accounts_robot_services = {
apigee = "service-%s@gcp-sa-apigee"
artifactregistry = "service-%s@gcp-sa-artifactregistry"
bq = "bq-%s@bigquery-encryption"
cloudasset = "service-%s@gcp-sa-cloudasset"
cloudbuild = "service-%s@gcp-sa-cloudbuild"
cloudfunctions = "service-%s@gcf-admin-robot"
cloudrun = "service-%s@serverless-robot-prod"
composer = "service-%s@cloudcomposer-accounts"
compute = "service-%s@compute-system"
container-engine = "service-%s@container-engine-robot"
containerregistry = "service-%s@containerregistry"
dataflow = "service-%s@dataflow-service-producer-prod"
dataproc = "service-%s@dataproc-accounts"
fleet = "service-%s@gcp-sa-gkehub"
gae-flex = "service-%s@gae-api-prod"
# TODO: deprecate gcf
gcf = "service-%s@gcf-admin-robot"
# TODO: jit?
gke-mcs = "service-%s@gcp-sa-mcsd"
monitoring-notifications = "service-%s@gcp-sa-monitoring-notification"
multicluster-ingress = "service-%s@gcp-sa-multiclusteringress"
multicluster-discovery = "service-%s@gcp-sa-mcsd"
notebooks = "service-%s@gcp-sa-notebooks"
pubsub = "service-%s@gcp-sa-pubsub"
secretmanager = "service-%s@gcp-sa-secretmanager"
servicemesh = "service-%s@gcp-sa-servicemesh"
sql = "service-%s@gcp-sa-cloud-sql"
sqladmin = "service-%s@gcp-sa-cloud-sql"
storage = "service-%s@gs-project-accounts"
}
service_accounts_default = {
compute = "${local.project.number}[email protected]"
gae = "${local.project.project_id}@appspot.gserviceaccount.com"
}
service_account_cloud_services = (
"${local.project.number}@cloudservices.gserviceaccount.com"
)
service_accounts_robots = merge(
{
for k, v in local._service_accounts_robot_services :
k => "${format(v, local.project.number)}.iam.gserviceaccount.com"
},
{
gke-mcs-importer = "${local.project.project_id}.svc.id.goog[gke-mcs/gke-mcs-importer]"
}
)
# JIT-ed service accounts are created without default roles granted, these needs to be assigned manually to them
# Roles can be found here: https://cloud.google.com/iam/docs/service-agents
# Remember to update "Service identities requiring manual IAM grants" in README.md when updating this list
service_accounts_jit_services = [
"apigee.googleapis.com", # grant roles/apigee.serviceAgent to apigee
"artifactregistry.googleapis.com", # grant roles/artifactregistry.serviceAgent to artifactregistry
"cloudasset.googleapis.com", # grant roles/cloudasset.serviceAgent to cloudasset
"cloudbuild.googleapis.com", # grant roles/cloudbuild.builds.builder to cloudbuild
"gkehub.googleapis.com", # grant roles/gkehub.serviceAgent to fleet
"multiclusteringress.googleapis.com", # grant roles/multiclusteringress.serviceAgent to multicluster-ingress
"pubsub.googleapis.com", # grant roles/pubsub.serviceAgent to pubsub
"meshconfig.googleapis.com", # grant roles/anthosservicemesh.serviceAgent to meshconfig
"secretmanager.googleapis.com", # no grants needed
"sqladmin.googleapis.com", # grant roles/cloudsql.serviceAgent to sqladmin (TODO: verify)
]
service_accounts_cmek_service_keys = distinct(flatten([
for s in keys(var.service_encryption_key_ids) : [
for ss in try(local._service_accounts_cmek_service_dependencies[s], [s]) : [
for key in var.service_encryption_key_ids[s] : {
service = ss
key = key
} if key != null
]
]
]))
}
data "google_storage_project_service_account" "gcs_sa" {
count = contains(var.services, "storage.googleapis.com") ? 1 : 0
project = local.project.project_id
depends_on = [google_project_service.project_services]
}
data "google_bigquery_default_service_account" "bq_sa" {
count = contains(var.services, "bigquery.googleapis.com") ? 1 : 0
project = local.project.project_id
depends_on = [google_project_service.project_services]
}
resource "google_project_service_identity" "servicenetworking" {
provider = google-beta
count = contains(var.services, "servicenetworking.googleapis.com") ? 1 : 0
project = local.project.project_id
service = "servicenetworking.googleapis.com"
depends_on = [google_project_service.project_services]
}
resource "google_project_iam_member" "servicenetworking" {
count = contains(var.services, "servicenetworking.googleapis.com") ? 1 : 0
project = local.project.project_id
role = "roles/servicenetworking.serviceAgent"
member = "serviceAccount:${google_project_service_identity.servicenetworking.0.email}"
}
# Secret Manager SA created just in time, we need to trigger the creation.
resource "google_project_service_identity" "jit_si" {
for_each = setintersection(var.services, local.service_accounts_jit_services)
provider = google-beta
project = local.project.project_id
service = each.value
depends_on = [google_project_service.project_services]
}
resource "google_kms_crypto_key_iam_member" "service_identity_cmek" {
for_each = {
for service_key in local.service_accounts_cmek_service_keys :
"${service_key.service}.${service_key.key}" => service_key
if service_key != service_key.key
}
crypto_key_id = each.value.key
role = "roles/cloudkms.cryptoKeyEncrypterDecrypter"
member = "serviceAccount:${local.service_accounts_robots[each.value.service]}"
depends_on = [
google_project.project,
google_project_service.project_services,
google_project_service_identity.jit_si,
data.google_bigquery_default_service_account.bq_sa,
data.google_project.project,
data.google_storage_project_service_account.gcs_sa,
]
}
resource "google_project_default_service_accounts" "default_service_accounts" {
count = upper(var.default_service_account) == "KEEP" ? 0 : 1
action = upper(var.default_service_account)
project = local.project.project_id
restore_policy = "REVERT_AND_IGNORE_FAILURE"
depends_on = [google_project_service.project_services]
}