feat(config): update rule validation (#1466)

This commit is contained in:
Richard Gomez authored and GitHub committed 2024-08-22 09:55:44 -05:00
1 parent 6dfcf5e949
commit 1aae66d564
4 files changed
+64 -26

No files matched your search

+5 -10
View File
@@ -2,7 +2,6 @@ package config
import (
_ "embed"
"fmt"
"regexp"
"sort"
"strings"
@@ -82,10 +81,6 @@ func (vc *ViperConfig) Translate() (Config, error) {
rulesMap := make(map[string]Rule)
for _, r := range vc.Rules {
if strings.TrimSpace(r.ID) == "" {
return Config{}, fmt.Errorf("rule ID is missing or empty, regex: %s", r.Regex)
}
var allowlistRegexes []*regexp.Regexp
for _, a := range r.Allowlist.Regexes {
allowlistRegexes = append(allowlistRegexes, regexp.MustCompile(a))
@@ -120,8 +115,8 @@ func (vc *ViperConfig) Translate() (Config, error) {
configPathRegex = regexp.MustCompile(r.Path)
}
r := Rule{
Description: r.Description,
RuleID: r.ID,
Description: r.Description,
Regex: configRegex,
Path: configPathRegex,
SecretGroup: r.SecretGroup,
@@ -136,11 +131,11 @@ func (vc *ViperConfig) Translate() (Config, error) {
StopWords: r.Allowlist.StopWords,
},
}
orderedRules = append(orderedRules, r.RuleID)
if r.Regex != nil && r.SecretGroup > r.Regex.NumSubexp() {
return Config{}, fmt.Errorf("%s: invalid regex secret group %d, max regex secret group %d", r.RuleID, r.SecretGroup, r.Regex.NumSubexp())
if err := r.Validate(); err != nil {
return Config{}, err
}
orderedRules = append(orderedRules, r.RuleID)
rulesMap[r.RuleID] = r
}
var allowlistRegexes []*regexp.Regexp