Skip to content

Commit

Permalink
fix(auth): add internal opt to skip validation on transports (#9999)
Browse files Browse the repository at this point in the history
Updates: #9823
  • Loading branch information
codyoss committed Apr 19, 2024
1 parent d44db47 commit 9e20ef8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
6 changes: 6 additions & 0 deletions auth/grpctransport/grpctransport.go
Expand Up @@ -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 != "")
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions auth/grpctransport/grpctransport_test.go
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions auth/httptransport/httptransport.go
Expand Up @@ -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) ||
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions auth/httptransport/httptransport_test.go
Expand Up @@ -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
Expand Down

0 comments on commit 9e20ef8

Please sign in to comment.