Skip to content

Commit 69626e3

Browse files
authored
feat(transport): add universe domain support (#2355)
1 parent 6c3b622 commit 69626e3

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

transport/http/dial.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"context"
1212
"crypto/tls"
1313
"errors"
14+
"fmt"
1415
"net"
1516
"net/http"
1617
"time"
@@ -88,6 +89,13 @@ func newTransport(ctx context.Context, base http.RoundTripper, settings *interna
8889
if err != nil {
8990
return nil, err
9091
}
92+
credsUniverseDomain, err := creds.GetUniverseDomain()
93+
if err != nil {
94+
return nil, err
95+
}
96+
if settings.GetUniverseDomain() != credsUniverseDomain {
97+
return nil, errUniverseNotMatch(settings.GetUniverseDomain(), credsUniverseDomain)
98+
}
9199
paramTransport.quotaProject = internal.GetQuotaProject(creds, settings.QuotaProject)
92100
ts := creds.TokenSource
93101
if settings.ImpersonationConfig == nil && settings.TokenSource != nil {
@@ -101,6 +109,15 @@ func newTransport(ctx context.Context, base http.RoundTripper, settings *interna
101109
return trans, nil
102110
}
103111

112+
func errUniverseNotMatch(settingsUD, credsUD string) error {
113+
return fmt.Errorf(
114+
"the configured universe domain (%q) does not match the universe "+
115+
"domain found in the credentials (%q). If you haven't configured "+
116+
"WithUniverseDomain explicitly, googleapis.com is the default",
117+
settingsUD,
118+
credsUD)
119+
}
120+
104121
func newSettings(opts []option.ClientOption) (*internal.DialSettings, error) {
105122
var o internal.DialSettings
106123
for _, opt := range opts {

transport/http/dial_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111

1212
"go.opencensus.io/plugin/ochttp"
1313
"golang.org/x/oauth2"
14+
"golang.org/x/oauth2/google"
15+
"google.golang.org/api/option"
1416
)
1517

1618
func TestNewClient(t *testing.T) {
@@ -37,3 +39,17 @@ func TestNewClient(t *testing.T) {
3739
t.Fatalf("got %s, want: %s", got, want)
3840
}
3941
}
42+
43+
func TestNewClient_MismatchedUniverseDomainCreds(t *testing.T) {
44+
rootTokenScope := "https://www.googleapis.com/auth/cloud-platform"
45+
universeDomain := "example.com"
46+
universeDomainDefault := "googleapis.com"
47+
creds := &google.Credentials{} // universeDomainDefault
48+
wantErr := errUniverseNotMatch(universeDomain, universeDomainDefault)
49+
_, _, err := NewClient(context.Background(), option.WithUniverseDomain(universeDomain),
50+
option.WithCredentials(creds), option.WithScopes(rootTokenScope))
51+
52+
if err.Error() != wantErr.Error() {
53+
t.Fatalf("got: %v, want: %v", err, wantErr)
54+
}
55+
}

0 commit comments

Comments
 (0)