Skip to content

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

chore: no guess dsn (#15491)

Danny Xu66a71c6d7346
diff --git a/backend/plugin/db/cockroachdb/cockroachdb.go b/backend/plugin/db/cockroachdb/cockroachdb.go
index 33af824399..a8046c2005 100644
--- a/backend/plugin/db/cockroachdb/cockroachdb.go
+++ b/backend/plugin/db/cockroachdb/cockroachdb.go
@@ -82,18 +82,13 @@ func (driver *Driver) Open(ctx context.Context, _ storepb.Engine, config db.Conn
if err != nil {
return nil, err
}
- return &noDeadlineConn{Conn: conn}, nil
+ return &util.NoDeadlineConn{Conn: conn}, nil
}
}
driver.databaseName = config.ConnectionContext.DatabaseName
if config.ConnectionContext.DatabaseName == "" {
- databaseName, cfg, err := guessDSN(pgxConnConfig, config.DataSource.Username)
- if err != nil {
- return nil, err
- }
- pgxConnConfig = cfg
- driver.databaseName = databaseName
+ pgxConnConfig.Database = "postgres"
}
driver.config = config
@@ -134,8 +129,6 @@ func getRoutingIDFromCockroachCloudURL(host string) string {
}
func getCockroachConnectionConfig(config db.ConnectionConfig) (*pgx.ConnConfig, error) {
- // Require username for Postgres, as the guessDSN 1st guess is to use the username as the connecting database
- // if database name is not explicitly specified.
if config.DataSource.Username == "" {
return nil, errors.Errorf("user must be set")
}
@@ -185,39 +178,6 @@ func getCockroachConnectionConfig(config db.ConnectionConfig) (*pgx.ConnConfig,
return connConfig, nil
}
-type noDeadlineConn struct{ net.Conn }
-
-func (*noDeadlineConn) SetDeadline(time.Time) error { return nil }
-func (*noDeadlineConn) SetReadDeadline(time.Time) error { return nil }
-func (*noDeadlineConn) SetWriteDeadline(time.Time) error { return nil }
-
-// guessDSN will guess a valid DB connection and its database name.
-func guessDSN(baseConnConfig *pgx.ConnConfig, username string) (string, *pgx.ConnConfig, error) {
- // Some postgres server default behavior is to use username as the database name if not specified,
- // while some postgres server explicitly requires the database name to be present (e.g. render.com).
- guesses := []string{"postgres", username, "template1"}
- // dsn+" dbname=bytebase"
- for _, guessDatabase := range guesses {
- connConfig := *baseConnConfig
- connConfig.Database = guessDatabase
- if err := func() error {
- connectionString := stdlib.RegisterConnConfig(&connConfig)
- defer stdlib.UnregisterConnConfig(connectionString)
- db, err := sql.Open(driverName, connectionString)
- if err != nil {
- return err
- }
- defer db.Close()
- return db.Ping()
- }(); err != nil {
- slog.Debug("guessDSN attempt failed", log.BBError(err))
- continue
- }
- return guessDatabase, &connConfig, nil
- }
- return "", nil, errors.Errorf("cannot connect to the instance, make sure the connection info is correct")
-}
-
// Close closes the driver.
func (driver *Driver) Close(context.Context) error {
stdlib.UnregisterConnConfig(driver.connectionString)
diff --git a/backend/plugin/db/pg/pg.go b/backend/plugin/db/pg/pg.go
index 87ebf8a95d..d6d0f6900f 100644
--- a/backend/plugin/db/pg/pg.go
+++ b/backend/plugin/db/pg/pg.go
@@ -100,18 +100,13 @@ func (driver *Driver) Open(ctx context.Context, _ storepb.Engine, config db.Conn
if err != nil {
return nil, err
}
- return &noDeadlineConn{Conn: conn}, nil
+ return &util.NoDeadlineConn{Conn: conn}, nil
}
}
driver.databaseName = config.ConnectionContext.DatabaseName
if config.ConnectionContext.DatabaseName == "" {
- databaseName, cfg, err := guessDSN(pgxConnConfig, config.DataSource.Username)
- if err != nil {
- return nil, err
- }
- pgxConnConfig = cfg
- driver.databaseName = databaseName
+ pgxConnConfig.Database = "postgres"
}
driver.config = config
@@ -135,8 +130,6 @@ func (driver *Driver) Open(ctx context.Context, _ storepb.Engine, config db.Conn
}
func getPGConnectionConfig(config db.ConnectionConfig) (*pgx.ConnConfig, error) {
- // Require username for Postgres, as the guessDSN 1st guess is to use the username as the connecting database
- // if database name is not explicitly specified.
if config.DataSource.Username == "" {
return nil, errors.Errorf("user must be set")
}
@@ -237,39 +230,6 @@ func getCloudSQLConnectionConfig(ctx context.Context, conf db.ConnectionConfig)
return config, nil
}
-type noDeadlineConn struct{ net.Conn }
-
-func (*noDeadlineConn) SetDeadline(time.Time) error { return nil }
-func (*noDeadlineConn) SetReadDeadline(time.Time) error { return nil }
-func (*noDeadlineConn) SetWriteDeadline(time.Time) error { return nil }
-
-// guessDSN will guess a valid DB connection and its database name.
-func guessDSN(baseConnConfig *pgx.ConnConfig, username string) (string, *pgx.ConnConfig, error) {
- // Some postgres server default behavior is to use username as the database name if not specified,
- // while some postgres server explicitly requires the database name to be present (e.g. render.com).
- guesses := []string{"postgres", username, "template1"}
- // dsn+" dbname=bytebase"
- for _, guessDatabase := range guesses {
- connConfig := *baseConnConfig
- connConfig.Database = guessDatabase
- if err := func() error {
- connectionString := stdlib.RegisterConnConfig(&connConfig)
- defer stdlib.UnregisterConnConfig(connectionString)
- db, err := sql.Open(driverName, connectionString)
- if err != nil {
- return err
- }
- defer db.Close()
- return db.Ping()
- }(); err != nil {
- slog.Debug("guessDSN attempt failed", log.BBError(err))
- continue
- }
- return guessDatabase, &connConfig, nil
- }
- return "", nil, errors.Errorf("cannot connect to the instance, make sure the connection info is correct")
-}
-
// Close closes the driver.
func (driver *Driver) Close(context.Context) error {
stdlib.UnregisterConnConfig(driver.connectionString)
diff --git a/backend/plugin/db/redis/redis.go b/backend/plugin/db/redis/redis.go
index 5e1242a56f..a776a7512e 100644
--- a/backend/plugin/db/redis/redis.go
+++ b/backend/plugin/db/redis/redis.go
@@ -82,7 +82,7 @@ func (d *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionC
if err != nil {
return nil, err
}
- return &noDeadlineConn{Conn: conn}, nil
+ return &util.NoDeadlineConn{Conn: conn}, nil
}
}
client := redis.NewClient(options)
@@ -116,7 +116,7 @@ func (d *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionC
if err != nil {
return nil, err
}
- return &noDeadlineConn{Conn: conn}, nil
+ return &util.NoDeadlineConn{Conn: conn}, nil
}
}
d.databaseName = fmt.Sprintf("%d", db)
@@ -146,7 +146,7 @@ func (d *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionC
if err != nil {
return nil, err
}
- return &noDeadlineConn{Conn: conn}, nil
+ return &util.NoDeadlineConn{Conn: conn}, nil
}
}
client := redis.NewClusterClient(options)
@@ -160,12 +160,6 @@ func (d *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionC
return d, nil
}
-type noDeadlineConn struct{ net.Conn }
-
-func (*noDeadlineConn) SetDeadline(time.Time) error { return nil }
-func (*noDeadlineConn) SetReadDeadline(time.Time) error { return nil }
-func (*noDeadlineConn) SetWriteDeadline(time.Time) error { return nil }
-
// Close closes the redis driver.
func (d *Driver) Close(context.Context) error {
var err error
diff --git a/backend/plugin/db/redshift/redshift.go b/backend/plugin/db/redshift/redshift.go
index b06ef5f7a0..2afb932817 100644
--- a/backend/plugin/db/redshift/redshift.go
+++ b/backend/plugin/db/redshift/redshift.go
@@ -66,8 +66,6 @@ func newDriver(db.DriverConfig) db.Driver {
// Open opens a Postgres driver.
func (driver *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionConfig) (db.Driver, error) {
- // Require username for Postgres, as the guessDSN 1st guess is to use the username as the connecting database
- // if database name is not explicitly specified.
if config.DataSource.Username == "" {
return nil, errors.Errorf("user must be set")
}
@@ -104,7 +102,7 @@ func (driver *Driver) Open(_ context.Context, _ storepb.Engine, config db.Connec
if err != nil {
return nil, err
}
- return &noDeadlineConn{Conn: conn}, nil
+ return &util.NoDeadlineConn{Conn: conn}, nil
}
}
driver.databaseName = config.ConnectionContext.DatabaseName
@@ -125,12 +123,6 @@ func (driver *Driver) Open(_ context.Context, _ storepb.Engine, config db.Connec
return driver, nil
}
-type noDeadlineConn struct{ net.Conn }
-
-func (*noDeadlineConn) SetDeadline(time.Time) error { return nil }
-func (*noDeadlineConn) SetReadDeadline(time.Time) error { return nil }
-func (*noDeadlineConn) SetWriteDeadline(time.Time) error { return nil }
-
// Close closes the database and prevents new queries from starting.
// Close then waits for all queries that have started processing on the server to finish.
func (driver *Driver) Close(context.Context) error {
diff --git a/backend/plugin/db/risingwave/risingwave.go b/backend/plugin/db/risingwave/risingwave.go
index 9c1e0970e9..f8d6d74b62 100644
--- a/backend/plugin/db/risingwave/risingwave.go
+++ b/backend/plugin/db/risingwave/risingwave.go
@@ -62,8 +62,6 @@ func newDriver(config db.DriverConfig) db.Driver {
// Open opens a RisingWave driver.
func (driver *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionConfig) (db.Driver, error) {
- // Require username for Postgres, as the guessDSN 1st guess is to use the username as the connecting database
- // if database name is not explicitly specified.
if config.DataSource.Username == "" {
return nil, errors.Errorf("user must be set")
}
@@ -108,7 +106,7 @@ func (driver *Driver) Open(_ context.Context, _ storepb.Engine, config db.Connec
if err != nil {
return nil, err
}
- return &noDeadlineConn{Conn: conn}, nil
+ return &util.NoDeadlineConn{Conn: conn}, nil
}
}
if config.ConnectionContext.ReadOnly {
@@ -117,12 +115,7 @@ func (driver *Driver) Open(_ context.Context, _ storepb.Engine, config db.Connec
driver.databaseName = config.ConnectionContext.DatabaseName
if config.ConnectionContext.DatabaseName == "" {
- databaseName, cfg, err := guessDSN(pgxConnConfig)
- if err != nil {
- return nil, err
- }
- pgxConnConfig = cfg
- driver.databaseName = databaseName
+ pgxConnConfig.Database = "postgres"
}
driver.config = config
@@ -135,37 +128,6 @@ func (driver *Driver) Open(_ context.Context, _ storepb.Engine, config db.Connec
return driver, nil
}
-type noDeadlineConn struct{ net.Conn }
-
-func (*noDeadlineConn) SetDeadline(time.Time) error { return nil }
-func (*noDeadlineConn) SetReadDeadline(time.Time) error { return nil }
-func (*noDeadlineConn) SetWriteDeadline(time.Time) error { return nil }
-
-// guessDSN will guess a valid DB connection and its database name.
-func guessDSN(baseConnConfig *pgx.ConnConfig) (string, *pgx.ConnConfig, error) {
- // RisingWave creates the default `dev` database.
- guesses := []string{"dev"}
- for _, guessDatabase := range guesses {
- connConfig := *baseConnConfig
- connConfig.Database = guessDatabase
- if err := func() error {
- connectionString := stdlib.RegisterConnConfig(&connConfig)
- defer stdlib.UnregisterConnConfig(connectionString)
- db, err := sql.Open(driverName, connectionString)
- if err != nil {
- return err
- }
- defer db.Close()
- return db.Ping()
- }(); err != nil {
- slog.Debug("guessDSN attempt failed", log.BBError(err))
- continue
- }
- return guessDatabase, &connConfig, nil
- }
- return "", nil, errors.Errorf("cannot connect to the instance, make sure the connection info is correct")
-}
-
// Close closes the driver.
func (driver *Driver) Close(context.Context) error {
stdlib.UnregisterConnConfig(driver.connectionString)
diff --git a/backend/plugin/db/util/ssh.go b/backend/plugin/db/util/ssh.go
index 84c0ce1781..fc6174da76 100644
--- a/backend/plugin/db/util/ssh.go
+++ b/backend/plugin/db/util/ssh.go
@@ -6,6 +6,7 @@ import (
"log/slog"
"net"
"os"
+ "time"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
@@ -127,3 +128,9 @@ func init() {
PortFIFO <- i + 6113
}
}
+
+type NoDeadlineConn struct{ net.Conn }
+
+func (*NoDeadlineConn) SetDeadline(time.Time) error { return nil }
+func (*NoDeadlineConn) SetReadDeadline(time.Time) error { return nil }
+func (*NoDeadlineConn) SetWriteDeadline(time.Time) error { return nil }