Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 2958abc

Browse files
authored
fix/msp/postgresqlroles: wait for databases to be provisioned (#63362)
Wait for databases to be provisioned before granting database-specific roles to the operator access user. ## Test plan Re-apply fixed https://sourcegraph.slack.com/archives/C05E2LHPQLX/p1718850688397579, indicating a race condition on database creation. Diff looks good: ```diff @@ -1447,10 +1472,15 @@ "path": "cloudrun/cloudrun-postgresqlroles-msp_iam-operator_access_service_account_table_grant", "uniqueId": "cloudrun-postgresqlroles-msp_iam-operator_access_service_account_table_grant" } }, "database": "msp_iam", + "depends_on": [ + "google_sql_database.postgresql-database-enterprise-portal", + "google_sql_database.postgresql-database-enterprise_portal", + "google_sql_database.postgresql-database-msp_iam" + ], "object_type": "table", "objects": [ ], "privileges": [ "SELECT" ``` ## Changelog - MSP Cloud SQL: Fix race condition between database creation and role grants for the read-only operator access user
1 parent 0505269 commit 2958abc

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

dev/managedservicesplatform/internal/resource/cloudsql/cloudsql.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ type Output struct {
3434
// OperatorAccessUser is the SQL user corresponding to the operator access
3535
// service account.
3636
OperatorAccessUser sqluser.SqlUser
37+
// Databases created in the Cloud SQL instance, used for resources that
38+
// depend on database creation.
39+
Databases []cdktf.ITerraformDependable
3740
}
3841

3942
type Config struct {
@@ -209,6 +212,7 @@ func New(scope constructs.Construct, id resourceid.ID, config Config) (*Output,
209212
instance, config.WorkloadIdentity, databaseResources),
210213
OperatorAccessUser: newSqlUserForIdentity(scope, id.TerraformID("operator_access_service_account_user"),
211214
instance, config.OperatorAccessIdentity, databaseResources),
215+
Databases: databaseResources,
212216
}, nil
213217
}
214218

dev/managedservicesplatform/internal/resource/postgresqlroles/postgresqlroles.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,18 @@ func New(scope constructs.Construct, id resourceid.ID, config Config) (*Output,
5959
// Operator access: grant restricted read-only permissions, based on
6060
// https://github.com/sourcegraph/deploy-sourcegraph-managed/blob/ded74a806bb6d1925cb894a8755ed52db7585a4f/modules/terraform-managed-instance-new/sql.tf#L153-L179
6161
for _, db := range config.Databases {
62-
_ = grant.NewGrant(scope, id.Group(db).TerraformID("operator_access_service_account_connect_grant"), &grant.GrantConfig{
62+
id := id.Group(db)
63+
_ = grant.NewGrant(scope, id.TerraformID("operator_access_service_account_connect_grant"), &grant.GrantConfig{
6364
Provider: pgProvider,
6465
Database: &db,
6566
Role: config.CloudSQL.OperatorAccessUser.Name(),
6667
ObjectType: pointers.Ptr("database"),
6768
Privileges: pointers.Ptr(pointers.Slice([]string{
6869
"CONNECT",
6970
})),
71+
DependsOn: &config.CloudSQL.Databases,
7072
})
71-
_ = grant.NewGrant(scope, id.Group(db).TerraformID("operator_access_service_account_table_grant"), &grant.GrantConfig{
73+
_ = grant.NewGrant(scope, id.TerraformID("operator_access_service_account_table_grant"), &grant.GrantConfig{
7274
Provider: pgProvider,
7375
Database: &db,
7476
Role: config.CloudSQL.OperatorAccessUser.Name(),
@@ -80,6 +82,7 @@ func New(scope constructs.Construct, id resourceid.ID, config Config) (*Output,
8082
Privileges: pointers.Ptr(pointers.Slice([]string{
8183
"SELECT",
8284
})),
85+
DependsOn: &config.CloudSQL.Databases,
8386
})
8487
}
8588

0 commit comments

Comments
 (0)