Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor: query user and role, hostname should be lowercase #28880

Merged
merged 5 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions executor/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that there is a similar function userExistsInternal. Should it be modified, too?
Besides, I prefer to put strings.ToLower in userExists so the callers don't have to care about the cases.

if err != nil {
return err
}
Expand All @@ -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
}
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`;")
Comment on lines +509 to +510
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also verify the persisted value in mysql.user?

It should match the following:

mysql [localhost:8024] {root} ((none)) > select user,host from mysql.user where user='engineering'\G
*************************** 1. row ***************************
user: engineering
host: %
*************************** 2. row ***************************
user: engineering
host: india
*************************** 3. row ***************************
user: engineering
host: us
3 rows in set (0.00 sec)

Otherwise LGTM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The case you mentioned should be ok, but after your reminder, I found such a case,I think we should fix it:

In MySQL

mysql> select user,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| sqwe             | %         |
| test_user        | %         |
| tidb_security    | %         |
| tidb_system      | %         |
| u1               | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
9 rows in set (0.00 sec)

mysql> select host from mysql.user where host = 'LOCALHOST'\G
*************************** 1. row ***************************
host: localhost
*************************** 2. row ***************************
host: localhost
*************************** 3. row ***************************
host: localhost
*************************** 4. row ***************************
host: localhost
4 rows in set (0.00 sec)

In TiDB

mysql> select host from mysql.user;
+-------+
| host  |
+-------+
| %     |
| %     |
| %     |
| %     |
| %     |
| %     |
| india |
| us    |
+-------+
8 rows in set (0.00 sec)

mysql> select host from mysql.user where host = 'INDIA'\G
Empty set (0.00 sec)

mysql> select host from mysql.user where host = 'india'\G
*************************** 1. row ***************************
host: india
1 row in set (0.00 sec)

_, 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