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

Fix panic of empty value in filter config #799

Merged
merged 4 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 38 additions & 1 deletion drainer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,43 @@ func (cfg *Config) configFromFile(path string) error {
return util.StrictDecodeFile(path, "drainer", cfg)
}

func (cfg *Config) validateFilter() error {
for _, db := range cfg.SyncerCfg.DoDBs {
if len(db) == 0 {
return errors.New("empty schema name in `replicate-do-db` config")
}
}

dbs := strings.Split(cfg.SyncerCfg.IgnoreSchemas, ",")
for _, db := range dbs {
if len(db) == 0 {
return errors.New("empty schema name in `ignore-schemas` config")
}
}

for _, tb := range cfg.SyncerCfg.DoTables {
if len(tb.Schema) == 0 {
return errors.New("empty schema name in `replicate-do-table` config")
}

if len(tb.Table) == 0 {
return errors.New("empty table name in `replicate-do-table` config")
}
}

for _, tb := range cfg.SyncerCfg.IgnoreTables {
if len(tb.Schema) == 0 {
return errors.New("empty schema name in `ignore-table` config")
}

if len(tb.Table) == 0 {
return errors.New("empty table name in `ignore-table` config")
}
}

return nil
}

// validate checks whether the configuration is valid
func (cfg *Config) validate() error {
if err := validateAddr(cfg.ListenAddr); err != nil {
Expand Down Expand Up @@ -254,7 +291,7 @@ func (cfg *Config) validate() error {
}
}

return nil
return cfg.validateFilter()
}

func (cfg *Config) adjustConfig() error {
Expand Down
33 changes: 33 additions & 0 deletions drainer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
. "github.com/pingcap/check"
"github.com/pingcap/parser/mysql"
dsync "github.com/pingcap/tidb-binlog/drainer/sync"
"github.com/pingcap/tidb-binlog/pkg/filter"
"github.com/pingcap/tidb-binlog/pkg/util"
pkgzk "github.com/pingcap/tidb-binlog/pkg/zk"
"github.com/samuel/go-zookeeper/zk"
Expand Down Expand Up @@ -67,6 +68,38 @@ func (t *testDrainerSuite) TestConfig(c *C) {
c.Assert(cfg.SyncerCfg.SQLMode, Equals, mysql.SQLMode(0))
}

func (t *testDrainerSuite) TestValidateFilter(c *C) {
cfg := NewConfig()
c.Assert(cfg.validateFilter(), IsNil)

cfg = NewConfig()
cfg.SyncerCfg.DoDBs = []string{""}
c.Assert(cfg.validateFilter(), NotNil)

cfg = NewConfig()
cfg.SyncerCfg.IgnoreSchemas = "a,,c"
c.Assert(cfg.validateFilter(), NotNil)

emptyScheme := []filter.TableName{{Schema: "", Table: "t"}}
emptyTable := []filter.TableName{{Schema: "s", Table: ""}}

cfg = NewConfig()
cfg.SyncerCfg.DoTables = emptyScheme
c.Assert(cfg.validateFilter(), NotNil)

cfg = NewConfig()
cfg.SyncerCfg.DoTables = emptyTable
c.Assert(cfg.validateFilter(), NotNil)

cfg = NewConfig()
cfg.SyncerCfg.IgnoreTables = emptyScheme
c.Assert(cfg.validateFilter(), NotNil)

cfg = NewConfig()
cfg.SyncerCfg.IgnoreTables = emptyTable
c.Assert(cfg.validateFilter(), NotNil)
}

func (t *testDrainerSuite) TestValidate(c *C) {
cfg := NewConfig()

Expand Down
6 changes: 3 additions & 3 deletions pkg/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ func NewFilter(ignoreDBs []string, ignoreTables []TableName, doDBs []string, doT
func (s *Filter) addOneRegex(originStr string) {
if _, ok := s.reMap[originStr]; !ok {
var re *regexp.Regexp
if originStr[0] != '~' {
re = regexp.MustCompile(fmt.Sprintf("(?i)^%s$", originStr))
} else {
if len(originStr) > 0 && originStr[0] == '~' {
re = regexp.MustCompile(fmt.Sprintf("(?i)%s", originStr[1:]))
} else { // must match completely
re = regexp.MustCompile(fmt.Sprintf("(?i)^%s$", originStr))
Copy link
Contributor

Choose a reason for hiding this comment

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

If originStr is empty, the regex become ^$?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, the else case means you must match totally.
but we check the config for empty schema and table at startup time, so it will not run into this case now actually, because it's meaningless for an empty schema and table, may cause by misconfiguring.

}
s.reMap[originStr] = re
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ func (t *testFilterSuite) TestFilter(c *C) {
filter = NewFilter(nil, ignoreTables, nil, nil)
c.Assert(filter.SkipSchemaAndTable("ignore", "ignore"), IsTrue)
c.Assert(filter.SkipSchemaAndTable("not_ignore", "not_ignore"), IsFalse)

// with empty string
filter = NewFilter(nil, nil, []string{""} /*doDBs*/, nil)
c.Assert(filter.SkipSchemaAndTable("", "any"), IsFalse)
c.Assert(filter.SkipSchemaAndTable("any", ""), IsTrue)
}