Skip to content

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

refactor: connection config (#13323)

Danny Xu82740bd04d60
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index 765531bd64..3db9d7b222 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -435,10 +435,7 @@ func DoExport(ctx context.Context, storeInstance *store.Store, dbFactory *dbfact
}
queryContext := &db.QueryContext{
- CurrentDatabase: database.DatabaseName,
- }
- if request.Limit != 0 {
- queryContext.Limit = int(request.Limit)
+ Limit: int(request.Limit),
}
start := time.Now().UnixNano()
result, err := driver.QueryConn(ctx, conn, request.Statement, queryContext)
@@ -823,9 +820,8 @@ func (s *SQLService) doQuery(ctx context.Context, request *v1pb.QueryRequest, in
start := time.Now().UnixNano()
results, err := driver.QueryConn(ctx, conn, request.Statement, &db.QueryContext{
- Limit: int(request.Limit),
- Explain: request.Explain,
- CurrentDatabase: database.DatabaseName,
+ Limit: int(request.Limit),
+ Explain: request.Explain,
})
select {
case <-ctx.Done():
diff --git a/backend/component/dbfactory/dbfactory.go b/backend/component/dbfactory/dbfactory.go
index 36e53a7fca..b787a86361 100644
--- a/backend/component/dbfactory/dbfactory.go
+++ b/backend/component/dbfactory/dbfactory.go
@@ -134,10 +134,6 @@ func (d *DBFactory) GetDataSourceDriver(ctx context.Context, instance *store.Ins
if databaseName == "" {
databaseName = dataSource.Database
}
- connectionDatabase := ""
- if datashare {
- connectionDatabase = dataSource.Database
- }
password, err := common.Unobfuscate(dataSource.ObfuscatedPassword, d.secret)
if err != nil {
return nil, err
@@ -222,7 +218,7 @@ func (d *DBFactory) GetDataSourceDriver(ctx context.Context, instance *store.Ins
Host: dataSource.Host,
Port: dataSource.Port,
Database: databaseName,
- ConnectionDatabase: connectionDatabase,
+ DataShare: datashare,
SRV: dataSource.SRV,
AuthenticationDatabase: dataSource.AuthenticationDatabase,
SID: dataSource.SID,
diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.go
index f015614ae9..0b2b2c6803 100644
--- a/backend/plugin/db/driver.go
+++ b/backend/plugin/db/driver.go
@@ -179,10 +179,9 @@ type ConnectionConfig struct {
Username string
Password string
Database string
- // The database used to connect.
// It's only set for Redshift datashare database.
- ConnectionDatabase string
- TLSConfig TLSConfig
+ DataShare bool
+ TLSConfig TLSConfig
// Only used for Hive.
SASLConfig SASLConfig
// ReadOnly is only supported for Postgres at the moment.
@@ -250,9 +249,6 @@ type QueryContext struct {
// Limit is the maximum row count returned. No limit enforced if limit <= 0
Limit int
Explain bool
-
- // CurrentDatabase is for MySQL
- CurrentDatabase string
}
// DatabaseRoleMessage is the API message for database role.
diff --git a/backend/plugin/db/mongodb/mongodb.go b/backend/plugin/db/mongodb/mongodb.go
index ef8e31c96d..3e07f3fc56 100644
--- a/backend/plugin/db/mongodb/mongodb.go
+++ b/backend/plugin/db/mongodb/mongodb.go
@@ -122,7 +122,7 @@ func (driver *Driver) Execute(ctx context.Context, statement string, _ db.Execut
} else {
// Write the tlsCAFile to a temporary file, and use the temporary file as the value of --tlsCAFile.
// The reason is that the --tlsCAFile option of mongosh does not support the value of the certificate directly.
- caFileName := fmt.Sprintf("mongodb-tls-ca-%s-%s", driver.connCfg.ConnectionDatabase, uuid)
+ caFileName := fmt.Sprintf("mongodb-tls-ca-%s-%s", driver.connCfg.Database, uuid)
defer func() {
// While error occurred in mongosh, the temporary file may not created, so we ignore the error here.
_ = os.Remove(caFileName)
@@ -134,7 +134,7 @@ func (driver *Driver) Execute(ctx context.Context, statement string, _ db.Execut
}
if driver.connCfg.TLSConfig.SslKey != "" && driver.connCfg.TLSConfig.SslCert != "" {
- clientCertName := fmt.Sprintf("mongodb-tls-client-cert-%s-%s", driver.connCfg.ConnectionDatabase, uuid)
+ clientCertName := fmt.Sprintf("mongodb-tls-client-cert-%s-%s", driver.connCfg.Database, uuid)
defer func() {
// While error occurred in mongosh, the temporary file may not created, so we ignore the error here.
_ = os.Remove(clientCertName)
@@ -282,7 +282,7 @@ func (driver *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement stri
} else {
// Write the tlsCAFile to a temporary file, and use the temporary file as the value of --tlsCAFile.
// The reason is that the --tlsCAFile option of mongosh does not support the value of the certificate directly.
- caFileName := fmt.Sprintf("mongodb-tls-ca-%s-%s", driver.connCfg.ConnectionDatabase, uuid)
+ caFileName := fmt.Sprintf("mongodb-tls-ca-%s-%s", driver.connCfg.Database, uuid)
defer func() {
// While error occurred in mongosh, the temporary file may not created, so we ignore the error here.
_ = os.Remove(caFileName)
@@ -294,7 +294,7 @@ func (driver *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement stri
}
if driver.connCfg.TLSConfig.SslKey != "" && driver.connCfg.TLSConfig.SslCert != "" {
- clientCertName := fmt.Sprintf("mongodb-tls-client-cert-%s-%s", driver.connCfg.ConnectionDatabase, uuid)
+ clientCertName := fmt.Sprintf("mongodb-tls-client-cert-%s-%s", driver.connCfg.Database, uuid)
defer func() {
// While error occurred in mongosh, the temporary file may not created, so we ignore the error here.
_ = os.Remove(clientCertName)
@@ -316,7 +316,7 @@ func (driver *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement stri
}
}
- queryResultFileName := fmt.Sprintf("mongodb-query-%s-%s", driver.connCfg.ConnectionDatabase, uuid.New().String())
+ queryResultFileName := fmt.Sprintf("mongodb-query-%s-%s", driver.connCfg.Database, uuid.New().String())
defer func() {
// While error occurred in mongosh, the temporary file may not created, so we ignore the error here.
_ = os.Remove(queryResultFileName)
diff --git a/backend/plugin/db/pg/pg.go b/backend/plugin/db/pg/pg.go
index 4282ddc045..95fc1a921e 100644
--- a/backend/plugin/db/pg/pg.go
+++ b/backend/plugin/db/pg/pg.go
@@ -67,16 +67,16 @@ func newDriver(config db.DriverConfig) db.Driver {
// Open opens a Postgres driver.
func (driver *Driver) Open(ctx context.Context, _ storepb.Engine, config db.ConnectionConfig) (db.Driver, error) {
- var connConfig *pgx.ConnConfig
+ var pgxConnConfig *pgx.ConnConfig
var err error
switch config.AuthenticationType {
case storepb.DataSourceOptions_GOOGLE_CLOUD_SQL_IAM:
- connConfig, err = getCloudSQLConnectionConfig(ctx, config)
+ pgxConnConfig, err = getCloudSQLConnectionConfig(ctx, config)
case storepb.DataSourceOptions_AWS_RDS_IAM:
- connConfig, err = getRDSConnectionConfig(ctx, config)
+ pgxConnConfig, err = getRDSConnectionConfig(ctx, config)
default:
- connConfig, err = getPGConnectionConfig(config)
+ pgxConnConfig, err = getPGConnectionConfig(config)
}
if err != nil {
return nil, err
@@ -89,7 +89,7 @@ func (driver *Driver) Open(ctx context.Context, _ storepb.Engine, config db.Conn
}
driver.sshClient = sshClient
- connConfig.Config.DialFunc = func(_ context.Context, network, addr string) (net.Conn, error) {
+ pgxConnConfig.Config.DialFunc = func(_ context.Context, network, addr string) (net.Conn, error) {
conn, err := sshClient.Dial(network, addr)
if err != nil {
return nil, err
@@ -100,16 +100,16 @@ func (driver *Driver) Open(ctx context.Context, _ storepb.Engine, config db.Conn
driver.databaseName = config.Database
if config.Database == "" {
- databaseName, cfg, err := guessDSN(connConfig, config.Username)
+ databaseName, cfg, err := guessDSN(pgxConnConfig, config.Username)
if err != nil {
return nil, err
}
- connConfig = cfg
+ pgxConnConfig = cfg
driver.databaseName = databaseName
}
driver.config = config
- driver.connectionString = stdlib.RegisterConnConfig(connConfig)
+ driver.connectionString = stdlib.RegisterConnConfig(pgxConnConfig)
db, err := sql.Open(driverName, driver.connectionString)
if err != nil {
return nil, err
diff --git a/backend/plugin/db/redshift/redshift.go b/backend/plugin/db/redshift/redshift.go
index d562f2db5b..de2dfcf06e 100644
--- a/backend/plugin/db/redshift/redshift.go
+++ b/backend/plugin/db/redshift/redshift.go
@@ -77,22 +77,20 @@ func (driver *Driver) Open(_ context.Context, _ storepb.Engine, config db.Connec
return nil, errors.Errorf("ssl-cert and ssl-key must be both set or unset")
}
- connConfig, err := pgx.ParseConfig(fmt.Sprintf("host=%s port=%s", config.Host, config.Port))
+ pgxConnConfig, err := pgx.ParseConfig(fmt.Sprintf("host=%s port=%s", config.Host, config.Port))
if err != nil {
return nil, err
}
- connConfig.Config.User = config.Username
- connConfig.Config.Password = config.Password
- connConfig.Config.Database = config.Database
- if config.ConnectionDatabase != "" {
- connConfig.Config.Database = config.ConnectionDatabase
- }
+ pgxConnConfig.Config.User = config.Username
+ pgxConnConfig.Config.Password = config.Password
+ pgxConnConfig.Config.Database = config.Database
+
if config.TLSConfig.SslCert != "" {
cfg, err := config.TLSConfig.GetSslConfig()
if err != nil {
return nil, err
}
- connConfig.TLSConfig = cfg
+ pgxConnConfig.TLSConfig = cfg
}
if config.SSHConfig.Host != "" {
sshClient, err := util.GetSSHClient(config.SSHConfig)
@@ -101,7 +99,7 @@ func (driver *Driver) Open(_ context.Context, _ storepb.Engine, config db.Connec
}
driver.sshClient = sshClient
- connConfig.Config.DialFunc = func(_ context.Context, network, addr string) (net.Conn, error) {
+ pgxConnConfig.Config.DialFunc = func(_ context.Context, network, addr string) (net.Conn, error) {
conn, err := sshClient.Dial(network, addr)
if err != nil {
return nil, err
@@ -110,15 +108,15 @@ func (driver *Driver) Open(_ context.Context, _ storepb.Engine, config db.Connec
}
}
driver.databaseName = config.Database
- driver.datashare = config.ConnectionDatabase != ""
+ driver.datashare = config.DataShare
driver.config = config
// Datashare doesn't support read-only transactions.
if config.ReadOnly && !driver.datashare {
- connConfig.RuntimeParams["default_transaction_read_only"] = "true"
+ pgxConnConfig.RuntimeParams["default_transaction_read_only"] = "true"
}
- driver.connectionString = stdlib.RegisterConnConfig(connConfig)
+ driver.connectionString = stdlib.RegisterConnConfig(pgxConnConfig)
db, err := sql.Open(driverName, driver.connectionString)
if err != nil {
return nil, err
@@ -344,7 +342,7 @@ func (driver *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement s
for _, singleSQL := range singleSQLs {
statement := singleSQL.Text
if driver.datashare {
- statement = strings.ReplaceAll(statement, fmt.Sprintf("%s.", queryContext.CurrentDatabase), "")
+ statement = strings.ReplaceAll(statement, fmt.Sprintf("%s.", driver.databaseName), "")
}
if queryContext != nil && queryContext.Explain {
statement = fmt.Sprintf("EXPLAIN %s", statement)
diff --git a/backend/plugin/db/risingwave/risingwave.go b/backend/plugin/db/risingwave/risingwave.go
index a2dbca5470..85d6180eb0 100644
--- a/backend/plugin/db/risingwave/risingwave.go
+++ b/backend/plugin/db/risingwave/risingwave.go
@@ -82,19 +82,19 @@ func (driver *Driver) Open(_ context.Context, _ storepb.Engine, config db.Connec
}
connStr := fmt.Sprintf("host=%s port=%s", config.Host, config.Port)
- connConfig, err := pgx.ParseConfig(connStr)
+ pgxConnConfig, err := pgx.ParseConfig(connStr)
if err != nil {
return nil, err
}
- connConfig.Config.User = config.Username
- connConfig.Config.Password = config.Password
- connConfig.Config.Database = config.Database
+ pgxConnConfig.Config.User = config.Username
+ pgxConnConfig.Config.Password = config.Password
+ pgxConnConfig.Config.Database = config.Database
if config.TLSConfig.SslCert != "" {
cfg, err := config.TLSConfig.GetSslConfig()
if err != nil {
return nil, err
}
- connConfig.TLSConfig = cfg
+ pgxConnConfig.TLSConfig = cfg
}
if config.SSHConfig.Host != "" {
sshClient, err := util.GetSSHClient(config.SSHConfig)
@@ -103,7 +103,7 @@ func (driver *Driver) Open(_ context.Context, _ storepb.Engine, config db.Connec
}
driver.sshClient = sshClient
- connConfig.Config.DialFunc = func(_ context.Context, network, addr string) (net.Conn, error) {
+ pgxConnConfig.Config.DialFunc = func(_ context.Context, network, addr string) (net.Conn, error) {
conn, err := sshClient.Dial(network, addr)
if err != nil {
return nil, err
@@ -112,21 +112,21 @@ func (driver *Driver) Open(_ context.Context, _ storepb.Engine, config db.Connec
}
}
if config.ReadOnly {
- connConfig.RuntimeParams["default_transaction_read_only"] = "true"
+ pgxConnConfig.RuntimeParams["default_transaction_read_only"] = "true"
}
driver.databaseName = config.Database
if config.Database == "" {
- databaseName, cfg, err := guessDSN(connConfig)
+ databaseName, cfg, err := guessDSN(pgxConnConfig)
if err != nil {
return nil, err
}
- connConfig = cfg
+ pgxConnConfig = cfg
driver.databaseName = databaseName
}
driver.config = config
- driver.connectionString = stdlib.RegisterConnConfig(connConfig)
+ driver.connectionString = stdlib.RegisterConnConfig(pgxConnConfig)
db, err := sql.Open(driverName, driver.connectionString)
if err != nil {
return nil, err