Skip to content

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

Allow instance to specify empty password

天舟317fc47bd252
diff --git a/api/instance.go b/api/instance.go
index 8c7176f3e4..e77b8a37cd 100644
--- a/api/instance.go
+++ b/api/instance.go
@@ -79,13 +79,14 @@ type InstancePatch struct {
UpdaterId int
// Domain specific fields
- Name *string `jsonapi:"attr,name"`
- EngineVersion *string
- ExternalLink *string `jsonapi:"attr,externalLink"`
- Host *string `jsonapi:"attr,host"`
- Port *string `jsonapi:"attr,port"`
- Username *string `jsonapi:"attr,username"`
- Password *string `jsonapi:"attr,password"`
+ Name *string `jsonapi:"attr,name"`
+ EngineVersion *string
+ ExternalLink *string `jsonapi:"attr,externalLink"`
+ Host *string `jsonapi:"attr,host"`
+ Port *string `jsonapi:"attr,port"`
+ Username *string `jsonapi:"attr,username"`
+ Password *string `jsonapi:"attr,password"`
+ UseEmptyPassword bool `jsonapi:"attr,useEmptyPassword"`
}
// Instance migration schema status
diff --git a/api/sql.go b/api/sql.go
index 126e17c8d7..4718925ce0 100644
--- a/api/sql.go
+++ b/api/sql.go
@@ -7,12 +7,13 @@ import (
)
type ConnectionInfo struct {
- Engine db.Type `jsonapi:"attr,engine"`
- 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"`
+ Engine db.Type `jsonapi:"attr,engine"`
+ Host string `jsonapi:"attr,host"`
+ Port string `jsonapi:"attr,port"`
+ Username string `jsonapi:"attr,username"`
+ Password string `jsonapi:"attr,password"`
+ UseEmptyPassword bool `jsonapi:"attr,useEmptyPassword"`
+ InstanceId *int `jsonapi:"attr,instanceId"`
}
type SqlSyncSchema struct {
diff --git a/frontend/src/components/InstanceForm.vue b/frontend/src/components/InstanceForm.vue
index b7dd3db2a1..d3c5d5a1a0 100644
--- a/frontend/src/components/InstanceForm.vue
+++ b/frontend/src/components/InstanceForm.vue
@@ -292,16 +292,39 @@ input[type="number"] {
</div>
<div class="sm:col-span-1 sm:col-start-1">
- <label for="password" class="textlabel block"> Password </label>
+ <div class="flex flex-row items-center space-x-2">
+ <label for="password" class="textlabel block">Password</label>
+ <BBCheckbox
+ :title="'Empty'"
+ :value="state.useEmptyPassword"
+ @toggle="
+ (on) => {
+ state.useEmptyPassword = on;
+ }
+ "
+ />
+ </div>
<input
id="password"
name="password"
type="text"
class="textfield mt-1 w-full"
autocomplete="off"
- placeholder="YOUR_DB_PWD - write only"
- :disabled="!allowEdit"
- :value="create ? state.instance.password : state.updatedPassword"
+ :placeholder="
+ state.useEmptyPassword
+ ? 'No password'
+ : 'YOUR_DB_PWD - write only'
+ "
+ :disabled="!allowEdit || state.useEmptyPassword"
+ :value="
+ create
+ ? state.useEmptyPassword
+ ? ''
+ : state.instance.password
+ : state.useEmptyPassword
+ ? ''
+ : state.updatedPassword
+ "
@input="
create
? (state.instance.password = $event.target.value)
@@ -413,6 +436,7 @@ interface LocalState {
instance: Instance | InstanceCreate;
// Only used in non-create case.
updatedPassword: string;
+ useEmptyPassword: boolean;
showCreateInstanceWarningModal: boolean;
createInstanceWarning: string;
showCreateUserExample: boolean;
@@ -457,6 +481,7 @@ export default {
username: "",
},
updatedPassword: "",
+ useEmptyPassword: false,
showCreateInstanceWarningModal: false,
createInstanceWarning: "",
showCreateUserExample: props.create,
@@ -484,9 +509,11 @@ export default {
});
const valueChanged = computed(() => {
+ console.log("value", state.useEmptyPassword);
return (
!isEqual(state.instance, state.originalInstance) ||
- !isEmpty(state.updatedPassword)
+ !isEmpty(state.updatedPassword) ||
+ state.useEmptyPassword
);
});
@@ -526,7 +553,8 @@ export default {
const connectionInfo: ConnectionInfo = {
engine: state.instance.engine,
username: state.instance.username,
- password: state.instance.password,
+ password: state.useEmptyPassword ? "" : state.instance.password,
+ useEmptyPassword: state.useEmptyPassword,
host: state.instance.host,
port: state.instance.port,
};
@@ -548,10 +576,14 @@ export default {
// Conceptually, data source is the proper place to store connnection info (thinking of DSN)
const doCreate = () => {
state.creatingOrUpdating = true;
+ if (state.useEmptyPassword) {
+ state.instance.password = "";
+ }
store
.dispatch("instance/createInstance", state.instance)
.then((createdInstance) => {
state.creatingOrUpdating = false;
+ state.useEmptyPassword = false;
emit("dismiss");
router.push(`/instance/${instanceSlug(createdInstance)}`);
@@ -568,7 +600,7 @@ export default {
};
const doUpdate = () => {
- const patchedInstance: InstancePatch = {};
+ const patchedInstance: InstancePatch = { useEmptyPassword: false };
if (state.instance.name != state.originalInstance!.name) {
patchedInstance.name = state.instance.name;
}
@@ -584,7 +616,9 @@ export default {
if (state.instance.username != state.originalInstance!.username) {
patchedInstance.username = state.instance.username;
}
- if (!isEmpty(state.updatedPassword)) {
+ if (state.useEmptyPassword) {
+ patchedInstance.useEmptyPassword = true;
+ } else if (!isEmpty(state.updatedPassword)) {
patchedInstance.password = state.updatedPassword;
}
@@ -600,6 +634,7 @@ export default {
// Make hard copy since we are going to make equal comparsion to determine the update button enable state.
state.instance = cloneDeep(state.originalInstance!);
state.updatedPassword = "";
+ state.useEmptyPassword = false;
store.dispatch("notification/pushNotification", {
module: "bytebase",
@@ -624,8 +659,13 @@ export default {
engine: state.instance.engine,
username: state.instance.username,
password: props.create
- ? state.instance.password
+ ? state.useEmptyPassword
+ ? ""
+ : state.instance.password
+ : state.useEmptyPassword
+ ? ""
: state.updatedPassword,
+ useEmptyPassword: state.useEmptyPassword,
host: state.instance.host,
port: state.instance.port,
instanceId: props.create ? undefined : (state.instance as Instance).id,
diff --git a/frontend/src/types/instance.ts b/frontend/src/types/instance.ts
index 7a3a02244f..ef670aaa63 100644
--- a/frontend/src/types/instance.ts
+++ b/frontend/src/types/instance.ts
@@ -79,6 +79,7 @@ export type InstancePatch = {
port?: string;
username?: string;
password?: string;
+ useEmptyPassword: boolean;
};
export type MigrationSchemaStatus = "UNKNOWN" | "OK" | "NOT_EXIST";
diff --git a/frontend/src/types/sql.ts b/frontend/src/types/sql.ts
index 86522f5660..da99d58c59 100644
--- a/frontend/src/types/sql.ts
+++ b/frontend/src/types/sql.ts
@@ -8,6 +8,7 @@ export type ConnectionInfo = {
// In mysql, username can be empty which means anonymous user
username?: string;
password?: string;
+ useEmptyPassword: boolean;
// 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
diff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.go
index 7995aa0e3c..80e37c6724 100644
--- a/plugin/db/mysql/mysql.go
+++ b/plugin/db/mysql/mysql.go
@@ -74,7 +74,10 @@ func (driver *Driver) Open(ctx context.Context, dbType db.Type, config db.Connec
}
loggedDSN := fmt.Sprintf("%s:<<redacted password>>@%s(%s:%s)/%s?%s", config.Username, protocol, config.Host, port, config.Database, strings.Join(params, "&"))
- dsn := fmt.Sprintf("%s:%s@%s(%s:%s)/%s?%s", config.Username, config.Password, protocol, config.Host, port, config.Database, strings.Join(params, "&"))
+ dsn := fmt.Sprintf("%s@%s(%s:%s)/%s?%s", config.Username, protocol, config.Host, port, config.Database, strings.Join(params, "&"))
+ if config.Password != "" {
+ dsn = fmt.Sprintf("%s:%s@%s(%s:%s)/%s?%s", config.Username, config.Password, protocol, config.Host, port, config.Database, strings.Join(params, "&"))
+ }
tlsKey := "db.mysql.tls"
if tlsConfig != nil {
if err := mysql.RegisterTLSConfig(tlsKey, tlsConfig); err != nil {
diff --git a/server/instance.go b/server/instance.go
index a50a0c6699..0f8255290b 100644
--- a/server/instance.go
+++ b/server/instance.go
@@ -127,7 +127,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
}
}
- if instancePatch.Username != nil || instancePatch.Password != nil {
+ if instancePatch.Username != nil || instancePatch.Password != nil || instancePatch.UseEmptyPassword {
instanceFind := &api.InstanceFind{
ID: &id,
}
@@ -153,7 +153,12 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
ID: adminDataSource.ID,
UpdaterId: c.Get(GetPrincipalIdContextKey()).(int),
Username: instancePatch.Username,
- Password: instancePatch.Password,
+ }
+ if instancePatch.Password != nil {
+ dataSourcePatch.Password = instancePatch.Password
+ } else if instancePatch.UseEmptyPassword {
+ password := ""
+ dataSourcePatch.Password = &password
}
_, err = s.DataSourceService.PatchDataSource(ctx, dataSourcePatch)
if err != nil {
diff --git a/server/sql.go b/server/sql.go
index a64206365f..d221486619 100644
--- a/server/sql.go
+++ b/server/sql.go
@@ -22,11 +22,11 @@ func (s *Server) registerSqlRoutes(g *echo.Group) {
}
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 {
+ // Instance detail page has a Test Connection button, if user doesn't input new password and doesn't specify
+ // to use empty 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.UseEmptyPassword && connectionInfo.InstanceId != nil {
adminPassword, err := s.FindInstanceAdminPasswordById(ctx, *connectionInfo.InstanceId)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to retrieve admin password for instance: %d", connectionInfo.InstanceId)).SetInternal(err)
@@ -49,15 +49,11 @@ func (s *Server) registerSqlRoutes(g *echo.Group) {
resultSet := &api.SqlResultSet{}
if err != nil {
- usePassword := "YES"
- if connectionInfo.Password == "" {
- usePassword = "NO"
- }
hostPort := connectionInfo.Host
if connectionInfo.Port != "" {
hostPort += ":" + connectionInfo.Port
}
- resultSet.Error = fmt.Errorf("failed to connect %q for user %q (using password: %s), %w", hostPort, connectionInfo.Username, usePassword, err).Error()
+ resultSet.Error = fmt.Errorf("failed to connect %q for user %q, %w", hostPort, connectionInfo.Username, err).Error()
} else {
defer db.Close(ctx)
if err := db.Ping(ctx); err != nil {