config_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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: map[string]Rule{"aws-access-key": {
  20. Description: "AWS Access Key",
  21. Regex: regexp.MustCompile("(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}"),
  22. Tags: []string{"key", "AWS"},
  23. Keywords: []string{},
  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: map[string]Rule{"aws-access-key": {
  38. Description: "AWS Access Key",
  39. Regex: regexp.MustCompile("(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}"),
  40. Tags: []string{"key", "AWS"},
  41. Keywords: []string{},
  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: map[string]Rule{"aws-access-key": {
  54. Description: "AWS Access Key",
  55. Regex: regexp.MustCompile("(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}"),
  56. Tags: []string{"key", "AWS"},
  57. Keywords: []string{},
  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: map[string]Rule{"discord-api-key": {
  72. Description: "Discord API key",
  73. Regex: regexp.MustCompile(`(?i)(discord[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([a-h0-9]{64})['\"]`),
  74. RuleID: "discord-api-key",
  75. Allowlist: Allowlist{},
  76. Entropy: 3.5,
  77. SecretGroup: 3,
  78. Tags: []string{},
  79. Keywords: []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. cfgName: "base",
  91. cfg: Config{
  92. Rules: map[string]Rule{
  93. "aws-access-key": {
  94. Description: "AWS Access Key",
  95. Regex: regexp.MustCompile("(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}"),
  96. Tags: []string{"key", "AWS"},
  97. Keywords: []string{},
  98. RuleID: "aws-access-key",
  99. },
  100. "aws-secret-key": {
  101. Description: "AWS Secret Key",
  102. Regex: regexp.MustCompile(`(?i)aws_(.{0,20})?=?.[\'\"0-9a-zA-Z\/+]{40}`),
  103. Tags: []string{"key", "AWS"},
  104. Keywords: []string{},
  105. RuleID: "aws-secret-key",
  106. },
  107. "aws-secret-key-again": {
  108. Description: "AWS Secret Key",
  109. Regex: regexp.MustCompile(`(?i)aws_(.{0,20})?=?.[\'\"0-9a-zA-Z\/+]{40}`),
  110. Tags: []string{"key", "AWS"},
  111. Keywords: []string{},
  112. RuleID: "aws-secret-key-again",
  113. },
  114. },
  115. },
  116. },
  117. }
  118. for _, tt := range tests {
  119. viper.Reset()
  120. viper.AddConfigPath(configPath)
  121. viper.SetConfigName(tt.cfgName)
  122. viper.SetConfigType("toml")
  123. err := viper.ReadInConfig()
  124. if err != nil {
  125. t.Error(err)
  126. }
  127. var vc ViperConfig
  128. err = viper.Unmarshal(&vc)
  129. if err != nil {
  130. t.Error(err)
  131. }
  132. cfg, err := vc.Translate()
  133. if tt.wantError != nil {
  134. if err == nil {
  135. t.Errorf("expected error")
  136. }
  137. assert.Equal(t, tt.wantError, err)
  138. }
  139. assert.Equal(t, cfg.Rules, tt.cfg.Rules)
  140. }
  141. }