Skip to content

Commit

Permalink
executor: query user and role, hostname should be lowercase (#28880)
Browse files Browse the repository at this point in the history
  • Loading branch information
7yyo authored Oct 27, 2021
1 parent 82c6041 commit ad75d08
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions executor/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,7 @@ func (e *SimpleExec) executeDropUser(ctx context.Context, s *ast.DropUserStmt) e

func userExists(ctx context.Context, sctx sessionctx.Context, name string, host string) (bool, error) {
exec := sctx.(sqlexec.RestrictedSQLExecutor)
stmt, err := exec.ParseWithParams(ctx, `SELECT * FROM %n.%n WHERE User=%? AND Host=%?;`, mysql.SystemDB, mysql.UserTable, name, host)
stmt, err := exec.ParseWithParams(ctx, `SELECT * FROM %n.%n WHERE User=%? AND Host=%?;`, mysql.SystemDB, mysql.UserTable, name, strings.ToLower(host))
if err != nil {
return false, err
}
Expand All @@ -1316,7 +1316,7 @@ func userExists(ctx context.Context, sctx sessionctx.Context, name string, host
// use the same internal executor to read within the same transaction, otherwise same as userExists
func userExistsInternal(sqlExecutor sqlexec.SQLExecutor, name string, host string) (bool, error) {
sql := new(strings.Builder)
sqlexec.MustFormatSQL(sql, `SELECT * FROM %n.%n WHERE User=%? AND Host=%?;`, mysql.SystemDB, mysql.UserTable, name, host)
sqlexec.MustFormatSQL(sql, `SELECT * FROM %n.%n WHERE User=%? AND Host=%?;`, mysql.SystemDB, mysql.UserTable, name, strings.ToLower(host))
recordSet, err := sqlExecutor.ExecuteInternal(context.TODO(), sql.String())
if err != nil {
return false, err
Expand Down
19 changes: 19 additions & 0 deletions executor/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,25 @@ func (s *testSuite7) TestUser(c *C) {
tk.MustExec(renameUserSQL)
querySQL = `select user,host from mysql.user where user = 'userD';`
tk.MustQuery(querySQL).Check(testkit.Rows("userD demo.com"))

tk.MustExec(`create user joan;`)
tk.MustExec(`create user sally;`)
tk.MustExec(`create role engineering;`)
tk.MustExec(`create role consultants;`)
tk.MustExec(`create role qa;`)
tk.MustExec(`grant engineering to joan;`)
tk.MustExec(`grant engineering to sally;`)
tk.MustExec(`grant engineering, consultants to joan, sally;`)
tk.MustExec(`grant qa to consultants;`)
tk.MustExec("CREATE ROLE `engineering`@`US`;")
tk.MustExec("create role `engineering`@`INDIA`;")
_, err = tk.Exec("grant `engineering`@`US` TO `engineering`@`INDIA`;")
c.Check(err, IsNil)

tk.MustQuery("select user,host from mysql.user where user='engineering' and host = 'india'").
Check(testkit.Rows("engineering india"))
tk.MustQuery("select user,host from mysql.user where user='engineering' and host = 'us'").
Check(testkit.Rows("engineering us"))
}

func (s *testSuite3) TestSetPwd(c *C) {
Expand Down

0 comments on commit ad75d08

Please sign in to comment.