-
Notifications
You must be signed in to change notification settings - Fork 0
/
sa.tf
167 lines (143 loc) · 4.96 KB
/
sa.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
163
164
165
166
167
/**
* 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.
*/
locals {
parent_type = var.parent_folder == "" ? "organization" : "folder"
parent_id = var.parent_folder == "" ? var.org_id : var.parent_folder
granular_sa = {
"org" = "Foundation Organization SA. Managed by Terraform.",
"env" = "Foundation Environment SA. Managed by Terraform.",
"net" = "Foundation Network SA. Managed by Terraform.",
"proj" = "Foundation Projects SA. Managed by Terraform.",
}
granular_sa_org_level_roles = {
"org" = [
"roles/orgpolicy.policyAdmin",
"roles/logging.configWriter",
"roles/resourcemanager.organizationAdmin",
"roles/securitycenter.notificationConfigEditor",
"roles/resourcemanager.organizationViewer",
"roles/accesscontextmanager.policyAdmin",
"roles/essentialcontacts.admin",
],
"net" = [
"roles/accesscontextmanager.policyAdmin",
"roles/compute.xpnAdmin",
],
"proj" = [
"roles/accesscontextmanager.policyAdmin",
"roles/serviceusage.serviceUsageConsumer"
],
}
granular_sa_parent_level_roles = {
"org" = [
"roles/resourcemanager.folderAdmin",
],
"env" = [
"roles/resourcemanager.folderAdmin"
],
"net" = [
"roles/resourcemanager.folderViewer",
"roles/compute.networkAdmin",
"roles/compute.securityAdmin",
"roles/compute.orgSecurityPolicyAdmin",
"roles/compute.orgSecurityResourceAdmin",
"roles/dns.admin",
],
"proj" = [
"roles/resourcemanager.folderViewer",
"roles/resourcemanager.folderIamAdmin",
"roles/compute.networkAdmin",
"roles/compute.xpnAdmin",
],
}
granular_sa_seed_project = {
"org" = [
"roles/storage.objectAdmin",
],
"env" = [
"roles/storage.objectAdmin"
],
"net" = [
"roles/storage.objectAdmin",
],
"proj" = [
"roles/storage.objectAdmin",
],
}
}
resource "google_service_account" "terraform-env-sa" {
for_each = local.granular_sa
project = module.seed_bootstrap.seed_project_id
account_id = "terraform-${each.key}-sa"
display_name = each.value
}
module "org_iam_member" {
source = "./modules/parent-iam-member"
for_each = local.granular_sa_org_level_roles
member = "serviceAccount:${google_service_account.terraform-env-sa[each.key].email}"
parent_type = "organization"
parent_id = var.org_id
roles = each.value
}
module "parent_iam_member" {
source = "./modules/parent-iam-member"
for_each = local.granular_sa_parent_level_roles
member = "serviceAccount:${google_service_account.terraform-env-sa[each.key].email}"
parent_type = local.parent_type
parent_id = local.parent_id
roles = each.value
}
module "project_iam_member" {
source = "./modules/parent-iam-member"
for_each = local.granular_sa_seed_project
member = "serviceAccount:${google_service_account.terraform-env-sa[each.key].email}"
parent_type = "project"
parent_id = module.seed_bootstrap.seed_project_id
roles = each.value
}
resource "google_billing_account_iam_member" "tf_billing_user" {
for_each = local.granular_sa
billing_account_id = var.billing_account
role = "roles/billing.user"
member = "serviceAccount:${google_service_account.terraform-env-sa[each.key].email}"
depends_on = [
google_service_account.terraform-env-sa
]
}
resource "google_billing_account_iam_member" "billing_admin_user" {
for_each = local.granular_sa
billing_account_id = var.billing_account
role = "roles/billing.admin"
member = "serviceAccount:${google_service_account.terraform-env-sa[each.key].email}"
depends_on = [
google_billing_account_iam_member.tf_billing_user
]
}
resource "google_artifact_registry_repository_iam_member" "terraform_sa_artifact_registry_reader" {
for_each = local.granular_sa
project = module.tf_source.cloudbuild_project_id
location = var.default_region
repository = local.gar_repository
role = "roles/artifactregistry.reader"
member = "serviceAccount:${google_service_account.terraform-env-sa[each.key].email}"
}
resource "google_sourcerepo_repository_iam_member" "member" {
for_each = local.granular_sa
project = module.tf_source.cloudbuild_project_id
repository = module.tf_source.csr_repos["gcp-policies"].name
role = "roles/viewer"
member = "serviceAccount:${google_service_account.terraform-env-sa[each.key].email}"
}