mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-28 12:14:22 +01:00
* working on deduping * my eyes... oh god my poor eyes * more readme * more readme * more readme * more readme and formatting
113 lines
2.7 KiB
Go
113 lines
2.7 KiB
Go
package config
|
|
|
|
import (
|
|
_ "embed"
|
|
"fmt"
|
|
"regexp"
|
|
)
|
|
|
|
//go:embed gitleaks.toml
|
|
var DefaultConfig string
|
|
|
|
// ViperConfig is the config struct used by the Viper config package
|
|
// to parse the config file. This struct does not include regular expressions.
|
|
// It is used as an intermediary to convert the Viper config to the Config struct.
|
|
type ViperConfig struct {
|
|
Description string
|
|
Rules []struct {
|
|
ID string
|
|
Description string
|
|
Entropy float64
|
|
SecretGroup int
|
|
Regex string
|
|
Path string
|
|
Tags []string
|
|
|
|
Allowlist struct {
|
|
Regexes []string
|
|
Paths []string
|
|
Commits []string
|
|
}
|
|
}
|
|
Allowlist struct {
|
|
Regexes []string
|
|
Paths []string
|
|
Commits []string
|
|
}
|
|
}
|
|
|
|
// Config is a configuration struct that contains rules and an allowlist if present.
|
|
type Config struct {
|
|
Path string
|
|
Description string
|
|
Rules []*Rule
|
|
Allowlist Allowlist
|
|
}
|
|
|
|
func (vc *ViperConfig) Translate() (Config, error) {
|
|
var rules []*Rule
|
|
for _, r := range vc.Rules {
|
|
var allowlistRegexes []*regexp.Regexp
|
|
for _, a := range r.Allowlist.Regexes {
|
|
allowlistRegexes = append(allowlistRegexes, regexp.MustCompile(a))
|
|
}
|
|
var allowlistPaths []*regexp.Regexp
|
|
for _, a := range r.Allowlist.Paths {
|
|
allowlistPaths = append(allowlistPaths, regexp.MustCompile(a))
|
|
}
|
|
|
|
if r.Tags == nil {
|
|
r.Tags = []string{}
|
|
}
|
|
|
|
var configRegex *regexp.Regexp
|
|
var configPathRegex *regexp.Regexp
|
|
if r.Regex == "" {
|
|
configRegex = nil
|
|
} else {
|
|
configRegex = regexp.MustCompile(r.Regex)
|
|
}
|
|
if r.Path == "" {
|
|
configPathRegex = nil
|
|
} else {
|
|
configPathRegex = regexp.MustCompile(r.Path)
|
|
}
|
|
r := &Rule{
|
|
Description: r.Description,
|
|
RuleID: r.ID,
|
|
Regex: configRegex,
|
|
Path: configPathRegex,
|
|
SecretGroup: r.SecretGroup,
|
|
Entropy: r.Entropy,
|
|
Tags: r.Tags,
|
|
Allowlist: Allowlist{
|
|
Regexes: allowlistRegexes,
|
|
Paths: allowlistPaths,
|
|
Commits: r.Allowlist.Commits,
|
|
},
|
|
}
|
|
if r.Regex != nil && r.SecretGroup > r.Regex.NumSubexp() {
|
|
return Config{}, fmt.Errorf("%s invalid regex secret group %d, max regex secret group %d", r.Description, r.SecretGroup, r.Regex.NumSubexp())
|
|
}
|
|
rules = append(rules, r)
|
|
|
|
}
|
|
var allowlistRegexes []*regexp.Regexp
|
|
for _, a := range vc.Allowlist.Regexes {
|
|
allowlistRegexes = append(allowlistRegexes, regexp.MustCompile(a))
|
|
}
|
|
var allowlistPaths []*regexp.Regexp
|
|
for _, a := range vc.Allowlist.Paths {
|
|
allowlistPaths = append(allowlistPaths, regexp.MustCompile(a))
|
|
}
|
|
return Config{
|
|
Description: vc.Description,
|
|
Rules: rules,
|
|
Allowlist: Allowlist{
|
|
Regexes: allowlistRegexes,
|
|
Paths: allowlistPaths,
|
|
Commits: vc.Allowlist.Commits,
|
|
},
|
|
}, nil
|
|
}
|