refactor(allowlist): use iota for condition (#1569)

This commit is contained in:
Richard Gomez authored and GitHub committed 2024-10-16 14:59:37 -05:00
1 parent 12034a71c5
commit 5c03fa4a3e
4 files changed
+22 -15

No files matched your search

+1 -1
View File
@@ -71,7 +71,7 @@ tags = [
{{ else }} {{ else }}
{{ with $rule.Allowlists }}{{ range $i, $allowlist := . }}{{ if or $allowlist.Regexes $allowlist.Paths $allowlist.Commits $allowlist.StopWords }} {{ with $rule.Allowlists }}{{ range $i, $allowlist := . }}{{ if or $allowlist.Regexes $allowlist.Paths $allowlist.Commits $allowlist.StopWords }}
[[rules.allowlists]] [[rules.allowlists]]
{{ with $allowlist.MatchCondition }}condition = "{{ . }}" {{ with $allowlist.MatchCondition }}condition = "{{ .String }}"
{{ end -}} {{ end -}}
{{- with $allowlist.Commits }}commits = [ {{- with $allowlist.Commits }}commits = [
{{ range $j, $commit := . }}"{{ $commit }}",{{ end }} {{ range $j, $commit := . }}"{{ $commit }}",{{ end }}
+10 -3
View File
@@ -6,13 +6,20 @@ import (
"strings" "strings"
) )
type AllowlistMatchCondition string type AllowlistMatchCondition int
const ( const (
AllowlistMatchOr AllowlistMatchCondition = "OR" AllowlistMatchOr AllowlistMatchCondition = iota
AllowlistMatchAnd = "AND" AllowlistMatchAnd
) )
func (a AllowlistMatchCondition) String() string {
return [...]string{
"OR",
"AND",
}[a]
}
// Allowlist allows a rule to be ignored for specific // Allowlist allows a rule to be ignored for specific
// regexes, paths, and/or commits // regexes, paths, and/or commits
type Allowlist struct { type Allowlist struct {
+9 -9
View File
@@ -34,7 +34,7 @@ func TestTranslate(t *testing.T) {
Keywords: []string{}, Keywords: []string{},
Allowlists: []Allowlist{ Allowlists: []Allowlist{
{ {
MatchCondition: "OR", MatchCondition: AllowlistMatchOr,
Regexes: []*regexp.Regexp{regexp.MustCompile("123")}, Regexes: []*regexp.Regexp{regexp.MustCompile("123")},
}, },
}, },
@@ -68,7 +68,7 @@ func TestTranslate(t *testing.T) {
Tags: []string{"key", "AWS"}, Tags: []string{"key", "AWS"},
Allowlists: []Allowlist{ Allowlists: []Allowlist{
{ {
MatchCondition: "OR", MatchCondition: AllowlistMatchOr,
Regexes: []*regexp.Regexp{regexp.MustCompile("AKIALALEMEL33243OLIA")}, Regexes: []*regexp.Regexp{regexp.MustCompile("AKIALALEMEL33243OLIA")},
}, },
}, },
@@ -87,7 +87,7 @@ func TestTranslate(t *testing.T) {
Tags: []string{"key", "AWS"}, Tags: []string{"key", "AWS"},
Allowlists: []Allowlist{ Allowlists: []Allowlist{
{ {
MatchCondition: "OR", MatchCondition: AllowlistMatchOr,
Commits: []string{"allowthiscommit"}, Commits: []string{"allowthiscommit"},
}, },
}, },
@@ -106,7 +106,7 @@ func TestTranslate(t *testing.T) {
Tags: []string{"key", "AWS"}, Tags: []string{"key", "AWS"},
Allowlists: []Allowlist{ Allowlists: []Allowlist{
{ {
MatchCondition: "OR", MatchCondition: AllowlistMatchOr,
Paths: []*regexp.Regexp{regexp.MustCompile(".go")}, Paths: []*regexp.Regexp{regexp.MustCompile(".go")},
}, },
}, },
@@ -184,11 +184,11 @@ func TestTranslate(t *testing.T) {
Tags: []string{"key", "AWS"}, Tags: []string{"key", "AWS"},
Allowlists: []Allowlist{ Allowlists: []Allowlist{
{ {
MatchCondition: "OR", MatchCondition: AllowlistMatchOr,
StopWords: []string{"fake"}, StopWords: []string{"fake"},
}, },
{ {
MatchCondition: "OR", MatchCondition: AllowlistMatchOr,
Commits: []string{"abcdefg1"}, Commits: []string{"abcdefg1"},
Paths: []*regexp.Regexp{regexp.MustCompile(`ignore\.xaml`)}, Paths: []*regexp.Regexp{regexp.MustCompile(`ignore\.xaml`)},
Regexes: []*regexp.Regexp{regexp.MustCompile(`foo.+bar`)}, Regexes: []*regexp.Regexp{regexp.MustCompile(`foo.+bar`)},
@@ -212,11 +212,11 @@ func TestTranslate(t *testing.T) {
Tags: []string{"key", "AWS"}, Tags: []string{"key", "AWS"},
Allowlists: []Allowlist{ Allowlists: []Allowlist{
{ {
MatchCondition: "OR", MatchCondition: AllowlistMatchOr,
StopWords: []string{"fake"}, StopWords: []string{"fake"},
}, },
{ {
MatchCondition: "AND", MatchCondition: AllowlistMatchAnd,
Commits: []string{"abcdefg1"}, Commits: []string{"abcdefg1"},
Paths: []*regexp.Regexp{regexp.MustCompile(`ignore\.xaml`)}, Paths: []*regexp.Regexp{regexp.MustCompile(`ignore\.xaml`)},
Regexes: []*regexp.Regexp{regexp.MustCompile(`foo.+bar`)}, Regexes: []*regexp.Regexp{regexp.MustCompile(`foo.+bar`)},
@@ -240,7 +240,7 @@ func TestTranslate(t *testing.T) {
Tags: []string{"key", "AWS"}, Tags: []string{"key", "AWS"},
Allowlists: []Allowlist{ Allowlists: []Allowlist{
{ {
MatchCondition: "OR", MatchCondition: AllowlistMatchOr,
Paths: []*regexp.Regexp{regexp.MustCompile(`something.py`)}, Paths: []*regexp.Regexp{regexp.MustCompile(`something.py`)},
}, },
}, },
+2 -2
View File
@@ -282,7 +282,7 @@ func (d *Detector) detectRule(fragment Fragment, currentRaw string, rule config.
} }
if isAllowed { if isAllowed {
logger.Trace(). logger.Trace().
Str("condition", string(a.MatchCondition)). Str("condition", a.MatchCondition.String()).
Bool("commit-allowed", commitAllowed). Bool("commit-allowed", commitAllowed).
Bool("path-allowed", commitAllowed). Bool("path-allowed", commitAllowed).
Msg("Skipping fragment due to rule allowlist") Msg("Skipping fragment due to rule allowlist")
@@ -474,7 +474,7 @@ MatchLoop:
if isAllowed { if isAllowed {
logger.Trace(). logger.Trace().
Str("finding", finding.Secret). Str("finding", finding.Secret).
Str("condition", string(a.MatchCondition)). Str("condition", a.MatchCondition.String()).
Bool("regex-allowed", regexAllowed). Bool("regex-allowed", regexAllowed).
Bool("contains-stopword", containsStopword). Bool("contains-stopword", containsStopword).
Msg("Skipping finding due to rule allowlist") Msg("Skipping finding due to rule allowlist")