From d6d493f2fa1e3b5486d0e384d1b343b5840879ea Mon Sep 17 00:00:00 2001 From: sev7ndayyoo <47801637+sev7ndayyoo@users.noreply.github.com> Date: Fri, 15 Oct 2021 20:44:16 +0800 Subject: [PATCH 1/3] hostname to lower --- executor/simple.go | 4 ++-- executor/simple_test.go | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/executor/simple.go b/executor/simple.go index a8deba10eef10..0402ccd0ba8fe 100644 --- a/executor/simple.go +++ b/executor/simple.go @@ -982,7 +982,7 @@ func (e *SimpleExec) executeGrantRole(ctx context.Context, s *ast.GrantRoleStmt) } for _, role := range s.Roles { - exists, err := userExists(ctx, e.ctx, role.Username, role.Hostname) + exists, err := userExists(ctx, e.ctx, role.Username, strings.ToLower(role.Hostname)) if err != nil { return err } @@ -991,7 +991,7 @@ func (e *SimpleExec) executeGrantRole(ctx context.Context, s *ast.GrantRoleStmt) } } for _, user := range s.Users { - exists, err := userExists(ctx, e.ctx, user.Username, user.Hostname) + exists, err := userExists(ctx, e.ctx, user.Username, strings.ToLower(user.Hostname)) if err != nil { return err } diff --git a/executor/simple_test.go b/executor/simple_test.go index 6ea03734aadb9..2dfadf04703b2 100644 --- a/executor/simple_test.go +++ b/executor/simple_test.go @@ -496,6 +496,20 @@ 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) } func (s *testSuite3) TestSetPwd(c *C) { From 362654cde3eccf00a94ac2427e0c98f321ace199 Mon Sep 17 00:00:00 2001 From: sev7ndayyoo <47801637+sev7ndayyoo@users.noreply.github.com> Date: Sat, 16 Oct 2021 16:13:12 +0800 Subject: [PATCH 2/3] add test case --- executor/simple_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/executor/simple_test.go b/executor/simple_test.go index 2dfadf04703b2..14e2a63cf2a85 100644 --- a/executor/simple_test.go +++ b/executor/simple_test.go @@ -510,6 +510,11 @@ func (s *testSuite7) TestUser(c *C) { 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) { From e09435b7e39b778f1ac45925f333b93c07dcbe91 Mon Sep 17 00:00:00 2001 From: sev7ndayyoo <47801637+sev7ndayyoo@users.noreply.github.com> Date: Wed, 20 Oct 2021 11:49:57 +0800 Subject: [PATCH 3/3] fix --- executor/simple.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/executor/simple.go b/executor/simple.go index 0402ccd0ba8fe..dae391314d8b3 100644 --- a/executor/simple.go +++ b/executor/simple.go @@ -982,7 +982,7 @@ func (e *SimpleExec) executeGrantRole(ctx context.Context, s *ast.GrantRoleStmt) } for _, role := range s.Roles { - exists, err := userExists(ctx, e.ctx, role.Username, strings.ToLower(role.Hostname)) + exists, err := userExists(ctx, e.ctx, role.Username, role.Hostname) if err != nil { return err } @@ -991,7 +991,7 @@ func (e *SimpleExec) executeGrantRole(ctx context.Context, s *ast.GrantRoleStmt) } } for _, user := range s.Users { - exists, err := userExists(ctx, e.ctx, user.Username, strings.ToLower(user.Hostname)) + exists, err := userExists(ctx, e.ctx, user.Username, user.Hostname) if err != nil { return err } @@ -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