Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2498,8 +2498,10 @@ func (cc *clientConn) upgradeToTLS(tlsConfig *tls.Config) error {
}

func (cc *clientConn) handleChangeUser(ctx context.Context, data []byte) error {
oldUser, oldDBName, oldAuthPlugin := cc.user, cc.dbname, cc.authPlugin
oldCtx := cc.getCtx()
user, data := parseNullTermString(data)
cc.user = string(hack.String(user))
newUser := string(hack.String(user))
if len(data) < 1 {
return mysql.ErrMalformPacket
}
Expand All @@ -2511,7 +2513,7 @@ func (cc *clientConn) handleChangeUser(ctx context.Context, data []byte) error {
pass := data[:passLen]
data = data[passLen:]
dbName, data := parseNullTermString(data)
cc.dbname = string(hack.String(dbName))
newDBName := string(hack.String(dbName))
pluginName := ""
if len(data) > 0 {
// skip character set
Expand All @@ -2524,13 +2526,22 @@ func (cc *clientConn) handleChangeUser(ctx context.Context, data []byte) error {
}
}

if err := cc.ctx.Close(); err != nil {
logutil.Logger(ctx).Debug("close old context failed", zap.Error(err))
}
// session was closed by `ctx.Close` and should `openSession` explicitly to renew session.
// `openSession` won't run again in `openSessionAndDoAuth` because ctx is not nil.
cc.user = newUser
cc.dbname = newDBName
err := cc.openSession()
restoreOldSession := func() {
if newCtx := cc.getCtx(); newCtx != nil && newCtx != oldCtx {
if err := newCtx.Close(); err != nil {
logutil.Logger(ctx).Debug("close new context failed", zap.Error(err))
}
}
cc.setCtx(oldCtx)
cc.user = oldUser
cc.dbname = oldDBName
cc.authPlugin = oldAuthPlugin
}
if err != nil {
restoreOldSession()
return err
}
fakeResp := &handshakeResponse41{
Expand All @@ -2540,19 +2551,27 @@ func (cc *clientConn) handleChangeUser(ctx context.Context, data []byte) error {
}
if fakeResp.AuthPlugin != "" {
failpoint.Inject("ChangeUserAuthSwitch", func(val failpoint.Value) {
restoreOldSession()
failpoint.Return(errors.Errorf("%v", val))
})
newpass, err := cc.checkAuthPlugin(ctx, fakeResp)
if err != nil {
restoreOldSession()
return err
}
if len(newpass) > 0 {
fakeResp.Auth = newpass
}
}
if err := cc.openSessionAndDoAuth(fakeResp.Auth, fakeResp.AuthPlugin); err != nil {
restoreOldSession()
return err
}
if oldCtx != nil {
if err := oldCtx.Close(); err != nil {
logutil.Logger(ctx).Debug("close old context failed", zap.Error(err))
}
}
return cc.handleCommonConnectionReset(ctx)
}

Expand Down
50 changes: 50 additions & 0 deletions server/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,56 @@ func TestChangeUserAuth(t *testing.T) {
err = cc.handleChangeUser(ctx, data)
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/server/ChangeUserAuthSwitch"))
require.EqualError(t, err, t.Name())
require.Same(t, tc, cc.getCtx())
require.Equal(t, "root", cc.user)
}

func TestChangeUserAuthFailureRestoresOldSession(t *testing.T) {
store := testkit.CreateMockStore(t)

cfg := newTestConfig()
cfg.Port = 0
cfg.Status.StatusPort = 0

drv := NewTiDBDriver(store)
srv, err := NewServer(cfg, drv)
require.NoError(t, err)
defer srv.Close()

cc := &clientConn{
connectionID: 1,
alloc: arena.NewAllocator(1024),
chunkAlloc: chunk.NewAllocator(),
peerHost: "localhost",
collation: mysql.DefaultCollationID,
capability: mysql.ClientProtocol41,
pkt: &packetIO{bufWriter: bufio.NewWriter(bytes.NewBuffer(nil))},
server: srv,
user: "root",
dbname: "old_db",
}
se, err := session.CreateSession4Test(store)
require.NoError(t, err)
require.NoError(t, se.Auth(&auth.UserIdentity{Username: "root", Hostname: "localhost"}, nil, nil))
tc := &TiDBContext{
Session: se,
stmts: make(map[int]*TiDBStatement),
}
cc.setCtx(tc)

data := []byte{}
data = append(data, "missing_user"...)
data = append(data, 0)
data = append(data, 0)
data = append(data, "new_db"...)
data = append(data, 0)
data = append(data, 0, 0)
err = cc.handleChangeUser(context.Background(), data)
require.Error(t, err)
require.Same(t, tc, cc.getCtx())
require.Equal(t, "root", cc.user)
require.Equal(t, "old_db", cc.dbname)
require.Equal(t, "root", cc.ctx.GetSessionVars().User.Username)
}

func TestAuthPlugin2(t *testing.T) {
Expand Down
Loading