| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- 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
- }
|