Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: downgrade pg ssl verify level (#15483)
h3n4lc315a94d3c00
diff --git a/backend/plugin/db/cockroachdb/cockroachdb.go b/backend/plugin/db/cockroachdb/cockroachdb.goindex 25af2592ce..33af824399 100644--- a/backend/plugin/db/cockroachdb/cockroachdb.go+++ b/backend/plugin/db/cockroachdb/cockroachdb.go@@ -154,7 +154,7 @@ func getCockroachConnectionConfig(config db.ConnectionConfig) (*pgx.ConnConfig,}connStr := fmt.Sprintf("host=%s port=%s", config.DataSource.Host, config.DataSource.Port)- sslMode := getSSLMode(config.DataSource)+ sslMode := db.GetPGSSLMode(config.DataSource)connStr += fmt.Sprintf(" sslmode=%s", sslMode)routingID := getRoutingIDFromCockroachCloudURL(config.DataSource.Host)@@ -298,67 +298,48 @@ func (driver *Driver) Execute(ctx context.Context, statement string, opts db.Exevar nonTransactionAndSetRoleStmts []stringvar nonTransactionAndSetRoleStmtsIndex []int32var isPlsql bool- oneshot := true- // HACK(p0ny): always split for pg- //nolint- if true || len(statement) <= common.MaxSheetCheckSize {- singleSQLs, err := crdbparser.SplitSQLStatement(statement)- if err != nil {- return 0, err- }- for i, singleSQL := range singleSQLs {- commands = append(commands, base.SingleSQL{Text: singleSQL})- originalIndex = append(originalIndex, int32(i))- }-- // If the statement is a single statement and is a PL/pgSQL block,- // we should execute it as a single statement without transaction.- // If the statement is a PL/pgSQL block, we should execute it as a single statement.- // https://www.postgresql.org/docs/current/plpgsql-control-structures.html- if len(singleSQLs) == 1 && isPlSQLBlock(singleSQLs[0]) {- isPlsql = true- }- // HACK(p0ny): always split for pg- //nolint- if false && len(commands) <= common.MaximumCommands {- oneshot = false- }- var tmpCommands []base.SingleSQL- var tmpOriginalIndex []int32- for i, command := range commands {- switch {- case isSetRoleStatement(command.Text):- nonTransactionAndSetRoleStmts = append(nonTransactionAndSetRoleStmts, command.Text)- nonTransactionAndSetRoleStmtsIndex = append(nonTransactionAndSetRoleStmtsIndex, originalIndex[i])- case IsNonTransactionStatement(command.Text):- nonTransactionAndSetRoleStmts = append(nonTransactionAndSetRoleStmts, command.Text)- nonTransactionAndSetRoleStmtsIndex = append(nonTransactionAndSetRoleStmtsIndex, originalIndex[i])- continue- case isSuperuserStatement(command.Text):- // Use superuser privilege to run privileged statements.- slog.Info("Use superuser privilege to run privileged statements", slog.String("statement", command.Text))- ct := command.Text- if !strings.HasSuffix(strings.TrimRightFunc(ct, unicode.IsSpace), ";") {- ct += ";"- }- command.Text = fmt.Sprintf("SET LOCAL ROLE NONE;%sSET LOCAL ROLE '%s';", ct, owner)+ singleSQLs, err := crdbparser.SplitSQLStatement(statement)+ if err != nil {+ return 0, err+ }+ for i, singleSQL := range singleSQLs {+ commands = append(commands, base.SingleSQL{Text: singleSQL})+ originalIndex = append(originalIndex, int32(i))+ }++ // If the statement is a single statement and is a PL/pgSQL block,+ // we should execute it as a single statement without transaction.+ // If the statement is a PL/pgSQL block, we should execute it as a single statement.+ // https://www.postgresql.org/docs/current/plpgsql-control-structures.html+ if len(singleSQLs) == 1 && isPlSQLBlock(singleSQLs[0]) {+ isPlsql = true+ }++ var tmpCommands []base.SingleSQL+ var tmpOriginalIndex []int32+ for i, command := range commands {+ switch {+ case isSetRoleStatement(command.Text):+ nonTransactionAndSetRoleStmts = append(nonTransactionAndSetRoleStmts, command.Text)+ nonTransactionAndSetRoleStmtsIndex = append(nonTransactionAndSetRoleStmtsIndex, originalIndex[i])+ case IsNonTransactionStatement(command.Text):+ nonTransactionAndSetRoleStmts = append(nonTransactionAndSetRoleStmts, command.Text)+ nonTransactionAndSetRoleStmtsIndex = append(nonTransactionAndSetRoleStmtsIndex, originalIndex[i])+ continue+ case isSuperuserStatement(command.Text):+ // Use superuser privilege to run privileged statements.+ slog.Info("Use superuser privilege to run privileged statements", slog.String("statement", command.Text))+ ct := command.Text+ if !strings.HasSuffix(strings.TrimRightFunc(ct, unicode.IsSpace), ";") {+ ct += ";"}- tmpCommands = append(tmpCommands, command)- tmpOriginalIndex = append(tmpOriginalIndex, originalIndex[i])- }- commands, originalIndex = tmpCommands, tmpOriginalIndex- }- // HACK(p0ny): always split for pg- //nolint- if false && oneshot {- commands = []base.SingleSQL{- {- Text: statement,- },+ command.Text = fmt.Sprintf("SET LOCAL ROLE NONE;%sSET LOCAL ROLE '%s';", ct, owner)}- originalIndex = []int32{0}+ tmpCommands = append(tmpCommands, command)+ tmpOriginalIndex = append(tmpOriginalIndex, originalIndex[i])}+ commands, originalIndex = tmpCommands, tmpOriginalIndexconn, err := driver.db.Conn(ctx)if err != nil {diff --git a/backend/plugin/db/cockroachdb/util.go b/backend/plugin/db/cockroachdb/util.godeleted file mode 100644index db162e2d37..0000000000--- a/backend/plugin/db/cockroachdb/util.go+++ /dev/null@@ -1,33 +0,0 @@-package cockroachdb--import (- storepb "github.com/bytebase/bytebase/proto/generated-go/store"-)--// sslMode is the PGSSLMode type.-// https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLMODE-type sslMode string--const (- SSLModeDisable sslMode = "disable"- SSLModeAllow sslMode = "allow"- // It is the default mode of sslmode.- // https://www.postgresql.org/docs/current/libpq-ssl.html- SSLModePrefer sslMode = "prefer"- SSLModeRequire sslMode = "require"- SSLModeVerifyCA sslMode = "verify-ca"- SSLModeVerifyFull sslMode = "verify-full"-)--func getSSLMode(ds *storepb.DataSource) sslMode {- sslMode := SSLModePrefer- if ds.GetUseSsl() {- sslMode = SSLModeVerifyFull- if ds.GetSslCa() != "" {- if ds.GetSshHost() != "" {- sslMode = SSLModeVerifyCA- }- }- }- return sslMode-}diff --git a/backend/plugin/db/pg/pg.go b/backend/plugin/db/pg/pg.goindex 923b60ee3a..87ebf8a95d 100644--- a/backend/plugin/db/pg/pg.go+++ b/backend/plugin/db/pg/pg.go@@ -155,7 +155,7 @@ func getPGConnectionConfig(config db.ConnectionConfig) (*pgx.ConnConfig, error)}connStr := fmt.Sprintf("host=%s port=%s", config.DataSource.Host, config.DataSource.Port)- sslMode := getSSLMode(config.DataSource)+ sslMode := db.GetPGSSLMode(config.DataSource)connStr += fmt.Sprintf(" sslmode=%s", sslMode)// Add target_session_attrs=read-write if specified in ExtraConnectionParametersdiff --git a/backend/plugin/db/pg/util.go b/backend/plugin/db/pg/util.godeleted file mode 100644index af59215d28..0000000000--- a/backend/plugin/db/pg/util.go+++ /dev/null@@ -1,33 +0,0 @@-package pg--import (- storepb "github.com/bytebase/bytebase/proto/generated-go/store"-)--// sslMode is the PGSSLMode type.-// https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLMODE-type sslMode string--const (- SSLModeDisable sslMode = "disable"- SSLModeAllow sslMode = "allow"- // It is the default mode of sslmode.- // https://www.postgresql.org/docs/current/libpq-ssl.html- SSLModePrefer sslMode = "prefer"- SSLModeRequire sslMode = "require"- SSLModeVerifyCA sslMode = "verify-ca"- SSLModeVerifyFull sslMode = "verify-full"-)--func getSSLMode(ds *storepb.DataSource) sslMode {- sslMode := SSLModePrefer- if ds.GetUseSsl() {- sslMode = SSLModeVerifyFull- if ds.GetSslCa() != "" {- if ds.GetSshHost() != "" {- sslMode = SSLModeVerifyCA- }- }- }- return sslMode-}diff --git a/backend/plugin/db/ssl.go b/backend/plugin/db/ssl.goindex 6fd59805da..ff56c63483 100644--- a/backend/plugin/db/ssl.go+++ b/backend/plugin/db/ssl.go@@ -76,3 +76,26 @@ func GetTLSConfig(ds *storepb.DataSource) (*tls.Config, error) {}return cfg, nil}++// SSLMode is the PGSSLMode type.+// https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLMODE+type SSLMode string++const (+ SSLModeDisable SSLMode = "disable"+ SSLModeAllow SSLMode = "allow"+ // It is the default mode of sslmode.+ // https://www.postgresql.org/docs/current/libpq-ssl.html+ SSLModePrefer SSLMode = "prefer"+ SSLModeRequire SSLMode = "require"+ SSLModeVerifyCA SSLMode = "verify-ca"+ SSLModeVerifyFull SSLMode = "verify-full"+)++func GetPGSSLMode(ds *storepb.DataSource) SSLMode {+ sslMode := SSLModeDisable+ if ds.GetUseSsl() {+ sslMode = SSLModeVerifyCA+ }+ return sslMode+}