Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(oracle): bound the dial timeout and honor extra parameters in any spelling (#21574)

zchpeter608ac5c9112e
diff --git a/backend/plugin/db/oracle/oracle.go b/backend/plugin/db/oracle/oracle.go
index 10c4139448..7ed0fca184 100644
--- a/backend/plugin/db/oracle/oracle.go
+++ b/backend/plugin/db/oracle/oracle.go
@@ -9,6 +9,7 @@ import (
"log/slog"
"math/big"
"strconv"
+ "strings"
"time"
// Import go-ora Oracle driver.
@@ -58,14 +59,7 @@ func (d *Driver) Open(ctx context.Context, _ storepb.Engine, config db.Connectio
if err != nil {
return nil, errors.Errorf("invalid port %q", config.DataSource.Port)
}
- options := make(map[string]string)
- options["CONNECTION TIMEOUT"] = "0"
- if config.DataSource.GetSid() != "" {
- options["SID"] = config.DataSource.GetSid()
- }
- for key, value := range config.DataSource.GetExtraConnectionParameters() {
- options[key] = value
- }
+ options := connectionOptions(config.DataSource.GetSid(), config.DataSource.GetExtraConnectionParameters())
dsn := goora.BuildUrl(config.DataSource.Host, port, config.DataSource.GetServiceName(), config.DataSource.Username, config.Password, options)
db, err := sql.Open("oracle", dsn)
if err != nil {
@@ -83,6 +77,39 @@ func (d *Driver) Open(ctx context.Context, _ storepb.Engine, config db.Connectio
return d, nil
}
+const (
+ sidOption = "SID"
+ dialTimeoutOption = "CONNECTION TIMEOUT"
+ dialTimeoutOptionAlias = "CONNECT TIMEOUT"
+ // defaultDialTimeoutSeconds bounds the wait for one address to accept a connection, DNS
+ // lookup included, so an unreachable address fails over in seconds. It does not limit
+ // how long a statement runs.
+ defaultDialTimeoutSeconds = "10"
+)
+
+// connectionOptions returns the go-ora URL options for a data source: the extra connection
+// parameters as typed, plus the SID field and the default dial timeout unless an extra
+// parameter sets the same option.
+//
+// go-ora upper-cases option keys and reads CONNECT TIMEOUT as CONNECTION TIMEOUT. A Bytebase
+// key sent next to a user key for the same option would leave the winner to go-ora's map
+// iteration order, so the user's key replaces it in any spelling.
+func connectionOptions(sid string, extraParameters map[string]string) map[string]string {
+ options := make(map[string]string, len(extraParameters)+2)
+ userOptions := make(map[string]bool, len(extraParameters))
+ for key, value := range extraParameters {
+ options[key] = value
+ userOptions[strings.ToUpper(key)] = true
+ }
+ if sid != "" && !userOptions[sidOption] {
+ options[sidOption] = sid
+ }
+ if !userOptions[dialTimeoutOption] && !userOptions[dialTimeoutOptionAlias] {
+ options[dialTimeoutOption] = defaultDialTimeoutSeconds
+ }
+ return options
+}
+
// Close closes the driver.
func (d *Driver) Close(_ context.Context) error {
return d.db.Close()
diff --git a/backend/plugin/db/oracle/oracle_test.go b/backend/plugin/db/oracle/oracle_test.go
index 1f28b44db2..49e4703dff 100644
--- a/backend/plugin/db/oracle/oracle_test.go
+++ b/backend/plugin/db/oracle/oracle_test.go
@@ -2,9 +2,13 @@ package oracle
import (
"fmt"
+ "maps"
"strings"
"testing"
+ "time"
+ goora "github.com/sijms/go-ora/v2"
+ "github.com/sijms/go-ora/v2/configurations"
"github.com/stretchr/testify/require"
"github.com/bytebase/bytebase/backend/common"
@@ -105,3 +109,163 @@ func TestBuildExecuteCommandsSplitsLargeOracleScript(t *testing.T) {
require.Equal(t, "CREATE TABLE APPS.M_SKU_STOCK_KEY_TMP (\n SKU_CODE VARCHAR2(100) NOT NULL,\n STOCK_KEY VARCHAR2(100)\n)", strings.TrimSpace(commands[0].Text))
require.Equal(t, "CREATE INDEX IDX_TMP_SKU_CODE ON APPS.M_SKU_STOCK_KEY_TMP(SKU_CODE)", strings.TrimSpace(commands[1].Text))
}
+
+func TestConnectionOptions(t *testing.T) {
+ tests := []struct {
+ name string
+ sid string
+ extraParameters map[string]string
+ want map[string]string
+ }{
+ {
+ name: "no extra parameters",
+ want: map[string]string{"CONNECTION TIMEOUT": "10"},
+ },
+ {
+ name: "SID field",
+ sid: "ORCL",
+ want: map[string]string{"CONNECTION TIMEOUT": "10", "SID": "ORCL"},
+ },
+ {
+ name: "dial timeout in the exact spelling",
+ extraParameters: map[string]string{"CONNECTION TIMEOUT": "5"},
+ want: map[string]string{"CONNECTION TIMEOUT": "5"},
+ },
+ {
+ name: "dial timeout in lowercase",
+ extraParameters: map[string]string{"connection timeout": "5"},
+ want: map[string]string{"connection timeout": "5"},
+ },
+ {
+ name: "dial timeout alias",
+ extraParameters: map[string]string{"CONNECT TIMEOUT": "5"},
+ want: map[string]string{"CONNECT TIMEOUT": "5"},
+ },
+ {
+ name: "dial timeout alias in mixed case",
+ extraParameters: map[string]string{"Connect Timeout": "5"},
+ want: map[string]string{"Connect Timeout": "5"},
+ },
+ {
+ name: "dial timeout turned off",
+ extraParameters: map[string]string{"CONNECTION TIMEOUT": "0"},
+ want: map[string]string{"CONNECTION TIMEOUT": "0"},
+ },
+ {
+ name: "read timeout is a different option",
+ extraParameters: map[string]string{"TIMEOUT": "30"},
+ want: map[string]string{"TIMEOUT": "30", "CONNECTION TIMEOUT": "10"},
+ },
+ {
+ name: "SID key in the exact spelling",
+ sid: "ORCL",
+ extraParameters: map[string]string{"SID": "STANDBY"},
+ want: map[string]string{"CONNECTION TIMEOUT": "10", "SID": "STANDBY"},
+ },
+ {
+ name: "SID key in lowercase",
+ sid: "ORCL",
+ extraParameters: map[string]string{"sid": "STANDBY"},
+ want: map[string]string{"CONNECTION TIMEOUT": "10", "sid": "STANDBY"},
+ },
+ {
+ name: "SID key without the SID field",
+ extraParameters: map[string]string{"Sid": "STANDBY"},
+ want: map[string]string{"CONNECTION TIMEOUT": "10", "Sid": "STANDBY"},
+ },
+ {
+ name: "other keys pass through in their own spelling",
+ sid: "ORCL",
+ extraParameters: map[string]string{
+ "SERVER": "standby-1.example.com:1521, standby-2.example.com:1521",
+ "SSL": "true",
+ "ssl verify": "false",
+ "WALLET": "/opt/oracle/wallet",
+ "WALLET PASSWORD": "wallet-secret",
+ "AUTH TYPE": "TCPS",
+ "prefetch_rows": "500",
+ },
+ want: map[string]string{
+ "CONNECTION TIMEOUT": "10",
+ "SID": "ORCL",
+ "SERVER": "standby-1.example.com:1521, standby-2.example.com:1521",
+ "SSL": "true",
+ "ssl verify": "false",
+ "WALLET": "/opt/oracle/wallet",
+ "WALLET PASSWORD": "wallet-secret",
+ "AUTH TYPE": "TCPS",
+ "prefetch_rows": "500",
+ },
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ extraParameters := maps.Clone(test.extraParameters)
+ got := connectionOptions(test.sid, extraParameters)
+ require.Equal(t, test.want, got)
+ require.Equal(t, test.extraParameters, extraParameters)
+ })
+ }
+}
+
+// go-ora reads the options back from the URL, so these cases check what the driver ends up
+// with rather than what Bytebase sent.
+func TestConnectionOptionsAsReadByDriver(t *testing.T) {
+ parse := func(t *testing.T, sid string, extraParameters map[string]string) *configurations.ConnectionConfig {
+ t.Helper()
+ dsn := goora.BuildUrl("primary.example.com", 1521, "ORCLPDB1", "bytebase", "secret", connectionOptions(sid, extraParameters))
+ config, err := configurations.ParseConfig(dsn)
+ require.NoError(t, err)
+ return config
+ }
+
+ t.Run("default", func(t *testing.T) {
+ config := parse(t, "", nil)
+ require.Equal(t, 10*time.Second, config.ConnectTimeout)
+ // A read timeout would cut off long statements.
+ require.Zero(t, config.Timeout)
+ require.Empty(t, config.SID)
+ })
+
+ for _, key := range []string{"CONNECTION TIMEOUT", "connection timeout", "CONNECT TIMEOUT", "connect timeout", "Connect Timeout"} {
+ t.Run("dial timeout as "+key, func(t *testing.T) {
+ config := parse(t, "", map[string]string{key: "5"})
+ require.Equal(t, 5*time.Second, config.ConnectTimeout)
+ require.Zero(t, config.Timeout)
+ })
+ }
+
+ t.Run("dial timeout turned off", func(t *testing.T) {
+ config := parse(t, "", map[string]string{"connect timeout": "0"})
+ require.Zero(t, config.ConnectTimeout)
+ })
+
+ t.Run("SID field", func(t *testing.T) {
+ config := parse(t, "ORCL", nil)
+ require.Equal(t, "ORCL", config.SID)
+ })
+
+ t.Run("SID key replaces the SID field", func(t *testing.T) {
+ config := parse(t, "ORCL", map[string]string{"sid": "STANDBY"})
+ require.Equal(t, "STANDBY", config.SID)
+ })
+
+ t.Run("second address over TCPS", func(t *testing.T) {
+ config := parse(t, "", map[string]string{
+ "SERVER": "standby-1.example.com:2484, standby-2.example.com:2484",
+ "SSL": "true",
+ "SSL VERIFY": "false",
+ "AUTH TYPE": "TCPS",
+ })
+ require.Equal(t, []configurations.ServerAddr{
+ {Addr: "primary.example.com", Port: 1521},
+ {Addr: "standby-1.example.com", Port: 2484},
+ {Addr: "standby-2.example.com", Port: 2484},
+ }, config.Servers)
+ require.True(t, config.SSL)
+ require.False(t, config.SSLVerify)
+ require.Equal(t, configurations.TCPS, config.AuthType)
+ require.Equal(t, 10*time.Second, config.ConnectTimeout)
+ })
+}