Add test for configuring custom regex with webhook verification (#946)

This commit is contained in:
Miccah
2022-12-02 11:23:20 -06:00
committed by GitHub
parent 3b055ce3f9
commit 5a339b0ca1
+57 -22
View File
@@ -1,7 +1,6 @@
package custom_detectors package custom_detectors
import ( import (
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@@ -9,7 +8,8 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/protoyaml" "github.com/trufflesecurity/trufflehog/v3/pkg/protoyaml"
) )
const testCustomRegexYaml = `name: Internal bi tool func TestCustomRegexTemplateParsing(t *testing.T) {
testCustomRegexTemplateYaml := `name: Internal bi tool
keywords: keywords:
- secret_v1_ - secret_v1_
- pat_v2_ - pat_v2_
@@ -25,8 +25,8 @@ verify:
- 200-250 - 200-250
- '288'` - '288'`
// Helper function to test equality to the data in testCustomRegexYaml. var got custom_detectorspb.CustomRegex
func assertExpected(t *testing.T, got *custom_detectorspb.CustomRegex) { assert.NoError(t, protoyaml.UnmarshalStrict([]byte(testCustomRegexTemplateYaml), &got))
assert.Equal(t, "Internal bi tool", got.Name) assert.Equal(t, "Internal bi tool", got.Name)
assert.Equal(t, []string{"secret_v1_", "pat_v2_"}, got.Keywords) assert.Equal(t, []string{"secret_v1_", "pat_v2_"}, got.Keywords)
assert.Equal(t, map[string]string{ assert.Equal(t, map[string]string{
@@ -40,29 +40,64 @@ func assertExpected(t *testing.T, got *custom_detectorspb.CustomRegex) {
assert.Equal(t, []string{"200-250", "288"}, got.Verify[0].SuccessRanges) assert.Equal(t, []string{"200-250", "288"}, got.Verify[0].SuccessRanges)
} }
func TestCustomRegexParsing(t *testing.T) { func TestCustomRegexWebhookParsing(t *testing.T) {
var message custom_detectorspb.CustomRegex testCustomRegexWebhookYaml := `name: Internal bi tool
keywords:
- secret_v1_
- pat_v2_
regex:
id_pat_example: ([a-zA-Z0-9]{32})
secret_pat_example: ([a-zA-Z0-9]{32})
verify:
- endpoint: http://localhost:8000/
unsafe: true
headers:
- 'Authorization: Bearer token'`
assert.NoError(t, protoyaml.UnmarshalStrict([]byte(testCustomRegexYaml), &message)) var got custom_detectorspb.CustomRegex
assertExpected(t, &message) assert.NoError(t, protoyaml.UnmarshalStrict([]byte(testCustomRegexWebhookYaml), &got))
assert.Equal(t, "Internal bi tool", got.Name)
assert.Equal(t, []string{"secret_v1_", "pat_v2_"}, got.Keywords)
assert.Equal(t, map[string]string{
"id_pat_example": "([a-zA-Z0-9]{32})",
"secret_pat_example": "([a-zA-Z0-9]{32})",
}, got.Regex)
assert.Equal(t, 1, len(got.Verify))
assert.Equal(t, "http://localhost:8000/", got.Verify[0].Endpoint)
assert.Equal(t, true, got.Verify[0].Unsafe)
assert.Equal(t, []string{"Authorization: Bearer token"}, got.Verify[0].Headers)
} }
// TestCustomDetectorsParsing tests the full `detectors` configuration.
func TestCustomDetectorsParsing(t *testing.T) { func TestCustomDetectorsParsing(t *testing.T) {
var testYamlConfig string // TODO: Support both template and webhook.
// Build a config file using testCustomRegexYaml. testYamlConfig := `detectors:
{ - name: Internal bi tool
var lines []string keywords:
for i, line := range strings.Split(testCustomRegexYaml, "\n") { - secret_v1_
if i == 0 { - pat_v2_
lines = append(lines, line) regex:
continue id_pat_example: ([a-zA-Z0-9]{32})
} secret_pat_example: ([a-zA-Z0-9]{32})
lines = append(lines, " "+line) verify:
} - endpoint: http://localhost:8000/
testYamlConfig = "detectors:\n- " + strings.Join(lines, "\n") unsafe: true
} headers:
- 'Authorization: Bearer token'`
var messages custom_detectorspb.CustomDetectors var messages custom_detectorspb.CustomDetectors
assert.NoError(t, protoyaml.UnmarshalStrict([]byte(testYamlConfig), &messages)) assert.NoError(t, protoyaml.UnmarshalStrict([]byte(testYamlConfig), &messages))
assertExpected(t, messages.Detectors[0]) assert.Equal(t, 1, len(messages.Detectors))
got := messages.Detectors[0]
assert.Equal(t, "Internal bi tool", got.Name)
assert.Equal(t, []string{"secret_v1_", "pat_v2_"}, got.Keywords)
assert.Equal(t, map[string]string{
"id_pat_example": "([a-zA-Z0-9]{32})",
"secret_pat_example": "([a-zA-Z0-9]{32})",
}, got.Regex)
assert.Equal(t, 1, len(got.Verify))
assert.Equal(t, "http://localhost:8000/", got.Verify[0].Endpoint)
assert.Equal(t, true, got.Verify[0].Unsafe)
assert.Equal(t, []string{"Authorization: Bearer token"}, got.Verify[0].Headers)
} }