Skip to content

Commit

Permalink
executor: fix revoke USAGE (#41774)
Browse files Browse the repository at this point in the history
close #41773
  • Loading branch information
xhebox committed Feb 28, 2023
1 parent 61ed093 commit 6c1674c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
13 changes: 3 additions & 10 deletions executor/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,9 @@ func (e *GrantExec) grantLevelPriv(priv *ast.PrivElem, user *ast.UserSpec, inter
if priv.Priv == mysql.ExtendedPriv {
return e.grantDynamicPriv(priv.Name, user, internalSession)
}
if priv.Priv == mysql.UsagePriv {
return nil
}
switch e.Level.Level {
case ast.GrantLevelGlobal:
return e.grantGlobalLevel(priv, user, internalSession)
Expand Down Expand Up @@ -491,10 +494,6 @@ func (e *GrantExec) grantDynamicPriv(privName string, user *ast.UserSpec, intern

// grantGlobalLevel manipulates mysql.user table.
func (e *GrantExec) grantGlobalLevel(priv *ast.PrivElem, user *ast.UserSpec, internalSession sessionctx.Context) error {
if priv.Priv == 0 || priv.Priv == mysql.UsagePriv {
return nil
}

sql := new(strings.Builder)
sqlexec.MustFormatSQL(sql, `UPDATE %n.%n SET `, mysql.SystemDB, mysql.UserTable)
err := composeGlobalPrivUpdate(sql, priv.Priv, "Y")
Expand All @@ -510,9 +509,6 @@ func (e *GrantExec) grantGlobalLevel(priv *ast.PrivElem, user *ast.UserSpec, int

// grantDBLevel manipulates mysql.db table.
func (e *GrantExec) grantDBLevel(priv *ast.PrivElem, user *ast.UserSpec, internalSession sessionctx.Context) error {
if priv.Priv == mysql.UsagePriv {
return nil
}
for _, v := range mysql.StaticGlobalOnlyPrivs {
if v == priv.Priv {
return ErrWrongUsage.GenWithStackByArgs("DB GRANT", "GLOBAL PRIVILEGES")
Expand All @@ -539,9 +535,6 @@ func (e *GrantExec) grantDBLevel(priv *ast.PrivElem, user *ast.UserSpec, interna

// grantTableLevel manipulates mysql.tables_priv table.
func (e *GrantExec) grantTableLevel(priv *ast.PrivElem, user *ast.UserSpec, internalSession sessionctx.Context) error {
if priv.Priv == mysql.UsagePriv {
return nil
}
dbName := e.Level.DBName
if len(dbName) == 0 {
dbName = e.ctx.GetSessionVars().CurrentDB
Expand Down
3 changes: 3 additions & 0 deletions executor/revoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ func (e *RevokeExec) revokeOneUser(internalSession sessionctx.Context, user, hos
}

func (e *RevokeExec) revokePriv(internalSession sessionctx.Context, priv *ast.PrivElem, user, host string) error {
if priv.Priv == mysql.UsagePriv {
return nil
}
switch e.Level.Level {
case ast.GrantLevelGlobal:
return e.revokeGlobalPriv(internalSession, priv, user, host)
Expand Down
15 changes: 15 additions & 0 deletions executor/revoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,18 @@ func TestRevokeOnNonExistTable(t *testing.T) {
tk.MustExec("DROP TABLE t1;")
tk.MustExec("REVOKE ALTER ON d1.t1 FROM issue28533;")
}

// Check https://github.com/pingcap/tidb/issues/41773.
func TestIssue41773(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table if not exists xx (id int)")
tk.MustExec("CREATE USER 't1234'@'%' IDENTIFIED BY 'sNGNQo12fEHe0n3vU';")
tk.MustExec("GRANT USAGE ON * TO 't1234'@'%';")
tk.MustExec("GRANT USAGE ON test.* TO 't1234'@'%';")
tk.MustExec("GRANT USAGE ON test.xx TO 't1234'@'%';")
tk.MustExec("REVOKE USAGE ON * FROM 't1234'@'%';")
tk.MustExec("REVOKE USAGE ON test.* FROM 't1234'@'%';")
tk.MustExec("REVOKE USAGE ON test.xx FROM 't1234'@'%';")
}

0 comments on commit 6c1674c

Please sign in to comment.