Skip to content

Commit

Permalink
executor,infoschema: move dataForUserPrivileges from infoschema t…
Browse files Browse the repository at this point in the history
…o `executor` (#15155)
  • Loading branch information
Rustin170506 authored Mar 6, 2020
1 parent 91694dc commit 5d0a260
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 73 deletions.
1 change: 1 addition & 0 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,7 @@ func (b *executorBuilder) buildMemTable(v *plannercore.PhysicalMemTable) Executo
strings.ToLower(infoschema.TableCollations),
strings.ToLower(infoschema.TableCharacterSets),
strings.ToLower(infoschema.TableKeyColumn),
strings.ToLower(infoschema.TableUserPrivileges),
strings.ToLower(infoschema.TableCollationCharacterSetApplicability):
return &MemTableReaderExec{
baseExecutor: newBaseExecutor(b.ctx, v.Schema(), v.ExplainID()),
Expand Down
7 changes: 7 additions & 0 deletions executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func (e *memtableRetriever) retrieve(ctx context.Context, sctx sessionctx.Contex
e.rows = dataForKeyColumnUsage(sctx, dbs)
case infoschema.TableCollationCharacterSetApplicability:
e.rows = dataForCollationCharacterSetApplicability()
case infoschema.TableUserPrivileges:
e.setDataFromUserPrivileges(sctx)
}
if err != nil {
return nil, err
Expand Down Expand Up @@ -385,3 +387,8 @@ func keyColumnUsageInTable(schema *model.DBInfo, table *model.TableInfo) [][]typ
}
return rows
}

func (e *memtableRetriever) setDataFromUserPrivileges(ctx sessionctx.Context) {
pm := privilege.GetPrivilegeManager(ctx)
e.rows = pm.UserPrivilegesTable()
}
65 changes: 65 additions & 0 deletions executor/infoschema_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,68 @@ func (s *testInfoschemaTableSuite) TestKeyColumnUsage(c *C) {
keyColumnTester.MustExec("set role r_stats_meta")
c.Assert(len(keyColumnTester.MustQuery("select * from information_schema.KEY_COLUMN_USAGE where TABLE_NAME='stats_meta';").Rows()), Greater, 0)
}

func (s *testInfoschemaTableSuite) TestUserPrivileges(c *C) {
tk := testkit.NewTestKit(c, s.store)
//test the privilege of new user for information_schema.table_constraints
tk.MustExec("create user constraints_tester")
constraintsTester := testkit.NewTestKit(c, s.store)
constraintsTester.MustExec("use information_schema")
c.Assert(constraintsTester.Se.Auth(&auth.UserIdentity{
Username: "constraints_tester",
Hostname: "127.0.0.1",
}, nil, nil), IsTrue)
constraintsTester.MustQuery("select * from information_schema.TABLE_CONSTRAINTS;").Check([][]interface{}{})

//test the privilege of user with privilege of mysql.gc_delete_range for information_schema.table_constraints
tk.MustExec("CREATE ROLE r_gc_delete_range ;")
tk.MustExec("GRANT ALL PRIVILEGES ON mysql.gc_delete_range TO r_gc_delete_range;")
tk.MustExec("GRANT r_gc_delete_range TO constraints_tester;")
constraintsTester.MustExec("set role r_gc_delete_range")
c.Assert(len(constraintsTester.MustQuery("select * from information_schema.TABLE_CONSTRAINTS where TABLE_NAME='gc_delete_range';").Rows()), Greater, 0)
constraintsTester.MustQuery("select * from information_schema.TABLE_CONSTRAINTS where TABLE_NAME='tables_priv';").Check([][]interface{}{})

//test the privilege of new user for information_schema
tk.MustExec("create user tester1")
tk1 := testkit.NewTestKit(c, s.store)
tk1.MustExec("use information_schema")
c.Assert(tk1.Se.Auth(&auth.UserIdentity{
Username: "tester1",
Hostname: "127.0.0.1",
}, nil, nil), IsTrue)
tk1.MustQuery("select * from information_schema.STATISTICS;").Check([][]interface{}{})

//test the privilege of user with some privilege for information_schema
tk.MustExec("create user tester2")
tk.MustExec("CREATE ROLE r_columns_priv;")
tk.MustExec("GRANT ALL PRIVILEGES ON mysql.columns_priv TO r_columns_priv;")
tk.MustExec("GRANT r_columns_priv TO tester2;")
tk2 := testkit.NewTestKit(c, s.store)
tk2.MustExec("use information_schema")
c.Assert(tk2.Se.Auth(&auth.UserIdentity{
Username: "tester2",
Hostname: "127.0.0.1",
}, nil, nil), IsTrue)
tk2.MustExec("set role r_columns_priv")
result := tk2.MustQuery("select * from information_schema.STATISTICS where TABLE_NAME='columns_priv' and COLUMN_NAME='Host';")
c.Assert(len(result.Rows()), Greater, 0)
tk2.MustQuery("select * from information_schema.STATISTICS where TABLE_NAME='tables_priv' and COLUMN_NAME='Host';").Check(
[][]interface{}{})

//test the privilege of user with all privilege for information_schema
tk.MustExec("create user tester3")
tk.MustExec("CREATE ROLE r_all_priv;")
tk.MustExec("GRANT ALL PRIVILEGES ON mysql.* TO r_all_priv;")
tk.MustExec("GRANT r_all_priv TO tester3;")
tk3 := testkit.NewTestKit(c, s.store)
tk3.MustExec("use information_schema")
c.Assert(tk3.Se.Auth(&auth.UserIdentity{
Username: "tester3",
Hostname: "127.0.0.1",
}, nil, nil), IsTrue)
tk3.MustExec("set role r_all_priv")
result = tk3.MustQuery("select * from information_schema.STATISTICS where TABLE_NAME='columns_priv' and COLUMN_NAME='Host';")
c.Assert(len(result.Rows()), Greater, 0)
result = tk3.MustQuery("select * from information_schema.STATISTICS where TABLE_NAME='tables_priv' and COLUMN_NAME='Host';")
c.Assert(len(result.Rows()), Greater, 0)
}
21 changes: 10 additions & 11 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ const (
tableProfiling = "PROFILING"
tablePartitions = "PARTITIONS"
// TableKeyColumn is the string constant of KEY_COLUMN_USAGE
TableKeyColumn = "KEY_COLUMN_USAGE"
tableReferConst = "REFERENTIAL_CONSTRAINTS"
tableSessionVar = "SESSION_VARIABLES"
tablePlugins = "PLUGINS"
tableConstraints = "TABLE_CONSTRAINTS"
tableTriggers = "TRIGGERS"
tableUserPrivileges = "USER_PRIVILEGES"
TableKeyColumn = "KEY_COLUMN_USAGE"
tableReferConst = "REFERENTIAL_CONSTRAINTS"
tableSessionVar = "SESSION_VARIABLES"
tablePlugins = "PLUGINS"
tableConstraints = "TABLE_CONSTRAINTS"
tableTriggers = "TRIGGERS"
// TableUserPrivileges is the string constant of infoschema user privilege table.
TableUserPrivileges = "USER_PRIVILEGES"
tableSchemaPrivileges = "SCHEMA_PRIVILEGES"
tableTablePrivileges = "TABLE_PRIVILEGES"
tableColumnPrivileges = "COLUMN_PRIVILEGES"
Expand Down Expand Up @@ -147,7 +148,7 @@ var tableIDMap = map[string]int64{
tablePlugins: autoid.InformationSchemaDBID + 15,
tableConstraints: autoid.InformationSchemaDBID + 16,
tableTriggers: autoid.InformationSchemaDBID + 17,
tableUserPrivileges: autoid.InformationSchemaDBID + 18,
TableUserPrivileges: autoid.InformationSchemaDBID + 18,
tableSchemaPrivileges: autoid.InformationSchemaDBID + 19,
tableTablePrivileges: autoid.InformationSchemaDBID + 20,
tableColumnPrivileges: autoid.InformationSchemaDBID + 21,
Expand Down Expand Up @@ -2194,7 +2195,7 @@ var tableNameToColumns = map[string][]columnInfo{
tablePlugins: pluginsCols,
tableConstraints: tableConstraintsCols,
tableTriggers: tableTriggersCols,
tableUserPrivileges: tableUserPrivilegesCols,
TableUserPrivileges: tableUserPrivilegesCols,
tableSchemaPrivileges: tableSchemaPrivilegesCols,
tableTablePrivileges: tableTablePrivilegesCols,
tableColumnPrivileges: tableColumnPrivilegesCols,
Expand Down Expand Up @@ -2290,8 +2291,6 @@ func (it *infoschemaTable) getRows(ctx sessionctx.Context, cols []*table.Column)
fullRows, err = dataForPartitions(ctx, dbs)
case tableReferConst:
case tablePlugins, tableTriggers:
case tableUserPrivileges:
fullRows = dataForUserPrivileges(ctx)
case tableRoutines:
// TODO: Fill the following tables.
case tableSchemaPrivileges:
Expand Down
62 changes: 0 additions & 62 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,68 +457,6 @@ func (s *testTableSuite) TestSomeTables(c *C) {
tk.MustQuery("select * from information_schema.SESSION_VARIABLES where VARIABLE_NAME='tidb_retry_limit';").Check(testkit.Rows("tidb_retry_limit 10"))
tk.MustQuery("select * from information_schema.TABLE_CONSTRAINTS where TABLE_NAME='gc_delete_range';").Check(testkit.Rows("def mysql delete_range_index mysql gc_delete_range UNIQUE"))

//test the privilege of new user for information_schema.table_constraints
tk.MustExec("create user constraints_tester")
constraintsTester := testkit.NewTestKit(c, s.store)
constraintsTester.MustExec("use information_schema")
c.Assert(constraintsTester.Se.Auth(&auth.UserIdentity{
Username: "constraints_tester",
Hostname: "127.0.0.1",
}, nil, nil), IsTrue)
constraintsTester.MustQuery("select * from information_schema.TABLE_CONSTRAINTS;").Check([][]interface{}{})

//test the privilege of user with privilege of mysql.gc_delete_range for information_schema.table_constraints
tk.MustExec("CREATE ROLE r_gc_delete_range ;")
tk.MustExec("GRANT ALL PRIVILEGES ON mysql.gc_delete_range TO r_gc_delete_range;")
tk.MustExec("GRANT r_gc_delete_range TO constraints_tester;")
constraintsTester.MustExec("set role r_gc_delete_range")
c.Assert(len(constraintsTester.MustQuery("select * from information_schema.TABLE_CONSTRAINTS where TABLE_NAME='gc_delete_range';").Rows()), Greater, 0)
constraintsTester.MustQuery("select * from information_schema.TABLE_CONSTRAINTS where TABLE_NAME='tables_priv';").Check([][]interface{}{})

//test the privilege of new user for information_schema
tk.MustExec("create user tester1")
tk1 := testkit.NewTestKit(c, s.store)
tk1.MustExec("use information_schema")
c.Assert(tk1.Se.Auth(&auth.UserIdentity{
Username: "tester1",
Hostname: "127.0.0.1",
}, nil, nil), IsTrue)
tk1.MustQuery("select * from information_schema.STATISTICS;").Check([][]interface{}{})

//test the privilege of user with some privilege for information_schema
tk.MustExec("create user tester2")
tk.MustExec("CREATE ROLE r_columns_priv;")
tk.MustExec("GRANT ALL PRIVILEGES ON mysql.columns_priv TO r_columns_priv;")
tk.MustExec("GRANT r_columns_priv TO tester2;")
tk2 := testkit.NewTestKit(c, s.store)
tk2.MustExec("use information_schema")
c.Assert(tk2.Se.Auth(&auth.UserIdentity{
Username: "tester2",
Hostname: "127.0.0.1",
}, nil, nil), IsTrue)
tk2.MustExec("set role r_columns_priv")
result := tk2.MustQuery("select * from information_schema.STATISTICS where TABLE_NAME='columns_priv' and COLUMN_NAME='Host';")
c.Assert(len(result.Rows()), Greater, 0)
tk2.MustQuery("select * from information_schema.STATISTICS where TABLE_NAME='tables_priv' and COLUMN_NAME='Host';").Check(
[][]interface{}{})

//test the privilege of user with all privilege for information_schema
tk.MustExec("create user tester3")
tk.MustExec("CREATE ROLE r_all_priv;")
tk.MustExec("GRANT ALL PRIVILEGES ON mysql.* TO r_all_priv;")
tk.MustExec("GRANT r_all_priv TO tester3;")
tk3 := testkit.NewTestKit(c, s.store)
tk3.MustExec("use information_schema")
c.Assert(tk3.Se.Auth(&auth.UserIdentity{
Username: "tester3",
Hostname: "127.0.0.1",
}, nil, nil), IsTrue)
tk3.MustExec("set role r_all_priv")
result = tk3.MustQuery("select * from information_schema.STATISTICS where TABLE_NAME='columns_priv' and COLUMN_NAME='Host';")
c.Assert(len(result.Rows()), Greater, 0)
result = tk3.MustQuery("select * from information_schema.STATISTICS where TABLE_NAME='tables_priv' and COLUMN_NAME='Host';")
c.Assert(len(result.Rows()), Greater, 0)

sm := &mockSessionManager{make(map[uint64]*util.ProcessInfo, 2)}
sm.processInfoMap[1] = &util.ProcessInfo{
ID: 1,
Expand Down

0 comments on commit 5d0a260

Please sign in to comment.