Skip to content

Commit e2426c3

Browse files
tiancaiamaozz-jason
authored andcommitted
executor: let flush privileges do nothing when `skip-grant-tab… (#11027)
1 parent b43bdb7 commit e2426c3

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

executor/executor_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ var _ = Suite(&testBypassSuite{})
9494
var _ = Suite(&testUpdateSuite{})
9595
var _ = Suite(&testOOMSuite{})
9696
var _ = Suite(&testPointGetSuite{})
97+
var _ = Suite(&testFlushSuite{})
9798

9899
type testSuite struct {
99100
cluster *mocktikv.Cluster

executor/simple.go

+7
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,13 @@ func (e *SimpleExec) executeFlush(s *ast.FlushStmt) error {
812812
return errors.New("FLUSH TABLES WITH READ LOCK is not supported. Please use @@tidb_snapshot")
813813
}
814814
case ast.FlushPrivileges:
815+
// If skip-grant-table is configured, do not flush privileges.
816+
// Because LoadPrivilegeLoop does not run and the privilege Handle is nil,
817+
// Call dom.PrivilegeHandle().Update would panic.
818+
if config.GetGlobalConfig().Security.SkipGrantTable {
819+
return nil
820+
}
821+
815822
dom := domain.GetDomain(e.ctx)
816823
sysSessionPool := dom.SysSessionPool()
817824
ctx, err := sysSessionPool.Get()

executor/simple_test.go

+29-2
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@ package executor_test
1616
import (
1717
"context"
1818

19-
"github.com/pingcap/tidb/planner/core"
20-
2119
. "github.com/pingcap/check"
2220
"github.com/pingcap/parser/auth"
2321
"github.com/pingcap/parser/model"
2422
"github.com/pingcap/parser/mysql"
2523
"github.com/pingcap/parser/terror"
24+
"github.com/pingcap/tidb/config"
2625
"github.com/pingcap/tidb/domain"
2726
"github.com/pingcap/tidb/executor"
27+
"github.com/pingcap/tidb/planner/core"
2828
"github.com/pingcap/tidb/session"
2929
"github.com/pingcap/tidb/sessionctx"
30+
"github.com/pingcap/tidb/store/mockstore"
31+
"github.com/pingcap/tidb/store/mockstore/mocktikv"
3032
"github.com/pingcap/tidb/util/testkit"
3133
"github.com/pingcap/tidb/util/testutil"
3234
)
@@ -386,6 +388,31 @@ func (s *testSuite3) TestFlushPrivileges(c *C) {
386388
// After flush.
387389
_, err = se.Execute(ctx, `SELECT Password FROM mysql.User WHERE User="testflush" and Host="localhost"`)
388390
c.Check(err, IsNil)
391+
392+
}
393+
394+
type testFlushSuite struct{}
395+
396+
func (s *testFlushSuite) TestFlushPrivilegesPanic(c *C) {
397+
// Run in a separate suite because this test need to set SkipGrantTable config.
398+
cluster := mocktikv.NewCluster()
399+
mocktikv.BootstrapWithSingleStore(cluster)
400+
mvccStore := mocktikv.MustNewMVCCStore()
401+
store, err := mockstore.NewMockTikvStore(
402+
mockstore.WithCluster(cluster),
403+
mockstore.WithMVCCStore(mvccStore),
404+
)
405+
c.Assert(err, IsNil)
406+
defer store.Close()
407+
408+
config.GetGlobalConfig().Security.SkipGrantTable = true
409+
dom, err := session.BootstrapSession(store)
410+
c.Assert(err, IsNil)
411+
defer dom.Close()
412+
413+
tk := testkit.NewTestKit(c, store)
414+
tk.MustExec("FLUSH PRIVILEGES")
415+
config.GetGlobalConfig().Security.SkipGrantTable = false
389416
}
390417

391418
func (s *testSuite3) TestDropStats(c *C) {

0 commit comments

Comments
 (0)