Skip to content

Commit

Permalink
privilege: fix user change after show grants and add user existed c…
Browse files Browse the repository at this point in the history
…heck for `show grants` (#19568) (#19588)

Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
ti-srebot authored Sep 4, 2020
1 parent 22de543 commit 5f03307
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
5 changes: 2 additions & 3 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/diagnosticspb"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/auth"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/distsql"
Expand Down Expand Up @@ -628,9 +629,7 @@ func (b *executorBuilder) buildShow(v *plannercore.PhysicalShow) Executor {
// Note: "show grants" result are different from "show grants for current_user",
// The former determine privileges with roles, while the later doesn't.
vars := e.ctx.GetSessionVars()
e.User = vars.User
e.User.Hostname = vars.User.AuthHostname
e.User.Username = vars.User.AuthUsername
e.User = &auth.UserIdentity{Username: vars.User.AuthUsername, Hostname: vars.User.AuthHostname}
e.Roles = vars.ActiveRoles
}
if e.Tp == ast.ShowMasterStatus {
Expand Down
22 changes: 22 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/executor"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/privilege/privileges"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
Expand Down Expand Up @@ -161,6 +162,27 @@ func (s *testSuite5) TestShowGrantsPrivilege(c *C) {
tk2.MustQuery("show grants")
}

func (s *testSuite5) TestIssue18878(c *C) {
tk := testkit.NewTestKit(c, s.store)
se, err := session.CreateSession4Test(s.store)
c.Assert(err, IsNil)
c.Assert(se.Auth(&auth.UserIdentity{Username: "root", Hostname: "127.0.0.1", AuthHostname: "%"}, nil, nil), IsTrue)
tk.Se = se
tk.MustQuery("select user()").Check(testkit.Rows("root@127.0.0.1"))
tk.MustQuery("show grants")
tk.MustQuery("select user()").Check(testkit.Rows("root@127.0.0.1"))
err = tk.QueryToErr("show grants for root@127.0.0.1")
c.Assert(err.Error(), Equals, privileges.ErrNonexistingGrant.FastGenByArgs("root", "127.0.0.1").Error())
err = tk.QueryToErr("show grants for root@localhost")
c.Assert(err.Error(), Equals, privileges.ErrNonexistingGrant.FastGenByArgs("root", "localhost").Error())
err = tk.QueryToErr("show grants for root@1.1.1.1")
c.Assert(err.Error(), Equals, privileges.ErrNonexistingGrant.FastGenByArgs("root", "1.1.1.1").Error())
tk.MustExec("create user `show_grants`@`127.0.%`")
err = tk.QueryToErr("show grants for `show_grants`@`127.0.0.1`")
c.Assert(err.Error(), Equals, privileges.ErrNonexistingGrant.FastGenByArgs("show_grants", "127.0.0.1").Error())
tk.MustQuery("show grants for `show_grants`@`127.0.%`")
}

func (s *testSuite5) TestIssue3641(c *C) {
tk := testkit.NewTestKit(c, s.store)
_, err := tk.Exec("show tables;")
Expand Down
14 changes: 13 additions & 1 deletion privilege/privileges/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,19 @@ func (p *MySQLPrivilege) showGrants(user, host string, roles []*auth.RoleIdentit
allRoles := p.FindAllRole(roles)
// Show global grants.
var currentPriv mysql.PrivilegeType
var hasGrantOptionPriv bool = false
var hasGrantOptionPriv, userExists = false, false
// Check whether user exists.
if userList, ok := p.UserMap[user]; ok {
for _, record := range userList {
if host == record.Host || record.hostMatch(host) {
userExists = true
break
}
}
if !userExists {
return gs
}
}
var g string
for _, record := range p.User {
if record.baseRecord.match(user, host) {
Expand Down

0 comments on commit 5f03307

Please sign in to comment.