Skip to content

Commit

Permalink
backend: update db in the handshake (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
djshow832 authored Sep 30, 2022
1 parent 3b6c664 commit 876ba28
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
3 changes: 0 additions & 3 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ issues:
linters:
- gosec
text: "G402:"
- linters:
- unused
source: "updateAuthInfoFromSessionStates"

linters:
enable:
Expand Down
4 changes: 4 additions & 0 deletions pkg/proxy/backend/backend_conn_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/pingcap/TiProxy/pkg/manager/router"
pnet "github.com/pingcap/TiProxy/pkg/proxy/net"
"github.com/pingcap/tidb/parser/mysql"
"github.com/siddontang/go/hack"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -265,6 +266,9 @@ func (mgr *BackendConnManager) tryRedirect(ctx context.Context) {
if sessionStates, sessionToken, rs.err = mgr.querySessionStates(); rs.err != nil {
return
}
if rs.err = mgr.updateAuthInfoFromSessionStates(hack.Slice(sessionStates)); rs.err != nil {
return
}

newConn := NewBackendConnection(rs.to)
if rs.err = newConn.Connect(); rs.err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/proxy/backend/backend_conn_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,10 @@ func TestSpecialCmds(t *testing.T) {
return nil
},
backend: func(packetIO *pnet.PacketIO) error {
ts.mb.sessionStates = "{\"current-db\":\"session_db\"}"
require.NoError(t, ts.redirectSucceed4Backend(packetIO))
require.Equal(t, "another_user", ts.mb.username)
require.Equal(t, "another_db", ts.mb.db)
require.Equal(t, "session_db", ts.mb.db)
expectCap := pnet.Capability(ts.mp.authenticator.supportedServerCapabilities.Uint32() &^ (mysql.ClientMultiStatements | mysql.ClientPluginAuthLenencClientData))
gotCap := pnet.Capability(ts.mb.clientCapability &^ mysql.ClientPluginAuthLenencClientData)
require.Equal(t, expectCap, gotCap, "expected=%s,got=%s", expectCap, gotCap)
Expand Down
48 changes: 24 additions & 24 deletions pkg/proxy/backend/mock_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,33 @@ import (
)

type backendConfig struct {
// for auth
tlsConfig *tls.Config
authPlugin string
salt []byte
columns int
loops int
params int
rows int
respondType respondType // for cmd
stmtNum int
capability uint32
status uint16
authSucceed bool
switchAuth bool
// for both auth and cmd
abnormalExit bool
tlsConfig *tls.Config
authPlugin string
sessionStates string
salt []byte
columns int
loops int
params int
rows int
respondType respondType
stmtNum int
capability uint32
status uint16
authSucceed bool
switchAuth bool
abnormalExit bool
}

func newBackendConfig() *backendConfig {
return &backendConfig{
capability: defaultTestBackendCapability,
salt: mockSalt,
authPlugin: mysql.AuthCachingSha2Password,
switchAuth: true,
authSucceed: true,
loops: 1,
stmtNum: 1,
capability: defaultTestBackendCapability,
salt: mockSalt,
authPlugin: mysql.AuthCachingSha2Password,
switchAuth: true,
authSucceed: true,
loops: 1,
stmtNum: 1,
sessionStates: mockSessionStates,
}
}

Expand Down Expand Up @@ -372,7 +372,7 @@ func (mb *mockBackend) respondSessionStates(packetIO *pnet.PacketIO) error {
names := []string{sessionStatesCol, sessionTokenCol}
values := [][]any{
{
mockSessionStates, mockCmdStr,
mb.sessionStates, mockCmdStr,
},
}
return mb.writeResultSet(packetIO, names, values)
Expand Down

0 comments on commit 876ba28

Please sign in to comment.