From 92f1fe90437c8587628c70adef6fc41364391f01 Mon Sep 17 00:00:00 2001 From: Hagar Fisher Date: Fri, 11 Aug 2023 18:46:36 +0300 Subject: [PATCH] appending custom rules to all rules array --- secrets/secrets.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/secrets/secrets.go b/secrets/secrets.go index d98bd3b0..c0976d93 100644 --- a/secrets/secrets.go +++ b/secrets/secrets.go @@ -128,6 +128,30 @@ func (s *Secrets) AddRegexRules(patterns []string) error { return nil } +func addCustomRules(rules []CustomRuleConfiguration) ([]Rule, error) { + var customRules []Rule + customRules = make([]Rule, len(rules)) + for idx, rule := range rules { + regex, err := regexp.Compile(rule.RegexPattern) + if err != nil { + return nil, fmt.Errorf("failed to compile custom regex rule %s: %w", rule.RuleID, err) + } + customRules[idx] = Rule{ + Rule: config.Rule{ + Description: rule.Description, + RuleID: rule.RuleID, + Regex: regex, + Keywords: []string{}, + }, + Tags: rule.Tags, + } + if rule.SecretGroup != 0 { + customRules[idx].Rule.SecretGroup = rule.SecretGroup + } + } + return customRules, nil +} + func getFindingId(item plugins.Item, finding report.Finding) string { idParts := []string{item.ID, finding.RuleID, finding.Secret} sha := sha1.Sum([]byte(strings.Join(idParts, "-"))) @@ -369,6 +393,12 @@ func loadAllRules() ([]Rule, error) { allRules = append(allRules, Rule{Rule: *rules.YandexAccessToken(), Tags: []string{TagAccessToken}}) allRules = append(allRules, Rule{Rule: *rules.ZendeskSecretKey(), Tags: []string{TagSecretKey}}) + builtCustomRules, err := addCustomRules(customRules) + if err != nil { + return nil, err + } + allRules = append(allRules, builtCustomRules...) + return allRules, nil }