Skip to content

Commit

Permalink
filter,table-filter: replace blacklist/whitelist by blocklist/allowli…
Browse files Browse the repository at this point in the history
…st (#353)
  • Loading branch information
kennytm authored Jun 18, 2020
1 parent 6ddacc7 commit d38eaa2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
28 changes: 14 additions & 14 deletions pkg/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (c *cache) set(key string, action ActionType) {
// Rules contains Filter rules.
type Rules = tfilter.MySQLReplicationRules

// Filter implements whitelist and blacklist filters.
// Filter implements table filter in the style of MySQL replication rules.
type Filter struct {
selector.Selector
patternMap map[string]*regexp.Regexp
Expand Down Expand Up @@ -101,7 +101,7 @@ const (
type nodeEndRule struct {
kind int
r *regexp.Regexp
isWhiteList bool
isAllowList bool
}

// initRules initialize the rules to regex expr or trie node.
Expand Down Expand Up @@ -168,17 +168,17 @@ func (f *Filter) initOneRegex(originStr string) error {
return nil
}

func (f *Filter) initSchemaRule(dbStr string, isWhiteList bool) error {
func (f *Filter) initSchemaRule(dbStr string, isAllowList bool) error {
if strings.HasPrefix(dbStr, "~") {
return f.initOneRegex(dbStr[1:])
}
return f.Selector.Insert(dbStr, "", &nodeEndRule{
kind: dbRule,
isWhiteList: isWhiteList,
isAllowList: isAllowList,
}, selector.Append)
}

func (f *Filter) initTableRule(dbStr, tableStr string, isWhiteList bool) error {
func (f *Filter) initTableRule(dbStr, tableStr string, isAllowList bool) error {
dbIsRegex := strings.HasPrefix(dbStr, "~")
tblIsRegex := strings.HasPrefix(tableStr, "~")
if dbIsRegex && tblIsRegex {
Expand All @@ -197,7 +197,7 @@ func (f *Filter) initTableRule(dbStr, tableStr string, isWhiteList bool) error {
}
err = f.Selector.Insert(tableStr, "", &nodeEndRule{
kind: tblRuleOnlyTblPart,
isWhiteList: isWhiteList,
isAllowList: isAllowList,
}, selector.Append)
if err != nil {
return err
Expand All @@ -210,15 +210,15 @@ func (f *Filter) initTableRule(dbStr, tableStr string, isWhiteList bool) error {
err = f.Selector.Insert(dbStr, "", &nodeEndRule{
kind: tblRuleOnlyDBPart,
r: f.patternMap[tableStr[1:]],
isWhiteList: isWhiteList,
isAllowList: isAllowList,
}, selector.Append)
if err != nil {
return err
}
} else {
err := f.Selector.Insert(dbStr, tableStr, &nodeEndRule{
kind: tblRuleFull,
isWhiteList: isWhiteList,
isAllowList: isAllowList,
}, selector.Append)
if err != nil {
return err
Expand Down Expand Up @@ -317,7 +317,7 @@ func (f *Filter) filterOnTables(tb *Table) bool {
return len(f.rules.DoTables) == 0
}

func (f *Filter) matchDB(patternDBS []string, a string, isWhiteListCheck bool) bool {
func (f *Filter) matchDB(patternDBS []string, a string, isAllowListCheck bool) bool {
for _, b := range patternDBS {
isRegex := strings.HasPrefix(b, "~")
if isRegex && f.matchString(b[1:], a) {
Expand All @@ -327,14 +327,14 @@ func (f *Filter) matchDB(patternDBS []string, a string, isWhiteListCheck bool) b
ruleSet := f.Selector.Match(a, "")
for _, r := range ruleSet {
rule := r.(*nodeEndRule)
if rule.kind == dbRule && rule.isWhiteList == isWhiteListCheck {
if rule.kind == dbRule && rule.isAllowList == isAllowListCheck {
return true
}
}
return false
}

func (f *Filter) matchTable(patternTBS []*Table, tb *Table, isWhiteListCheck bool) bool {
func (f *Filter) matchTable(patternTBS []*Table, tb *Table, isAllowListCheck bool) bool {
for _, ptb := range patternTBS {
dbIsRegex, tblIsRegex := strings.HasPrefix(ptb.Schema, "~"), strings.HasPrefix(ptb.Name, "~")
if dbIsRegex && tblIsRegex {
Expand All @@ -348,22 +348,22 @@ func (f *Filter) matchTable(patternTBS []*Table, tb *Table, isWhiteListCheck boo
ruleSet := f.Selector.Match(tb.Name, "")
for _, r := range ruleSet {
rule := r.(*nodeEndRule)
if rule.kind == tblRuleOnlyTblPart && rule.isWhiteList == isWhiteListCheck {
if rule.kind == tblRuleOnlyTblPart && rule.isAllowList == isAllowListCheck {
return true
}
}
}
ruleSet := f.Selector.Match(tb.Schema, "")
for _, r := range ruleSet {
rule := r.(*nodeEndRule)
if rule.kind == tblRuleOnlyDBPart && rule.isWhiteList == isWhiteListCheck && rule.r.MatchString(tb.Name) {
if rule.kind == tblRuleOnlyDBPart && rule.isAllowList == isAllowListCheck && rule.r.MatchString(tb.Name) {
return true
}
}
ruleSet = f.Selector.Match(tb.Schema, tb.Name)
for _, r := range ruleSet {
rule := r.(*nodeEndRule)
if rule.kind == tblRuleFull && rule.isWhiteList == isWhiteListCheck {
if rule.kind == tblRuleFull && rule.isAllowList == isAllowListCheck {
return true
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/table-filter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Try to run with `./main -f 'employee.*' -f '*.WorkOrder'` and see the result.

## Syntax

### Whitelist
### Allowlist

The input to the `filter.Parse()` function is a list of table filter rules.
Each rule specifies what the fully-qualified name of the table to be accepted.
Expand Down Expand Up @@ -121,11 +121,11 @@ Blank lines (empty strings) are ignored.
A leading `#` marks a comment and is ignored.
`#` not at start of line may be considered syntax error.

### Blacklist
### Blocklist

An `!` at the beginning of the line means the pattern after it is used to
exclude tables from being processed. This effectively turns the filter into a
blacklist.
blocklist.

```ini
*.*
Expand Down Expand Up @@ -189,7 +189,7 @@ You cannot place an unescaped `/` between `\Q`…`\E`.)
When a table name matches none of the rules in the filter list, the default
behavior is to ignore such unmatched tables.

To build a blacklist, an explicit `*.*` must be used as the first rule,
To build a blocklist, an explicit `*.*` must be used as the first rule,
otherwise all tables will be excluded.

```sh
Expand Down
8 changes: 4 additions & 4 deletions pkg/table-filter/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ func (t *Table) Clone() *Table {

// MySQLReplicationRules is a set of rules based on MySQL's replication filter.
type MySQLReplicationRules struct {
// DoTables is a whitelist of tables.
// DoTables is an allowlist of tables.
DoTables []*Table `json:"do-tables" toml:"do-tables" yaml:"do-tables"`
// DoDBs is the whitelist of schemas.
// DoDBs is an allowlist of schemas.
DoDBs []string `json:"do-dbs" toml:"do-dbs" yaml:"do-dbs"`

// IgnoreTables is a blacklist of tables.
// IgnoreTables is a blocklist of tables.
IgnoreTables []*Table `json:"ignore-tables" toml:"ignore-tables" yaml:"ignore-tables"`
// IgnoreDBs is a blacklist of schemas.
// IgnoreDBs is a blocklist of schemas.
IgnoreDBs []string `json:"ignore-dbs" toml:"ignore-dbs" yaml:"ignore-dbs"`
}

Expand Down

0 comments on commit d38eaa2

Please sign in to comment.