diff --git a/pkg/custom_detectors/custom_detectors_test.go b/pkg/custom_detectors/custom_detectors_test.go index 96a160019..d53af541c 100644 --- a/pkg/custom_detectors/custom_detectors_test.go +++ b/pkg/custom_detectors/custom_detectors_test.go @@ -1,7 +1,6 @@ package custom_detectors import ( - "strings" "testing" "github.com/stretchr/testify/assert" @@ -9,7 +8,8 @@ import ( "github.com/trufflesecurity/trufflehog/v3/pkg/protoyaml" ) -const testCustomRegexYaml = `name: Internal bi tool +func TestCustomRegexTemplateParsing(t *testing.T) { + testCustomRegexTemplateYaml := `name: Internal bi tool keywords: - secret_v1_ - pat_v2_ @@ -25,8 +25,8 @@ verify: - 200-250 - '288'` -// Helper function to test equality to the data in testCustomRegexYaml. -func assertExpected(t *testing.T, got *custom_detectorspb.CustomRegex) { + var got custom_detectorspb.CustomRegex + assert.NoError(t, protoyaml.UnmarshalStrict([]byte(testCustomRegexTemplateYaml), &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{ @@ -40,29 +40,64 @@ func assertExpected(t *testing.T, got *custom_detectorspb.CustomRegex) { assert.Equal(t, []string{"200-250", "288"}, got.Verify[0].SuccessRanges) } -func TestCustomRegexParsing(t *testing.T) { - var message custom_detectorspb.CustomRegex +func TestCustomRegexWebhookParsing(t *testing.T) { + 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)) - assertExpected(t, &message) + var got custom_detectorspb.CustomRegex + 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) { - var testYamlConfig string - // Build a config file using testCustomRegexYaml. - { - var lines []string - for i, line := range strings.Split(testCustomRegexYaml, "\n") { - if i == 0 { - lines = append(lines, line) - continue - } - lines = append(lines, " "+line) - } - testYamlConfig = "detectors:\n- " + strings.Join(lines, "\n") - } + // TODO: Support both template and webhook. + testYamlConfig := `detectors: +- 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'` var messages custom_detectorspb.CustomDetectors 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) }