config_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. Keywords: []string{},
  25. RuleID: "aws-access-key",
  26. Allowlist: Allowlist{
  27. Regexes: []*regexp.Regexp{
  28. regexp.MustCompile("AKIALALEMEL33243OLIA"),
  29. },
  30. },
  31. },
  32. },
  33. },
  34. },
  35. {
  36. cfgName: "allow_commit",
  37. cfg: Config{
  38. Rules: []*Rule{
  39. {
  40. Description: "AWS Access Key",
  41. Regex: regexp.MustCompile("(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[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: []*Rule{
  56. {
  57. Description: "AWS Access Key",
  58. Regex: regexp.MustCompile("(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}"),
  59. Tags: []string{"key", "AWS"},
  60. Keywords: []string{},
  61. RuleID: "aws-access-key",
  62. Allowlist: Allowlist{
  63. Paths: []*regexp.Regexp{
  64. regexp.MustCompile(".go"),
  65. },
  66. },
  67. },
  68. },
  69. },
  70. },
  71. {
  72. cfgName: "entropy_group",
  73. cfg: Config{
  74. Rules: []*Rule{
  75. {
  76. Description: "Discord API key",
  77. Regex: regexp.MustCompile(`(?i)(discord[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([a-h0-9]{64})['\"]`),
  78. RuleID: "discord-api-key",
  79. Allowlist: Allowlist{},
  80. Entropy: 3.5,
  81. SecretGroup: 3,
  82. Tags: []string{},
  83. Keywords: []string{},
  84. },
  85. },
  86. },
  87. },
  88. {
  89. cfgName: "bad_entropy_group",
  90. cfg: Config{},
  91. wantError: fmt.Errorf("Discord API key invalid regex secret group 5, max regex secret group 3"),
  92. },
  93. }
  94. for _, tt := range tests {
  95. viper.Reset()
  96. viper.AddConfigPath(configPath)
  97. viper.SetConfigName(tt.cfgName)
  98. viper.SetConfigType("toml")
  99. err := viper.ReadInConfig()
  100. if err != nil {
  101. t.Error(err)
  102. }
  103. var vc ViperConfig
  104. err = viper.Unmarshal(&vc)
  105. if err != nil {
  106. t.Error(err)
  107. }
  108. cfg, err := vc.Translate()
  109. if tt.wantError != nil {
  110. if err == nil {
  111. t.Errorf("expected error")
  112. }
  113. assert.Equal(t, tt.wantError, err)
  114. }
  115. assert.Equal(t, cfg.Rules, tt.cfg.Rules)
  116. }
  117. }