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

executor: forbid user to drop important system table #7471

Merged
merged 8 commits into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
31 changes: 31 additions & 0 deletions executor/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,17 @@ func (e *DDLExec) executeCreateIndex(s *ast.CreateIndexStmt) error {
return errors.Trace(err)
}

var errForbidDrop = errors.New("Drop tidb system tables is forbidden")

func (e *DDLExec) executeDropDatabase(s *ast.DropDatabaseStmt) error {
dbName := model.NewCIStr(s.Name)

// Protect important system table from been dropped by a mistake.
// I can hardly find a case that a user really need to do this.
if dbName.L == "mysql" {
return errors.Trace(errForbidDrop)
}

err := domain.GetDomain(e.ctx).DDL().DropSchema(e.ctx, dbName)
if infoschema.ErrDatabaseNotExists.Equal(err) {
if s.IfExists {
Expand All @@ -179,6 +188,22 @@ func (e *DDLExec) executeDropDatabase(s *ast.DropDatabaseStmt) error {
return errors.Trace(err)
}

var systemTables = map[string]struct{}{
Copy link
Member

Choose a reason for hiding this comment

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

Add a comment for this map. Explain why we prevent users from dropping them.

"tidb": {},
"gc_delete_range": {},
"gc_delete_range_done": {},
}

func isSystemTable(schema, table string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we put it to infoschema.go? I think it is like "IsMemoryDB", we can put these together.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This function is removed, we forbid dropping tables in "mysql" database.

if schema != "mysql" {
return false
}
if _, ok := systemTables[table]; ok {
return true
}
return false
}

func (e *DDLExec) executeDropTable(s *ast.DropTableStmt) error {
var notExistTables []string
for _, tn := range s.Tables {
Expand All @@ -198,6 +223,12 @@ func (e *DDLExec) executeDropTable(s *ast.DropTableStmt) error {
return errors.Trace(err)
}

// Protect important system table from been dropped by a mistake.
// I can hardly find a case that a user really need to do this.
if isSystemTable(tn.Schema.L, tn.Name.L) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What about forbidding drop any tables in database mysql? It can avoid we add some important table in mysql, and forget to modify the systemTables map.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What's your opinion? @shenli @lamxTyler

Copy link
Member

Choose a reason for hiding this comment

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

I suggest protecting all the tables in the mysql database.

return errors.Trace(errForbidDrop)
Copy link
Member

Choose a reason for hiding this comment

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

Add the system table name in the error message.

}

if config.CheckTableBeforeDrop {
log.Warnf("admin check table `%s`.`%s` before drop.", fullti.Schema.O, fullti.Name.O)
sql := fmt.Sprintf("admin check table `%s`.`%s`", fullti.Schema.O, fullti.Name.O)
Expand Down
6 changes: 6 additions & 0 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ func (s *testSuite) TestCreateDropDatabase(c *C) {
c.Assert(err.Error(), Equals, plan.ErrNoDB.Error())
_, err = tk.Exec("select * from t;")
c.Assert(err.Error(), Equals, plan.ErrNoDB.Error())

_, err = tk.Exec("drop database mysql")
c.Assert(err, NotNil)
}

func (s *testSuite) TestCreateDropTable(c *C) {
Expand All @@ -137,6 +140,9 @@ func (s *testSuite) TestCreateDropTable(c *C) {
tk.MustExec("drop table if exists drop_test")
tk.MustExec("create table drop_test (a int)")
tk.MustExec("drop table drop_test")

_, err := tk.Exec("drop table mysql.gc_delete_range")
c.Assert(err, NotNil)
}

func (s *testSuite) TestCreateDropIndex(c *C) {
Expand Down