config_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package config
  2. import (
  3. "fmt"
  4. "github.com/google/go-cmp/cmp"
  5. "regexp"
  6. "testing"
  7. "github.com/spf13/viper"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. const configPath = "../testdata/config/"
  12. func TestTranslate(t *testing.T) {
  13. tests := []struct {
  14. cfgName string
  15. cfg Config
  16. wantError error
  17. }{
  18. {
  19. cfgName: "allow_aws_re",
  20. cfg: Config{
  21. Rules: map[string]Rule{"aws-access-key": {
  22. Description: "AWS Access Key",
  23. Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
  24. Tags: []string{"key", "AWS"},
  25. Keywords: []string{},
  26. RuleID: "aws-access-key",
  27. Allowlist: Allowlist{
  28. Regexes: []*regexp.Regexp{
  29. regexp.MustCompile("AKIALALEMEL33243OLIA"),
  30. },
  31. },
  32. },
  33. },
  34. },
  35. },
  36. {
  37. cfgName: "allow_commit",
  38. cfg: Config{
  39. Rules: map[string]Rule{"aws-access-key": {
  40. Description: "AWS Access Key",
  41. Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
  42. Tags: []string{"key", "AWS"},
  43. Keywords: []string{},
  44. RuleID: "aws-access-key",
  45. Allowlist: Allowlist{
  46. Commits: []string{"allowthiscommit"},
  47. },
  48. },
  49. },
  50. },
  51. },
  52. {
  53. cfgName: "allow_path",
  54. cfg: Config{
  55. Rules: map[string]Rule{"aws-access-key": {
  56. Description: "AWS Access Key",
  57. Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
  58. Tags: []string{"key", "AWS"},
  59. Keywords: []string{},
  60. RuleID: "aws-access-key",
  61. Allowlist: Allowlist{
  62. Paths: []*regexp.Regexp{
  63. regexp.MustCompile(".go"),
  64. },
  65. },
  66. },
  67. },
  68. },
  69. },
  70. {
  71. cfgName: "entropy_group",
  72. cfg: Config{
  73. Rules: map[string]Rule{"discord-api-key": {
  74. Description: "Discord API key",
  75. Regex: regexp.MustCompile(`(?i)(discord[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([a-h0-9]{64})['\"]`),
  76. RuleID: "discord-api-key",
  77. Allowlist: Allowlist{},
  78. Entropy: 3.5,
  79. SecretGroup: 3,
  80. Tags: []string{},
  81. Keywords: []string{},
  82. },
  83. },
  84. },
  85. },
  86. {
  87. cfgName: "missing_id",
  88. cfg: Config{},
  89. wantError: fmt.Errorf("rule |id| is missing or empty, regex: (?i)(discord[a-z0-9_ .\\-,]{0,25})(=|>|:=|\\|\\|:|<=|=>|:).{0,5}['\\\"]([a-h0-9]{64})['\\\"]"),
  90. },
  91. {
  92. cfgName: "no_regex_or_path",
  93. cfg: Config{},
  94. wantError: fmt.Errorf("discord-api-key: both |regex| and |path| are empty, this rule will have no effect"),
  95. },
  96. {
  97. cfgName: "bad_entropy_group",
  98. cfg: Config{},
  99. wantError: fmt.Errorf("discord-api-key: invalid regex secret group 5, max regex secret group 3"),
  100. },
  101. {
  102. cfgName: "base",
  103. cfg: Config{
  104. Rules: map[string]Rule{
  105. "aws-access-key": {
  106. Description: "AWS Access Key",
  107. Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
  108. Tags: []string{"key", "AWS"},
  109. Keywords: []string{},
  110. RuleID: "aws-access-key",
  111. },
  112. "aws-secret-key": {
  113. Description: "AWS Secret Key",
  114. Regex: regexp.MustCompile(`(?i)aws_(.{0,20})?=?.[\'\"0-9a-zA-Z\/+]{40}`),
  115. Tags: []string{"key", "AWS"},
  116. Keywords: []string{},
  117. RuleID: "aws-secret-key",
  118. },
  119. "aws-secret-key-again": {
  120. Description: "AWS Secret Key",
  121. Regex: regexp.MustCompile(`(?i)aws_(.{0,20})?=?.[\'\"0-9a-zA-Z\/+]{40}`),
  122. Tags: []string{"key", "AWS"},
  123. Keywords: []string{},
  124. RuleID: "aws-secret-key-again",
  125. },
  126. },
  127. },
  128. },
  129. {
  130. cfgName: "extend_rule_allowlist",
  131. cfg: Config{
  132. Rules: map[string]Rule{
  133. "aws-secret-key-again-again": {
  134. RuleID: "aws-secret-key-again-again",
  135. Description: "AWS Secret Key",
  136. Regex: regexp.MustCompile(`(?i)aws_(.{0,20})?=?.[\'\"0-9a-zA-Z\/+]{40}`),
  137. Tags: []string{"key", "AWS"},
  138. Keywords: []string{},
  139. Allowlist: Allowlist{
  140. Commits: []string{"abcdefg1"},
  141. Regexes: []*regexp.Regexp{
  142. regexp.MustCompile(`foo.+bar`),
  143. },
  144. Paths: []*regexp.Regexp{
  145. regexp.MustCompile(`ignore\.xaml`),
  146. },
  147. StopWords: []string{"example"},
  148. },
  149. },
  150. },
  151. },
  152. },
  153. {
  154. cfgName: "extend_empty_regexpath",
  155. cfg: Config{
  156. Rules: map[string]Rule{
  157. "aws-secret-key-again-again": {
  158. RuleID: "aws-secret-key-again-again",
  159. Description: "AWS Secret Key",
  160. Regex: regexp.MustCompile(`(?i)aws_(.{0,20})?=?.[\'\"0-9a-zA-Z\/+]{40}`),
  161. Tags: []string{"key", "AWS"},
  162. Keywords: []string{},
  163. Allowlist: Allowlist{
  164. Paths: []*regexp.Regexp{
  165. regexp.MustCompile(`something.py`),
  166. },
  167. },
  168. },
  169. },
  170. },
  171. },
  172. }
  173. for _, tt := range tests {
  174. t.Run(tt.cfgName, func(t *testing.T) {
  175. t.Cleanup(func() {
  176. extendDepth = 0
  177. viper.Reset()
  178. })
  179. viper.AddConfigPath(configPath)
  180. viper.SetConfigName(tt.cfgName)
  181. viper.SetConfigType("toml")
  182. err := viper.ReadInConfig()
  183. require.NoError(t, err)
  184. var vc ViperConfig
  185. err = viper.Unmarshal(&vc)
  186. require.NoError(t, err)
  187. cfg, err := vc.Translate()
  188. if !assert.Equal(t, tt.wantError, err) {
  189. return
  190. }
  191. var regexComparer = func(x, y *regexp.Regexp) bool {
  192. // Compare the string representation of the regex patterns.
  193. if x == nil || y == nil {
  194. return x == y
  195. }
  196. return x.String() == y.String()
  197. }
  198. opts := cmp.Options{cmp.Comparer(regexComparer)}
  199. if diff := cmp.Diff(tt.cfg.Rules, cfg.Rules, opts); diff != "" {
  200. t.Errorf("%s diff: (-want +got)\n%s", tt.cfgName, diff)
  201. }
  202. })
  203. }
  204. }