[INS-398] Added tests to ensure that custom endpoint configuration works in artifactory detectors (#4832)
* Added tests to ensure that custom endpoint configuration works in artifactory detectors * updated tests to use testify for assertions instead of vanilla logic
This commit is contained in:
@@ -7,9 +7,11 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
|
||||
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb"
|
||||
@@ -96,6 +98,31 @@ func TestArtifactory_FromChunk(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestArtifactory_FromChunk_WithCustomEndpoint(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
mockSecret := "AKCp5bueTFpfypEqQbGJPp7eHFi28fBivfWczrjbPb9erDff9LbXZbj6UsRExVXA8asWGc9fM"
|
||||
appURL := "trufflesecurity.com"
|
||||
|
||||
s := Scanner{}
|
||||
s.UseFoundEndpoints(true)
|
||||
err := s.SetConfiguredEndpoints(appURL)
|
||||
if err != nil {
|
||||
t.Fatal("Error in setting configured endpoint")
|
||||
}
|
||||
data := []byte(fmt.Sprintf("You can find a artifactory secret %s ", mockSecret))
|
||||
|
||||
got, err := s.FromData(ctx, true, data)
|
||||
|
||||
require.NoError(t, err, "unexpected error from FromData")
|
||||
require.Greaterf(t, len(got), 0, "Expected Alteast 1 result")
|
||||
|
||||
expectedRawV2 := []byte(mockSecret + appURL)
|
||||
if string(got[0].RawV2) != string(expectedRawV2) {
|
||||
t.Errorf("Artifactory.FromData() rawV2 secret mismatch: got %s, want %s", string(got[0].RawV2), string(expectedRawV2))
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkFromData(benchmark *testing.B) {
|
||||
ctx := context.Background()
|
||||
s := Scanner{}
|
||||
|
||||
@@ -190,3 +190,22 @@ func TestArtifactory_Pattern(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestArtifactory_Endpoint_Contains_CustomEndpoint(t *testing.T) {
|
||||
appURL := "example.com"
|
||||
s := Scanner{}
|
||||
s.UseFoundEndpoints(true)
|
||||
err := s.SetConfiguredEndpoints(appURL)
|
||||
if err != nil {
|
||||
t.Fatal("Error in setting configured endpoint")
|
||||
}
|
||||
configuredEndpoints := s.Endpoints()
|
||||
if len(configuredEndpoints) == 0 {
|
||||
t.Fatal("No Confiured endpoint found")
|
||||
}
|
||||
for _, ep := range configuredEndpoints {
|
||||
if ep != appURL {
|
||||
t.Fatalf("expected endpoint %s to be present in endpoints list, got %s", appURL, ep)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,13 @@ package artifactoryreferencetoken
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
|
||||
@@ -148,6 +150,51 @@ func TestArtifactoryreferencetoken_FromChunk(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestArtifactoryreferencetoken_FromChunk_WithCustomEndpoint(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors6")
|
||||
if err != nil {
|
||||
t.Fatalf("could not get test secrets from GCP: %s", err)
|
||||
}
|
||||
|
||||
instanceURL := testSecrets.MustGetField("ARTIFACTORY_URL")
|
||||
secret := testSecrets.MustGetField("ARTIFACTORYREFERENCETOKEN")
|
||||
|
||||
s := Scanner{}
|
||||
s.UseFoundEndpoints(true)
|
||||
err = s.SetConfiguredEndpoints(instanceURL)
|
||||
if err != nil {
|
||||
t.Fatal("Error in setting configured endpoint")
|
||||
}
|
||||
data := []byte(fmt.Sprintf("You can find a artifactory secret %s ", secret))
|
||||
want := []detectors.Result{
|
||||
{
|
||||
DetectorType: detector_typepb.DetectorType_ArtifactoryReferenceToken,
|
||||
Verified: true,
|
||||
RawV2: []byte(secret + strings.TrimPrefix(instanceURL, "https://")),
|
||||
},
|
||||
}
|
||||
got, err := s.FromData(ctx, true, data)
|
||||
require.NoError(t, err, "unexpected error from FromData")
|
||||
require.Equalf(t, len(want), len(got), "expected %d results", len(want))
|
||||
|
||||
for i := range got {
|
||||
if len(got[i].RawV2) == 0 {
|
||||
t.Fatalf("no raw secret present: \n %+v", got[i])
|
||||
}
|
||||
if string(got[i].RawV2) != string(want[i].RawV2) {
|
||||
t.Fatalf("expected rawV2 to be %s, got %s", string(want[i].RawV2), string(got[i].RawV2))
|
||||
}
|
||||
}
|
||||
|
||||
ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "RawV2", "verificationError", "primarySecret", "AnalysisInfo")
|
||||
if diff := cmp.Diff(got, want, ignoreOpts); diff != "" {
|
||||
t.Errorf("Artifactoryreferencetoken.FromData() diff: (-got +want)\n%s", diff)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func BenchmarkFromData(benchmark *testing.B) {
|
||||
ctx := context.Background()
|
||||
s := Scanner{}
|
||||
|
||||
@@ -204,3 +204,22 @@ func TestArtifactoryReferenceToken_Pattern(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestArtifactoryreferencetoken_Endpoint_Contains_CustomEndpoint(t *testing.T) {
|
||||
appURL := "example.com"
|
||||
s := Scanner{}
|
||||
s.UseFoundEndpoints(true)
|
||||
err := s.SetConfiguredEndpoints(appURL)
|
||||
if err != nil {
|
||||
t.Fatal("Error in setting configured endpoint")
|
||||
}
|
||||
configuredEndpoints := s.Endpoints()
|
||||
if len(configuredEndpoints) == 0 {
|
||||
t.Fatal("No Confiured endpoint found")
|
||||
}
|
||||
for _, ep := range configuredEndpoints {
|
||||
if ep != appURL {
|
||||
t.Fatalf("expected endpoint %s to be present in endpoints list, got %s", appURL, ep)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user