Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
[INS-403] Support Custom endpoint config in hashicorpvaultauth Detector (#4825)
Muneeb Ullah Khan88c7519a4728
diff --git a/pkg/detectors/hashicorpvaultauth/hashicorpvaultauth.go b/pkg/detectors/hashicorpvaultauth/hashicorpvaultauth.goindex 6d1404810..4f2a3b124 100644--- a/pkg/detectors/hashicorpvaultauth/hashicorpvaultauth.go+++ b/pkg/detectors/hashicorpvaultauth/hashicorpvaultauth.go@@ -11,20 +11,22 @@ import (regexp "github.com/wasilibs/go-re2"- "github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb")type Scanner struct {client *http.Client+ detectors.DefaultMultiPartCredentialProvider+ detectors.EndpointSetter}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)+var _ detectors.EndpointCustomizer = (*Scanner)(nil)var (- defaultClient = common.SaneHttpClient()+ defaultClient = detectors.DetectorHttpClientWithNoLocalAddressesroleIdPat = regexp.MustCompile(detectors.PrefixRegex([]string{"role"}) + `\b([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\b`)@@ -61,15 +63,24 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultuniqueVaultUrls[url] = struct{}{}}- // If no names or secrets found, return empty results- if len(uniqueRoleIds) == 0 || len(uniqueSecretIds) == 0 || len(uniqueVaultUrls) == 0 {+ // If no roleIds or secrets found, return empty results+ if len(uniqueRoleIds) == 0 || len(uniqueSecretIds) == 0 {return results, nil}+ endpoints := make([]string, 0, len(uniqueVaultUrls))+ for endpoint := range uniqueVaultUrls {+ endpoints = append(endpoints, endpoint)+ }++ var uniqueUrls = make(map[string]struct{})+ for _, endpoint := range s.Endpoints(endpoints...) {+ uniqueUrls[endpoint] = struct{}{}+ }// create combination results that can be verifiedfor roleId := range uniqueRoleIds {for secretId := range uniqueSecretIds {- for vaultUrl := range uniqueVaultUrls {+ for vaultUrl := range uniqueUrls {s1 := detectors.Result{DetectorType: detector_typepb.DetectorType_HashiCorpVaultAuth,Raw: []byte(secretId),diff --git a/pkg/detectors/hashicorpvaultauth/hashicorpvaultauth_integration_test.go b/pkg/detectors/hashicorpvaultauth/hashicorpvaultauth_integration_test.goindex 68c51fd5a..b18436aa4 100644--- a/pkg/detectors/hashicorpvaultauth/hashicorpvaultauth_integration_test.go+++ b/pkg/detectors/hashicorpvaultauth/hashicorpvaultauth_integration_test.go@@ -142,6 +142,8 @@ func TestHashiCorpVaultAuth_FromChunk(t *testing.T) {}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {+ tt.s.UseCloudEndpoint(true)+ tt.s.UseFoundEndpoints(true)got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)if (err != nil) != tt.wantErr {t.Errorf("HashiCorpVaultAuth.FromData() error = %v, wantErr %v", err, tt.wantErr)@@ -164,6 +166,49 @@ func TestHashiCorpVaultAuth_FromChunk(t *testing.T) {}}+func TestHashiCorpVaultAuth_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)+ }+ roleId := testSecrets.MustGetField("HASHICORPVAULTAUTH_ROLE_ID")+ secretId := testSecrets.MustGetField("HASHICORPVAULTAUTH_SECRET_ID")+ vaultUrl := testSecrets.MustGetField("HASHICORPVAULTAUTH_URL")+ s := Scanner{}+ s.UseCloudEndpoint(true)+ s.UseFoundEndpoints(true)+ s.SetConfiguredEndpoints(vaultUrl)+ data := fmt.Appendf(nil, "hashicorp config:\nrole_id: %s\nsecret_id: %s", roleId, secretId)++ results, err := s.FromData(context.Background(), true, data)+ if err != nil {+ t.Fatalf("unexpected error from FromData: %v", err)+ }+ if len(results) == 0 {+ t.Fatal("expected at least one result from FromData, got 0")+ }+ want := []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_HashiCorpVaultAuth,+ Verified: true,+ VerificationFromCache: false,+ Raw: []byte(secretId),+ RawV2: []byte(fmt.Sprintf("%s:%s", roleId, secretId)),+ ExtraData: map[string]string{+ "URL": vaultUrl,+ },+ StructuredData: nil,+ },+ }++ ignoreOpts := cmpopts.IgnoreUnexported(detectors.Result{})+ if diff := cmp.Diff(results, want, ignoreOpts); diff != "" {+ t.Errorf("HashiCorpVaultAuth.FromData() %s diff: (-got +want)\n%s", "TestHashiCorpVaultAuth_WithCustomEndpoint", diff)+ }+}+func BenchmarkFromData(benchmark *testing.B) {ctx := context.Background()s := Scanner{}diff --git a/pkg/detectors/hashicorpvaultauth/hashicorpvaultauth_test.go b/pkg/detectors/hashicorpvaultauth/hashicorpvaultauth_test.goindex a06869dfc..dbfefda4a 100644--- a/pkg/detectors/hashicorpvaultauth/hashicorpvaultauth_test.go+++ b/pkg/detectors/hashicorpvaultauth/hashicorpvaultauth_test.go@@ -126,7 +126,8 @@ func TestHashiCorpVaultAppRoleAuth_Pattern(t *testing.T) {t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input)return}-+ d.UseCloudEndpoint(true)+ d.UseFoundEndpoints(true)results, err := d.FromData(context.Background(), false, []byte(test.input))if err != nil {t.Errorf("error = %v", err)diff --git a/pkg/engine/engine_test.go b/pkg/engine/engine_test.goindex b3aacd6cf..2f4c2b33d 100644--- a/pkg/engine/engine_test.go+++ b/pkg/engine/engine_test.go@@ -1378,6 +1378,7 @@ func TestEngineInitializesCloudProviderDetectors(t *testing.T) {detector_typepb.DetectorType_ArtifactoryAccessToken: {},detector_typepb.DetectorType_ArtifactoryReferenceToken: {},detector_typepb.DetectorType_TableauPersonalAccessToken: {},+ detector_typepb.DetectorType_HashiCorpVaultAuth: {},// these do not have any cloud endpoint}