Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable SQL IAM usage for mysql-shard #1462

Merged
merged 4 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions terraform/gcp/modules/mysql-shard/mysql.tf
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,32 @@ resource "google_sql_user" "trillian" {
depends_on = [google_sql_database_instance.trillian]
}

// be sure to manually GRANT SELECT, INSERT, CREATE privileges for this user
resource "google_sql_user" "iam_user" {
name = var.cloud_sql_iam_service_account
instance = google_sql_database_instance.trillian.name
type = "CLOUD_IAM_SERVICE_ACCOUNT"
}

resource "google_project_iam_member" "db_iam_auth" {
project = var.project_id
role = "roles/cloudsql.instanceUser"
member = "serviceAccount:${var.cloud_sql_iam_service_account}"
}

resource "google_sql_user" "breakglass_iam_group" {
count = var.breakglass_iam_group != "" ? 1 : 0
name = var.breakglass_iam_group
instance = google_sql_database_instance.trillian.name
type = "CLOUD_IAM_GROUP"
}

resource "google_project_iam_member" "breakglass_iam_group_permissions" {
for_each = toset([
"roles/cloudsql.client",
"roles/cloudsql.instanceUser"
])
project = var.project_id
role = each.key
member = var.breakglass_iam_group != "" ? "group:${var.breakglass_iam_group}" : null
}
10 changes: 10 additions & 0 deletions terraform/gcp/modules/mysql-shard/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,13 @@ variable "collation" {
description = "collation setting for database"
default = "utf8mb3_general_ci"
}

variable "cloud_sql_iam_service_account" {
type = string
description = "name of Cloud SQL IAM service account to create database user for"
}

variable "breakglass_iam_group" {
type = string
description = "name of Cloud IAM group to use for database access in case of emergency"
}
4 changes: 2 additions & 2 deletions terraform/gcp/modules/mysql/mysql.tf
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,12 @@ resource "google_sql_user" "breakglass_iam_group" {
type = "CLOUD_IAM_GROUP"
}

resource "google_project_iam_binding" "breakglass_iam_group_permissions" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iam binding was authoritative, was there anything wiped out?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because mysql-shard reuses the same mysql code, it isn't happy with having two authoritative clauses in the same plan.

resource "google_project_iam_member" "breakglass_iam_group_permissions" {
for_each = toset([
"roles/cloudsql.client",
"roles/cloudsql.instanceUser"
])
project = var.project_id
role = each.key
members = var.breakglass_iam_group != "" ? ["group:${var.breakglass_iam_group}"] : []
member = var.breakglass_iam_group != "" ? "group:${var.breakglass_iam_group}" : null
}
4 changes: 4 additions & 0 deletions terraform/gcp/modules/sigstore/sigstore.tf
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ module "ctlog_shards" {
binary_log_backup_enabled = var.mysql_binary_log_backup_enabled
collation = var.mysql_collation

cloud_sql_iam_service_account = module.mysql.trillian_serviceaccount
breakglass_iam_group = var.breakglass_sql_iam_group

depends_on = [
module.gke-cluster,
Expand Down Expand Up @@ -432,6 +434,8 @@ module "standalone_mysqls" {
binary_log_backup_enabled = var.mysql_binary_log_backup_enabled
collation = var.mysql_collation

cloud_sql_iam_service_account = module.mysql.trillian_serviceaccount
breakglass_iam_group = var.breakglass_sql_iam_group

depends_on = [
module.gke-cluster,
Expand Down
Loading