Skip to content

allow to set certain client-side capabilities #616

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
Aug 26, 2021
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
13 changes: 11 additions & 2 deletions client/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,18 @@ func (c *Conn) writeAuthHandshake() error {
if !authPluginAllowed(c.authPluginName) {
return fmt.Errorf("unknow auth plugin name '%s'", c.authPluginName)
}
// Adjust client capability flags based on server support

// Set default client capabilities that reflect the abilities of this library
capability := CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION |
CLIENT_LONG_PASSWORD | CLIENT_TRANSACTIONS | CLIENT_PLUGIN_AUTH | c.capability&CLIENT_LONG_FLAG
CLIENT_LONG_PASSWORD | CLIENT_TRANSACTIONS | CLIENT_PLUGIN_AUTH
// Adjust client capability flags based on server support
capability |= c.capability & CLIENT_LONG_FLAG
// Adjust client capability flags on specific client requests
// Only flags that would make any sense setting and aren't handled elsewhere
// in the library are supported here
capability |= c.ccaps&CLIENT_FOUND_ROWS | c.ccaps&CLIENT_IGNORE_SPACE |
c.ccaps&CLIENT_MULTI_STATEMENTS | c.ccaps&CLIENT_MULTI_RESULTS |
c.ccaps&CLIENT_PS_MULTI_RESULTS | c.ccaps&CLIENT_CONNECT_ATTRS

// To enable TLS / SSL
if c.tlsConfig != nil {
Expand Down
35 changes: 35 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,41 @@ func (s *clientTestSuite) TestConn_Ping(c *C) {
c.Assert(err, IsNil)
}

func (s *clientTestSuite) TestConn_SetCapability(c *C) {
caps := []uint32{
mysql.CLIENT_LONG_PASSWORD,
mysql.CLIENT_FOUND_ROWS,
mysql.CLIENT_LONG_FLAG,
mysql.CLIENT_CONNECT_WITH_DB,
mysql.CLIENT_NO_SCHEMA,
mysql.CLIENT_COMPRESS,
mysql.CLIENT_ODBC,
mysql.CLIENT_LOCAL_FILES,
mysql.CLIENT_IGNORE_SPACE,
mysql.CLIENT_PROTOCOL_41,
mysql.CLIENT_INTERACTIVE,
mysql.CLIENT_SSL,
mysql.CLIENT_IGNORE_SIGPIPE,
mysql.CLIENT_TRANSACTIONS,
mysql.CLIENT_RESERVED,
mysql.CLIENT_SECURE_CONNECTION,
mysql.CLIENT_MULTI_STATEMENTS,
mysql.CLIENT_MULTI_RESULTS,
mysql.CLIENT_PS_MULTI_RESULTS,
mysql.CLIENT_PLUGIN_AUTH,
mysql.CLIENT_CONNECT_ATTRS,
mysql.CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA,
}

for _, cap := range caps {
c.Assert(s.c.ccaps&cap > 0, IsFalse)
s.c.SetCapability(cap)
c.Assert(s.c.ccaps&cap > 0, IsTrue)
s.c.UnsetCapability(cap)
c.Assert(s.c.ccaps&cap > 0, IsFalse)
}
}

// NOTE for MySQL 5.5 and 5.6, server side has to config SSL to pass the TLS test, otherwise, it will throw error that
// MySQL server does not support TLS required by the client. However, for MySQL 5.7 and above, auto generated certificates
// are used by default so that manual config is no longer necessary.
Expand Down
13 changes: 13 additions & 0 deletions client/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ type Conn struct {
tlsConfig *tls.Config
proto string

// server capabilities
capability uint32
// client-set capabilities only
ccaps uint32

status uint16

Expand Down Expand Up @@ -120,6 +123,16 @@ func (c *Conn) Ping() error {
return nil
}

// SetCapability enables the use of a specific capability
func (c *Conn) SetCapability(cap uint32) {
c.ccaps |= cap
}

// UnsetCapability disables the use of a specific capability
func (c *Conn) UnsetCapability(cap uint32) {
c.ccaps &= ^cap
}

// UseSSL: use default SSL
// pass to options when connect
func (c *Conn) UseSSL(insecureSkipVerify bool) {
Expand Down