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

expression: MySQL compatible current_user function #7801

Merged
merged 8 commits into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 1 addition & 3 deletions expression/builtin_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,12 @@ func (b *builtinCurrentUserSig) Clone() builtinFunc {

// evalString evals a builtinCurrentUserSig.
// See https://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_current-user
// TODO: The value of CURRENT_USER() can differ from the value of USER(). We will finish this after we support grant tables.
func (b *builtinCurrentUserSig) evalString(row chunk.Row) (string, bool, error) {
data := b.ctx.GetSessionVars()
if data == nil || data.User == nil {
return "", true, errors.Errorf("Missing session variable when eval builtin")
}

return data.User.String(), false, nil
return data.User.MatchedIdentityString(), false, nil
}

type userFunctionClass struct {
Expand Down
2 changes: 1 addition & 1 deletion expression/builtin_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (s *testEvaluatorSuite) TestCurrentUser(c *C) {
defer testleak.AfterTest(c)()
ctx := mock.NewContext()
sessionVars := ctx.GetSessionVars()
sessionVars.User = &auth.UserIdentity{Username: "root", Hostname: "localhost"}
sessionVars.User = &auth.UserIdentity{Username: "root", Hostname: "localhost", MatchedUsername: "root", MatchedHostname: "localhost"}

fc := funcs[ast.CurrentUser]
f, err := fc.getFunction(ctx, nil)
Expand Down
6 changes: 3 additions & 3 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2347,13 +2347,13 @@ func (s *testIntegrationSuite) TestInfoBuiltin(c *C) {
// for current_user
sessionVars := tk.Se.GetSessionVars()
originUser := sessionVars.User
sessionVars.User = &auth.UserIdentity{Username: "root", Hostname: "localhost"}
sessionVars.User = &auth.UserIdentity{Username: "root", Hostname: "localhost", MatchedUsername: "root", MatchedHostname: "127.0.%%"}
result = tk.MustQuery("select current_user()")
result.Check(testkit.Rows("root@localhost"))
result.Check(testkit.Rows("root@127.0.%%"))
sessionVars.User = originUser

// for user
sessionVars.User = &auth.UserIdentity{Username: "root", Hostname: "localhost"}
sessionVars.User = &auth.UserIdentity{Username: "root", Hostname: "localhost", MatchedUsername: "root", MatchedHostname: "127.0.%%"}
result = tk.MustQuery("select user()")
result.Check(testkit.Rows("root@localhost"))
sessionVars.User = originUser
Expand Down
4 changes: 4 additions & 0 deletions privilege/privilege.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ type Manager interface {
// ConnectionVerification verifies user privilege for connection.
ConnectionVerification(user, host string, auth, salt []byte) bool
Copy link
Contributor

Choose a reason for hiding this comment

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

How about change this method signature to

// ConnectionVerification verifies user privilege for connection.
// Returns the actual user+host from the privileges table, including any wildcard characters.
ConnectionVerification(user, host string, auth, salt []byte) (string, string, bool)

Then we can remove the ConnectionMatchIdentity method ?


// Returns the actual user+host from the privileges table,
// including any wildcard characters.
ConnectionMatchIdentity(user, host string) (string, string)

// DBIsVisible returns true is the database is visible to current user.
DBIsVisible(db string) bool

Expand Down
13 changes: 13 additions & 0 deletions privilege/privileges/privileges.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ func (p *UserPrivileges) ConnectionVerification(user, host string, authenticatio
return true
}

// ConnectionMatchIdentity return the user+host that matched in the priv cache
func (p *UserPrivileges) ConnectionMatchIdentity(user, host string) (string, string) {
if SkipWithGrant {
// This is MySQL 5.7 compatible behavior
return "skip-grants user", "skip-grants host"
}

mysqlPriv := p.Handle.Get()
record := mysqlPriv.connectionVerification(user, host)
return record.User, record.Host

}

// DBIsVisible implements the Manager interface.
func (p *UserPrivileges) DBIsVisible(db string) bool {
if SkipWithGrant {
Expand Down
8 changes: 6 additions & 2 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1000,16 +1000,20 @@ func (s *session) Auth(user *auth.UserIdentity, authentication []byte, salt []by

// Check IP.
if pm.ConnectionVerification(user.Username, user.Hostname, authentication, salt) {
user.MatchedUsername, user.MatchedHostname = pm.ConnectionMatchIdentity(user.Username, user.Hostname)
Copy link
Contributor

Choose a reason for hiding this comment

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

It could be done once if ConnectionVerification returns the matched user & host.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good. I thought about this some more, and I think we should call it AuthUsername + AuthHostname, since it describes which user/hostname matched for auth purposes. Would you agree?

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree!

s.sessionVars.User = user
return true
}

// Check Hostname.
for _, addr := range getHostByIP(user.Hostname) {
if pm.ConnectionVerification(user.Username, addr, authentication, salt) {
u, h := pm.ConnectionMatchIdentity(user.Username, addr)
s.sessionVars.User = &auth.UserIdentity{
Username: user.Username,
Hostname: addr,
Username: user.Username,
Hostname: addr,
MatchedUsername: u,
MatchedHostname: h,
}
return true
}
Expand Down
14 changes: 11 additions & 3 deletions util/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import (

// UserIdentity represents username and hostname.
type UserIdentity struct {
Username string
Hostname string
CurrentUser bool
Username string
Hostname string
CurrentUser bool
MatchedUsername string
MatchedHostname string
}

// String converts UserIdentity to the format user@host.
Expand All @@ -36,6 +38,12 @@ func (user *UserIdentity) String() string {
return fmt.Sprintf("%s@%s", user.Username, user.Hostname)
}

// MatchedIdentityString returns matched identity in user@host format
func (user *UserIdentity) MatchedIdentityString() string {
// TODO: Escape username and hostname.
return fmt.Sprintf("%s@%s", user.MatchedUsername, user.MatchedHostname)
}

// CheckScrambledPassword check scrambled password received from client.
// The new authentication is performed in following manner:
// SERVER: public_seed=create_random_string()
Expand Down