-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.tf
193 lines (153 loc) · 4.69 KB
/
main.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
##################################
## Google Cloud Identity - Main ##
##################################
# Create a local variables
locals {
sa_name = "sa-${var.prefix}-${var.app_name}-${var.environment}"
owners_group_name = lower("${var.prefix}-${var.app_name}-${var.environment}-owners")
editors_group_name = lower("${var.prefix}-${var.app_name}-${var.environment}-editors")
viewers_group_name = lower("${var.prefix}-${var.app_name}-${var.environment}-viewers")
}
# Create a GCP Service Account
resource "google_service_account" "service-account" {
account_id = local.sa_name
display_name = local.sa_name
description = "Terraform Service Account for ${var.app_name}"
project = var.project_id
}
# Create a GCP IAM Policy for Service Account
data "google_iam_policy" "service-account-iam-policy" {
binding {
role = "roles/owner"
members = [
"serviceAccount:${google_service_account.service-account.email}",
]
}
}
# Assign IAM roles to the Service Account
resource "google_service_account_iam_policy" "service-account-iam" {
service_account_id = google_service_account.service-account.id
policy_data = data.google_iam_policy.service-account-iam-policy.policy_data
}
# Generate the Service Account Key
resource "google_service_account_key" "service-account-key" {
depends_on = [google_service_account.service-account]
service_account_id = google_service_account.service-account.id
}
# Save Service Account to JSON (DEBUG)
resource "local_file" "service-account-key-json" {
content = base64decode(google_service_account_key.service-account-key.private_key)
filename = "${path.module}/${local.sa_name}.json"
}
# Create Owners Group
resource "google_cloud_identity_group" "owners-group" {
provider = google-beta
display_name = local.owners_group_name
parent = "customers/${var.customer_id}"
group_key {
id = "${local.owners_group_name}@${var.company_domain}"
}
labels = {
"cloudidentity.googleapis.com/groups.discussion_forum" = ""
}
}
# Create Editors Group
resource "google_cloud_identity_group" "editors-group" {
provider = google-beta
display_name = local.editors_group_name
parent = "customers/${var.customer_id}"
group_key {
id = "${local.editors_group_name}@${var.company_domain}"
}
labels = {
"cloudidentity.googleapis.com/groups.discussion_forum" = ""
}
}
# Create Viewers Group
resource "google_cloud_identity_group" "viewers-group" {
provider = google-beta
display_name = local.viewers_group_name
parent = "customers/${var.customer_id}"
group_key {
id = "${local.viewers_group_name}@${var.company_domain}"
}
labels = {
"cloudidentity.googleapis.com/groups.discussion_forum" = ""
}
}
# Assign Groups to the Project
resource "google_project_iam_policy" "group-iam-policy" {
depends_on = [
google_cloud_identity_group.owners-group,
google_cloud_identity_group.editors-group,
google_cloud_identity_group.viewers-group
]
project = var.project_id
policy_data = data.google_iam_policy.groups-iam-policy.policy_data
}
# Groups Policy
data "google_iam_policy" "groups-iam-policy" {
binding {
role = "roles/owner"
members = [
"group:${local.owners_group_name}@${var.company_domain}",
]
}
binding {
role = "roles/editor"
members = [
"group:${local.editors_group_name}@${var.company_domain}",
]
}
binding {
role = "roles/reader"
members = [
"group:${local.viewers_group_name}@${var.company_domain}",
]
}
}
# Add members to the Owners Group
resource "google_cloud_identity_group_membership" "owners-group-membership" {
provider = google-beta
group = google_cloud_identity_group.owners-group.id
for_each = toset(var.owners_members)
member_key {
id = each.value
}
roles {
name = "MEMBER"
}
roles {
name = "MANAGER"
}
}
# Add members to the Editors Group
resource "google_cloud_identity_group_membership" "editors-group-membership" {
provider = google-beta
group = google_cloud_identity_group.editors-group.id
for_each = toset(var.editors_members)
member_key {
id = each.value
}
roles {
name = "MEMBER"
}
roles {
name = "MANAGER"
}
}
# Add members to the Viewers Group
resource "google_cloud_identity_group_membership" "viewers-group-membership" {
provider = google-beta
group = google_cloud_identity_group.viewers-group.id
for_each = toset(var.viewers_members)
member_key {
id = each.value
}
roles {
name = "MEMBER"
}
roles {
name = "MANAGER"
}
}