Skip to content

Find, verify, and analyze leaked credentials

[INS-398] Added tests to ensure that custom endpoint configuration works in artifactory detectors (#4832)

Muneeb Ullah Khan0e7f63031df7
diff --git a/pkg/detectors/artifactory/artifactory_integration_test.go b/pkg/detectors/artifactory/artifactory_integration_test.go
index 9b1ace34f..3698b20aa 100644
--- a/pkg/detectors/artifactory/artifactory_integration_test.go
+++ b/pkg/detectors/artifactory/artifactory_integration_test.go
@@ -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{}
diff --git a/pkg/detectors/artifactory/artifactory_test.go b/pkg/detectors/artifactory/artifactory_test.go
index 6c9eed790..e3dced4ae 100644
--- a/pkg/detectors/artifactory/artifactory_test.go
+++ b/pkg/detectors/artifactory/artifactory_test.go
@@ -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)
+ }
+ }
+}
diff --git a/pkg/detectors/artifactoryreferencetoken/artifactoryreferencetoken_integration_test.go b/pkg/detectors/artifactoryreferencetoken/artifactoryreferencetoken_integration_test.go
index 66dedf281..48bf55350 100644
--- a/pkg/detectors/artifactoryreferencetoken/artifactoryreferencetoken_integration_test.go
+++ b/pkg/detectors/artifactoryreferencetoken/artifactoryreferencetoken_integration_test.go
@@ -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{}
diff --git a/pkg/detectors/artifactoryreferencetoken/artifactoryreferencetoken_test.go b/pkg/detectors/artifactoryreferencetoken/artifactoryreferencetoken_test.go
index 39be5e0fa..ea80471cd 100644
--- a/pkg/detectors/artifactoryreferencetoken/artifactoryreferencetoken_test.go
+++ b/pkg/detectors/artifactoryreferencetoken/artifactoryreferencetoken_test.go
@@ -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)
+ }
+ }
+}