Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Use OAuth2 http client with GCS (#1220)
ahrav2cf6f831d4b3
diff --git a/pkg/sources/gcs/gcs.go b/pkg/sources/gcs/gcs.goindex aa3a5bfd1..4206362bf 100644--- a/pkg/sources/gcs/gcs.go+++ b/pkg/sources/gcs/gcs.go@@ -3,18 +3,23 @@ package gcsimport ("fmt""io"+ "net/http""os""strconv""sync"+ "cloud.google.com/go/storage"diskbufferreader "github.com/bill-rich/disk-buffer-reader""github.com/go-errors/errors""github.com/go-logr/logr"+ "golang.org/x/oauth2"+ "golang.org/x/oauth2/endpoints""google.golang.org/protobuf/proto""google.golang.org/protobuf/types/known/anypb""github.com/trufflesecurity/trufflehog/v3/pkg/context""github.com/trufflesecurity/trufflehog/v3/pkg/handlers"+ "github.com/trufflesecurity/trufflehog/v3/pkg/pb/credentialspb""github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb""github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb""github.com/trufflesecurity/trufflehog/v3/pkg/sources"@@ -107,6 +112,12 @@ func configureGCSManager(aCtx context.Context, conn *sourcespb.GCS, concurrencygcsManagerAuthOption = withDefaultADC(aCtx)case *sourcespb.GCS_Unauthenticated:gcsManagerAuthOption = withoutAuthentication()+ case *sourcespb.GCS_Oauth:+ client, err := oauth2Client(aCtx, conn.GetOauth())+ if err != nil {+ return nil, fmt.Errorf("error creating oauth2 client: %w", err)+ }+ gcsManagerAuthOption = withHTTPClient(aCtx, client)default:return nil, fmt.Errorf("unknown GCS authentication type: %T", conn.Credential)@@ -132,6 +143,31 @@ func configureGCSManager(aCtx context.Context, conn *sourcespb.GCS, concurrencyreturn gcsManager, nil}+func oauth2Client(ctx context.Context, creds *credentialspb.Oauth2) (*http.Client, error) {+ if creds == nil {+ return nil, fmt.Errorf("oauth2 credentials are required")+ }+ if creds.GetClientId() == "" || creds.GetRefreshToken() == "" || creds.GetAccessToken() == "" {+ return nil, fmt.Errorf("oauth2 credentials are incomplete, client_id, refresh_token, and access_token are required")+ }++ conf := &oauth2.Config{+ ClientID: creds.GetClientId(),+ Scopes: []string{storage.ScopeReadOnly},+ Endpoint: oauth2.Endpoint{+ AuthURL: endpoints.Google.AuthURL,+ TokenURL: endpoints.Google.TokenURL,+ },+ }++ tok := &oauth2.Token{+ AccessToken: creds.GetAccessToken(),+ RefreshToken: creds.GetRefreshToken(),+ }++ return conf.Client(ctx, tok), nil+}+func setGCSManagerBucketOptions(conn *sourcespb.GCS) gcsManagerOption {return setGCSManagerOptions(conn.GetIncludeBuckets(), conn.GetExcludeBuckets(), withIncludeBuckets, withExcludeBuckets)}diff --git a/pkg/sources/gcs/gcs_manager.go b/pkg/sources/gcs/gcs_manager.goindex 4758e2e34..79e69b930 100644--- a/pkg/sources/gcs/gcs_manager.go+++ b/pkg/sources/gcs/gcs_manager.go@@ -4,6 +4,7 @@ import (aCtx "context""fmt""io"+ "net/http""runtime""sync""sync/atomic"@@ -119,6 +120,19 @@ func (s *attributes) setBucketCnt(bkt string, cnt uint64) {type gcsManagerOption func(*gcsManager) error+// withHTTPClient uses the provided HTTP client when creating a new GCS client.+func withHTTPClient(ctx context.Context, httpClient *http.Client) gcsManagerOption {+ client, err := storage.NewClient(ctx, option.WithHTTPClient(httpClient), option.WithScopes(storage.ScopeReadOnly))+ return func(m *gcsManager) error {+ if err != nil {+ return err+ }++ m.client = client+ return nil+ }+}+// withAPIKey uses the provided API key when creating a new GCS client.// This can ONLY be used for public buckets.func withAPIKey(ctx context.Context, apiKey string) gcsManagerOption {diff --git a/pkg/sources/gcs/gcs_manager_test.go b/pkg/sources/gcs/gcs_manager_test.goindex 95bab761f..a373839ea 100644--- a/pkg/sources/gcs/gcs_manager_test.go+++ b/pkg/sources/gcs/gcs_manager_test.go@@ -2,6 +2,7 @@ package gcsimport ("fmt"+ "net/http""sort""testing""time"@@ -328,6 +329,12 @@ func TestNewGcsManager(t *testing.T) {maxObjectSize: defaultMaxObjectSize,},},+ {+ name: "new gcs manager, with HTTP client",+ projID: testProjectID,+ opts: []gcsManagerOption{withHTTPClient(ctx, &http.Client{})},+ want: &gcsManager{projectID: testProjectID, concurrency: defaultConcurrency, maxObjectSize: defaultMaxObjectSize},+ },}for _, tc := range testCases {diff --git a/pkg/sources/gcs/gcs_test.go b/pkg/sources/gcs/gcs_test.goindex 871ab9eca..2fc9a8f5a 100644--- a/pkg/sources/gcs/gcs_test.go+++ b/pkg/sources/gcs/gcs_test.go@@ -3,6 +3,7 @@ package gcsimport ("fmt""io"+ "net/http""sort""testing""time"@@ -13,6 +14,7 @@ import ("google.golang.org/protobuf/types/known/anypb""github.com/trufflesecurity/trufflehog/v3/pkg/context"+ "github.com/trufflesecurity/trufflehog/v3/pkg/pb/credentialspb""github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb""github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb""github.com/trufflesecurity/trufflehog/v3/pkg/sources"@@ -132,6 +134,74 @@ func TestConfigureGCSManager(t *testing.T) {}}+func TestSourceOauth2Client(t *testing.T) {+ testCases := []struct {+ name string+ creds *credentialspb.Oauth2+ want *http.Client+ wantErr bool+ }{+ {+ name: "valid creds",+ creds: &credentialspb.Oauth2{+ RefreshToken: "some-refresh-token",+ ClientId: "some-client-id",+ AccessToken: "some-access-token",+ },+ want: &http.Client{},+ },+ {+ name: "invalid creds, nil creds",+ wantErr: true,+ },+ {+ name: "invalid creds, empty refresh token",+ creds: &credentialspb.Oauth2{+ RefreshToken: "",+ AccessToken: "some-access-token",+ ClientId: "some-client-id",+ },+ wantErr: true,+ },+ {+ name: "invalid creds, empty client id",+ creds: &credentialspb.Oauth2{+ RefreshToken: "some-refresh-token",+ AccessToken: "some-access-token",+ ClientId: "",+ },+ wantErr: true,+ },+ {+ name: "invalid creds, empty access token",+ creds: &credentialspb.Oauth2{+ AccessToken: "",+ RefreshToken: "some-refresh-token",+ ClientId: "some-client-id",+ },+ wantErr: true,+ },+ }++ for _, tc := range testCases {+ t.Run(tc.name, func(t *testing.T) {+ ctx := context.Background()+ got, err := oauth2Client(ctx, tc.creds)+ if (err != nil) != tc.wantErr {+ t.Errorf("source.oauth2Client() error = %v", err)+ }++ if !tc.wantErr {+ if diff := cmp.Diff(tc.want, got,+ cmpopts.IgnoreFields(http.Client{}, "Transport"),+ ); diff != "" {+ t.Errorf("source.oauth2Client() diff: (-want +got)\n%s", diff)+ }+ }+ })+ }+}+type mockObjectManager struct {wantErr bool}