config_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package config
  2. import (
  3. "fmt"
  4. "regexp"
  5. "testing"
  6. "github.com/spf13/viper"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. const configPath = "../testdata/config/"
  10. func TestTranslate(t *testing.T) {
  11. tests := []struct {
  12. cfgName string
  13. cfg Config
  14. wantError error
  15. }{
  16. {
  17. cfgName: "allow_aws_re",
  18. cfg: Config{
  19. Rules: []*Rule{
  20. {
  21. Description: "AWS Access Key",
  22. Regex: regexp.MustCompile("(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}"),
  23. Tags: []string{"key", "AWS"},
  24. RuleID: "aws-access-key",
  25. Allowlist: Allowlist{
  26. Regexes: []*regexp.Regexp{
  27. regexp.MustCompile("AKIALALEMEL33243OLIA"),
  28. },
  29. },
  30. },
  31. },
  32. },
  33. },
  34. {
  35. cfgName: "allow_commit",
  36. cfg: Config{
  37. Rules: []*Rule{
  38. {
  39. Description: "AWS Access Key",
  40. Regex: regexp.MustCompile("(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}"),
  41. Tags: []string{"key", "AWS"},
  42. RuleID: "aws-access-key",
  43. Allowlist: Allowlist{
  44. Commits: []string{"allowthiscommit"},
  45. },
  46. },
  47. },
  48. },
  49. },
  50. {
  51. cfgName: "allow_path",
  52. cfg: Config{
  53. Rules: []*Rule{
  54. {
  55. Description: "AWS Access Key",
  56. Regex: regexp.MustCompile("(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}"),
  57. Tags: []string{"key", "AWS"},
  58. RuleID: "aws-access-key",
  59. Allowlist: Allowlist{
  60. Paths: []*regexp.Regexp{
  61. regexp.MustCompile(".go"),
  62. },
  63. },
  64. },
  65. },
  66. },
  67. },
  68. {
  69. cfgName: "entropy_group",
  70. cfg: Config{
  71. Rules: []*Rule{
  72. {
  73. Description: "Discord API key",
  74. Regex: regexp.MustCompile(`(?i)(discord[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([a-h0-9]{64})['\"]`),
  75. RuleID: "discord-api-key",
  76. Allowlist: Allowlist{},
  77. Entropy: 3.5,
  78. SecretGroup: 3,
  79. Tags: []string{},
  80. },
  81. },
  82. },
  83. },
  84. {
  85. cfgName: "bad_entropy_group",
  86. cfg: Config{},
  87. wantError: fmt.Errorf("Discord API key invalid regex secret group 5, max regex secret group 3"),
  88. },
  89. }
  90. for _, tt := range tests {
  91. viper.Reset()
  92. viper.AddConfigPath(configPath)
  93. viper.SetConfigName(tt.cfgName)
  94. viper.SetConfigType("toml")
  95. err := viper.ReadInConfig()
  96. if err != nil {
  97. t.Error(err)
  98. }
  99. var vc ViperConfig
  100. viper.Unmarshal(&vc)
  101. cfg, err := vc.Translate()
  102. if tt.wantError != nil {
  103. if err == nil {
  104. t.Errorf("expected error")
  105. }
  106. assert.Equal(t, tt.wantError, err)
  107. }
  108. assert.Equal(t, cfg.Rules, tt.cfg.Rules)
  109. }
  110. }
  111. func TestIncludeEntropy(t *testing.T) {
  112. tests := []struct {
  113. rule Rule
  114. secret string
  115. entropy float32
  116. include bool
  117. }{
  118. {
  119. rule: Rule{
  120. RuleID: "generic-api-key",
  121. SecretGroup: 4,
  122. Entropy: 3.5,
  123. Regex: regexp.MustCompile(`(?i)((key|api|token|secret|password)[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([0-9a-zA-Z\-_=]{8,64})['\"]`),
  124. },
  125. secret: `e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5`,
  126. entropy: 3.7906235872459746,
  127. include: true,
  128. },
  129. {
  130. rule: Rule{
  131. RuleID: "generic-api-key",
  132. SecretGroup: 4,
  133. Entropy: 4,
  134. Regex: regexp.MustCompile(`(?i)((key|api|token|secret|password)[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([0-9a-zA-Z\-_=]{8,64})['\"]`),
  135. },
  136. secret: `e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5`,
  137. entropy: 3.7906235872459746,
  138. include: false,
  139. },
  140. {
  141. rule: Rule{
  142. RuleID: "generic-api-key",
  143. SecretGroup: 4,
  144. Entropy: 3.0,
  145. Regex: regexp.MustCompile(`(?i)((key|api|token|secret|password)[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([0-9a-zA-Z\-_=]{8,64})['\"]`),
  146. },
  147. secret: `ssh-keyboard-interactive`,
  148. entropy: 0,
  149. include: false,
  150. },
  151. }
  152. for _, tt := range tests {
  153. include, entropy := tt.rule.IncludeEntropy(tt.secret)
  154. assert.Equal(t, true, tt.rule.EntropySet())
  155. assert.Equal(t, tt.entropy, float32(entropy))
  156. assert.Equal(t, tt.include, include)
  157. }
  158. }