Skip to content

Commit

Permalink
executor: cleanup entries from mysql.db on revoke (pingcap#38370) (pi…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Oct 12, 2022
1 parent b2ae5e6 commit 7228bb3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 13 additions & 1 deletion executor/revoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ func (e *RevokeExec) revokeGlobalPriv(internalSession sessionctx.Context, priv *
}

func (e *RevokeExec) revokeDBPriv(internalSession sessionctx.Context, priv *ast.PrivElem, userName, host string) error {
ctx := context.Background()
dbName := e.Level.DBName
if len(dbName) == 0 {
dbName = e.ctx.GetSessionVars().CurrentDB
Expand All @@ -236,7 +237,18 @@ func (e *RevokeExec) revokeDBPriv(internalSession sessionctx.Context, priv *ast.
}
sqlexec.MustFormatSQL(sql, " WHERE User=%? AND Host=%? AND DB=%?", userName, host, dbName)

_, err = internalSession.(sqlexec.SQLExecutor).ExecuteInternal(context.Background(), sql.String())
_, err = internalSession.(sqlexec.SQLExecutor).ExecuteInternal(ctx, sql.String())
if err != nil {
return err
}

sql = new(strings.Builder)
sqlexec.MustFormatSQL(sql, "DELETE FROM %n.%n WHERE User=%? AND Host=%? AND DB=%?", mysql.SystemDB, mysql.DBTable, userName, host, dbName)

for _, v := range append(mysql.AllDBPrivs, mysql.GrantPriv) {
sqlexec.MustFormatSQL(sql, " AND %n='N'", v.ColumnString())
}
_, err = internalSession.(sqlexec.SQLExecutor).ExecuteInternal(ctx, sql.String())
return err
}

Expand Down
8 changes: 7 additions & 1 deletion executor/revoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ func (s *testSuite1) TestRevokeDBScope(c *C) {

tk.MustQuery(check).Check(testkit.Rows("Y"))
tk.MustExec(sql)
tk.MustQuery(check).Check(testkit.Rows("N"))
if v == mysql.AllDBPrivs[len(mysql.AllDBPrivs)-1] {
// When all privileges are set to 'N', then the record should be removed as well.
// https://github.com/pingcap/tidb/issues/38363
tk.MustQuery(check).Check(testkit.Rows())
} else {
tk.MustQuery(check).Check(testkit.Rows("N"))
}
}
}

Expand Down

0 comments on commit 7228bb3

Please sign in to comment.