Skip to content

Commit 9e20ef8

Browse files
authored
fix(auth): add internal opt to skip validation on transports (#9999)
Updates: #9823
1 parent d44db47 commit 9e20ef8

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

auth/grpctransport/grpctransport.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ func (o *Options) validate() error {
9696
if o == nil {
9797
return errors.New("grpctransport: opts required to be non-nil")
9898
}
99+
if o.InternalOptions != nil && o.InternalOptions.SkipValidation {
100+
return nil
101+
}
99102
hasCreds := o.Credentials != nil ||
100103
(o.DetectOpts != nil && len(o.DetectOpts.CredentialsJSON) > 0) ||
101104
(o.DetectOpts != nil && o.DetectOpts.CredentialsFile != "")
@@ -151,6 +154,9 @@ type InternalOptions struct {
151154
// DefaultScopes specifies the default OAuth2 scopes to be used for a
152155
// service.
153156
DefaultScopes []string
157+
// SkipValidation bypasses validation on Options. It should only be used
158+
// internally for clients that needs more control over their transport.
159+
SkipValidation bool
154160
}
155161

156162
// Dial returns a GRPCClientConnPool that can be used to communicate with a

auth/grpctransport/grpctransport_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,27 @@ func TestDial_FailsValidation(t *testing.T) {
117117
}
118118
}
119119

120+
func TestDial_SkipValidation(t *testing.T) {
121+
opts := &Options{
122+
DisableAuthentication: true,
123+
Credentials: auth.NewCredentials(&auth.CredentialsOptions{
124+
TokenProvider: &staticTP{tok: &auth.Token{Value: "fakeToken"}},
125+
}),
126+
}
127+
t.Run("invalid opts", func(t *testing.T) {
128+
if err := opts.validate(); err == nil {
129+
t.Fatalf("opts.validate() = nil, want error")
130+
}
131+
})
132+
133+
t.Run("skip invalid opts", func(t *testing.T) {
134+
opts.InternalOptions = &InternalOptions{SkipValidation: true}
135+
if err := opts.validate(); err != nil {
136+
t.Fatalf("opts.validate() = %v, want nil", err)
137+
}
138+
})
139+
}
140+
120141
func TestOptions_ResolveDetectOptions(t *testing.T) {
121142
tests := []struct {
122143
name string

auth/httptransport/httptransport.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ func (o *Options) validate() error {
7474
if o == nil {
7575
return errors.New("httptransport: opts required to be non-nil")
7676
}
77+
if o.InternalOptions != nil && o.InternalOptions.SkipValidation {
78+
return nil
79+
}
7780
hasCreds := o.APIKey != "" ||
7881
o.Credentials != nil ||
7982
(o.DetectOpts != nil && len(o.DetectOpts.CredentialsJSON) > 0) ||
@@ -131,6 +134,9 @@ type InternalOptions struct {
131134
// DefaultScopes specifies the default OAuth2 scopes to be used for a
132135
// service.
133136
DefaultScopes []string
137+
// SkipValidation bypasses validation on Options. It should only be used
138+
// internally for clients that needs more control over their transport.
139+
SkipValidation bool
134140
}
135141

136142
// AddAuthorizationMiddleware adds a middleware to the provided client's

auth/httptransport/httptransport_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,27 @@ func TestNewClient_FailsValidation(t *testing.T) {
133133
}
134134
}
135135

136+
func TestDial_SkipValidation(t *testing.T) {
137+
opts := &Options{
138+
DisableAuthentication: true,
139+
Credentials: auth.NewCredentials(&auth.CredentialsOptions{
140+
TokenProvider: staticTP("fakeToken"),
141+
}),
142+
}
143+
t.Run("invalid opts", func(t *testing.T) {
144+
if err := opts.validate(); err == nil {
145+
t.Fatalf("opts.validate() = nil, want error")
146+
}
147+
})
148+
149+
t.Run("skip invalid opts", func(t *testing.T) {
150+
opts.InternalOptions = &InternalOptions{SkipValidation: true}
151+
if err := opts.validate(); err != nil {
152+
t.Fatalf("opts.validate() = %v, want nil", err)
153+
}
154+
})
155+
}
156+
136157
func TestOptions_ResolveDetectOptions(t *testing.T) {
137158
tests := []struct {
138159
name string

0 commit comments

Comments
 (0)