mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-29 12:44:22 +01:00
feat(allowlist): promote optimizations (#1908)
This commit is contained in:
1 parent
7fb4eda723
commit
bd79c3eaca
4 files changed
+20
-49
No files matched your search
+9
-22
@@ -59,11 +59,6 @@ type Allowlist struct {
|
||||
// validated is an internal flag to track whether `Validate()` has been called.
|
||||
validated bool
|
||||
|
||||
// EnableExperimentalOptimizations must be set prior to calling `Validate()`.
|
||||
// See: https://github.com/gitleaks/gitleaks/pull/1731
|
||||
//
|
||||
// NOTE: This flag may be removed in the future.
|
||||
EnableExperimentalOptimizations bool
|
||||
// commitMap is a normalized version of Commits, used for efficiency purposes.
|
||||
// TODO: possible optimizations so that both short and long hashes work.
|
||||
commitMap map[string]struct{}
|
||||
@@ -92,11 +87,8 @@ func (a *Allowlist) Validate() error {
|
||||
// Commits are case-insensitive.
|
||||
uniqueCommits[strings.TrimSpace(strings.ToLower(commit))] = struct{}{}
|
||||
}
|
||||
if a.EnableExperimentalOptimizations {
|
||||
a.commitMap = uniqueCommits
|
||||
} else {
|
||||
a.Commits = maps.Keys(uniqueCommits)
|
||||
}
|
||||
a.Commits = maps.Keys(uniqueCommits)
|
||||
a.commitMap = uniqueCommits
|
||||
}
|
||||
if len(a.StopWords) > 0 {
|
||||
uniqueStopwords := make(map[string]struct{})
|
||||
@@ -105,21 +97,16 @@ func (a *Allowlist) Validate() error {
|
||||
}
|
||||
|
||||
values := maps.Keys(uniqueStopwords)
|
||||
if a.EnableExperimentalOptimizations {
|
||||
a.stopwordTrie = ahocorasick.NewTrieBuilder().AddStrings(values).Build()
|
||||
} else {
|
||||
a.StopWords = values
|
||||
}
|
||||
a.StopWords = values
|
||||
a.stopwordTrie = ahocorasick.NewTrieBuilder().AddStrings(values).Build()
|
||||
}
|
||||
|
||||
// Combine patterns into a single expression.
|
||||
if a.EnableExperimentalOptimizations {
|
||||
if len(a.Paths) > 0 {
|
||||
a.pathPat = joinRegexOr(a.Paths)
|
||||
}
|
||||
if len(a.Regexes) > 0 {
|
||||
a.regexPat = joinRegexOr(a.Regexes)
|
||||
}
|
||||
if len(a.Paths) > 0 {
|
||||
a.pathPat = joinRegexOr(a.Paths)
|
||||
}
|
||||
if len(a.Regexes) > 0 {
|
||||
a.regexPat = joinRegexOr(a.Regexes)
|
||||
}
|
||||
|
||||
a.validated = true
|
||||
|
||||
@@ -151,7 +151,7 @@ func TestValidate(t *testing.T) {
|
||||
cmpopts.IgnoreUnexported(Allowlist{}),
|
||||
}
|
||||
)
|
||||
if diff := cmp.Diff(tt.input, tt.expected, opts); diff != "" {
|
||||
if diff := cmp.Diff(tt.expected, tt.input, opts); diff != "" {
|
||||
t.Errorf("diff: (-want +got)\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
+9
-20
@@ -50,12 +50,6 @@ type ViperConfig struct {
|
||||
// TODO: Remove this in 9.x.
|
||||
AllowList *viperGlobalAllowlist
|
||||
Allowlists []*viperGlobalAllowlist
|
||||
|
||||
// EnableExperimentalAllowlistOptimizations enables a preview feature.
|
||||
// See: https://github.com/gitleaks/gitleaks/pull/1731
|
||||
//
|
||||
// NOTE: This flag may be removed in the future.
|
||||
EnableExperimentalAllowlistOptimizations bool
|
||||
}
|
||||
|
||||
type viperRuleAllowlist struct {
|
||||
@@ -261,14 +255,13 @@ func (vc *ViperConfig) parseAllowlist(a *viperRuleAllowlist) (*Allowlist, error)
|
||||
}
|
||||
|
||||
allowlist := &Allowlist{
|
||||
Description: a.Description,
|
||||
MatchCondition: matchCondition,
|
||||
Commits: a.Commits,
|
||||
Paths: allowlistPaths,
|
||||
RegexTarget: regexTarget,
|
||||
Regexes: allowlistRegexes,
|
||||
StopWords: a.StopWords,
|
||||
EnableExperimentalOptimizations: vc.EnableExperimentalAllowlistOptimizations,
|
||||
Description: a.Description,
|
||||
MatchCondition: matchCondition,
|
||||
Commits: a.Commits,
|
||||
Paths: allowlistPaths,
|
||||
RegexTarget: regexTarget,
|
||||
Regexes: allowlistRegexes,
|
||||
StopWords: a.StopWords,
|
||||
}
|
||||
if err := allowlist.Validate(); err != nil {
|
||||
return nil, err
|
||||
@@ -292,9 +285,7 @@ func (c *Config) extendDefault(parent *ViperConfig) error {
|
||||
if err := viper.ReadConfig(strings.NewReader(DefaultConfig)); err != nil {
|
||||
return fmt.Errorf("failed to load extended default config, err: %w", err)
|
||||
}
|
||||
defaultViperConfig := ViperConfig{
|
||||
EnableExperimentalAllowlistOptimizations: parent.EnableExperimentalAllowlistOptimizations,
|
||||
}
|
||||
defaultViperConfig := ViperConfig{}
|
||||
if err := viper.Unmarshal(&defaultViperConfig); err != nil {
|
||||
return fmt.Errorf("failed to load extended default config, err: %w", err)
|
||||
}
|
||||
@@ -314,9 +305,7 @@ func (c *Config) extendPath(parent *ViperConfig) error {
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
return fmt.Errorf("failed to load extended config, err: %w", err)
|
||||
}
|
||||
extensionViperConfig := ViperConfig{
|
||||
EnableExperimentalAllowlistOptimizations: parent.EnableExperimentalAllowlistOptimizations,
|
||||
}
|
||||
extensionViperConfig := ViperConfig{}
|
||||
if err := viper.Unmarshal(&extensionViperConfig); err != nil {
|
||||
return fmt.Errorf("failed to load extended config, err: %w", err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user