Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: support GCP Cloud SQL IAM metadata DB auth (#20681)
p0nyf2dc4295bde7
diff --git a/backend/store/db_connection.go b/backend/store/db_connection.goindex 013f5e44aa..915c90da10 100644--- a/backend/store/db_connection.go+++ b/backend/store/db_connection.go@@ -13,6 +13,7 @@ import ("github.com/jackc/pgx/v5""github.com/jackc/pgx/v5/stdlib""github.com/pkg/errors"+ "go.uber.org/multierr""github.com/bytebase/bytebase/backend/common/qb""github.com/bytebase/bytebase/backend/store/dbauth"@@ -22,6 +23,7 @@ import (type DBConnectionManager struct {mu sync.Mutexdb *sql.DB+ cleanup func() errorpgURLOrFile string // Either a PostgreSQL URL or a file pathwatcher *fsnotify.WatcherstopWatcher chan struct{}@@ -61,12 +63,13 @@ func (m *DBConnectionManager) Initialize(ctx context.Context) error {}// Create initial connection- db, err := createConnectionWithTracer(ctx, pgURL)+ conn, err := createConnectionWithTracer(ctx, pgURL)if err != nil {return err}- m.db = db+ m.db = conn.db+ m.cleanup = conn.cleanupreturn nil}@@ -89,8 +92,9 @@ func (m *DBConnectionManager) Close() error {return nil}- err := m.db.Close()+ err := closeMetadataDBConnection(&metadataDBConnection{db: m.db, cleanup: m.cleanup})m.db = nil+ m.cleanup = nilreturn err}@@ -148,7 +152,7 @@ func (m *DBConnectionManager) reloadConnection(ctx context.Context, filePath strslog.Info("PG URL file content updated, reconnecting database")// Create new connection first (zero downtime)- newDB, err := createConnectionWithTracer(ctx, newURL)+ newConn, err := createConnectionWithTracer(ctx, newURL)if err != nil {slog.Error("Failed to create new database connection", "error", err)return@@ -157,7 +161,9 @@ func (m *DBConnectionManager) reloadConnection(ctx context.Context, filePath str// Swap connections atomicallym.mu.Lock()oldDB := m.db- m.db = newDB+ oldCleanup := m.cleanup+ m.db = newConn.db+ m.cleanup = newConn.cleanupm.mu.Unlock()// Gracefully drain old connections and force close after 1 hour@@ -170,7 +176,7 @@ func (m *DBConnectionManager) reloadConnection(ctx context.Context, filePath str// Force close after 1 hour as a safety measurego func() {time.Sleep(1 * time.Hour)- if err := oldDB.Close(); err != nil {+ if err := closeMetadataDBConnection(&metadataDBConnection{db: oldDB, cleanup: oldCleanup}); err != nil {slog.Warn("Failed to force close old database connection", "error", err)}}()@@ -236,13 +242,37 @@ func readURLFromFile(path string) (string, error) {return strings.TrimSpace(string(content)), nil}-func createConnectionWithTracer(ctx context.Context, pgURL string) (*sql.DB, error) {+type metadataDBConnection struct {+ db *sql.DB+ cleanup func() error+}++func (c *metadataDBConnection) close() error {+ return closeMetadataDBConnection(c)+}++func closeMetadataDBConnection(conn *metadataDBConnection) error {+ if conn == nil {+ return nil+ }++ var err error+ if conn.db != nil {+ err = conn.db.Close()+ }+ if conn.cleanup != nil {+ err = multierr.Append(err, conn.cleanup())+ }+ return err+}++func createConnectionWithTracer(ctx context.Context, pgURL string) (*metadataDBConnection, error) {pgxConfig, err := pgx.ParseConfig(pgURL)if err != nil {return nil, errors.Wrap(err, "failed to parse database URL")}- openOptions, err := dbauth.Configure(ctx, pgxConfig)+ openOptions, cleanup, err := dbauth.Configure(ctx, pgxConfig)if err != nil {return nil, err}@@ -252,7 +282,7 @@ func createConnectionWithTracer(ctx context.Context, pgURL string) (*sql.DB, err// Validate connectionif err := db.PingContext(ctx); err != nil {- db.Close()+ _ = closeMetadataDBConnection(&metadataDBConnection{db: db, cleanup: cleanup})return nil, errors.Wrap(err, "failed to ping database")}@@ -261,22 +291,22 @@ func createConnectionWithTracer(ctx context.Context, pgURL string) (*sql.DB, errq := qb.Q().Space("SHOW max_connections")sql, args, err := q.ToSQL()if err != nil {- db.Close()+ _ = closeMetadataDBConnection(&metadataDBConnection{db: db, cleanup: cleanup})return nil, errors.Wrap(err, "failed to build sql")}if err := db.QueryRowContext(ctx, sql, args...).Scan(&maxConns); err != nil {- db.Close()+ _ = closeMetadataDBConnection(&metadataDBConnection{db: db, cleanup: cleanup})return nil, errors.Wrap(err, "failed to get max_connections")}q = qb.Q().Space("SHOW superuser_reserved_connections")sql, args, err = q.ToSQL()if err != nil {- db.Close()+ _ = closeMetadataDBConnection(&metadataDBConnection{db: db, cleanup: cleanup})return nil, errors.Wrap(err, "failed to build sql")}if err := db.QueryRowContext(ctx, sql, args...).Scan(&reservedConns); err != nil {- db.Close()+ _ = closeMetadataDBConnection(&metadataDBConnection{db: db, cleanup: cleanup})return nil, errors.Wrap(err, "failed to get superuser_reserved_connections")}@@ -286,5 +316,5 @@ func createConnectionWithTracer(ctx context.Context, pgURL string) (*sql.DB, err}db.SetMaxOpenConns(maxOpenConns)- return db, nil+ return &metadataDBConnection{db: db, cleanup: cleanup}, nil}diff --git a/backend/store/db_connection_lifecycle_test.go b/backend/store/db_connection_lifecycle_test.gonew file mode 100644index 0000000000..9e73ff8f42--- /dev/null+++ b/backend/store/db_connection_lifecycle_test.go@@ -0,0 +1,35 @@+package store++import (+ "errors"+ "testing"++ "github.com/stretchr/testify/require"+)++func TestMetadataDBConnectionCloseRunsCleanup(t *testing.T) {+ cleanupCalls := 0+ conn := &metadataDBConnection{+ cleanup: func() error {+ cleanupCalls+++ return nil+ },+ }++ err := conn.close()++ require.NoError(t, err)+ require.Equal(t, 1, cleanupCalls)+}++func TestMetadataDBConnectionCloseReturnsCleanupError(t *testing.T) {+ conn := &metadataDBConnection{+ cleanup: func() error {+ return errors.New("cleanup failed")+ },+ }++ err := conn.close()++ require.ErrorContains(t, err, "cleanup failed")+}diff --git a/backend/store/db_connection_test.go b/backend/store/db_connection_test.goindex 387068a983..e793b8a5c8 100644--- a/backend/store/db_connection_test.go+++ b/backend/store/db_connection_test.go@@ -32,6 +32,16 @@ func TestIsFilePath(t *testing.T) {in: "host=example.com port=5432 user=bb dbname=bytebase bytebase_aws_rds_iam=true bytebase_aws_region=us-east-1 sslmode=verify-full",want: false,},+ {+ name: "keyword value GCP IAM DSN",+ in: "user=bb dbname=bytebase bytebase_gcp_cloud_sql_iam=true bytebase_gcp_cloud_sql_instance_connection_name=project:region:instance",+ want: false,+ },+ {+ name: "keyword value GCP IAM DSN with runtime parameter first",+ in: "bytebase_gcp_cloud_sql_iam=true bytebase_gcp_cloud_sql_instance_connection_name=project:region:instance user=bb dbname=bytebase",+ want: false,+ },{name: "keyword value DSN with runtime parameter first",in: "application_name=bytebase host=example.com port=5432 user=bb dbname=bytebase",diff --git a/backend/store/dbauth/auth.go b/backend/store/dbauth/auth.goindex 8e4f7a83fd..753a021f08 100644--- a/backend/store/dbauth/auth.go+++ b/backend/store/dbauth/auth.go@@ -5,29 +5,67 @@ import ("github.com/jackc/pgx/v5""github.com/jackc/pgx/v5/stdlib"+ "github.com/pkg/errors")// Configure applies metadata database authentication settings to pgxConfig.-func Configure(ctx context.Context, pgxConfig *pgx.ConnConfig) ([]stdlib.OptionOpenDB, error) {- authConfig, err := awsConfigFromPGXConfig(pgxConfig)+func Configure(ctx context.Context, pgxConfig *pgx.ConnConfig) ([]stdlib.OptionOpenDB, func() error, error) {+ var configures []func() ([]stdlib.OptionOpenDB, func() error, error)++ awsConfig, err := awsConfigFromPGXConfig(pgxConfig)+ if err != nil {+ return nil, nil, err+ }+ if awsConfig != nil && awsConfig.enabled {+ configures = append(configures, func() ([]stdlib.OptionOpenDB, func() error, error) {+ return configureAWS(ctx, awsConfig)+ })+ }++ gcpConfig, err := gcpConfigFromPGXConfig(pgxConfig)if err != nil {- return nil, err+ return nil, nil, err}- if authConfig == nil || !authConfig.enabled {- return nil, nil+ if gcpConfig != nil && gcpConfig.enabled {+ configures = append(configures, func() ([]stdlib.OptionOpenDB, func() error, error) {+ return configureGCP(ctx, pgxConfig, gcpConfig)+ })}+ return configureSingleProvider(configures)+}++func configureSingleProvider(configures []func() ([]stdlib.OptionOpenDB, func() error, error)) ([]stdlib.OptionOpenDB, func() error, error) {+ switch len(configures) {+ case 0:+ return nil, nil, nil+ case 1:+ return configures[0]()+ default:+ return nil, nil, errors.New("multiple metadata database IAM auth providers are enabled")+ }+}++func configureAWS(ctx context.Context, authConfig *awsConfig) ([]stdlib.OptionOpenDB, func() error, error) {tokenProvider, err := newAWSMetadataDBTokenProvider(ctx, authConfig.region)if err != nil {- return nil, err+ return nil, nil, err+ }+ return awsOpenOptions(authConfig, tokenProvider), nil, nil+}++func configureGCP(ctx context.Context, pgxConfig *pgx.ConnConfig, authConfig *gcpConfig) ([]stdlib.OptionOpenDB, func() error, error) {+ dialer, err := newGCPMetadataDBDialer(ctx)+ if err != nil {+ return nil, nil, err}- return awsOpenOptions(authConfig, tokenProvider), nil+ return configureGCPWithDialer(pgxConfig, authConfig, dialer)}// IsKeywordValueRuntimeParam reports whether key is a Bytebase metadata DB auth runtime parameter.func IsKeywordValueRuntimeParam(key string) bool {switch key {- case awsRDSIAMParam, awsRegionParam:+ case awsRDSIAMParam, awsRegionParam, gcpCloudSQLIAMParam, gcpCloudSQLInstanceConnectionNameParam:return truedefault:return falsediff --git a/backend/store/dbauth/aws_test.go b/backend/store/dbauth/aws_test.goindex 2279d92c61..260e09df47 100644--- a/backend/store/dbauth/aws_test.go+++ b/backend/store/dbauth/aws_test.go@@ -225,16 +225,17 @@ func TestAWSOpenOptions(t *testing.T) {func TestConfigure(t *testing.T) {pgxConfig := mustParsePGXConfig(t, "postgres://[email protected]:5432/bytebase?sslmode=verify-full")- options, err := Configure(context.Background(), pgxConfig)+ options, cleanup, err := Configure(context.Background(), pgxConfig)require.NoError(t, err)require.Empty(t, options)+ require.Nil(t, cleanup)}func TestConfigureRequiresAWSRegion(t *testing.T) {pgxConfig := mustParsePGXConfig(t, "postgres://[email protected]:5432/bytebase?sslmode=verify-full&bytebase_aws_rds_iam=true")- _, err := Configure(context.Background(), pgxConfig)+ _, _, err := Configure(context.Background(), pgxConfig)require.ErrorContains(t, err, "bytebase_aws_region is required")}@@ -242,10 +243,11 @@ func TestConfigureRequiresAWSRegion(t *testing.T) {func TestConfigureEnabledAWSReturnsOpenOption(t *testing.T) {pgxConfig := mustParsePGXConfig(t, "postgres://[email protected]:5432/bytebase?sslmode=verify-full&bytebase_aws_rds_iam=true&bytebase_aws_region=us-east-1")- options, err := Configure(context.Background(), pgxConfig)+ options, cleanup, err := Configure(context.Background(), pgxConfig)require.NoError(t, err)require.Len(t, options, 1)+ require.Nil(t, cleanup)}func TestIsKeywordValueRuntimeParam(t *testing.T) {diff --git a/backend/store/dbauth/gcp.go b/backend/store/dbauth/gcp.gonew file mode 100644index 0000000000..2f88da237d--- /dev/null+++ b/backend/store/dbauth/gcp.go@@ -0,0 +1,88 @@+package dbauth++import (+ "context"+ "net"++ "cloud.google.com/go/cloudsqlconn"+ "github.com/jackc/pgx/v5"+ "github.com/jackc/pgx/v5/stdlib"+ "github.com/pkg/errors"+)++const (+ gcpCloudSQLIAMParam = "bytebase_gcp_cloud_sql_iam"+ gcpCloudSQLInstanceConnectionNameParam = "bytebase_gcp_cloud_sql_instance_connection_name"+)++type gcpConfig struct {+ enabled bool+ instanceConnectionName string+}++type gcpDialer interface {+ Dial(ctx context.Context, instanceConnectionName string) (net.Conn, error)+ Close() error+}++type gcpMetadataDBDialer struct {+ dialer *cloudsqlconn.Dialer+}++func newGCPMetadataDBDialer(ctx context.Context) (*gcpMetadataDBDialer, error) {+ dialer, err := cloudsqlconn.NewDialer(ctx, cloudsqlconn.WithIAMAuthN())+ if err != nil {+ return nil, errors.Wrap(err, "failed to create GCP Cloud SQL dialer")+ }+ return &gcpMetadataDBDialer{dialer: dialer}, nil+}++func (d *gcpMetadataDBDialer) Dial(ctx context.Context, instanceConnectionName string) (net.Conn, error) {+ return d.dialer.Dial(ctx, instanceConnectionName)+}++func (d *gcpMetadataDBDialer) Close() error {+ return d.dialer.Close()+}++func gcpConfigFromPGXConfig(pgxConfig *pgx.ConnConfig) (*gcpConfig, error) {+ iamEnabled := pgxConfig.RuntimeParams[gcpCloudSQLIAMParam] == "true"+ instanceConnectionName := pgxConfig.RuntimeParams[gcpCloudSQLInstanceConnectionNameParam]+ delete(pgxConfig.RuntimeParams, gcpCloudSQLIAMParam)+ delete(pgxConfig.RuntimeParams, gcpCloudSQLInstanceConnectionNameParam)++ if !iamEnabled {+ return nil, nil+ }++ if instanceConnectionName == "" {+ return nil, errors.Errorf("%s is required when metadata database GCP Cloud SQL IAM auth is enabled", gcpCloudSQLInstanceConnectionNameParam)+ }++ if pgxConfig.User == "" {+ return nil, errors.New("database user is required when metadata database GCP Cloud SQL IAM auth is enabled")+ }++ return &gcpConfig{+ enabled: true,+ instanceConnectionName: instanceConnectionName,+ }, nil+}++func applyGCPConfig(pgxConfig *pgx.ConnConfig, authConfig *gcpConfig, dialer gcpDialer) {+ if authConfig == nil || !authConfig.enabled {+ return+ }+ pgxConfig.DialFunc = func(ctx context.Context, _, _ string) (net.Conn, error) {+ conn, err := dialer.Dial(ctx, authConfig.instanceConnectionName)+ if err != nil {+ return nil, errors.Wrapf(err, "failed to dial metadata database GCP Cloud SQL instance %q", authConfig.instanceConnectionName)+ }+ return conn, nil+ }+}++func configureGCPWithDialer(pgxConfig *pgx.ConnConfig, authConfig *gcpConfig, dialer gcpDialer) ([]stdlib.OptionOpenDB, func() error, error) {+ applyGCPConfig(pgxConfig, authConfig, dialer)+ return nil, dialer.Close, nil+}diff --git a/backend/store/dbauth/gcp_test.go b/backend/store/dbauth/gcp_test.gonew file mode 100644index 0000000000..92696548a1--- /dev/null+++ b/backend/store/dbauth/gcp_test.go@@ -0,0 +1,138 @@+package dbauth++import (+ "context"+ "net"+ "testing"++ "github.com/jackc/pgx/v5"+ "github.com/stretchr/testify/require"+)++type fakeGCPDialer struct {+ calls []fakeGCPDialerCall+ closeCalls int+ err error+}++type fakeGCPDialerCall struct {+ ctx context.Context+ instanceConnectionName string+}++func (d *fakeGCPDialer) Dial(ctx context.Context, instanceConnectionName string) (net.Conn, error) {+ d.calls = append(d.calls, fakeGCPDialerCall{+ ctx: ctx,+ instanceConnectionName: instanceConnectionName,+ })+ return nil, d.err+}++func (d *fakeGCPDialer) Close() error {+ d.closeCalls+++ return nil+}++func TestGCPConfigFromPGXConfigDisabled(t *testing.T) {+ pgxConfig := mustParsePGXConfig(t, "postgres://bb:[email protected]:5432/bytebase?sslmode=disable")++ authConfig, err := gcpConfigFromPGXConfig(pgxConfig)++ require.NoError(t, err)+ require.Nil(t, authConfig)+}++func TestGCPConfigFromPGXConfigEnabledURL(t *testing.T) {+ pgxConfig := mustParsePGXConfig(t, "postgres://[email protected]:5432/bytebase?bytebase_gcp_cloud_sql_iam=true&bytebase_gcp_cloud_sql_instance_connection_name=project:region:instance")++ authConfig, err := gcpConfigFromPGXConfig(pgxConfig)++ require.NoError(t, err)+ require.NotNil(t, authConfig)+ require.True(t, authConfig.enabled)+ require.Equal(t, "project:region:instance", authConfig.instanceConnectionName)+ require.NotContains(t, pgxConfig.RuntimeParams, gcpCloudSQLIAMParam)+ require.NotContains(t, pgxConfig.RuntimeParams, gcpCloudSQLInstanceConnectionNameParam)+}++func TestGCPConfigFromPGXConfigEnabledKeywordValue(t *testing.T) {+ pgxConfig := mustParsePGXConfig(t, "user=bb dbname=bytebase bytebase_gcp_cloud_sql_iam=true bytebase_gcp_cloud_sql_instance_connection_name=project:region:instance")++ authConfig, err := gcpConfigFromPGXConfig(pgxConfig)++ require.NoError(t, err)+ require.NotNil(t, authConfig)+ require.True(t, authConfig.enabled)+ require.Equal(t, "project:region:instance", authConfig.instanceConnectionName)+ require.NotContains(t, pgxConfig.RuntimeParams, gcpCloudSQLIAMParam)+ require.NotContains(t, pgxConfig.RuntimeParams, gcpCloudSQLInstanceConnectionNameParam)+}++func TestGCPConfigFromPGXConfigStripsDisabledParams(t *testing.T) {+ pgxConfig := mustParsePGXConfig(t, "postgres://bb:[email protected]:5432/bytebase?bytebase_gcp_cloud_sql_iam=false&bytebase_gcp_cloud_sql_instance_connection_name=project:region:instance")++ authConfig, err := gcpConfigFromPGXConfig(pgxConfig)++ require.NoError(t, err)+ require.Nil(t, authConfig)+ require.NotContains(t, pgxConfig.RuntimeParams, gcpCloudSQLIAMParam)+ require.NotContains(t, pgxConfig.RuntimeParams, gcpCloudSQLInstanceConnectionNameParam)+}++func TestGCPConfigFromPGXConfigRequiresInstanceConnectionName(t *testing.T) {+ pgxConfig := mustParsePGXConfig(t, "postgres://[email protected]:5432/bytebase?bytebase_gcp_cloud_sql_iam=true")++ _, err := gcpConfigFromPGXConfig(pgxConfig)++ require.ErrorContains(t, err, "bytebase_gcp_cloud_sql_instance_connection_name is required")+}++func TestApplyGCPConfigSetsDialFunc(t *testing.T) {+ pgxConfig := &pgx.ConnConfig{}+ dialer := &fakeGCPDialer{}+ ctx := context.WithValue(context.Background(), contextKey{}, "marker")++ applyGCPConfig(pgxConfig, &gcpConfig{+ enabled: true,+ instanceConnectionName: "project:region:instance",+ }, dialer)++ require.NotNil(t, pgxConfig.DialFunc)++ conn, err := pgxConfig.DialFunc(ctx, "tcp", "ignored:5432")++ require.NoError(t, err)+ require.Nil(t, conn)+ require.Equal(t, []fakeGCPDialerCall{+ {+ ctx: ctx,+ instanceConnectionName: "project:region:instance",+ },+ }, dialer.calls)+}++func TestConfigureGCPWithDialerReturnsCleanup(t *testing.T) {+ pgxConfig := &pgx.ConnConfig{}+ dialer := &fakeGCPDialer{}++ openOptions, cleanup, err := configureGCPWithDialer(pgxConfig, &gcpConfig{+ enabled: true,+ instanceConnectionName: "project:region:instance",+ }, dialer)++ require.NoError(t, err)+ require.Empty(t, openOptions)+ require.NotNil(t, cleanup)+ require.NotNil(t, pgxConfig.DialFunc)++ require.NoError(t, cleanup())+ require.Equal(t, 1, dialer.closeCalls)+}++func TestConfigureRejectsMultipleMetadataDBAuthProviders(t *testing.T) {+ pgxConfig := mustParsePGXConfig(t, "postgres://[email protected]:5432/bytebase?sslmode=verify-full&bytebase_aws_rds_iam=true&bytebase_aws_region=us-east-1&bytebase_gcp_cloud_sql_iam=true&bytebase_gcp_cloud_sql_instance_connection_name=project:region:instance")++ _, _, err := Configure(context.Background(), pgxConfig)++ require.ErrorContains(t, err, "multiple metadata database IAM auth providers are enabled")+}diff --git a/docs/operations/high-availability.md b/docs/operations/high-availability.mdindex 9f54edbf0c..25c140d660 100644--- a/docs/operations/high-availability.md+++ b/docs/operations/high-availability.md@@ -34,6 +34,16 @@ postgres://[email protected]:5432/bytebase?sslmode=veThe PostgreSQL user must be granted `rds_iam`, and the AWS principal used by each Bytebase process must have `rds-db:connect` for that database user. Use verified TLS, such as `sslmode=verify-full`, with a trust store that validates the RDS certificate. Do not put AWS access keys in `PG_URL`; use the standard AWS SDK credential chain through the runtime environment, such as EKS IRSA, ECS task roles, EC2 instance profiles, environment variables, or shared AWS config.+### GCP Cloud SQL IAM authentication for metadata PostgreSQL++When the shared metadata PostgreSQL database is GCP Cloud SQL for PostgreSQL, every Bytebase replica can enable Cloud SQL IAM auth by adding the Bytebase GCP parameters to `PG_URL`. Keyword/value values are recommended because Cloud SQL IAM PostgreSQL user names commonly contain `@`:++```text[email protected] dbname=bytebase bytebase_gcp_cloud_sql_iam=true bytebase_gcp_cloud_sql_instance_connection_name=project-id:us-central1:bytebase-metadata+```++The Cloud SQL IAM database user must exist and have the needed PostgreSQL privileges. The Google principal used by each Bytebase process must have permission to connect to the Cloud SQL instance, typically through ambient Application Default Credentials such as GKE Workload Identity. Do not put Google service account keys in `PG_URL`.+## How replica detection worksBytebase tracks live replicas with heartbeats:diff --git a/helm-charts/bytebase/README.md b/helm-charts/bytebase/README.mdindex 01a08af63d..42b7e58049 100644--- a/helm-charts/bytebase/README.md+++ b/helm-charts/bytebase/README.md@@ -105,6 +105,8 @@ upgrade bytebase-release bytebase-repo/bytebase| `bytebase.option.externalPg.escapePassword` | Controls whether to escape the password in the connection string. `bytebase.option.externalPg.existingPgPasswordSecret` or `bytebase.option.externalPg.pgPassword` should be specified with this value together. **Experimental feature.** | false || `bytebase.option.externalPg.awsRdsIam.enabled` | Enables AWS RDS IAM authentication for the Bytebase metadata PostgreSQL database when the chart constructs PG_URL from externalPg fields, including `sslmode=verify-full`. | false || `bytebase.option.externalPg.awsRdsIam.region` | AWS region used to sign RDS IAM authentication tokens for the Bytebase metadata PostgreSQL database. Required when awsRdsIam.enabled is true. | "" |+| `bytebase.option.externalPg.gcpCloudSqlIam.enabled` | Enables GCP Cloud SQL IAM authentication for the Bytebase metadata PostgreSQL database when the chart constructs PG_URL from externalPg fields. | false |+| `bytebase.option.externalPg.gcpCloudSqlIam.instanceConnectionName` | GCP Cloud SQL instance connection name (`project:region:instance`) for the Bytebase metadata PostgreSQL database. Required when gcpCloudSqlIam.enabled is true. | "" || `bytebase.persistence.enabled` | Enable/disable persistence for Bytebase. | false || `bytebase.persistence.existingClaim` | Name of the existing PersistentVolumeClaim for Bytebase persistence. | "" || `bytebase.persistence.storage` | Size of the persistent volume for Bytebase data. | "2Gi" |diff --git a/helm-charts/bytebase/templates/statefulset.yaml b/helm-charts/bytebase/templates/statefulset.yamlindex 8375e99a87..17b4d6d7fe 100644--- a/helm-charts/bytebase/templates/statefulset.yaml+++ b/helm-charts/bytebase/templates/statefulset.yaml@@ -22,6 +22,11 @@{{- $externalPgEscapePgPassword := .Values.bytebase.option.externalPg.escapePassword -}}{{- $externalPgAWSRDSIAMEnabled := .Values.bytebase.option.externalPg.awsRdsIam.enabled -}}{{- $externalPgAWSRDSIAMRegion := .Values.bytebase.option.externalPg.awsRdsIam.region -}}+{{- $externalPgGCPCloudSQLIAMEnabled := .Values.bytebase.option.externalPg.gcpCloudSqlIam.enabled -}}+{{- $externalPgGCPCloudSQLIAMInstanceConnectionName := .Values.bytebase.option.externalPg.gcpCloudSqlIam.instanceConnectionName -}}+{{- if and $externalPgAWSRDSIAMEnabled $externalPgGCPCloudSQLIAMEnabled -}}+{{- fail "only one metadata database IAM auth provider can be enabled" -}}+{{- end -}}# bytbease.persistence{{- $persistenceEnable := .Values.bytebase.persistence.enabled -}}{{- $persistenceExistingClaim := .Values.bytebase.persistence.existingClaim -}}@@ -49,7 +54,7 @@ spec:app: bytebasespec:serviceAccountName: {{ template "bytebase.serviceAccountName" . }}- {{- if and $externalPgEscapePgPassword (not $externalPgAWSRDSIAMEnabled) }}+ {{- if and $externalPgEscapePgPassword (not $externalPgAWSRDSIAMEnabled) (not $externalPgGCPCloudSQLIAMEnabled) }}# If user specify the externalPgEscapePgPassword, we should escape the password in the PG_URL in the shell,# because the password may comes from a secret, which is invisible in helm.# Hence, we do the escaping in the initialization container, and write the escaped password to the shared volume.@@ -160,7 +165,10 @@ spec:value: {{ $externalPgURL }}{{- else }}# Otherwise, we should construct the external pg connection string from the specified parameters.- {{- if $externalPgAWSRDSIAMEnabled }}+ {{- if $externalPgGCPCloudSQLIAMEnabled }}+ - name: PG_URL+ value: {{ printf "user=%s dbname=%s bytebase_gcp_cloud_sql_iam=true bytebase_gcp_cloud_sql_instance_connection_name=%s" $externalPgUsername $externalPgDatabase (required "bytebase.option.externalPg.gcpCloudSqlIam.instanceConnectionName is required when bytebase.option.externalPg.gcpCloudSqlIam.enabled is true" $externalPgGCPCloudSQLIAMInstanceConnectionName) | quote }}+ {{- else if $externalPgAWSRDSIAMEnabled }}- name: PG_URLvalue: postgres://{{ $externalPgUsername }}@{{ $externalPgHost }}:{{ $externalPgPort }}/{{ $externalPgDatabase }}?sslmode=verify-full&bytebase_aws_rds_iam=true&bytebase_aws_region={{ required "bytebase.option.externalPg.awsRdsIam.region is required when bytebase.option.externalPg.awsRdsIam.enabled is true" $externalPgAWSRDSIAMRegion }}{{- else if and $externalPgExistingPgPasswordSecret (not $externalPgEscapePgPassword) }}@@ -182,7 +190,7 @@ spec:- -cargs:- |- {{- if and $externalPgEscapePgPassword (not $externalPgAWSRDSIAMEnabled) }}+ {{- if and $externalPgEscapePgPassword (not $externalPgAWSRDSIAMEnabled) (not $externalPgGCPCloudSQLIAMEnabled) }}# If user escape the password, we should read the escaped password from the shared volume, and construct the PG_PASSWORD.export PG_URL=postgres://{{ $externalPgUsername }}:$(cat /mnt/bytebase-shared-volume/pg_escaped_password)@{{ $externalPgHost }}:{{ $externalPgPort }}/{{ $externalPgDatabase }}{{- end }}diff --git a/helm-charts/bytebase/values.yaml b/helm-charts/bytebase/values.yamlindex a29f0b1a3b..c983a4a58d 100644--- a/helm-charts/bytebase/values.yaml+++ b/helm-charts/bytebase/values.yaml@@ -46,6 +46,9 @@ bytebase:awsRdsIam:enabled: falseregion: ""+ gcpCloudSqlIam:+ enabled: false+ instanceConnectionName: ""persistence:enabled: falseexistingClaim: ""