Add experimental allowlist optimizations (#1731)

* feat(allowlist): optimization experiments

* refactor(allowlist): remove useOptimizations flag

* refactor(allowlist): simplify Validate() with helper func

* refactor(config): replace global feature flag
This commit is contained in:
Richard Gomez authored and GitHub committed 2025-06-08 10:33:05 -05:00
1 parent 79068b35e5
commit 9faaa4a09c
9 files changed
+363 -42

No files matched your search

+11 -8
View File
@@ -250,7 +250,6 @@ func main() {
Str("rule-id", rule.RuleID).
Msg("Failed to validate rule")
}
sortRule(rule)
// check if rule is in ruleLookUp
if _, ok := ruleLookUp[rule.RuleID]; ok {
@@ -261,6 +260,13 @@ func main() {
// TODO: eventually change all the signatures to get ride of this
// nasty dereferencing.
ruleLookUp[rule.RuleID] = *rule
// Slices are de-duplicated with a map, every iteration has a different order.
// This is an awkward workaround.
for _, allowlist := range rule.Allowlists {
slices.Sort(allowlist.Commits)
slices.Sort(allowlist.StopWords)
}
}
tmpl, err := template.ParseFiles(templatePath)
@@ -276,14 +282,11 @@ func main() {
cfg := base.CreateGlobalConfig()
cfg.Rules = ruleLookUp
for _, allowlist := range cfg.Allowlists {
slices.Sort(allowlist.Commits)
slices.Sort(allowlist.StopWords)
}
if err = tmpl.Execute(f, cfg); err != nil {
logging.Fatal().Err(err).Msg("could not execute template")
}
}
// sortRule makes the generated config deterministic.
func sortRule(r *config.Rule) {
for _, a := range r.Allowlists {
slices.Sort(a.StopWords)
}
}
+5
View File
@@ -88,5 +88,10 @@ func createSingleRuleDetector(r *config.Rule) *detect.Detector {
cfg := base.CreateGlobalConfig()
cfg.Rules = rules
cfg.Keywords = uniqueKeywords
for _, a := range cfg.Allowlists {
if err := a.Validate(); err != nil {
logging.Fatal().Err(err).Msg("invalid global allowlist")
}
}
return detect.NewDetector(cfg)
}
+6
View File
@@ -77,6 +77,7 @@ func init() {
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-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
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")
@@ -222,6 +223,11 @@ func Config(cmd *cobra.Command) config.Config {
if err := viper.Unmarshal(&vc); err != nil {
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()
if err != nil {
logging.Fatal().Err(err).Msg("Failed to load config")