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

Use caching_sha2_password for proxy auth #24

Merged
merged 1 commit into from
Oct 9, 2024
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
74 changes: 74 additions & 0 deletions authserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package psdbproxy

import (
"crypto/rand"
"net"

"vitess.io/vitess/go/mysql"
querypb "vitess.io/vitess/go/vt/proto/query"
)

// cachingSha2AuthServerNone takes all comers.
type cachingSha2AuthServerNone struct{}

type noneGetter struct{}

// AuthMethods returns the list of registered auth methods
// implemented by this auth server.
func (a *cachingSha2AuthServerNone) AuthMethods() []mysql.AuthMethod {
return []mysql.AuthMethod{&mysqlCachingSha2AuthMethod{}}
}

// DefaultAuthMethodDescription returns MysqlNativePassword as the default
// authentication method for the auth server implementation.
func (a *cachingSha2AuthServerNone) DefaultAuthMethodDescription() mysql.AuthMethodDescription {
return mysql.CachingSha2Password
}

// Get returns the empty string
func (ng *noneGetter) Get() *querypb.VTGateCallerID {
return &querypb.VTGateCallerID{Username: "root"}
}

type mysqlCachingSha2AuthMethod struct{}

func (n *mysqlCachingSha2AuthMethod) Name() mysql.AuthMethodDescription {
return mysql.CachingSha2Password
}

func (n *mysqlCachingSha2AuthMethod) HandleUser(conn *mysql.Conn, user string) bool {
return true
}

func (n *mysqlCachingSha2AuthMethod) AuthPluginData() ([]byte, error) {
salt, err := newSalt()
if err != nil {
return nil, err
}
return append(salt, 0), nil
}

func (n *mysqlCachingSha2AuthMethod) AllowClearTextWithoutTLS() bool {
return true
}

func (n *mysqlCachingSha2AuthMethod) HandleAuthPluginData(c *mysql.Conn, user string, serverAuthPluginData []byte, clientAuthPluginData []byte, remoteAddr net.Addr) (mysql.Getter, error) {
return &noneGetter{}, nil
}

func newSalt() ([]byte, error) {
salt := make([]byte, 20)
if _, err := rand.Read(salt); err != nil {
return nil, err
}

// Salt must be a legal UTF8 string.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes the salt generation to not be uniformly random. It may be better to just hex encode the randomly generated bytes, so we get a uniform random string still.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not matter in practice since it's not actually used. I merely copied what is already there in Vitess (and this likely does the same as MySQL). So if this needs fixing, it should be fixed in Vitess.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hex encoding btw here is worse entropy wise though. Since it means half the entropy from the original source bytes (10 instead of 20) and the final character set is much smaller then.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, yeh, MySQL does do the same thing.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hex encoding btw here is worse entropy wise though. Since it means half the entropy from the original source bytes (10 instead of 20) and the final character set is much smaller then.

Not if all 40 characters are used. 20 bytes worth of entropy, but actually uniformly random.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GrahamCampbell It is not possible to use more characters. This is hardcoded to 20 in the MySQL protocol. There's no flexibility here afaik.

Heh, yeh, MySQL does do the same thing.

Yeah, so not something we will change here or in Vitess then. If you think it's worth it, you could report it to MySQL then.

for i := range len(salt) {
salt[i] &= 0x7f
if salt[i] == '\x00' || salt[i] == '$' {
salt[i]++
}
}

return salt, nil
}
3 changes: 2 additions & 1 deletion cmd/psdbproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/planetscale/psdb/auth"
"github.com/planetscale/psdbproxy"
"github.com/spf13/pflag"
"vitess.io/vitess/go/mysql"
)

var (
Expand Down Expand Up @@ -40,7 +41,7 @@ func main() {

ch := make(chan error)
go func() {
ch <- s.ListenAndServe()
ch <- s.ListenAndServe(mysql.CachingSha2Password)
}()

logger.Info(
Expand Down
18 changes: 14 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Server struct {
mysql.UnimplementedHandler
}

func (s *Server) Serve(l net.Listener) error {
func (s *Server) Serve(l net.Listener, authMethod mysql.AuthMethodDescription) error {
s.ensureSetup()

handler, err := s.handler()
Expand All @@ -37,9 +37,19 @@ func (s *Server) Serve(l net.Listener) error {
return err
}

var auth mysql.AuthServer
switch authMethod {
case mysql.CachingSha2Password:
auth = &cachingSha2AuthServerNone{}
case mysql.MysqlNativePassword:
auth = mysql.NewAuthServerNone()
default:
return fmt.Errorf("unsupported auth method: %v", authMethod)
}

listener, err := mysql.NewListenerWithConfig(mysql.ListenerConfig{
Listener: l,
AuthServer: mysql.NewAuthServerNone(),
AuthServer: auth,
Handler: handler,
ConnReadTimeout: s.ReadTimeout,
ConnWriteTimeout: 30 * time.Second,
Expand All @@ -55,12 +65,12 @@ func (s *Server) Serve(l net.Listener) error {
return nil
}

func (s *Server) ListenAndServe() error {
func (s *Server) ListenAndServe(authMethod mysql.AuthMethodDescription) error {
l, err := net.Listen("tcp", s.Addr)
if err != nil {
return err
}
return s.Serve(l)
return s.Serve(l, authMethod)
}

func (s *Server) Shutdown() {
Expand Down
Loading