Skip to content

Commit

Permalink
backend: ignore parsing attrs error during handshake (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
djshow832 authored May 19, 2023
1 parent 1538fcd commit 2f5bfd8
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 8 deletions.
17 changes: 17 additions & 0 deletions lib/util/errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,20 @@ func (e *Error) As(target interface{}) bool {
func (e *Error) Unwrap() error {
return errors.Unwrap(e.err)
}

type Warning struct {
Err error
}

func (e *Warning) Error() string {
return e.Err.Error()
}

func (e *Warning) Unwrap() error {
return e.Err
}

func (e *Warning) Is(target error) bool {
_, ok := target.(*Warning)
return ok
}
7 changes: 7 additions & 0 deletions lib/util/errors/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ func TestUnwrap(t *testing.T) {
require.ErrorIs(t, e2, e1, "stacktrace does not affect Is")
require.ErrorAs(t, e2, &e1, "stacktrace does not affect As")
}

func TestWarning(t *testing.T) {
warning := &serr.Warning{Err: serr.Wrapf(serr.New("internal err"), "msg")}
var w *serr.Warning
require.True(t, serr.As(warning, &w))
require.Equal(t, "internal err: msg", warning.Error())
}
8 changes: 7 additions & 1 deletion pkg/proxy/backend/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,13 @@ func (auth *Authenticator) handshakeFirstTime(logger *zap.Logger, cctx ConnConte
if isSSL {
cctx.SetValue(ConnContextKeyTLSState, clientIO.TLSConnectionState())
}
clientResp := pnet.ParseHandshakeResponse(pkt)
clientResp, err := pnet.ParseHandshakeResponse(pkt)
var warning *errors.Warning
if errors.As(err, &warning) {
logger.Warn("parse handshake response encounters error", zap.Error(err))
} else if err != nil {
return WrapUserError(err, parsePktErrMsg)
}
if err = handshakeHandler.HandleHandshakeResp(cctx, clientResp); err != nil {
return WrapUserError(err, err.Error())
}
Expand Down
1 change: 1 addition & 0 deletions pkg/proxy/backend/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

const (
connectErrMsg = "No available TiDB instances, please check TiDB cluster"
parsePktErrMsg = "TiProxy fails to parse the packet, please contact PingCAP"
handshakeErrMsg = "TiProxy fails to connect to TiDB, please check network"
capabilityErrMsg = "Verify TiDB capability failed, please upgrade TiDB"
)
Expand Down
5 changes: 4 additions & 1 deletion pkg/proxy/backend/mock_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ func (mb *mockBackend) authenticate(packetIO *pnet.PacketIO) error {
return err
}
}
resp := pnet.ParseHandshakeResponse(clientPkt)
resp, err := pnet.ParseHandshakeResponse(clientPkt)
if err != nil {
return err
}
mb.username = resp.User
mb.db = resp.DB
mb.authData = resp.AuthData
Expand Down
10 changes: 5 additions & 5 deletions pkg/proxy/net/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type HandshakeResp struct {
Collation uint8
}

func ParseHandshakeResponse(data []byte) *HandshakeResp {
func ParseHandshakeResponse(data []byte) (*HandshakeResp, error) {
resp := new(HandshakeResp)
pos := 0
// capability
Expand Down Expand Up @@ -138,18 +138,18 @@ func ParseHandshakeResponse(data []byte) *HandshakeResp {
}

// attrs
var err error
if resp.Capability&mysql.ClientConnectAtts > 0 {
if num, null, off := ParseLengthEncodedInt(data[pos:]); !null {
pos += off
row := data[pos : pos+int(num)]
attrs, err := parseAttrs(row)
resp.Attrs, err = parseAttrs(row)
if err != nil {
return nil
err = &errors.Warning{Err: errors.Wrapf(err, "parse attrs failed")}
}
resp.Attrs = attrs
}
}
return resp
return resp, err
}

func parseAttrs(data []byte) (map[string]string, error) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/proxy/net/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestHandshakeResp(t *testing.T) {
Collation: 0,
}
b := MakeHandshakeResponse(resp1)
resp2 := ParseHandshakeResponse(b)
resp2, err := ParseHandshakeResponse(b)
require.Equal(t, resp1, resp2)
require.NoError(t, err)
}

0 comments on commit 2f5bfd8

Please sign in to comment.