-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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, executor: add SET ROLE
and CURRENT_ROLE
support
#9581
Merged
+220
−5
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1d9eb3c
edit gomod
c83ca1d
edit gomod
bd065da
add executor
0b4323b
add test for role graph
1e55aaa
fix package import
e3ad310
add test for current role
231736f
add role check
6bdae4b
address comment
7c7bcae
edit go sum
31f9078
edit go sum
b6f8861
edit gomod
9d4d422
fix gosum
9bbff7e
fix gosum
1887af2
fix tidy
129b57c
Merge branch 'master' into setrole
jackysp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
|
||
"github.com/pingcap/errors" | ||
"github.com/pingcap/parser/ast" | ||
"github.com/pingcap/parser/auth" | ||
"github.com/pingcap/parser/mysql" | ||
"github.com/pingcap/parser/terror" | ||
"github.com/pingcap/tidb/sessionctx" | ||
|
@@ -104,12 +105,42 @@ type columnsPrivRecord struct { | |
patTypes []byte | ||
} | ||
|
||
// RoleGraphEdgesTable is used to cache relationship between and role. | ||
type roleGraphEdgesTable struct { | ||
roleList map[string]bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better to use
|
||
} | ||
|
||
// Find method is used to find role from table | ||
func (g roleGraphEdgesTable) Find(user, host string) bool { | ||
if host == "" { | ||
host = "%" | ||
} | ||
key := user + "@" + host | ||
if g.roleList == nil { | ||
return false | ||
} | ||
_, ok := g.roleList[key] | ||
return ok | ||
} | ||
|
||
// MySQLPrivilege is the in-memory cache of mysql privilege tables. | ||
type MySQLPrivilege struct { | ||
User []UserRecord | ||
DB []dbRecord | ||
TablesPriv []tablesPrivRecord | ||
ColumnsPriv []columnsPrivRecord | ||
RoleGraph map[string]roleGraphEdgesTable | ||
} | ||
|
||
// FindRole is used to detect whether there is edges between users and roles. | ||
func (p *MySQLPrivilege) FindRole(user string, host string, role *auth.RoleIdentity) bool { | ||
rec := p.matchUser(user, host) | ||
r := p.matchUser(role.Username, role.Hostname) | ||
if rec != nil && r != nil { | ||
key := rec.User + "@" + rec.Host | ||
return p.RoleGraph[key].Find(role.Username, role.Hostname) | ||
} | ||
return false | ||
} | ||
|
||
// LoadAll loads the tables from database to memory. | ||
|
@@ -142,6 +173,14 @@ func (p *MySQLPrivilege) LoadAll(ctx sessionctx.Context) error { | |
} | ||
log.Warn("mysql.columns_priv missing") | ||
} | ||
|
||
err = p.LoadRoleGraph(ctx) | ||
if err != nil { | ||
if !noSuchTable(err) { | ||
return errors.Trace(err) | ||
} | ||
log.Warn("mysql.role_edges missing") | ||
} | ||
return nil | ||
} | ||
|
||
|
@@ -155,6 +194,16 @@ func noSuchTable(err error) bool { | |
return false | ||
} | ||
|
||
// LoadRoleGraph loads the mysql.role_edges table from database. | ||
func (p *MySQLPrivilege) LoadRoleGraph(ctx sessionctx.Context) error { | ||
p.RoleGraph = make(map[string]roleGraphEdgesTable) | ||
err := p.loadTable(ctx, "select FROM_USER, FROM_HOST, TO_USER, TO_HOST from mysql.role_edges;", p.decodeRoleEdgesTable) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
return nil | ||
} | ||
|
||
// LoadUserTable loads the mysql.user table from database. | ||
func (p *MySQLPrivilege) LoadUserTable(ctx sessionctx.Context) error { | ||
err := p.loadTable(ctx, "select HIGH_PRIORITY Host,User,Password,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Process_priv,Grant_priv,References_priv,Alter_priv,Show_db_priv,Super_priv,Execute_priv,Create_view_priv,Show_view_priv,Index_priv,Create_user_priv,Trigger_priv,Create_role_priv,Drop_role_priv,account_locked from mysql.user;", p.decodeUserTableRow) | ||
|
@@ -381,6 +430,31 @@ func (p *MySQLPrivilege) decodeTablesPrivTableRow(row chunk.Row, fs []*ast.Resul | |
return nil | ||
} | ||
|
||
func (p *MySQLPrivilege) decodeRoleEdgesTable(row chunk.Row, fs []*ast.ResultField) error { | ||
var fromUser, fromHost, toHost, toUser string | ||
for i, f := range fs { | ||
switch { | ||
case f.ColumnAsName.L == "from_host": | ||
fromHost = row.GetString(i) | ||
case f.ColumnAsName.L == "from_user": | ||
fromUser = row.GetString(i) | ||
case f.ColumnAsName.L == "to_host": | ||
toHost = row.GetString(i) | ||
case f.ColumnAsName.L == "to_user": | ||
toUser = row.GetString(i) | ||
} | ||
} | ||
fromKey := fromUser + "@" + fromHost | ||
toKey := toUser + "@" + toHost | ||
roleGraph, ok := p.RoleGraph[toKey] | ||
if !ok { | ||
roleGraph = roleGraphEdgesTable{roleList: make(map[string]bool)} | ||
p.RoleGraph[toKey] = roleGraph | ||
} | ||
roleGraph.roleList[fromKey] = true | ||
return nil | ||
} | ||
|
||
func (p *MySQLPrivilege) decodeColumnsPrivTableRow(row chunk.Row, fs []*ast.ResultField) error { | ||
var value columnsPrivRecord | ||
for i, f := range fs { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is better to add some test cases for this built-in function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok