Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Use the existing instance admin password to test the connection if user does not supply new password
天舟a21c7a9e264d
diff --git a/api/sql.go b/api/sql.goindex 1a297ceaf8..b7f639a695 100644--- a/api/sql.go+++ b/api/sql.go@@ -7,11 +7,12 @@ import ()type ConnectionInfo struct {- DBType db.Type `jsonapi:"attr,dbType"`- Host string `jsonapi:"attr,host"`- Port string `jsonapi:"attr,port"`- Username string `jsonapi:"attr,username"`- Password string `jsonapi:"attr,password"`+ DBType db.Type `jsonapi:"attr,dbType"`+ Host string `jsonapi:"attr,host"`+ Port string `jsonapi:"attr,port"`+ Username string `jsonapi:"attr,username"`+ Password string `jsonapi:"attr,password"`+ InstanceId *int `jsonapi:"attr,instanceId"`}type SqlSyncSchema struct {diff --git a/frontend/src/components/InstanceForm.vue b/frontend/src/components/InstanceForm.vueindex aa2a0910ac..23d4f38ce8 100644--- a/frontend/src/components/InstanceForm.vue+++ b/frontend/src/components/InstanceForm.vue@@ -513,6 +513,7 @@ export default {: state.updatedPassword,host: state.instance.host,port: state.instance.port,+ instanceId: props.create ? undefined : (state.instance as Instance).id,};store.dispatch("sql/ping", connectionInfo)diff --git a/frontend/src/types/sql.ts b/frontend/src/types/sql.tsindex 481baf5c55..997dc4b49a 100644--- a/frontend/src/types/sql.ts+++ b/frontend/src/types/sql.ts@@ -1,3 +1,5 @@+import { InstanceId } from "./id";+export type DBType = "MYSQL";export type ConnectionInfo = {@@ -7,6 +9,11 @@ export type ConnectionInfo = {// In mysql, username can be empty which means anonymous userusername?: string;password?: string;+ // Instance detail page has a Test Connection button, if user doesn't input new password, we+ // want the connection to use the existing password to test the connection, however, we do+ // not transfer the password back to client, thus we here pass the instanceId so the server+ // can fetch the corresponding password.+ instanceId?: InstanceId;};export type SqlResultSet = {diff --git a/server/instance.go b/server/instance.goindex 06ae863f0c..b952e867b2 100644--- a/server/instance.go+++ b/server/instance.go@@ -291,3 +291,19 @@ func (s *Server) ComposeInstanceAdminDataSource(ctx context.Context, instance *a}return nil}++func (s *Server) FindInstanceAdminPasswordById(ctx context.Context, instanceId int) (string, error) {+ dataSourceFind := &api.DataSourceFind{+ InstanceId: &instanceId,+ }+ dataSourceList, err := s.DataSourceService.FindDataSourceList(context.Background(), dataSourceFind)+ if err != nil {+ return "", err+ }+ for _, dataSource := range dataSourceList {+ if dataSource.Type == api.Admin {+ return dataSource.Password, nil+ }+ }+ return "", &bytebase.Error{Code: bytebase.ENOTFOUND, Message: fmt.Sprintf("missing admin password for instance: %d", instanceId)}+}diff --git a/server/sql.go b/server/sql.goindex caf767be0e..8277a5d7ce 100644--- a/server/sql.go+++ b/server/sql.go@@ -20,9 +20,22 @@ func (s *Server) registerSqlRoutes(g *echo.Group) {return echo.NewHTTPError(http.StatusBadRequest, "Malformatted sql ping request").SetInternal(err)}+ password := connectionInfo.Password+ // Instance detail page has a Test Connection button, if user doesn't input new password, we+ // want the connection to use the existing password to test the connection, however, we do+ // not transfer the password back to client, thus the client will pass the instanceId to+ // let server retrieve the password.+ if password == "" && connectionInfo.InstanceId != nil {+ adminPassword, err := s.FindInstanceAdminPasswordById(context.Background(), *connectionInfo.InstanceId)+ if err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to retrieve admin password for instance: %d", connectionInfo.InstanceId)).SetInternal(err)+ }+ password = adminPassword+ }+db, err := db.Open(connectionInfo.DBType, db.DriverConfig{Logger: s.l}, db.ConnectionConfig{Username: connectionInfo.Username,- Password: connectionInfo.Password,+ Password: password,Host: connectionInfo.Host,Port: connectionInfo.Port,})