From 4af85786d37743827a985c0b0d401b6193ac76b3 Mon Sep 17 00:00:00 2001 From: kaanaktas Date: Mon, 25 Jul 2022 14:04:27 +0100 Subject: [PATCH] implemented custom configuration for data filter rules --- datafilter/load.go | 77 +++++++++++++++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 22 deletions(-) diff --git a/datafilter/load.go b/datafilter/load.go index 819f1a3..828f287 100644 --- a/datafilter/load.go +++ b/datafilter/load.go @@ -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() @@ -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) @@ -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] } } } @@ -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: @@ -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 +}