Skip to content
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
12 changes: 12 additions & 0 deletions server/authentication_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ type AuthenticationHandler interface {
OnAuthFailure(conn *Conn, err error)
}

// ConnectionAuthenticator is an optional extension of AuthenticationHandler for
// authenticating a client from its connection rather than a password, e.g. by
// the client certificate it presented in the TLS handshake.
type ConnectionAuthenticator interface {
// AuthenticateConnection is called after the client's handshake response,
// once any TLS upgrade has completed, and before GetCredential.
// Return true to accept the client without a password exchange; OnAuthSuccess
// is then called as usual. Return false to authenticate with a password.
// Return an error to reject the connection.
AuthenticateConnection(conn *Conn) (bool, error)
}

func NewInMemoryAuthenticationHandler(defaultAuthMethod ...string) *InMemoryAuthenticationHandler {
d := mysql.AUTH_CACHING_SHA2_PASSWORD
if len(defaultAuthMethod) > 0 {
Expand Down
66 changes: 66 additions & 0 deletions server/connection_authenticator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package server

import (
"crypto/tls"
"net"
"testing"

"github.com/go-mysql-org/go-mysql/client"
"github.com/go-mysql-org/go-mysql/mysql"
"github.com/go-mysql-org/go-mysql/test_util/test_keys"
"github.com/stretchr/testify/require"
)

// clientCertificateHandler accepts any client that presents a TLS client certificate, and
// otherwise requires the password "secret".
type clientCertificateHandler struct{}

func (clientCertificateHandler) AuthenticateConnection(c *Conn) (bool, error) {
tlsConn, ok := c.Conn.Conn.(*tls.Conn)
return ok && len(tlsConn.ConnectionState().PeerCertificates) > 0, nil
}

func (clientCertificateHandler) GetCredential(string) (Credential, bool, error) {
return Credential{Passwords: []string{"secret"}, AuthPluginName: mysql.AUTH_CACHING_SHA2_PASSWORD}, true, nil
}

func (clientCertificateHandler) OnAuthSuccess(*Conn) error { return nil }

func (clientCertificateHandler) OnAuthFailure(*Conn, error) {}

func TestConnectionAuthenticatorClientCertificate(t *testing.T) {
cert, err := tls.X509KeyPair(test_keys.CertPem, test_keys.KeyPem)
require.NoError(t, err)
s := NewServer("8.0.12", mysql.DEFAULT_COLLATION_ID, mysql.AUTH_CACHING_SHA2_PASSWORD, test_keys.RSAKey(),
&tls.Config{Certificates: []tls.Certificate{cert}, ClientAuth: tls.RequestClientCert})
l, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
t.Cleanup(func() { l.Close() })
go func() {
for {
conn, err := l.Accept()
if err != nil {
return
}
go func() {
defer conn.Close()
_, _ = s.NewCustomizedConn(conn, clientCertificateHandler{}, &EmptyHandler{})
}()
}
}()

connect := func(password string, certificates ...tls.Certificate) error {
c, err := client.Connect(l.Addr().String(), "user", password, "", func(c *client.Conn) error {
c.SetTLSConfig(&tls.Config{InsecureSkipVerify: true, Certificates: certificates}) // Test certificate only.
return nil
})
if err == nil {
c.Close()
}
return err
}

require.NoError(t, connect("wrong", cert))
require.ErrorContains(t, connect("wrong"), "Access denied")
require.NoError(t, connect("secret"))
}
28 changes: 21 additions & 7 deletions server/handshake_resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ func (c *Conn) readHandshakeResponse() error {
return err
}

if authenticator, ok := c.authHandler.(ConnectionAuthenticator); ok {
authenticated, err := authenticator.AuthenticateConnection(c)
if err != nil {
return err
}
if authenticated {
return c.readConnectAttributes(data, pos)
}
}

cont, err := c.handleAuthMatch()
if err != nil {
return err
Expand All @@ -40,19 +50,23 @@ func (c *Conn) readHandshakeResponse() error {
return nil
}

// read connection attributes
if c.capability&mysql.CLIENT_CONNECT_ATTRS > 0 {
// readAttributes returns new position for further processing of data
_, err = c.readAttributes(data, pos)
if err != nil {
return err
}
if err := c.readConnectAttributes(data, pos); err != nil {
return err
}

// try to authenticate the client
return c.compareAuthData(c.authPluginName, authData)
}

func (c *Conn) readConnectAttributes(data []byte, pos int) error {
if c.capability&mysql.CLIENT_CONNECT_ATTRS == 0 {
return nil
}
// readAttributes returns new position for further processing of data
_, err := c.readAttributes(data, pos)
return err
}

func (c *Conn) readFirstPart() ([]byte, int, error) {
data, err := c.ReadPacket()
if err != nil {
Expand Down
Loading