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

*: remove more about tidb/parser/mysql #301

Merged
merged 7 commits into from
Jul 28, 2023
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
4 changes: 4 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ issues:
linters:
- gosec
text: "G402:"
- path: pkg/proxy/net/auth.go
linters:
- gosec
text: "G101:"

linters:
enable:
Expand Down
20 changes: 10 additions & 10 deletions pkg/proxy/backend/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Authenticator struct {
user string
attrs map[string]string
salt []byte
capability uint32 // client capability
capability pnet.Capability
collation uint8
proxyProtocol bool
requireBackendTLS bool
Expand Down Expand Up @@ -125,7 +125,7 @@ func (auth *Authenticator) handshakeFirstTime(logger *zap.Logger, cctx ConnConte
}
if commonCaps := frontendCapability & requiredFrontendCaps; commonCaps != requiredFrontendCaps {
logger.Error("require frontend capabilities", zap.Stringer("common", commonCaps), zap.Stringer("required", requiredFrontendCaps))
if writeErr := clientIO.WriteErrPacket(mysql.NewErr(mysql.ErrNotSupportedAuthMode)); writeErr != nil {
if writeErr := clientIO.WriteErrPacket(mysql.ErrNotSupportedAuthMode); writeErr != nil {
return writeErr
}
return errors.Wrapf(ErrCapabilityNegotiation, "require %s from frontend", requiredFrontendCaps&^commonCaps)
Expand All @@ -134,12 +134,12 @@ func (auth *Authenticator) handshakeFirstTime(logger *zap.Logger, cctx ConnConte
if frontendCapability^commonCaps != 0 {
logger.Debug("frontend send capabilities unsupported by proxy", zap.Stringer("common", commonCaps), zap.Stringer("frontend", frontendCapability^commonCaps), zap.Stringer("proxy", proxyCapability^commonCaps))
}
auth.capability = commonCaps.Uint32()
if auth.capability&mysql.ClientPluginAuth == 0 {
auth.capability = commonCaps
if auth.capability&pnet.ClientPluginAuth == 0 {
logger.Warn("frontend may not support plugin auth", zap.Stringer("capability", commonCaps))
// Some clients (e.g. node/mysql) support ClientAuthPlugin but don't have the capability set correctly.
// Always set it to ensure capability.
auth.capability |= mysql.ClientPluginAuth
auth.capability |= pnet.ClientPluginAuth
}

if isSSL {
Expand Down Expand Up @@ -267,7 +267,7 @@ func (auth *Authenticator) handshakeSecondTime(logger *zap.Logger, clientIO, bac

if err = auth.writeAuthHandshake(
backendIO, backendTLSConfig, backendCapability,
mysql.AuthTiDBSessionToken, hack.Slice(sessionToken), mysql.ClientPluginAuth,
pnet.AuthTiDBSessionToken, hack.Slice(sessionToken), pnet.ClientPluginAuth,
); err != nil {
return err
}
Expand All @@ -293,7 +293,7 @@ func (auth *Authenticator) writeAuthHandshake(
backendCapability pnet.Capability,
authPlugin string,
authData []byte,
authCap uint32,
authCap pnet.Capability,
) error {
// Always handshake with SSL enabled and enable auth_plugin.
resp := &pnet.HandshakeResp{
Expand All @@ -307,7 +307,7 @@ func (auth *Authenticator) writeAuthHandshake(
}

if len(resp.Attrs) > 0 {
resp.Capability |= mysql.ClientConnectAtts
resp.Capability |= pnet.ClientConnectAttrs
}

var pkt []byte
Expand All @@ -322,7 +322,7 @@ func (auth *Authenticator) writeAuthHandshake(
enableTLS = pnet.Capability(auth.capability)&pnet.ClientSSL != 0 && backendCapability&pnet.ClientSSL != 0 && backendTLSConfig != nil
}
if enableTLS {
resp.Capability |= mysql.ClientSSL
resp.Capability |= pnet.ClientSSL
pkt = pnet.MakeHandshakeResponse(resp)
// write SSL Packet
if err := backendIO.WritePacket(pkt[:32], true); err != nil {
Expand All @@ -339,7 +339,7 @@ func (auth *Authenticator) writeAuthHandshake(
return err
}
} else {
resp.Capability &= ^mysql.ClientSSL
resp.Capability &= ^pnet.ClientSSL
pkt = pnet.MakeHandshakeResponse(resp)
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/proxy/backend/backend_conn_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,11 @@ func (mgr *BackendConnManager) ExecuteCmd(ctx context.Context, request []byte) (
val := binary.LittleEndian.Uint16(request[1:])
switch val {
case 0:
mgr.authenticator.capability |= mysql.ClientMultiStatements
mgr.cmdProcessor.capability |= mysql.ClientMultiStatements
mgr.authenticator.capability |= pnet.ClientMultiStatements
mgr.cmdProcessor.capability |= pnet.ClientMultiStatements
case 1:
mgr.authenticator.capability &^= mysql.ClientMultiStatements
mgr.cmdProcessor.capability &^= mysql.ClientMultiStatements
mgr.authenticator.capability &^= pnet.ClientMultiStatements
mgr.cmdProcessor.capability &^= pnet.ClientMultiStatements
default:
err = errors.Errorf("unrecognized set_option value:%d", val)
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/proxy/backend/cmd_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
type CmdProcessor struct {
// Each prepared statement has an independent status.
preparedStmtStatus map[int]uint32
capability uint32
capability pnet.Capability
// Only includes in_trans or quit status.
serverStatus uint32
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/proxy/backend/cmd_processor_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (cp *CmdProcessor) forwardCommand(clientIO, backendIO *pnet.PacketIO, reque
case mysql.ErrHeader:
return cp.handleErrorPacket(response)
case mysql.EOFHeader:
if cp.capability&mysql.ClientDeprecateEOF == 0 {
if cp.capability&pnet.ClientDeprecateEOF == 0 {
cp.handleEOFPacket(request, response)
} else {
cp.handleOKPacket(request, response)
Expand Down Expand Up @@ -111,7 +111,7 @@ func (cp *CmdProcessor) forwardUntilResultEnd(clientIO, backendIO *pnet.PacketIO
}
return 0, cp.handleErrorPacket(response)
}
if cp.capability&mysql.ClientDeprecateEOF == 0 {
if cp.capability&pnet.ClientDeprecateEOF == 0 {
if pnet.IsEOFPacket(response) {
return cp.handleEOFPacket(request, response), clientIO.Flush()
}
Expand All @@ -136,7 +136,7 @@ func (cp *CmdProcessor) forwardPrepareCmd(clientIO, backendIO *pnet.PacketIO) er
numColumns := binary.LittleEndian.Uint16(response[5:])
numParams := binary.LittleEndian.Uint16(response[7:])
expectedPackets := int(numColumns) + int(numParams)
if cp.capability&mysql.ClientDeprecateEOF == 0 {
if cp.capability&pnet.ClientDeprecateEOF == 0 {
if numColumns > 0 {
expectedPackets++
}
Expand Down Expand Up @@ -235,7 +235,7 @@ func (cp *CmdProcessor) forwardLoadInFile(clientIO, backendIO *pnet.PacketIO, re
}

func (cp *CmdProcessor) forwardResultSet(clientIO, backendIO *pnet.PacketIO, request []byte) (uint16, error) {
if cp.capability&mysql.ClientDeprecateEOF == 0 {
if cp.capability&pnet.ClientDeprecateEOF == 0 {
var response []byte
// read columns
for {
Expand Down
4 changes: 2 additions & 2 deletions pkg/proxy/backend/cmd_processor_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (cp *CmdProcessor) readResultColumns(packetIO *pnet.PacketIO, result *gomys

for {
if fieldIndex == len(result.Fields) {
if cp.capability&mysql.ClientDeprecateEOF == 0 {
if cp.capability&pnet.ClientDeprecateEOF == 0 {
if data, err = packetIO.ReadPacket(); err != nil {
return err
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func (cp *CmdProcessor) readResultRows(packetIO *pnet.PacketIO, result *gomysql.
if data, err = packetIO.ReadPacket(); err != nil {
return err
}
if cp.capability&mysql.ClientDeprecateEOF == 0 {
if cp.capability&pnet.ClientDeprecateEOF == 0 {
if pnet.IsEOFPacket(data) {
result.Status = binary.LittleEndian.Uint16(data[3:])
break
Expand Down
18 changes: 9 additions & 9 deletions pkg/proxy/backend/mock_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func newBackendConfig() *backendConfig {
return &backendConfig{
capability: defaultTestBackendCapability,
salt: mockSalt,
authPlugin: mysql.AuthCachingSha2Password,
authPlugin: pnet.AuthCachingSha2Password,
authSucceed: true,
loops: 1,
stmtNum: 1,
Expand Down Expand Up @@ -117,11 +117,11 @@ func (mb *mockBackend) verifyPassword(packetIO *pnet.PacketIO, resp *pnet.Handsh
}
}
if mb.authSucceed {
if err := packetIO.WriteOKPacket(mb.status, mysql.OKHeader); err != nil {
if err := packetIO.WriteOKPacket(mb.status, pnet.OKHeader); err != nil {
return err
}
} else {
if err := packetIO.WriteErrPacket(mysql.NewErr(mysql.ErrAccessDenied)); err != nil {
if err := packetIO.WriteErrPacket(mysql.ErrAccessDenied); err != nil {
return err
}
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func (mb *mockBackend) respondOnce(packetIO *pnet.PacketIO) error {
case responseTypeOK:
return mb.respondOK(packetIO)
case responseTypeErr:
return packetIO.WriteErrPacket(mysql.NewErr(mysql.ErrUnknown))
return packetIO.WriteErrPacket(mysql.ErrUnknown)
case responseTypeResultSet:
if pnet.Command(pkt[0]) == pnet.ComQuery && string(pkt[1:]) == sqlQueryState {
return mb.respondSessionStates(packetIO)
Expand All @@ -171,15 +171,15 @@ func (mb *mockBackend) respondOnce(packetIO *pnet.PacketIO) error {
if _, err := packetIO.ReadPacket(); err != nil {
return err
}
return packetIO.WriteOKPacket(mb.status, mysql.OKHeader)
return packetIO.WriteOKPacket(mb.status, pnet.OKHeader)
case responseTypePrepareOK:
return mb.respondPrepare(packetIO)
case responseTypeRow:
return mb.respondRows(packetIO)
case responseTypeNone:
return nil
}
return packetIO.WriteErrPacket(mysql.NewErr(mysql.ErrUnknown))
return packetIO.WriteErrPacket(mysql.ErrUnknown)
}

func (mb *mockBackend) respondOK(packetIO *pnet.PacketIO) error {
Expand All @@ -190,7 +190,7 @@ func (mb *mockBackend) respondOK(packetIO *pnet.PacketIO) error {
} else {
status &= ^mysql.ServerMoreResultsExists
}
if err := packetIO.WriteOKPacket(status, mysql.OKHeader); err != nil {
if err := packetIO.WriteOKPacket(status, pnet.OKHeader); err != nil {
return err
}
}
Expand All @@ -209,7 +209,7 @@ func (mb *mockBackend) respondColumns(packetIO *pnet.PacketIO) error {

func (mb *mockBackend) writeResultEndPacket(packetIO *pnet.PacketIO, status uint16) error {
if mb.capability&pnet.ClientDeprecateEOF > 0 {
return packetIO.WriteOKPacket(status, mysql.EOFHeader)
return packetIO.WriteOKPacket(status, pnet.EOFHeader)
}
return packetIO.WriteEOFPacket(status)
}
Expand Down Expand Up @@ -312,7 +312,7 @@ func (mb *mockBackend) respondLoadFile(packetIO *pnet.PacketIO) error {
break
}
}
if err := packetIO.WriteOKPacket(status, mysql.OKHeader); err != nil {
if err := packetIO.WriteOKPacket(status, pnet.OKHeader); err != nil {
return err
}
}
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 @@ -78,7 +78,7 @@ func (mc *mockClient) authenticate(packetIO *pnet.PacketIO) error {
AuthPlugin: mc.authPlugin,
Attrs: mc.attrs,
AuthData: mc.authData,
Capability: mc.capability.Uint32(),
Capability: mc.capability,
Collation: mc.collation,
}
pkt = pnet.MakeHandshakeResponse(resp)
Expand Down
2 changes: 1 addition & 1 deletion pkg/proxy/backend/mock_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func newMockProxy(t *testing.T, cfg *proxyConfig) *mockProxy {
CheckBackendInterval: cfg.checkBackendInterval,
}),
}
mp.cmdProcessor.capability = cfg.capability.Uint32()
mp.cmdProcessor.capability = cfg.capability
return mp
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/proxy/net/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2023 PingCAP, Inc.
// SPDX-License-Identifier: Apache-2.0

package net

const (
AuthNativePassword = "mysql_native_password"
AuthCachingSha2Password = "caching_sha2_password"
AuthTiDBSM3Password = "tidb_sm3_password"
AuthMySQLClearPassword = "mysql_clear_password"
AuthSocket = "auth_socket"
AuthTiDBSessionToken = "tidb_session_token"
AuthTiDBAuthToken = "tidb_auth_token"
)
30 changes: 30 additions & 0 deletions pkg/proxy/net/header.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2023 PingCAP, Inc.
// SPDX-License-Identifier: Apache-2.0

package net

type Header byte

// Header information.
const (
OKHeader Header = 0x00
ErrHeader Header = 0xff
EOFHeader Header = 0xfe
AuthSwitchHeader Header = 0xfe
LocalInFileHeader Header = 0xfb
)

var headerStrings = map[Header]string{
OKHeader: "OK",
ErrHeader: "ERR",
EOFHeader: "EOF/AuthSwitch",
LocalInFileHeader: "LOCAL_IN_FILE",
}

func (f Header) Byte() byte {
return byte(f)
}

func (f Header) String() string {
return headerStrings[f]
}
Loading