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

executor: fix revoke USAGE #41774

Merged
merged 2 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'@'%';")
}