-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
privilege, session, server: consistently map user login to identity (#30204) #30450
Merged
ti-chi-bot
merged 3 commits into
pingcap:release-5.3
from
ti-srebot:release-5.3-7fc6ebbda4dd
Feb 21, 2022
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -211,6 +211,9 @@ func (cc *clientConn) String() string { | |
// https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchRequest | ||
// https://bugs.mysql.com/bug.php?id=93044 | ||
func (cc *clientConn) authSwitchRequest(ctx context.Context, plugin string) ([]byte, error) { | ||
failpoint.Inject("FakeAuthSwitch", func() { | ||
failpoint.Return([]byte(plugin), nil) | ||
}) | ||
enclen := 1 + len(plugin) + 1 + len(cc.salt) + 1 | ||
data := cc.alloc.AllocWithLen(4, enclen) | ||
data = append(data, mysql.AuthSwitchRequest) // switch request | ||
|
@@ -708,40 +711,29 @@ func (cc *clientConn) readOptionalSSLRequestAndHandshakeResponse(ctx context.Con | |
|
||
func (cc *clientConn) handleAuthPlugin(ctx context.Context, resp *handshakeResponse41) error { | ||
if resp.Capability&mysql.ClientPluginAuth > 0 { | ||
newAuth, err := cc.checkAuthPlugin(ctx, &resp.AuthPlugin) | ||
newAuth, err := cc.checkAuthPlugin(ctx, resp) | ||
if err != nil { | ||
logutil.Logger(ctx).Warn("failed to check the user authplugin", zap.Error(err)) | ||
return err | ||
} | ||
if len(newAuth) > 0 { | ||
resp.Auth = newAuth | ||
} | ||
|
||
switch resp.AuthPlugin { | ||
case mysql.AuthCachingSha2Password: | ||
resp.Auth, err = cc.authSha(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
case mysql.AuthNativePassword: | ||
case mysql.AuthSocket: | ||
default: | ||
logutil.Logger(ctx).Warn("Unknown Auth Plugin", zap.String("plugin", resp.AuthPlugin)) | ||
} | ||
} else { | ||
// MySQL 5.1 and older clients don't support authentication plugins. | ||
logutil.Logger(ctx).Warn("Client without Auth Plugin support; Please upgrade client") | ||
if cc.ctx == nil { | ||
err := cc.openSession() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
userplugin, err := cc.ctx.AuthPluginForUser(&auth.UserIdentity{Username: cc.user, Hostname: cc.peerHost}) | ||
_, err := cc.checkAuthPlugin(ctx, resp) | ||
if err != nil { | ||
return err | ||
} | ||
if userplugin != mysql.AuthNativePassword && userplugin != "" { | ||
return errNotSupportedAuthMode | ||
} | ||
Comment on lines
-732
to
-744
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes are also based on master, and |
||
resp.AuthPlugin = mysql.AuthNativePassword | ||
} | ||
return nil | ||
|
@@ -845,7 +837,7 @@ func (cc *clientConn) openSessionAndDoAuth(authData []byte, authPlugin string) e | |
} | ||
|
||
// Check if the Authentication Plugin of the server, client and user configuration matches | ||
func (cc *clientConn) checkAuthPlugin(ctx context.Context, authPlugin *string) ([]byte, error) { | ||
func (cc *clientConn) checkAuthPlugin(ctx context.Context, resp *handshakeResponse41) ([]byte, error) { | ||
// Open a context unless this was done before. | ||
if cc.ctx == nil { | ||
err := cc.openSession() | ||
|
@@ -854,22 +846,54 @@ func (cc *clientConn) checkAuthPlugin(ctx context.Context, authPlugin *string) ( | |
} | ||
} | ||
|
||
userplugin, err := cc.ctx.AuthPluginForUser(&auth.UserIdentity{Username: cc.user, Hostname: cc.peerHost}) | ||
authData := resp.Auth | ||
hasPassword := "YES" | ||
if len(authData) == 0 { | ||
hasPassword = "NO" | ||
} | ||
host, _, err := cc.PeerHost(hasPassword) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Find the identity of the user based on username and peer host. | ||
identity, err := cc.ctx.MatchIdentity(cc.user, host) | ||
if err != nil { | ||
return nil, errAccessDenied.FastGenByArgs(cc.user, host, hasPassword) | ||
} | ||
// Get the plugin for the identity. | ||
userplugin, err := cc.ctx.AuthPluginForUser(identity) | ||
if err != nil { | ||
logutil.Logger(ctx).Warn("Failed to get authentication method for user", | ||
zap.String("user", cc.user), zap.String("host", host)) | ||
} | ||
failpoint.Inject("FakeUser", func(val failpoint.Value) { | ||
userplugin = val.(string) | ||
}) | ||
if userplugin == mysql.AuthSocket { | ||
*authPlugin = mysql.AuthSocket | ||
if !cc.isUnixSocket { | ||
return nil, errAccessDenied.FastGenByArgs(cc.user, host, hasPassword) | ||
} | ||
resp.AuthPlugin = mysql.AuthSocket | ||
user, err := user.LookupId(fmt.Sprint(cc.socketCredUID)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return []byte(user.Username), nil | ||
} | ||
if len(userplugin) == 0 { | ||
logutil.Logger(ctx).Warn("No user plugin set, assuming MySQL Native Password", | ||
zap.String("user", cc.user), zap.String("host", cc.peerHost)) | ||
*authPlugin = mysql.AuthNativePassword | ||
// No user plugin set, assuming MySQL Native Password | ||
// This happens if the account doesn't exist or if the account doesn't have | ||
// a password set. | ||
if resp.AuthPlugin != mysql.AuthNativePassword { | ||
if resp.Capability&mysql.ClientPluginAuth > 0 { | ||
resp.AuthPlugin = mysql.AuthNativePassword | ||
authData, err := cc.authSwitchRequest(ctx, mysql.AuthNativePassword) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return authData, nil | ||
} | ||
} | ||
return nil, nil | ||
} | ||
|
||
|
@@ -878,13 +902,18 @@ func (cc *clientConn) checkAuthPlugin(ctx context.Context, authPlugin *string) ( | |
// or if the authentication method send by the server doesn't match the authentication | ||
// method send by the client (*authPlugin) then we need to switch the authentication | ||
// method to match the one configured for that specific user. | ||
if (cc.authPlugin != userplugin) || (cc.authPlugin != *authPlugin) { | ||
authData, err := cc.authSwitchRequest(ctx, userplugin) | ||
if err != nil { | ||
return nil, err | ||
if (cc.authPlugin != userplugin) || (cc.authPlugin != resp.AuthPlugin) { | ||
if resp.Capability&mysql.ClientPluginAuth > 0 { | ||
authData, err := cc.authSwitchRequest(ctx, userplugin) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp.AuthPlugin = userplugin | ||
return authData, nil | ||
} else if userplugin != mysql.AuthNativePassword { | ||
// MySQL 5.1 and older don't support authentication plugins yet | ||
return nil, errNotSupportedAuthMode | ||
} | ||
*authPlugin = userplugin | ||
return authData, nil | ||
} | ||
|
||
return nil, nil | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is intentional. It was removed from master, I believe because this is handled in
cc.checkAuthPlugin
instead. The code could be cleaned up slightly, but that's for another PR.