Skip to content

Commit

Permalink
implemented custom configuration for data filter rules
Browse files Browse the repository at this point in the history
  • Loading branch information
kaanaktas committed Jul 25, 2022
1 parent 9500cb9 commit 4af8578
Showing 1 changed file with 55 additions and 22 deletions.
77 changes: 55 additions & 22 deletions datafilter/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ type ruleSet struct {
}

type rules struct {
Name string `json:"name"`
Path string `json:"path"`
Name string `json:"name"`
Path string `json:"path"`
CustomPath string `json:"custom_path"`
}

var cacheIn = cache.NewInMemory()
Expand All @@ -28,24 +29,6 @@ var dataFilterRuleSet []byte
//go:embed rules/*
var ruleFs embed.FS

func indexOfRuleSet(ruleSet []ruleSet, ruleType string) int {
for i, set := range ruleSet {
if ruleType == set.Type {
return i
}
}
return -1
}

func indexOfRule(rules []rules, ruleName string) int {
for i, rule := range rules {
if ruleName == rule.Name {
return i
}
}
return -1
}

func Load(dataFilterRuleSetPath string) {
var ruleSet, customRuleSet []ruleSet
err := json.Unmarshal(dataFilterRuleSet, &ruleSet)
Expand Down Expand Up @@ -78,7 +61,7 @@ func Load(dataFilterRuleSetPath string) {
if index == -1 {
ruleSet[rsIndex].Rules = append(ruleSet[rsIndex].Rules, customRules[k])
} else {
(ruleSet[rsIndex]).Rules[index].Path = customRules[k].Path
(ruleSet[rsIndex]).Rules[index] = customRules[k]
}
}
}
Expand All @@ -92,13 +75,36 @@ func Load(dataFilterRuleSetPath string) {
panic(err)
}

var patterns []pattern
var patterns, customPatterns []pattern
err = json.Unmarshal(content, &patterns)
if err != nil {
msg := fmt.Sprintf("Can't unmarshall the content of %s. Error: %s", rule.Path, err)
panic(msg)
}

if rule.CustomPath != "" {
content, err = config.ReadFile(filepath.Join(config.RootDirectory, rule.CustomPath))
if err != nil {
msg := fmt.Sprintf("Error while reading %s. Error: %s", dataFilterRuleSetPath, err)
panic(msg)
}
err = json.Unmarshal(content, &customPatterns)
if err != nil {
msg := fmt.Sprintf("Can't unmarshall the content of datafilter_rule_set.json. Error: %s", err)
panic(msg)
}
}

for i := 0; i < len(customPatterns); i++ {
patternName := customPatterns[i].Name
patternIndex := indexOfPatterns(patterns, patternName)
if patternIndex == -1 {
patterns = append(patterns, customPatterns[i])
} else {
patterns[patternIndex] = customPatterns[i]
}
}

validateRule := make([]Validate, len(patterns))
switch set.Type {
case PAN:
Expand All @@ -116,3 +122,30 @@ func Load(dataFilterRuleSetPath string) {
}
log.Println("datafilter rules have been loaded successfully")
}

func indexOfRuleSet(ruleSet []ruleSet, ruleType string) int {
for i, set := range ruleSet {
if ruleType == set.Type {
return i
}
}
return -1
}

func indexOfRule(rules []rules, ruleName string) int {
for i, rule := range rules {
if ruleName == rule.Name {
return i
}
}
return -1
}

func indexOfPatterns(patterns []pattern, patternName string) int {
for i, pattern := range patterns {
if patternName == pattern.Name {
return i
}
}
return -1
}

0 comments on commit 4af8578

Please sign in to comment.