Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: change user #132

Merged
merged 3 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion pkg/proxy/backend/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
ErrCapabilityNegotiation = errors.New("capability negotiation failed")
)

const unknownAuthPlugin = "auth_unknown_plugin"
const requiredFrontendCaps = pnet.ClientProtocol41
const defRequiredBackendCaps = pnet.ClientDeprecateEOF

Expand Down Expand Up @@ -180,7 +181,7 @@ func (auth *Authenticator) handshakeFirstTime(logger *zap.Logger, clientIO *pnet
}

// Send an unknown auth plugin so that the backend will request the auth data again.
resp.AuthPlugin = "auth_unknown_plugin"
resp.AuthPlugin = unknownAuthPlugin
resp.Capability = auth.capability

if backendCapability&pnet.ClientSSL != 0 && backendTLSConfig != nil {
Expand Down
14 changes: 10 additions & 4 deletions pkg/proxy/backend/cmd_processor_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,21 @@ func (cp *CmdProcessor) executeCmd(request []byte, clientIO, backendIO *pnet.Pac
}
return true, err
}

if err = backendIO.WritePacket(request, true); err != nil {
return false, err
}
return false, cp.forwardCommand(clientIO, backendIO, request)
}

func (cp *CmdProcessor) forwardCommand(clientIO, backendIO *pnet.PacketIO, request []byte) error {
cmd := request[0]
if cmd != mysql.ComChangeUser {
if err := backendIO.WritePacket(request, true); err != nil {
return err
}
} else {
user, db := pnet.ParseChangeUser(request)
if err := backendIO.WritePacket(pnet.MakeChangeUser(user, db, unknownAuthPlugin, nil), true); err != nil {
xhebox marked this conversation as resolved.
Show resolved Hide resolved
return err
}
}
switch cmd {
case mysql.ComStmtPrepare:
return cp.forwardPrepareCmd(clientIO, backendIO)
Expand Down
2 changes: 1 addition & 1 deletion pkg/proxy/backend/mock_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (mc *mockClient) request(packetIO *pnet.PacketIO) error {
}

func (mc *mockClient) requestChangeUser(packetIO *pnet.PacketIO) error {
data := pnet.MakeChangeUser(mc.username, mc.dbName, mc.authData)
data := pnet.MakeChangeUser(mc.username, mc.dbName, mysql.AuthNativePassword, mc.authData)
if err := packetIO.WritePacket(data, true); err != nil {
return err
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/proxy/net/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func MakeHandshakeResponse(resp *HandshakeResp) []byte {
}

// MakeChangeUser creates the data of COM_CHANGE_USER. It's only used for testing.
func MakeChangeUser(username, db string, authData []byte) []byte {
func MakeChangeUser(username, db, authPlugin string, authData []byte) []byte {
length := 1 + len(username) + 1 + len(authData) + 1 + len(db) + 1
data := make([]byte, 0, length)
data = append(data, mysql.ComChangeUser)
Expand All @@ -226,6 +226,13 @@ func MakeChangeUser(username, db string, authData []byte) []byte {
data = append(data, authData...)
data = append(data, []byte(db)...)
data = append(data, 0x00)

// character set
data = append(data, 0x00)
data = append(data, 0x00)

data = append(data, []byte(authPlugin)...)
data = append(data, 0x00)
return data
}

Expand Down