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: fix show grants display empty entries #4734

Merged
merged 2 commits into from
Oct 11, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 12 additions & 6 deletions privilege/privileges/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,10 @@ func (p *MySQLPrivilege) showGrants(user, host string) []string {
for _, record := range p.User {
if record.User == user && record.Host == host {
g := userPrivToString(record.Privileges)
s := fmt.Sprintf(`GRANT %s ON *.* TO '%s'@'%s'`, g, record.User, record.Host)
gs = append(gs, s)
if len(g) > 0 {
s := fmt.Sprintf(`GRANT %s ON *.* TO '%s'@'%s'`, g, record.User, record.Host)
gs = append(gs, s)
}
break // it's unique
}
}
Expand All @@ -489,17 +491,21 @@ func (p *MySQLPrivilege) showGrants(user, host string) []string {
for _, record := range p.DB {
if record.User == user && record.Host == host {
g := dbPrivToString(record.Privileges)
s := fmt.Sprintf(`GRANT %s ON %s.* TO '%s'@'%s'`, g, record.DB, record.User, record.Host)
gs = append(gs, s)
if len(g) > 0 {
s := fmt.Sprintf(`GRANT %s ON %s.* TO '%s'@'%s'`, g, record.DB, record.User, record.Host)
gs = append(gs, s)
}
}
}

// Show table scope grants
for _, record := range p.TablesPriv {
if record.User == user && record.Host == host {
g := tablePrivToString(record.TablePriv)
s := fmt.Sprintf(`GRANT %s ON %s.%s TO '%s'@'%s'`, g, record.DB, record.TableName, record.User, record.Host)
gs = append(gs, s)
if len(g) > 0 {
s := fmt.Sprintf(`GRANT %s ON %s.%s TO '%s'@'%s'`, g, record.DB, record.TableName, record.User, record.Host)
gs = append(gs, s)
}
}
}
return gs
Expand Down
13 changes: 13 additions & 0 deletions privilege/privileges/privileges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,19 @@ func (s *testPrivilegeSuite) TestShowGrants(c *C) {
`GRANT ALL PRIVILEGES ON test1.* TO 'show'@'localhost'`,
`GRANT Update ON test.test TO 'show'@'localhost'`}
c.Assert(testutil.CompareUnorderedStringSlice(gs, expected), IsTrue)

// Fix a issue that empty privileges is displayed when revoke after grant.
mustExec(c, se, "TRUNCATE TABLE mysql.db")
mustExec(c, se, "TRUNCATE TABLE mysql.user")
mustExec(c, se, "TRUNCATE TABLE mysql.tables_priv")
mustExec(c, se, "GRANT ALL PRIVILEGES ON `te%`.* TO 'show'@'localhost'")
mustExec(c, se, "REVOKE ALL PRIVILEGES ON `te%`.* FROM 'show'@'localhost'")
mustExec(c, se, `FLUSH PRIVILEGES;`)
gs, err = pc.ShowGrants(se, &auth.UserIdentity{Username: "show", Hostname: "localhost"})
c.Assert(err, IsNil)
// It should not be "GRANT ON `te%`.* to 'show'@'localhost'"
c.Assert(gs, HasLen, 0)

}

func (s *testPrivilegeSuite) TestDropTablePriv(c *C) {
Expand Down