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

checker(dm): support wildcard in privilege checking #7739

Merged
merged 4 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
51 changes: 28 additions & 23 deletions dm/pkg/checker/privilege.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
_ "github.com/pingcap/tidb/types/parser_driver" // for parser driver
"github.com/pingcap/tidb/util/dbutil"
"github.com/pingcap/tidb/util/filter"
"github.com/pingcap/tidb/util/stringutil"
"github.com/pingcap/tiflow/dm/pkg/log"
"github.com/pingcap/tiflow/pkg/container/sortmap"
"go.uber.org/zap"
Expand Down Expand Up @@ -307,7 +308,7 @@ func VerifyPrivileges(
return nil, errors.Errorf("grant has no user %s", grant)
}

dbName := grantStmt.Level.DBName
dbPatChar, dbPatType := stringutil.CompilePattern(grantStmt.Level.DBName, '\\')
tableName := grantStmt.Level.TableName
switch grantStmt.Level.Level {
case ast.GrantLevelGlobal:
Expand Down Expand Up @@ -339,26 +340,28 @@ func VerifyPrivileges(
if privs.needGlobal {
continue
}
if _, ok := privs.dbs[dbName]; !ok {
continue
for dbName := range privs.dbs {
if stringutil.DoMatch(dbName, dbPatChar, dbPatType) {
delete(privs.dbs, dbName)
}
}
delete(privs.dbs, dbName)
}
continue
}
privs, ok := lackPrivs[privElem.Priv]
if !ok || privs.needGlobal {
continue
}
if _, ok := privs.dbs[dbName]; !ok {
continue
}
// dumpling could report error if an allow-list table is lack of privilege.
// we only check that SELECT is granted on all columns, otherwise we can't SHOW CREATE TABLE
if privElem.Priv == mysql.SelectPriv && len(privElem.Cols) != 0 {
continue
}
delete(privs.dbs, dbName)
for dbName := range privs.dbs {
if stringutil.DoMatch(dbName, dbPatChar, dbPatType) {
delete(privs.dbs, dbName)
}
}
}
case ast.GrantLevelTable:
for _, privElem := range grantStmt.Privs {
Expand All @@ -369,34 +372,36 @@ func VerifyPrivileges(
if privs.needGlobal {
continue
}
dbPrivs, ok := privs.dbs[dbName]
if !ok || dbPrivs.wholeDB {
continue
}
if _, ok := dbPrivs.tables[tableName]; !ok {
continue
for dbName, dbPrivs := range privs.dbs {
Copy link
Contributor

@D3Hunter D3Hunter Nov 30, 2022

Choose a reason for hiding this comment

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

https://dev.mysql.com/doc/refman/5.7/en/grant.html

The _ and % wildcards are permitted when specifying database names in GRANT statements that grant privileges at the database level (GRANT ... ON db_name.*).
When a database name is not used to grant privileges at the database level, but as a qualifier for granting privileges to some other object such as a table or routine (for example, GRANT ... ON db_name.tbl_name), MySQL interprets wildcard characters as literal characters.

wildcard cannot be used on table level grant, not need to change 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.

if dbPrivs.wholeDB {
continue
}
if !stringutil.DoMatch(dbName, dbPatChar, dbPatType) {
continue
}
delete(dbPrivs.tables, tableName)
}
delete(dbPrivs.tables, tableName)
}
continue
}
privs, ok := lackPrivs[privElem.Priv]
if !ok || privs.needGlobal {
continue
}
dbPrivs, ok := privs.dbs[dbName]
if !ok || dbPrivs.wholeDB {
continue
}
if _, ok := dbPrivs.tables[tableName]; !ok {
continue
}
// dumpling could report error if an allow-list table is lack of privilege.
// we only check that SELECT is granted on all columns, otherwise we can't SHOW CREATE TABLE
if privElem.Priv == mysql.SelectPriv && len(privElem.Cols) != 0 {
continue
}
delete(dbPrivs.tables, tableName)
for dbName, dbPrivs := range privs.dbs {
if dbPrivs.wholeDB {
continue
}
if !stringutil.DoMatch(dbName, dbPatChar, dbPatType) {
continue
}
delete(dbPrivs.tables, tableName)
}
}
}
}
Expand Down
69 changes: 69 additions & 0 deletions dm/pkg/checker/privilege_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,75 @@ func TestVerifyReplicationPrivileges(t *testing.T) {
}
}

func TestVerifyPrivilegesWildcard(t *testing.T) {
cases := []struct {
grants []string
checkTables []filter.Table
replicationState State
errStr string
}{
{
grants: []string{
"GRANT SELECT ON `demo\\_foobar`.* TO `dmuser`@`%`",
},
checkTables: []filter.Table{
{Schema: "demo_foobar", Name: "t1"},
},
replicationState: StateSuccess,
},
{
grants: []string{
"GRANT SELECT ON `demo\\_foobar`.* TO `dmuser`@`%`",
},
checkTables: []filter.Table{
{Schema: "demo2foobar", Name: "t1"},
},
replicationState: StateFailure,
errStr: "lack of Select privilege: {`demo2foobar`.`t1`}; ",
},
{
grants: []string{
"GRANT SELECT ON `demo_`.* TO `dmuser`@`%`",
},
checkTables: []filter.Table{
{Schema: "demo1", Name: "t1"},
{Schema: "demo2", Name: "t1"},
},
replicationState: StateSuccess,
},
{
grants: []string{
"GRANT SELECT ON `demo%`.* TO `dmuser`@`%`",
},
checkTables: []filter.Table{
{Schema: "demo_some", Name: "t1"},
{Schema: "block_db", Name: "t1"},
},
replicationState: StateFailure,
errStr: "lack of Select privilege: {`block_db`.`t1`}; ",
},
}

for i, cs := range cases {
t.Logf("case %d", i)
result := &Result{
State: StateFailure,
}
requiredPrivs := map[mysql.PrivilegeType]priv{
mysql.SelectPriv: {
dbs: genTableLevelPrivs(cs.checkTables),
},
}
err := verifyPrivilegesWithResult(result, cs.grants, requiredPrivs)
if cs.replicationState == StateSuccess {
require.Nil(t, err, "grants: %v", cs.grants)
} else {
require.NotNil(t, err, "grants: %v", cs.grants)
require.Equal(t, cs.errStr, err.ShortErr, "grants: %v", cs.grants)
}
}
}

func TestVerifyTargetPrivilege(t *testing.T) {
cases := []struct {
grants []string
Expand Down