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

privilege/privileges: sort user records in privilege cache #7211

Merged
merged 8 commits into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 14 additions & 2 deletions privilege/privileges/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,21 @@ func compareHost(x, y string) int {
return -1
}

if strings.HasSuffix(y, `%`) {
if !strings.HasSuffix(x, `%`) {
// One of them end with `%`.
xEnd := strings.HasSuffix(x, `%`)
yEnd := strings.HasSuffix(y, `%`)
if xEnd || yEnd {
switch {
case !xEnd && yEnd:
return -1
case xEnd && !yEnd:
return 1
case xEnd && yEnd:
// 192.168.199.% smaller than 192.168.%
Copy link
Contributor

Choose a reason for hiding this comment

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

what about '192.%.128.%' and '192.168.1.%' ?
create user 'test'@'192.%.1.%' will be ok in MySQL.

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 result will be nondeterministic

Copy link
Contributor

Choose a reason for hiding this comment

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

OK
We may add a FIXME label here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I add a TODO in at the beginning of this function.

// A not very accurate comparison, compare them by length.
if len(x) > len(y) {
return -1
}
}
return 0
}
Expand Down
11 changes: 11 additions & 0 deletions privilege/privileges/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,17 @@ func (s *testCacheSuite) TestSortUserTable(c *C) {
{Host: `%`, User: "jeffrey"},
}
checkUserRecord(p.User, result, c)

p.User = []privileges.UserRecord{
{Host: "192.168.%", User: "xxx"},
{Host: `192.168.199.%`, User: "xxx"},
}
p.SortUserTable()
result = []privileges.UserRecord{
{Host: `192.168.199.%`, User: "xxx"},
{Host: "192.168.%", User: "xxx"},
}
checkUserRecord(p.User, result, c)
}

func checkUserRecord(x, y []privileges.UserRecord, c *C) {
Expand Down