config.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. Keywords []string
  21. Path string
  22. Tags []string
  23. Allowlist struct {
  24. Regexes []string
  25. Paths []string
  26. Commits []string
  27. }
  28. }
  29. Allowlist struct {
  30. Regexes []string
  31. Paths []string
  32. Commits []string
  33. }
  34. }
  35. // Config is a configuration struct that contains rules and an allowlist if present.
  36. type Config struct {
  37. Path string
  38. Description string
  39. Rules []*Rule
  40. Allowlist Allowlist
  41. }
  42. func (vc *ViperConfig) Translate() (Config, error) {
  43. var rules []*Rule
  44. for _, r := range vc.Rules {
  45. var allowlistRegexes []*regexp.Regexp
  46. for _, a := range r.Allowlist.Regexes {
  47. allowlistRegexes = append(allowlistRegexes, regexp.MustCompile(a))
  48. }
  49. var allowlistPaths []*regexp.Regexp
  50. for _, a := range r.Allowlist.Paths {
  51. allowlistPaths = append(allowlistPaths, regexp.MustCompile(a))
  52. }
  53. if r.Keywords == nil {
  54. r.Keywords = []string{}
  55. }
  56. if r.Tags == nil {
  57. r.Tags = []string{}
  58. }
  59. var configRegex *regexp.Regexp
  60. var configPathRegex *regexp.Regexp
  61. if r.Regex == "" {
  62. configRegex = nil
  63. } else {
  64. configRegex = regexp.MustCompile(r.Regex)
  65. }
  66. if r.Path == "" {
  67. configPathRegex = nil
  68. } else {
  69. configPathRegex = regexp.MustCompile(r.Path)
  70. }
  71. r := &Rule{
  72. Description: r.Description,
  73. RuleID: r.ID,
  74. Regex: configRegex,
  75. Path: configPathRegex,
  76. SecretGroup: r.SecretGroup,
  77. Entropy: r.Entropy,
  78. Tags: r.Tags,
  79. Keywords: r.Keywords,
  80. Allowlist: Allowlist{
  81. Regexes: allowlistRegexes,
  82. Paths: allowlistPaths,
  83. Commits: r.Allowlist.Commits,
  84. },
  85. }
  86. if r.Regex != nil && r.SecretGroup > r.Regex.NumSubexp() {
  87. return Config{}, fmt.Errorf("%s invalid regex secret group %d, max regex secret group %d", r.Description, r.SecretGroup, r.Regex.NumSubexp())
  88. }
  89. rules = append(rules, r)
  90. }
  91. var allowlistRegexes []*regexp.Regexp
  92. for _, a := range vc.Allowlist.Regexes {
  93. allowlistRegexes = append(allowlistRegexes, regexp.MustCompile(a))
  94. }
  95. var allowlistPaths []*regexp.Regexp
  96. for _, a := range vc.Allowlist.Paths {
  97. allowlistPaths = append(allowlistPaths, regexp.MustCompile(a))
  98. }
  99. return Config{
  100. Description: vc.Description,
  101. Rules: rules,
  102. Allowlist: Allowlist{
  103. Regexes: allowlistRegexes,
  104. Paths: allowlistPaths,
  105. Commits: vc.Allowlist.Commits,
  106. },
  107. }, nil
  108. }