@@ -27,6 +27,13 @@ import (
2727 "github.com/dolthub/go-mysql-server/sql"
2828)
2929
30+ // DefaultAuthMethod specifies the MySQL auth protocol (e.g. mysql_native_password,
31+ // caching_sha2_password) that is used by default. When the auth server advertises
32+ // what auth protocol it prefers, as part of the auth handshake, it is controlled
33+ // by this constant. When a new user is created, if no auth plugin is specified, this
34+ // auth method will be used.
35+ const DefaultAuthMethod = mysql .MysqlNativePassword
36+
3037// authServer implements the mysql.AuthServer interface. It exposes configured AuthMethod implementations
3138// that the auth framework in Vitess uses to negotiate authentication with a client. By default, authServer
3239// configures support for the mysql_native_password auth plugin, as well as an extensible auth method, built
@@ -42,10 +49,10 @@ var _ mysql.AuthServer = (*authServer)(nil)
4249// mysql_native_password support, as well as an extensible auth method, built on the mysql_clear_password auth
4350// method, that allows integrators to extend authentication to allow additional schemes.
4451func newAuthServer (db * MySQLDb ) * authServer {
45- // The native password auth method allows auth over the mysql_native_password protocol
52+ // mysql_native_password auth support
4653 nativePasswordAuthMethod := mysql .NewMysqlNativeAuthMethod (
4754 & nativePasswordHashStorage {db : db },
48- & nativePasswordUserValidator { db : db } )
55+ newUserValidator ( db , mysql . MysqlNativePassword ) )
4956
5057 // TODO: Add CachingSha2Password AuthMethod
5158
@@ -67,7 +74,7 @@ func (as *authServer) AuthMethods() []mysql.AuthMethod {
6774
6875// DefaultAuthMethodDescription implements the mysql.AuthServer interface.
6976func (db * authServer ) DefaultAuthMethodDescription () mysql.AuthMethodDescription {
70- return mysql . MysqlNativePassword
77+ return DefaultAuthMethod
7178}
7279
7380// extendedAuthPlainTextStorage implements the mysql.PlainTextStorage interface and plugs into
@@ -205,8 +212,8 @@ func (nphs *nativePasswordHashStorage) UserEntryWithHash(_ []*x509.Certificate,
205212 if userEntry == nil || userEntry .Locked {
206213 return nil , mysql .NewSQLError (mysql .ERAccessDeniedError , mysql .SSAccessDeniedError , "Access denied for user '%v'" , user )
207214 }
208- if len (userEntry .Password ) > 0 {
209- if ! validateMysqlNativePassword (authResponse , salt , userEntry .Password ) {
215+ if len (userEntry .AuthString ) > 0 {
216+ if ! validateMysqlNativePassword (authResponse , salt , userEntry .AuthString ) {
210217 return nil , mysql .NewSQLError (mysql .ERAccessDeniedError , mysql .SSAccessDeniedError , "Access denied for user '%v'" , user )
211218 }
212219 } else if len (authResponse ) > 0 {
@@ -218,18 +225,32 @@ func (nphs *nativePasswordHashStorage) UserEntryWithHash(_ []*x509.Certificate,
218225 return sql.MysqlConnectionUser {User : userEntry .User , Host : userEntry .Host }, nil
219226}
220227
221- // nativePasswordUserValidator implements the mysql.UserValidator interface and plugs into the mysql_native_password
222- // auth method in Vitess. This implementation is called by the native password auth method to determine if a specific
223- // user and remote address can connect to this server via the mysql_native_password auth protocol.
224- type nativePasswordUserValidator struct {
228+ // userValidator implements the mysql.UserValidator interface. It looks up a user and host from the
229+ // associated mysql database (|db|) and validates that a user entry exists and that it is configured
230+ // for the specified authentication plugin (|authMethod|).
231+ type userValidator struct {
232+ // db is the mysql database that contains user information
225233 db * MySQLDb
234+
235+ // authMethod is the name of the auth plugin for which this validator will
236+ // validate users.
237+ authMethod mysql.AuthMethodDescription
226238}
227239
228- var _ mysql.UserValidator = (* nativePasswordUserValidator )(nil )
240+ var _ mysql.UserValidator = (* userValidator )(nil )
241+
242+ // newUserValidator creates a new userValidator instance, configured to use |db| to look up user
243+ // entries and validate that they have the specified auth plugin (|authMethod|) configured.
244+ func newUserValidator (db * MySQLDb , authMethod mysql.AuthMethodDescription ) * userValidator {
245+ return & userValidator {
246+ db : db ,
247+ authMethod : authMethod ,
248+ }
249+ }
229250
230251// HandleUser implements the mysql.UserValidator interface and verifies if the mysql_native_password auth method
231252// can be used for the specified |user| at the specified |remoteAddr|.
232- func (uv * nativePasswordUserValidator ) HandleUser (user string , remoteAddr net.Addr ) bool {
253+ func (uv * userValidator ) HandleUser (user string , remoteAddr net.Addr ) bool {
233254 // If the mysql database is not enabled, then we don't have user information, so
234255 // go ahead and return true without trying to look up the user in the db.
235256 if ! uv .db .Enabled () {
@@ -251,7 +272,7 @@ func (uv *nativePasswordUserValidator) HandleUser(user string, remoteAddr net.Ad
251272 }
252273 userEntry := db .GetUser (rd , user , host , false )
253274
254- return userEntry != nil && ( userEntry .Plugin == "" || userEntry . Plugin == string (mysql . MysqlNativePassword ) )
275+ return userEntry != nil && userEntry .Plugin == string (uv . authMethod )
255276}
256277
257278// extractHostAddress extracts the host address from |addr|, checking to see if it is a unix socket, and if
0 commit comments