Skip to content

Commit 365e4ab

Browse files
breezewishzz-jason
authored andcommitted
conn: Send auth plugin name (#4177)
1 parent 4cc39c3 commit 365e4ab

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

server/conn.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ import (
5959
var defaultCapability = mysql.ClientLongPassword | mysql.ClientLongFlag |
6060
mysql.ClientConnectWithDB | mysql.ClientProtocol41 |
6161
mysql.ClientTransactions | mysql.ClientSecureConnection | mysql.ClientFoundRows |
62-
mysql.ClientMultiResults | mysql.ClientLocalFiles | mysql.ClientConnectAtts
62+
mysql.ClientMultiResults | mysql.ClientLocalFiles |
63+
mysql.ClientConnectAtts | mysql.ClientPluginAuth
6364

6465
// clientConn represents a connection between server and client, it maintains connection specific state,
6566
// handles client query.
@@ -161,6 +162,9 @@ func (cc *clientConn) writeInitialHandshake() error {
161162
data = append(data, cc.salt[8:]...)
162163
// filler [00]
163164
data = append(data, 0)
165+
// auth-plugin name
166+
data = append(data, []byte("mysql_native_password")...)
167+
data = append(data, 0)
164168
err := cc.writePacket(data)
165169
if err != nil {
166170
return errors.Trace(err)

server/conn_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
package server
1515

1616
import (
17+
"bufio"
18+
"bytes"
19+
"encoding/binary"
20+
1721
. "github.com/pingcap/check"
1822
"github.com/pingcap/tidb/mysql"
1923
)
@@ -97,6 +101,37 @@ func (ts ConnTestSuite) TestIssue1768(c *C) {
97101
c.Assert(len(p.Auth) > 0, IsTrue)
98102
}
99103

104+
func (ts ConnTestSuite) TestInitialHandshake(c *C) {
105+
c.Parallel()
106+
var outBuffer bytes.Buffer
107+
cc := &clientConn{
108+
connectionID: 1,
109+
salt: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10},
110+
pkt: &packetIO{
111+
wb: bufio.NewWriter(&outBuffer),
112+
},
113+
}
114+
err := cc.writeInitialHandshake()
115+
c.Assert(err, IsNil)
116+
117+
expected := new(bytes.Buffer)
118+
expected.WriteByte(0x0a) // Protocol
119+
expected.WriteString(mysql.ServerVersion) // Version
120+
expected.WriteByte(0x00) // NULL
121+
binary.Write(expected, binary.LittleEndian, int32(1)) // Connection ID
122+
expected.Write([]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00}) // Salt
123+
binary.Write(expected, binary.LittleEndian, int16(defaultCapability&0xFFFF)) // Server Capability
124+
expected.WriteByte(uint8(mysql.DefaultCollationID)) // Server Language
125+
binary.Write(expected, binary.LittleEndian, mysql.ServerStatusAutocommit) // Server Status
126+
binary.Write(expected, binary.LittleEndian, int16((defaultCapability>>16)&0xFFFF)) // Extended Server Capability
127+
expected.WriteByte(0x15) // Authentication Plugin Length
128+
expected.Write([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}) // Unused
129+
expected.Write([]byte{0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x00}) // Salt
130+
expected.WriteString("mysql_native_password") // Authentication Plugin
131+
expected.WriteByte(0x00) // NULL
132+
c.Assert(outBuffer.Bytes()[4:], DeepEquals, expected.Bytes())
133+
}
134+
100135
func mapIdentical(m1, m2 map[string]string) bool {
101136
return mapBelong(m1, m2) && mapBelong(m2, m1)
102137
}

0 commit comments

Comments
 (0)