config_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package config
  2. import (
  3. "fmt"
  4. "regexp"
  5. "testing"
  6. "github.com/spf13/viper"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. const configPath = "../testdata/config/"
  11. func TestTranslate(t *testing.T) {
  12. tests := []struct {
  13. cfgName string
  14. cfg Config
  15. wantError error
  16. }{
  17. {
  18. cfgName: "allow_aws_re",
  19. cfg: Config{
  20. Rules: map[string]Rule{"aws-access-key": {
  21. Description: "AWS Access Key",
  22. Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[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: map[string]Rule{"aws-access-key": {
  39. Description: "AWS Access Key",
  40. Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
  41. Tags: []string{"key", "AWS"},
  42. Keywords: []string{},
  43. RuleID: "aws-access-key",
  44. Allowlist: Allowlist{
  45. Commits: []string{"allowthiscommit"},
  46. },
  47. },
  48. },
  49. },
  50. },
  51. {
  52. cfgName: "allow_path",
  53. cfg: Config{
  54. Rules: map[string]Rule{"aws-access-key": {
  55. Description: "AWS Access Key",
  56. Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
  57. Tags: []string{"key", "AWS"},
  58. Keywords: []string{},
  59. RuleID: "aws-access-key",
  60. Allowlist: Allowlist{
  61. Paths: []*regexp.Regexp{
  62. regexp.MustCompile(".go"),
  63. },
  64. },
  65. },
  66. },
  67. },
  68. },
  69. {
  70. cfgName: "entropy_group",
  71. cfg: Config{
  72. Rules: map[string]Rule{"discord-api-key": {
  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. Keywords: []string{},
  81. },
  82. },
  83. },
  84. },
  85. {
  86. cfgName: "missing_id",
  87. cfg: Config{},
  88. wantError: fmt.Errorf("rule |id| is missing or empty, regex: (?i)(discord[a-z0-9_ .\\-,]{0,25})(=|>|:=|\\|\\|:|<=|=>|:).{0,5}['\\\"]([a-h0-9]{64})['\\\"]"),
  89. },
  90. {
  91. cfgName: "no_regex_or_path",
  92. cfg: Config{},
  93. wantError: fmt.Errorf("discord-api-key: both |regex| and |path| are empty, this rule will have no effect"),
  94. },
  95. {
  96. cfgName: "bad_entropy_group",
  97. cfg: Config{},
  98. wantError: fmt.Errorf("discord-api-key: invalid regex secret group 5, max regex secret group 3"),
  99. },
  100. {
  101. cfgName: "base",
  102. cfg: Config{
  103. Rules: map[string]Rule{
  104. "aws-access-key": {
  105. Description: "AWS Access Key",
  106. Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
  107. Tags: []string{"key", "AWS"},
  108. Keywords: []string{},
  109. RuleID: "aws-access-key",
  110. },
  111. "aws-secret-key": {
  112. Description: "AWS Secret Key",
  113. Regex: regexp.MustCompile(`(?i)aws_(.{0,20})?=?.[\'\"0-9a-zA-Z\/+]{40}`),
  114. Tags: []string{"key", "AWS"},
  115. Keywords: []string{},
  116. RuleID: "aws-secret-key",
  117. },
  118. "aws-secret-key-again": {
  119. Description: "AWS Secret Key",
  120. Regex: regexp.MustCompile(`(?i)aws_(.{0,20})?=?.[\'\"0-9a-zA-Z\/+]{40}`),
  121. Tags: []string{"key", "AWS"},
  122. Keywords: []string{},
  123. RuleID: "aws-secret-key-again",
  124. },
  125. },
  126. },
  127. },
  128. }
  129. for _, tt := range tests {
  130. t.Run(tt.cfgName, func(t *testing.T) {
  131. viper.Reset()
  132. viper.AddConfigPath(configPath)
  133. viper.SetConfigName(tt.cfgName)
  134. viper.SetConfigType("toml")
  135. err := viper.ReadInConfig()
  136. require.NoError(t, err)
  137. var vc ViperConfig
  138. err = viper.Unmarshal(&vc)
  139. require.NoError(t, err)
  140. cfg, err := vc.Translate()
  141. assert.Equal(t, tt.wantError, err)
  142. assert.Equal(t, cfg.Rules, tt.cfg.Rules)
  143. })
  144. }
  145. }