-
Notifications
You must be signed in to change notification settings - Fork 56
/
mysql.tf
254 lines (214 loc) · 8.09 KB
/
mysql.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
* Copyright 2022 The Sigstore Authors
*
* 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.
*/
# MySQL setup
# Forked from https://github.com/GoogleCloudPlatform/gke-private-cluster-demo/blob/master/terraform/postgres.tf
// Enable required services for this module
resource "google_project_service" "service" {
for_each = toset([
"cloudresourcemanager.googleapis.com", // For IAM bindings. roles/resourcemanager.projectIamAdmin
"compute.googleapis.com", // For compute global address. roles/compute.networkAdmin
"iam.googleapis.com", // For creating service accounts and access control. roles/iam.serviceAccountAdmin
"secretmanager.googleapis.com", // For Secrets. roles/secretmanager.admin
"servicenetworking.googleapis.com", // For service networking connection. roles/servicenetworking.networksAdmin
"sqladmin.googleapis.com", // For Cloud SQL. roles/cloudsql.admin
])
project = var.project_id
service = each.key
// Do not disable the service on destroy. On destroy, we are going to
// destroy the project, but we need the APIs available to destroy the
// underlying resources.
disable_on_destroy = false
}
// Access to private cluster
resource "google_compute_global_address" "private_ip_address" {
name = format("%s-priv-ip", var.cluster_name)
project = var.project_id
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = var.network
depends_on = [google_project_service.service]
}
resource "google_service_networking_connection" "private_vpc_connection" {
network = var.network
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
depends_on = [google_compute_global_address.private_ip_address]
}
// Create the Google SA
resource "google_service_account" "dbuser_trillian" {
account_id = format("%s-mysql-sa", var.cluster_name)
display_name = "Trillian SA"
project = var.project_id
depends_on = [google_project_service.service]
}
// Attach cloudsql access permissions to the Google SA.
resource "google_project_iam_member" "db_admin_member_trillian" {
project = var.project_id
role = "roles/cloudsql.client"
member = "serviceAccount:${google_service_account.dbuser_trillian.email}"
depends_on = [google_service_account.dbuser_trillian]
}
resource "google_service_account_iam_member" "gke_sa_iam_member_trillian_logserver" {
service_account_id = google_service_account.dbuser_trillian.name
role = "roles/iam.workloadIdentityUser"
member = "serviceAccount:${var.project_id}.svc.id.goog[trillian-system/trillian-logserver]"
depends_on = [google_service_account.dbuser_trillian]
}
resource "google_project_iam_member" "logserver_iam" {
# // Give trillian logserver permission to export metrics to Stackdriver
for_each = toset([
"roles/logging.logWriter",
"roles/monitoring.metricWriter",
"roles/stackdriver.resourceMetadata.writer",
"roles/cloudtrace.agent"
])
project = var.project_id
role = each.key
member = "serviceAccount:${google_service_account.dbuser_trillian.email}"
depends_on = [google_service_account.dbuser_trillian]
}
resource "google_service_account_iam_member" "gke_sa_iam_member_trillian_logsigner" {
service_account_id = google_service_account.dbuser_trillian.name
role = "roles/iam.workloadIdentityUser"
member = "serviceAccount:${var.project_id}.svc.id.goog[trillian-system/trillian-logsigner]"
depends_on = [google_service_account.dbuser_trillian]
}
resource "random_id" "db_name_suffix" {
byte_length = 4
}
resource "google_sql_database_instance" "sigstore" {
project = var.project_id
name = var.instance_name != "" ? var.instance_name : format("%s-mysql-%s", var.cluster_name, random_id.db_name_suffix.hex)
database_version = var.database_version
region = var.region
# Set to false to delete this database
deletion_protection = var.deletion_protection
depends_on = [google_service_networking_connection.private_vpc_connection]
settings {
tier = var.tier
activation_policy = "ALWAYS"
availability_type = var.availability_type
ip_configuration {
ipv4_enabled = var.ipv4_enabled
private_network = var.network
require_ssl = var.require_ssl
}
database_flags {
name = "cloudsql_iam_authentication"
value = "on"
}
backup_configuration {
enabled = var.backup_enabled
binary_log_enabled = var.binary_log_backup_enabled
}
}
timeouts {
create = "10m"
update = "10m"
delete = "10m"
}
}
moved {
from = google_sql_database_instance.trillian
to = google_sql_database_instance.sigstore
}
resource "google_sql_database_instance" "read_replica" {
for_each = toset(var.replica_zones)
name = "${google_sql_database_instance.sigstore.name}-replica-${each.key}"
master_instance_name = google_sql_database_instance.sigstore.name
region = var.region
database_version = var.database_version
replica_configuration {
failover_target = false
}
settings {
tier = var.replica_tier
availability_type = "ZONAL"
ip_configuration {
ipv4_enabled = var.ipv4_enabled
private_network = var.network
require_ssl = var.require_ssl
}
database_flags {
name = "cloudsql_iam_authentication"
value = "on"
}
}
}
resource "google_sql_database" "trillian" {
name = var.db_name
project = var.project_id
instance = google_sql_database_instance.sigstore.name
collation = "utf8_general_ci"
depends_on = [google_sql_database_instance.sigstore]
}
resource "google_sql_database" "searchindexes" {
name = var.index_db_name
project = var.project_id
instance = google_sql_database_instance.sigstore.name
collation = "utf8_general_ci"
depends_on = [google_sql_database_instance.sigstore]
}
resource "random_id" "user-password" {
keepers = {
name = google_sql_database_instance.sigstore.name
}
byte_length = 8
depends_on = [google_sql_database_instance.sigstore]
}
resource "google_sql_user" "trillian" {
name = "trillian"
project = var.project_id
instance = google_sql_database_instance.sigstore.name
password = random_id.user-password.hex
host = "%"
depends_on = [google_sql_database_instance.sigstore]
}
resource "google_secret_manager_secret" "mysql-password" {
secret_id = "mysql-password"
replication {
auto {}
}
depends_on = [google_project_service.service]
}
resource "google_secret_manager_secret_version" "mysql-password" {
secret = google_secret_manager_secret.mysql-password.id
secret_data = google_sql_user.trillian.password
depends_on = [google_secret_manager_secret.mysql-password]
}
resource "google_secret_manager_secret" "mysql-user" {
secret_id = "mysql-user"
replication {
auto {}
}
depends_on = [google_project_service.service]
}
resource "google_secret_manager_secret_version" "mysql-user" {
secret = google_secret_manager_secret.mysql-user.id
secret_data = google_sql_user.trillian.name
}
resource "google_secret_manager_secret" "mysql-database" {
secret_id = "mysql-database"
replication {
auto {}
}
depends_on = [google_project_service.service]
}
resource "google_secret_manager_secret_version" "mysql-database" {
secret = google_secret_manager_secret.mysql-database.id
secret_data = google_sql_database.trillian.name
}