Skip to content

Commit 0621d50

Browse files
authored
Add database for rekor search indexes (#1085)
Update the mysql and rekor modules to instantiate a new database in the primary SQL instance for search index storage. The rekor IAM service accounts are bound to their GKE equivalents and given permission to access the Cloud SQL instance, which makes the cloud-sql-proxy sidecar in the Rekor deployment work. The "trillian" database instance resource is renamed to "sigstore" since the instance now encompasses two databases, one of which is not for trillian. The mysql module creates a trillian mysql user, which is not an IAM user. This user already has effectively admin grants on the SQL instance, so it is capable of connecting to the new instance and creating a new user named for the new database would not reduce the overall privileges, so we reuse the trillian mysql user for the new database. Signed-off-by: Colleen Murphy <[email protected]>
1 parent 6d618cc commit 0621d50

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

terraform/gcp/modules/mysql/mysql.tf

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ resource "random_id" "db_name_suffix" {
102102
byte_length = 4
103103
}
104104

105-
resource "google_sql_database_instance" "trillian" {
105+
resource "google_sql_database_instance" "sigstore" {
106106
project = var.project_id
107107
name = var.instance_name != "" ? var.instance_name : format("%s-mysql-%s", var.cluster_name, random_id.db_name_suffix.hex)
108108
database_version = var.database_version
@@ -142,11 +142,16 @@ resource "google_sql_database_instance" "trillian" {
142142
}
143143
}
144144

145+
moved {
146+
from = google_sql_database_instance.trillian
147+
to = google_sql_database_instance.sigstore
148+
}
149+
145150
resource "google_sql_database_instance" "read_replica" {
146151
for_each = toset(var.replica_zones)
147152

148-
name = "${google_sql_database_instance.trillian.name}-replica-${each.key}"
149-
master_instance_name = google_sql_database_instance.trillian.name
153+
name = "${google_sql_database_instance.sigstore.name}-replica-${each.key}"
154+
master_instance_name = google_sql_database_instance.sigstore.name
150155
region = var.region
151156
database_version = var.database_version
152157

@@ -174,27 +179,35 @@ resource "google_sql_database_instance" "read_replica" {
174179
resource "google_sql_database" "trillian" {
175180
name = var.db_name
176181
project = var.project_id
177-
instance = google_sql_database_instance.trillian.name
182+
instance = google_sql_database_instance.sigstore.name
178183
collation = "utf8_general_ci"
179-
depends_on = [google_sql_database_instance.trillian]
184+
depends_on = [google_sql_database_instance.sigstore]
185+
}
186+
187+
resource "google_sql_database" "searchindexes" {
188+
name = var.index_db_name
189+
project = var.project_id
190+
instance = google_sql_database_instance.sigstore.name
191+
collation = "utf8_general_ci"
192+
depends_on = [google_sql_database_instance.sigstore]
180193
}
181194

182195
resource "random_id" "user-password" {
183196
keepers = {
184-
name = google_sql_database_instance.trillian.name
197+
name = google_sql_database_instance.sigstore.name
185198
}
186199

187200
byte_length = 8
188-
depends_on = [google_sql_database_instance.trillian]
201+
depends_on = [google_sql_database_instance.sigstore]
189202
}
190203

191204
resource "google_sql_user" "trillian" {
192205
name = "trillian"
193206
project = var.project_id
194-
instance = google_sql_database_instance.trillian.name
207+
instance = google_sql_database_instance.sigstore.name
195208
password = random_id.user-password.hex
196209
host = "%"
197-
depends_on = [google_sql_database_instance.trillian]
210+
depends_on = [google_sql_database_instance.sigstore]
198211
}
199212

200213
resource "google_secret_manager_secret" "mysql-password" {
@@ -239,4 +252,3 @@ resource "google_secret_manager_secret_version" "mysql-database" {
239252
secret = google_secret_manager_secret.mysql-database.id
240253
secret_data = google_sql_database.trillian.name
241254
}
242-

terraform/gcp/modules/mysql/outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ output "trillian_serviceaccount" {
2222
// Used when setting up the GKE cluster to talk to MySQL.
2323
output "mysql_instance" {
2424
description = "The generated name of the Cloud SQL instance"
25-
value = google_sql_database_instance.trillian.name
25+
value = google_sql_database_instance.sigstore.name
2626
}
2727

2828
// Full connection string for the MySQL DB>
2929
output "mysql_connection" {
3030
description = "The connection string dynamically generated for storage inside the Kubernetes configmap"
31-
value = format("%s:%s:%s", var.project_id, var.region, google_sql_database_instance.trillian.name)
31+
value = format("%s:%s:%s", var.project_id, var.region, google_sql_database_instance.sigstore.name)
3232
}
3333

3434
// MySQL DB username.

terraform/gcp/modules/mysql/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ variable "db_name" {
104104
default = "trillian"
105105
}
106106

107+
variable "index_db_name" {
108+
type = string
109+
description = "Name for the MySQL database for search indexes."
110+
default = "searchindexes"
111+
}
112+
107113
variable "database_version" {
108114
type = string
109115
description = "MySQL database version."

terraform/gcp/modules/rekor/service_accounts.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,17 @@ resource "google_project_iam_member" "rekor_profiler_agent" {
4848
member = "serviceAccount:${google_service_account.rekor-sa.email}"
4949
depends_on = [google_service_account.rekor-sa]
5050
}
51+
52+
resource "google_service_account_iam_member" "gke_sa_iam_member_rekor_server" {
53+
service_account_id = google_service_account.rekor-sa.name
54+
role = "roles/iam.workloadIdentityUser"
55+
member = "serviceAccount:${var.project_id}.svc.id.goog[rekor-system/rekor-server]"
56+
depends_on = [google_service_account.rekor-sa]
57+
}
58+
59+
resource "google_project_iam_member" "db_admin_member_rekor" {
60+
project = var.project_id
61+
role = "roles/cloudsql.client"
62+
member = "serviceAccount:${google_service_account.rekor-sa.email}"
63+
depends_on = [google_service_account.rekor-sa]
64+
}

0 commit comments

Comments
 (0)