mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-30 05:04: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
+1
-6
@@ -77,7 +77,6 @@ func init() {
|
|||||||
rootCmd.PersistentFlags().StringP("gitleaks-ignore-path", "i", ".", "path to .gitleaksignore file or folder containing one")
|
rootCmd.PersistentFlags().StringP("gitleaks-ignore-path", "i", ".", "path to .gitleaksignore file or folder containing one")
|
||||||
rootCmd.PersistentFlags().Int("max-decode-depth", 0, "allow recursive decoding up to this depth (default \"0\", no decoding is done)")
|
rootCmd.PersistentFlags().Int("max-decode-depth", 0, "allow recursive decoding up to this depth (default \"0\", no decoding is done)")
|
||||||
rootCmd.PersistentFlags().Int("max-archive-depth", 0, "allow scanning into nested archives up to this depth (default \"0\", no archive traversal is done)")
|
rootCmd.PersistentFlags().Int("max-archive-depth", 0, "allow scanning into nested archives up to this depth (default \"0\", no archive traversal is done)")
|
||||||
rootCmd.PersistentFlags().BoolP("experimental-optimizations", "", false, "enables experimental allowlist optimizations, increasing performance at the cost of startup time")
|
|
||||||
|
|
||||||
// Add diagnostics flags
|
// Add diagnostics flags
|
||||||
rootCmd.PersistentFlags().String("diagnostics", "", "enable diagnostics (http OR comma-separated list: cpu,mem,trace). cpu=CPU prof, mem=memory prof, trace=exec tracing, http=serve via net/http/pprof")
|
rootCmd.PersistentFlags().String("diagnostics", "", "enable diagnostics (http OR comma-separated list: cpu,mem,trace). cpu=CPU prof, mem=memory prof, trace=exec tracing, http=serve via net/http/pprof")
|
||||||
@@ -223,11 +222,7 @@ func Config(cmd *cobra.Command) config.Config {
|
|||||||
if err := viper.Unmarshal(&vc); err != nil {
|
if err := viper.Unmarshal(&vc); err != nil {
|
||||||
logging.Fatal().Err(err).Msg("Failed to load config")
|
logging.Fatal().Err(err).Msg("Failed to load config")
|
||||||
}
|
}
|
||||||
// set experimental feature flag(s)
|
|
||||||
if mustGetBoolFlag(cmd, "experimental-optimizations") {
|
|
||||||
logging.Warn().Msgf("using experimental allowlist optimizations, updates may contain breaking changes!")
|
|
||||||
vc.EnableExperimentalAllowlistOptimizations = true
|
|
||||||
}
|
|
||||||
cfg, err := vc.Translate()
|
cfg, err := vc.Translate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Fatal().Err(err).Msg("Failed to load config")
|
logging.Fatal().Err(err).Msg("Failed to load config")
|
||||||
|
|||||||
+9
-22
@@ -59,11 +59,6 @@ type Allowlist struct {
|
|||||||
// validated is an internal flag to track whether `Validate()` has been called.
|
// validated is an internal flag to track whether `Validate()` has been called.
|
||||||
validated bool
|
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.
|
// commitMap is a normalized version of Commits, used for efficiency purposes.
|
||||||
// TODO: possible optimizations so that both short and long hashes work.
|
// TODO: possible optimizations so that both short and long hashes work.
|
||||||
commitMap map[string]struct{}
|
commitMap map[string]struct{}
|
||||||
@@ -92,11 +87,8 @@ func (a *Allowlist) Validate() error {
|
|||||||
// Commits are case-insensitive.
|
// Commits are case-insensitive.
|
||||||
uniqueCommits[strings.TrimSpace(strings.ToLower(commit))] = struct{}{}
|
uniqueCommits[strings.TrimSpace(strings.ToLower(commit))] = struct{}{}
|
||||||
}
|
}
|
||||||
if a.EnableExperimentalOptimizations {
|
a.Commits = maps.Keys(uniqueCommits)
|
||||||
a.commitMap = uniqueCommits
|
a.commitMap = uniqueCommits
|
||||||
} else {
|
|
||||||
a.Commits = maps.Keys(uniqueCommits)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if len(a.StopWords) > 0 {
|
if len(a.StopWords) > 0 {
|
||||||
uniqueStopwords := make(map[string]struct{})
|
uniqueStopwords := make(map[string]struct{})
|
||||||
@@ -105,21 +97,16 @@ func (a *Allowlist) Validate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
values := maps.Keys(uniqueStopwords)
|
values := maps.Keys(uniqueStopwords)
|
||||||
if a.EnableExperimentalOptimizations {
|
a.StopWords = values
|
||||||
a.stopwordTrie = ahocorasick.NewTrieBuilder().AddStrings(values).Build()
|
a.stopwordTrie = ahocorasick.NewTrieBuilder().AddStrings(values).Build()
|
||||||
} else {
|
|
||||||
a.StopWords = values
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Combine patterns into a single expression.
|
// Combine patterns into a single expression.
|
||||||
if a.EnableExperimentalOptimizations {
|
if len(a.Paths) > 0 {
|
||||||
if len(a.Paths) > 0 {
|
a.pathPat = joinRegexOr(a.Paths)
|
||||||
a.pathPat = joinRegexOr(a.Paths)
|
}
|
||||||
}
|
if len(a.Regexes) > 0 {
|
||||||
if len(a.Regexes) > 0 {
|
a.regexPat = joinRegexOr(a.Regexes)
|
||||||
a.regexPat = joinRegexOr(a.Regexes)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
a.validated = true
|
a.validated = true
|
||||||
|
|||||||
@@ -151,7 +151,7 @@ func TestValidate(t *testing.T) {
|
|||||||
cmpopts.IgnoreUnexported(Allowlist{}),
|
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)
|
t.Errorf("diff: (-want +got)\n%s", diff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-20
@@ -50,12 +50,6 @@ type ViperConfig struct {
|
|||||||
// TODO: Remove this in 9.x.
|
// TODO: Remove this in 9.x.
|
||||||
AllowList *viperGlobalAllowlist
|
AllowList *viperGlobalAllowlist
|
||||||
Allowlists []*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 {
|
type viperRuleAllowlist struct {
|
||||||
@@ -261,14 +255,13 @@ func (vc *ViperConfig) parseAllowlist(a *viperRuleAllowlist) (*Allowlist, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
allowlist := &Allowlist{
|
allowlist := &Allowlist{
|
||||||
Description: a.Description,
|
Description: a.Description,
|
||||||
MatchCondition: matchCondition,
|
MatchCondition: matchCondition,
|
||||||
Commits: a.Commits,
|
Commits: a.Commits,
|
||||||
Paths: allowlistPaths,
|
Paths: allowlistPaths,
|
||||||
RegexTarget: regexTarget,
|
RegexTarget: regexTarget,
|
||||||
Regexes: allowlistRegexes,
|
Regexes: allowlistRegexes,
|
||||||
StopWords: a.StopWords,
|
StopWords: a.StopWords,
|
||||||
EnableExperimentalOptimizations: vc.EnableExperimentalAllowlistOptimizations,
|
|
||||||
}
|
}
|
||||||
if err := allowlist.Validate(); err != nil {
|
if err := allowlist.Validate(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -292,9 +285,7 @@ func (c *Config) extendDefault(parent *ViperConfig) error {
|
|||||||
if err := viper.ReadConfig(strings.NewReader(DefaultConfig)); err != nil {
|
if err := viper.ReadConfig(strings.NewReader(DefaultConfig)); err != nil {
|
||||||
return fmt.Errorf("failed to load extended default config, err: %w", err)
|
return fmt.Errorf("failed to load extended default config, err: %w", err)
|
||||||
}
|
}
|
||||||
defaultViperConfig := ViperConfig{
|
defaultViperConfig := ViperConfig{}
|
||||||
EnableExperimentalAllowlistOptimizations: parent.EnableExperimentalAllowlistOptimizations,
|
|
||||||
}
|
|
||||||
if err := viper.Unmarshal(&defaultViperConfig); err != nil {
|
if err := viper.Unmarshal(&defaultViperConfig); err != nil {
|
||||||
return fmt.Errorf("failed to load extended default config, err: %w", err)
|
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 {
|
if err := viper.ReadInConfig(); err != nil {
|
||||||
return fmt.Errorf("failed to load extended config, err: %w", err)
|
return fmt.Errorf("failed to load extended config, err: %w", err)
|
||||||
}
|
}
|
||||||
extensionViperConfig := ViperConfig{
|
extensionViperConfig := ViperConfig{}
|
||||||
EnableExperimentalAllowlistOptimizations: parent.EnableExperimentalAllowlistOptimizations,
|
|
||||||
}
|
|
||||||
if err := viper.Unmarshal(&extensionViperConfig); err != nil {
|
if err := viper.Unmarshal(&extensionViperConfig); err != nil {
|
||||||
return fmt.Errorf("failed to load extended config, err: %w", err)
|
return fmt.Errorf("failed to load extended config, err: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user