Skip to content

Commit

Permalink
executor: fix revoke USAGE (#41774) (#41780)
Browse files Browse the repository at this point in the history
close #41773
  • Loading branch information
ti-chi-bot authored Feb 28, 2023
1 parent 73d82e3 commit 3246a21
Show file tree
Hide file tree
Showing 3 changed files with 22 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 @@ -444,6 +444,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 @@ -481,10 +484,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 @@ -499,9 +498,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 Down Expand Up @@ -534,9 +530,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 @@ -178,6 +178,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
16 changes: 16 additions & 0 deletions executor/revoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,19 @@ 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, clean := testkit.CreateMockStore(t)
defer clean()
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 3246a21

Please sign in to comment.