fix(auth): add internal opt to skip validation on transports (#9999)
Updates: #9823
diff --git a/auth/grpctransport/grpctransport.go b/auth/grpctransport/grpctransport.go
index 06db948..1299536 100644
--- a/auth/grpctransport/grpctransport.go
+++ b/auth/grpctransport/grpctransport.go
@@ -96,6 +96,9 @@
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 @@
// 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 46265f9..456a061 100644
--- a/auth/grpctransport/grpctransport_test.go
+++ b/auth/grpctransport/grpctransport_test.go
@@ -117,6 +117,27 @@
}
}
+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 d2d4769..d054dac 100644
--- a/auth/httptransport/httptransport.go
+++ b/auth/httptransport/httptransport.go
@@ -74,6 +74,9 @@
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 @@
// 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 e389748..f668267 100644
--- a/auth/httptransport/httptransport_test.go
+++ b/auth/httptransport/httptransport_test.go
@@ -133,6 +133,27 @@
}
}
+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