Skip to content

Commit 6bdae4b

Browse files
author
imtbkcat
committed
address comment
1 parent 231736f commit 6bdae4b

File tree

5 files changed

+16
-21
lines changed

5 files changed

+16
-21
lines changed

executor/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var (
4949
ErrTableaccessDenied = terror.ClassExecutor.New(mysql.ErrTableaccessDenied, mysql.MySQLErrName[mysql.ErrTableaccessDenied])
5050
ErrBadDB = terror.ClassExecutor.New(mysql.ErrBadDB, mysql.MySQLErrName[mysql.ErrBadDB])
5151
ErrWrongObject = terror.ClassExecutor.New(mysql.ErrWrongObject, mysql.MySQLErrName[mysql.ErrWrongObject])
52+
ErrRoleNotGranted = terror.ClassPrivilege.New(mysql.ErrRoleNotGranted, mysql.MySQLErrName[mysql.ErrRoleNotGranted])
5253
)
5354

5455
func init() {

executor/simple.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,8 @@ func (e *SimpleExec) executeSetRole(s *ast.SetRoleStmt) error {
103103
checker := privilege.GetPrivilegeManager(e.ctx)
104104
ok, roleName := checker.ActiveRoles(e.ctx, roleList)
105105
if !ok {
106-
errCode := mysql.ErrRoleNotGranted
107-
errRoleNotGranted := terror.ClassPrivilege.New(terror.ErrCode(errCode), mysql.MySQLErrName[uint16(errCode)])
108106
u := e.ctx.GetSessionVars().User
109-
return errRoleNotGranted.GenWithStackByArgs(roleName, u.String())
107+
return ErrRoleNotGranted.GenWithStackByArgs(roleName, u.String())
110108
}
111109
return nil
112110
}

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ require (
2323
github.com/go-sql-driver/mysql v0.0.0-20170715192408-3955978caca4
2424
github.com/gogo/protobuf v1.2.0 // indirect
2525
github.com/golang/groupcache v0.0.0-20181024230925-c65c006176ff // indirect
26-
github.com/golang/protobuf v1.2.0
26+
github.com/golang/protobuf v1.3.0
2727
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
2828
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c
2929
github.com/gorilla/context v1.1.1 // indirect
@@ -58,7 +58,7 @@ require (
5858
github.com/prometheus/common v0.0.0-20181020173914-7e9e6cabbd39 // indirect
5959
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d // indirect
6060
github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446 // indirect
61-
github.com/shirou/gopsutil v2.18.10+incompatible
61+
github.com/shirou/gopsutil v2.18.12+incompatible
6262
github.com/shurcooL/httpfs v0.0.0-20171119174359-809beceb2371 // indirect
6363
github.com/shurcooL/vfsgen v0.0.0-20181020040650-a97a25d856ca // indirect
6464
github.com/sirupsen/logrus v1.2.0
@@ -88,4 +88,4 @@ require (
8888
sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67
8989
)
9090

91-
replace github.com/pingcap/parser => github.com/imtbkcat/parser v0.0.0-20190306131438-36bf9d091519
91+
replace github.com/pingcap/parser => github.com/imtbkcat/parser setrole

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ github.com/grpc-ecosystem/grpc-gateway v1.5.1 h1:3scN4iuXkNOyP98jF55Lv8a9j1o/Iwv
7575
github.com/grpc-ecosystem/grpc-gateway v1.5.1/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw=
7676
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
7777
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
78-
github.com/imtbkcat/parser v0.0.0-20190306131438-36bf9d091519/go.mod h1:qupHD3o7J0aBb3bbVyXRnxe9kKy2MTY/6POS6NO/Ei8=
7978
github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo=
8079
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
8180
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=

privilege/privileges/cache.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ type columnsPrivRecord struct {
106106
}
107107

108108
// RoleGraphEdgesTable is used to cache relationship between and role.
109-
type RoleGraphEdgesTable struct {
109+
type roleGraphEdgesTable struct {
110110
roleList map[string]bool
111111
}
112112

113113
// Find method is used to find role from table
114-
func (g RoleGraphEdgesTable) Find(user, host string) bool {
114+
func (g roleGraphEdgesTable) Find(user, host string) bool {
115115
if host == "" {
116116
host = "%"
117117
}
@@ -120,10 +120,7 @@ func (g RoleGraphEdgesTable) Find(user, host string) bool {
120120
return false
121121
}
122122
_, ok := g.roleList[key]
123-
if ok {
124-
return true
125-
}
126-
return false
123+
return ok
127124
}
128125

129126
// MySQLPrivilege is the in-memory cache of mysql privilege tables.
@@ -132,10 +129,10 @@ type MySQLPrivilege struct {
132129
DB []dbRecord
133130
TablesPriv []tablesPrivRecord
134131
ColumnsPriv []columnsPrivRecord
135-
RoleGraph map[string]RoleGraphEdgesTable
132+
RoleGraph map[string]roleGraphEdgesTable
136133
}
137134

138-
// FindRole is used to detect whether this is an edge between user and role
135+
// FindRole is used to detect whether there is edges between users and roles.
139136
func (p *MySQLPrivilege) FindRole(user string, host string, role *auth.RoleIdentity) bool {
140137
rec := p.matchUser(user, host)
141138
r := p.matchUser(role.Username, role.Hostname)
@@ -199,7 +196,7 @@ func noSuchTable(err error) bool {
199196

200197
// LoadRoleGraph loads the mysql.role_edges table from database.
201198
func (p *MySQLPrivilege) LoadRoleGraph(ctx sessionctx.Context) error {
202-
p.RoleGraph = make(map[string]RoleGraphEdgesTable)
199+
p.RoleGraph = make(map[string]roleGraphEdgesTable)
203200
err := p.loadTable(ctx, "select FROM_USER, FROM_HOST, TO_USER, TO_HOST from mysql.role_edges;", p.decodeRoleEdgesTable)
204201
if err != nil {
205202
return errors.Trace(err)
@@ -449,12 +446,12 @@ func (p *MySQLPrivilege) decodeRoleEdgesTable(row chunk.Row, fs []*ast.ResultFie
449446
}
450447
fromKey := fromUser + "@" + fromHost
451448
toKey := toUser + "@" + toHost
452-
if _, ok := p.RoleGraph[toKey]; ok {
453-
p.RoleGraph[toKey].roleList[fromKey] = true
454-
} else {
455-
p.RoleGraph[toKey] = RoleGraphEdgesTable{roleList: make(map[string]bool)}
456-
p.RoleGraph[toKey].roleList[fromKey] = true
449+
roleGraph, ok := p.RoleGraph[toKey]
450+
if !ok {
451+
roleGraph = roleGraphEdgesTable{roleList: make(map[string]bool)}
452+
p.RoleGraph[toKey] = roleGraph
457453
}
454+
roleGraph.roleList[fromKey] = true
458455
return nil
459456
}
460457

0 commit comments

Comments
 (0)