Skip to content

Commit

Permalink
executor: grant privilege to table now check table existence use case…
Browse files Browse the repository at this point in the history
… insensitive (#36778)

close #34610
  • Loading branch information
tiancaiamao authored Aug 2, 2022
1 parent 34d877a commit 671fe70
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
5 changes: 3 additions & 2 deletions executor/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ func (e *GrantExec) Next(ctx context.Context, req *chunk.Chunk) error {
return err
}
}
// Note the table name compare is case sensitive here.
if tbl != nil && tbl.Meta().Name.String() != e.Level.TableName {
// Note the table name compare is not case sensitive here.
// In TiDB, system variable lower_case_table_names = 2 which means name comparisons are not case-sensitive.
if tbl != nil && tbl.Meta().Name.L != strings.ToLower(e.Level.TableName) {
return infoschema.ErrTableNotExists.GenWithStackByArgs(dbName, e.Level.TableName)
}
if len(e.Level.DBName) > 0 {
Expand Down
24 changes: 21 additions & 3 deletions executor/grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,9 @@ func TestGrantOnNonExistTable(t *testing.T) {
require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotExists))

tk.MustExec("create table if not exists xx (id int)")
// Case sensitive
_, err = tk.Exec("grant Select,Insert on XX to 'genius'")
require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotExists))
// Case insensitive, differ from MySQL default behaviour.
// In TiDB, system variable lower_case_table_names = 2, which means compare table name using lower case.
tk.MustExec("grant Select,Insert on XX to 'genius'")

_, err = tk.Exec("grant Select,Insert on xx to 'genius'")
require.NoError(t, err)
Expand Down Expand Up @@ -576,3 +576,21 @@ func TestNonExistTableIllegalGrant(t *testing.T) {
// Column level, not existing table, illegal privilege
tk.MustGetErrCode("grant create temporary tables (NotExistsCol) on NotExistsD29302.NotExistsT29302 to u29302;", mysql.ErrWrongUsage)
}

func TestIssue34610(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("DROP DATABASE IF EXISTS d1;")
tk.MustExec("CREATE DATABASE d1;")
tk.MustExec("USE d1;")
tk.MustExec("CREATE USER user_1@localhost;")
defer func() {
tk.MustExec("DROP DATABASE d1;")
tk.MustExec("DROP USER user_1@localhost;")
}()

tk.MustExec("CREATE TABLE T1(f1 INT);")
tk.MustGetErrCode("CREATE TABLE t1(f1 INT);", mysql.ErrTableExists)
tk.MustExec("GRANT SELECT ON T1 to user_1@localhost;")
tk.MustExec("GRANT SELECT ON t1 to user_1@localhost;")
}

0 comments on commit 671fe70

Please sign in to comment.