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

privilege: add privilege check for show stats (#19702) #19759

Merged
merged 9 commits into from
Sep 17, 2020
24 changes: 24 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,30 @@ func (s *testSuite2) TestShowGrantsPrivilege(c *C) {
tk2.MustQuery("show grants")
}

func (s *testSuite2) TestShowStatsPrivilege(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("create user show_stats")
tk1 := testkit.NewTestKit(c, s.store)
se, err := session.CreateSession4Test(s.store)
c.Assert(err, IsNil)
c.Assert(se.Auth(&auth.UserIdentity{Username: "show_stats", Hostname: "%"}, nil, nil), IsTrue)
tk1.Se = se
eqErr := plannercore.ErrDBaccessDenied.GenWithStackByArgs("show_stats", "%", mysql.SystemDB)
_, err = tk1.Exec("show stats_meta")
c.Assert(err.Error(), Equals, eqErr.Error())
_, err = tk1.Exec("SHOW STATS_BUCKETS")
c.Assert(err.Error(), Equals, eqErr.Error())
_, err = tk1.Exec("SHOW STATS_HEALTHY")
c.Assert(err.Error(), Equals, eqErr.Error())
_, err = tk1.Exec("SHOW STATS_HISTOGRAMS")
c.Assert(err.Error(), Equals, eqErr.Error())
tk.MustExec("grant select on mysql.* to show_stats")
tk1.MustExec("show stats_meta")
tk1.MustExec("SHOW STATS_BUCKETS")
tk1.MustExec("SHOW STATS_HEALTHY")
tk1.MustExec("SHOW STATS_HISTOGRAMS")
}

func (s *testSuite2) TestIssue18878(c *C) {
errNonexistingGrant := terror.ClassPrivilege.New(mysql.ErrNonexistingGrant, mysql.MySQLErrName[mysql.ErrNonexistingGrant])
tk := testkit.NewTestKit(c, s.store)
Expand Down
7 changes: 7 additions & 0 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,13 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (Plan,
case ast.ShowCreateView:
err := ErrSpecificAccessDenied.GenWithStackByArgs("SHOW VIEW")
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.ShowViewPriv, show.Table.Schema.L, show.Table.Name.L, "", err)
case ast.ShowStatsBuckets, ast.ShowStatsHistograms, ast.ShowStatsMeta, ast.ShowStatsHealthy:
user := b.ctx.GetSessionVars().User
var err error
if user != nil {
err = ErrDBaccessDenied.GenWithStackByArgs(user.AuthUsername, user.AuthHostname, mysql.SystemDB)
}
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SelectPriv, mysql.SystemDB, "", "", err)
}
p.SetSchema(buildShowSchema(show, isView))
}
Expand Down