From ad75d08f861c799da0e1933752315d48a535a614 Mon Sep 17 00:00:00 2001 From: #7 Date: Wed, 27 Oct 2021 11:26:48 +0800 Subject: [PATCH] executor: query user and role, hostname should be lowercase (#28880) --- executor/simple.go | 4 ++-- executor/simple_test.go | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/executor/simple.go b/executor/simple.go index a8deba10eef10..dae391314d8b3 100644 --- a/executor/simple.go +++ b/executor/simple.go @@ -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 } @@ -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 diff --git a/executor/simple_test.go b/executor/simple_test.go index 6ea03734aadb9..14e2a63cf2a85 100644 --- a/executor/simple_test.go +++ b/executor/simple_test.go @@ -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) {