From 9e20ef89f6287d6bd03b8697d5898dc43b4a77cf Mon Sep 17 00:00:00 2001 From: Cody Oss <6331106+codyoss@users.noreply.github.com> Date: Fri, 19 Apr 2024 12:30:20 -0500 Subject: [PATCH] fix(auth): add internal opt to skip validation on transports (#9999) Updates: #9823 --- auth/grpctransport/grpctransport.go | 6 ++++++ auth/grpctransport/grpctransport_test.go | 21 +++++++++++++++++++++ auth/httptransport/httptransport.go | 6 ++++++ auth/httptransport/httptransport_test.go | 21 +++++++++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/auth/grpctransport/grpctransport.go b/auth/grpctransport/grpctransport.go index 06db948c42e..1299536e60c 100644 --- a/auth/grpctransport/grpctransport.go +++ b/auth/grpctransport/grpctransport.go @@ -96,6 +96,9 @@ func (o *Options) validate() error { if o == nil { return errors.New("grpctransport: opts required to be non-nil") } + if o.InternalOptions != nil && o.InternalOptions.SkipValidation { + return nil + } hasCreds := o.Credentials != nil || (o.DetectOpts != nil && len(o.DetectOpts.CredentialsJSON) > 0) || (o.DetectOpts != nil && o.DetectOpts.CredentialsFile != "") @@ -151,6 +154,9 @@ type InternalOptions struct { // DefaultScopes specifies the default OAuth2 scopes to be used for a // service. DefaultScopes []string + // SkipValidation bypasses validation on Options. It should only be used + // internally for clients that needs more control over their transport. + SkipValidation bool } // Dial returns a GRPCClientConnPool that can be used to communicate with a diff --git a/auth/grpctransport/grpctransport_test.go b/auth/grpctransport/grpctransport_test.go index 46265f9deb9..456a06196a6 100644 --- a/auth/grpctransport/grpctransport_test.go +++ b/auth/grpctransport/grpctransport_test.go @@ -117,6 +117,27 @@ func TestDial_FailsValidation(t *testing.T) { } } +func TestDial_SkipValidation(t *testing.T) { + opts := &Options{ + DisableAuthentication: true, + Credentials: auth.NewCredentials(&auth.CredentialsOptions{ + TokenProvider: &staticTP{tok: &auth.Token{Value: "fakeToken"}}, + }), + } + t.Run("invalid opts", func(t *testing.T) { + if err := opts.validate(); err == nil { + t.Fatalf("opts.validate() = nil, want error") + } + }) + + t.Run("skip invalid opts", func(t *testing.T) { + opts.InternalOptions = &InternalOptions{SkipValidation: true} + if err := opts.validate(); err != nil { + t.Fatalf("opts.validate() = %v, want nil", err) + } + }) +} + func TestOptions_ResolveDetectOptions(t *testing.T) { tests := []struct { name string diff --git a/auth/httptransport/httptransport.go b/auth/httptransport/httptransport.go index d2d47690854..d054dac2588 100644 --- a/auth/httptransport/httptransport.go +++ b/auth/httptransport/httptransport.go @@ -74,6 +74,9 @@ func (o *Options) validate() error { if o == nil { return errors.New("httptransport: opts required to be non-nil") } + if o.InternalOptions != nil && o.InternalOptions.SkipValidation { + return nil + } hasCreds := o.APIKey != "" || o.Credentials != nil || (o.DetectOpts != nil && len(o.DetectOpts.CredentialsJSON) > 0) || @@ -131,6 +134,9 @@ type InternalOptions struct { // DefaultScopes specifies the default OAuth2 scopes to be used for a // service. DefaultScopes []string + // SkipValidation bypasses validation on Options. It should only be used + // internally for clients that needs more control over their transport. + SkipValidation bool } // AddAuthorizationMiddleware adds a middleware to the provided client's diff --git a/auth/httptransport/httptransport_test.go b/auth/httptransport/httptransport_test.go index e3897486f71..f6682672b17 100644 --- a/auth/httptransport/httptransport_test.go +++ b/auth/httptransport/httptransport_test.go @@ -133,6 +133,27 @@ func TestNewClient_FailsValidation(t *testing.T) { } } +func TestDial_SkipValidation(t *testing.T) { + opts := &Options{ + DisableAuthentication: true, + Credentials: auth.NewCredentials(&auth.CredentialsOptions{ + TokenProvider: staticTP("fakeToken"), + }), + } + t.Run("invalid opts", func(t *testing.T) { + if err := opts.validate(); err == nil { + t.Fatalf("opts.validate() = nil, want error") + } + }) + + t.Run("skip invalid opts", func(t *testing.T) { + opts.InternalOptions = &InternalOptions{SkipValidation: true} + if err := opts.validate(); err != nil { + t.Fatalf("opts.validate() = %v, want nil", err) + } + }) +} + func TestOptions_ResolveDetectOptions(t *testing.T) { tests := []struct { name string