Skip to content

Commit 1028164

Browse files
morgotiancaiamao
authored andcommitted
privilege: add USAGE in show grants for mysql compatibility (#7955)
1 parent 708611d commit 1028164

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

privilege/privileges/cache.go

+9
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,11 @@ func (p *MySQLPrivilege) DBIsVisible(user, host, db string) bool {
574574

575575
func (p *MySQLPrivilege) showGrants(user, host string) []string {
576576
var gs []string
577+
var hasGlobalGrant bool = false
577578
// Show global grants
578579
for _, record := range p.User {
579580
if record.User == user && record.Host == host {
581+
hasGlobalGrant = true
580582
g := userPrivToString(record.Privileges)
581583
if len(g) > 0 {
582584
s := fmt.Sprintf(`GRANT %s ON *.* TO '%s'@'%s'`, g, record.User, record.Host)
@@ -586,6 +588,12 @@ func (p *MySQLPrivilege) showGrants(user, host string) []string {
586588
}
587589
}
588590

591+
// This is a mysql convention.
592+
if len(gs) == 0 && hasGlobalGrant {
593+
s := fmt.Sprintf("GRANT USAGE ON *.* TO '%s'@'%s'", user, host)
594+
gs = append(gs, s)
595+
}
596+
589597
// Show db scope grants
590598
for _, record := range p.DB {
591599
if record.User == user && record.Host == host {
@@ -607,6 +615,7 @@ func (p *MySQLPrivilege) showGrants(user, host string) []string {
607615
}
608616
}
609617
}
618+
610619
return gs
611620
}
612621

privilege/privileges/privileges_test.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ func (s *testPrivilegeSuite) TestCheckTablePrivilege(c *C) {
147147

148148
func (s *testPrivilegeSuite) TestShowGrants(c *C) {
149149
se := newSession(c, s.store, s.dbName)
150+
ctx, _ := se.(sessionctx.Context)
150151
mustExec(c, se, `CREATE USER 'show'@'localhost' identified by '123';`)
151152
mustExec(c, se, `GRANT Index ON *.* TO 'show'@'localhost';`)
152153
mustExec(c, se, `FLUSH PRIVILEGES;`)
@@ -222,16 +223,23 @@ func (s *testPrivilegeSuite) TestShowGrants(c *C) {
222223
`GRANT Update ON test.test TO 'show'@'localhost'`}
223224
c.Assert(testutil.CompareUnorderedStringSlice(gs, expected), IsTrue)
224225

225-
// Fix a issue that empty privileges is displayed when revoke after grant.
226-
mustExec(c, se, "TRUNCATE TABLE mysql.db")
227-
mustExec(c, se, "TRUNCATE TABLE mysql.user")
228-
mustExec(c, se, "TRUNCATE TABLE mysql.tables_priv")
229-
mustExec(c, se, `GRANT ALL PRIVILEGES ON `+"`"+`te%`+"`"+`.* TO 'show'@'localhost'`)
230-
mustExec(c, se, `REVOKE ALL PRIVILEGES ON `+"`"+`te%`+"`"+`.* FROM 'show'@'localhost'`)
226+
// Expected behavior: Usage still exists after revoking all privileges
227+
mustExec(c, se, `REVOKE ALL PRIVILEGES ON *.* FROM 'show'@'localhost'`)
228+
mustExec(c, se, `REVOKE Select on test.* FROM 'show'@'localhost'`)
229+
mustExec(c, se, `REVOKE ALL ON test1.* FROM 'show'@'localhost'`)
230+
mustExec(c, se, `REVOKE UPDATE on test.test FROM 'show'@'localhost'`)
231231
mustExec(c, se, `FLUSH PRIVILEGES;`)
232232
gs, err = pc.ShowGrants(se, &auth.UserIdentity{Username: "show", Hostname: "localhost"})
233233
c.Assert(err, IsNil)
234-
// It should not be "GRANT ON `te%`.* to 'show'@'localhost'"
234+
c.Assert(gs, HasLen, 1)
235+
c.Assert(gs[0], Equals, `GRANT USAGE ON *.* TO 'show'@'localhost'`)
236+
237+
// Usage should not exist after dropping the user
238+
// Which we need privileges to do so!
239+
ctx.GetSessionVars().User = &auth.UserIdentity{Username: "root", Hostname: "localhost"}
240+
mustExec(c, se, `DROP USER 'show'@'localhost'`)
241+
mustExec(c, se, `FLUSH PRIVILEGES;`)
242+
gs, err = pc.ShowGrants(se, &auth.UserIdentity{Username: "show", Hostname: "localhost"})
235243
c.Assert(gs, HasLen, 0)
236244

237245
}

0 commit comments

Comments
 (0)