Skip to content

support client connection attributes on the server side #672

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

Merged
merged 1 commit into from
Feb 5, 2022
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
5 changes: 5 additions & 0 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Conn struct {
capability uint32
charset uint8
authPluginName string
attributes map[string]string
connectionID uint32
status uint16
warnings uint16
Expand Down Expand Up @@ -160,6 +161,10 @@ func (c *Conn) Charset() uint8 {
return c.charset
}

func (c *Conn) Attributes() map[string]string {
return c.attributes
}

func (c *Conn) ConnectionID() uint32 {
return c.connectionID
}
Expand Down
41 changes: 40 additions & 1 deletion server/handshake_resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ func (c *Conn) readHandshakeResponse() error {
return nil
}

// ignore connect attrs for now, the proxy does not support passing attrs to actual MySQL server
// read connection attributes
if c.capability&CLIENT_CONNECT_ATTRS > 0 {
// readAttributes returns new position for further processing of data
_, err = c.readAttributes(data, pos)
if err != nil {
return err
}
}

// try to authenticate the client
return c.compareAuthData(c.authPluginName, authData)
Expand Down Expand Up @@ -198,3 +205,35 @@ func (c *Conn) handleAuthMatch(authData []byte, pos int) (bool, error) {
}
return true, nil
}

func (c *Conn) readAttributes(data []byte, pos int) (int, error) {
attrs := make(map[string]string)
i := 0
var key string

for {
str, isNull, strLen, err := LengthEncodedString(data[pos:])

if err != nil {
return -1, err
}

if isNull {
break
}

// reading keys or values
if i%2 == 0 {
key = string(str)
} else {
attrs[key] = string(str)
}

pos += strLen
i++
}

c.attributes = attrs

return pos, nil
}
55 changes: 55 additions & 0 deletions server/handshake_resp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,58 @@ func TestDecodeFirstPart(t *testing.T) {
t.Fatalf("unexpected capability, got %d", c.capability)
}
}

func TestReadAttributes(t *testing.T) {
var err error
// example data from
// https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse41
data := []byte{
0xb2, 0x00, 0x00, 0x01, 0x85, 0xa2, 0x1e, 0x00, 0x00, 0x00,
0x00, 0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x72, 0x6f, 0x6f, 0x74, 0x00, 0x14, 0x22, 0x50, 0x79, 0xa2,
0x12, 0xd4, 0xe8, 0x82, 0xe5, 0xb3, 0xf4, 0x1a, 0x97, 0x75, 0x6b, 0xc8,
0xbe, 0xdb, 0x9f, 0x80, 0x6d, 0x79, 0x73, 0x71, 0x6c, 0x5f, 0x6e, 0x61,
0x74, 0x69, 0x76, 0x65, 0x5f, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72,
0x64, 0x00, 0x61, 0x03, 0x5f, 0x6f, 0x73, 0x09, 0x64, 0x65, 0x62, 0x69,
0x61, 0x6e, 0x36, 0x2e, 0x30, 0x0c, 0x5f, 0x63, 0x6c, 0x69, 0x65, 0x6e,
0x74, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x08, 0x6c, 0x69, 0x62, 0x6d, 0x79,
0x73, 0x71, 0x6c, 0x04, 0x5f, 0x70, 0x69, 0x64, 0x05, 0x32, 0x32, 0x33,
0x34, 0x34, 0x0f, 0x5f, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x76,
0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x08, 0x35, 0x2e, 0x36, 0x2e, 0x36,
0x2d, 0x6d, 0x39, 0x09, 0x5f, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72,
0x6d, 0x06, 0x78, 0x38, 0x36, 0x5f, 0x36, 0x34, 0x03, 0x66, 0x6f, 0x6f,
0x03, 0x62, 0x61, 0x72,
}
pos := 85

c := &Conn{}

pos, err = c.readAttributes(data, pos)
if err != nil {
t.Fatalf("unexpected error: got %v", err)
}

if pos != 182 {
t.Fatalf("unexpected position: got %d", pos)
}

if len(c.attributes) != 6 {
t.Fatalf("unexpected attribute length: got %d", len(c.attributes))
}

fixture := map[string]string{
"_client_name": "libmysql",
"_client_version": "5.6.6-m9",
"_os": "debian6.0",
"_pid": "22344",
"_platform": "x86_64",
"foo": "bar",
}

for k, v := range fixture {
if vv := c.attributes[k]; vv != v {
t.Fatalf("unexpected value for %s, got %s instead of %s", k, vv, v)
}
}
}
3 changes: 2 additions & 1 deletion server/server_conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ func NewServer(serverVersion string, collationId uint8, defaultAuthMethod string
// panic(fmt.Sprintf("default auth method is not one of the allowed auth methods"))
//}
var capFlag = CLIENT_LONG_PASSWORD | CLIENT_LONG_FLAG | CLIENT_CONNECT_WITH_DB | CLIENT_PROTOCOL_41 |
CLIENT_TRANSACTIONS | CLIENT_SECURE_CONNECTION | CLIENT_PLUGIN_AUTH | CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA
CLIENT_TRANSACTIONS | CLIENT_SECURE_CONNECTION | CLIENT_PLUGIN_AUTH | CLIENT_CONNECT_ATTRS |
CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA
if tlsConfig != nil {
capFlag |= CLIENT_SSL
}
Expand Down