config.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package config
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "regexp"
  6. )
  7. //go:embed gitleaks.toml
  8. var DefaultConfig string
  9. // ViperConfig is the config struct used by the Viper config package
  10. // to parse the config file. This struct does not include regular expressions.
  11. // It is used as an intermediary to convert the Viper config to the Config struct.
  12. type ViperConfig struct {
  13. Description string
  14. Rules []struct {
  15. ID string
  16. Description string
  17. Entropy float64
  18. SecretGroup int
  19. Regex string
  20. Path string
  21. Tags []string
  22. Allowlist struct {
  23. Regexes []string
  24. Paths []string
  25. Commits []string
  26. }
  27. }
  28. Allowlist struct {
  29. Regexes []string
  30. Paths []string
  31. Commits []string
  32. }
  33. }
  34. // Config is a configuration struct that contains rules and an allowlist if present.
  35. type Config struct {
  36. Path string
  37. Description string
  38. Rules []*Rule
  39. Allowlist Allowlist
  40. }
  41. func (vc *ViperConfig) Translate() (Config, error) {
  42. var rules []*Rule
  43. for _, r := range vc.Rules {
  44. var allowlistRegexes []*regexp.Regexp
  45. for _, a := range r.Allowlist.Regexes {
  46. allowlistRegexes = append(allowlistRegexes, regexp.MustCompile(a))
  47. }
  48. var allowlistPaths []*regexp.Regexp
  49. for _, a := range r.Allowlist.Paths {
  50. allowlistPaths = append(allowlistPaths, regexp.MustCompile(a))
  51. }
  52. if r.Tags == nil {
  53. r.Tags = []string{}
  54. }
  55. var configRegex *regexp.Regexp
  56. var configPathRegex *regexp.Regexp
  57. if r.Regex == "" {
  58. configRegex = nil
  59. } else {
  60. configRegex = regexp.MustCompile(r.Regex)
  61. }
  62. if r.Path == "" {
  63. configPathRegex = nil
  64. } else {
  65. configPathRegex = regexp.MustCompile(r.Path)
  66. }
  67. r := &Rule{
  68. Description: r.Description,
  69. RuleID: r.ID,
  70. Regex: configRegex,
  71. Path: configPathRegex,
  72. SecretGroup: r.SecretGroup,
  73. Entropy: r.Entropy,
  74. Tags: r.Tags,
  75. Allowlist: Allowlist{
  76. Regexes: allowlistRegexes,
  77. Paths: allowlistPaths,
  78. Commits: r.Allowlist.Commits,
  79. },
  80. }
  81. if r.Regex != nil && r.SecretGroup > r.Regex.NumSubexp() {
  82. return Config{}, fmt.Errorf("%s invalid regex secret group %d, max regex secret group %d", r.Description, r.SecretGroup, r.Regex.NumSubexp())
  83. }
  84. rules = append(rules, r)
  85. }
  86. var allowlistRegexes []*regexp.Regexp
  87. for _, a := range vc.Allowlist.Regexes {
  88. allowlistRegexes = append(allowlistRegexes, regexp.MustCompile(a))
  89. }
  90. var allowlistPaths []*regexp.Regexp
  91. for _, a := range vc.Allowlist.Paths {
  92. allowlistPaths = append(allowlistPaths, regexp.MustCompile(a))
  93. }
  94. return Config{
  95. Description: vc.Description,
  96. Rules: rules,
  97. Allowlist: Allowlist{
  98. Regexes: allowlistRegexes,
  99. Paths: allowlistPaths,
  100. Commits: vc.Allowlist.Commits,
  101. },
  102. }, nil
  103. }