Skip to content

Commit

Permalink
feat(transport): add universe domain support (#2355)
Browse files Browse the repository at this point in the history
  • Loading branch information
quartzmo authored Jan 19, 2024
1 parent 6c3b622 commit 69626e3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
17 changes: 17 additions & 0 deletions transport/http/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"context"
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
"time"
Expand Down Expand Up @@ -88,6 +89,13 @@ func newTransport(ctx context.Context, base http.RoundTripper, settings *interna
if err != nil {
return nil, err
}
credsUniverseDomain, err := creds.GetUniverseDomain()
if err != nil {
return nil, err
}
if settings.GetUniverseDomain() != credsUniverseDomain {
return nil, errUniverseNotMatch(settings.GetUniverseDomain(), credsUniverseDomain)
}
paramTransport.quotaProject = internal.GetQuotaProject(creds, settings.QuotaProject)
ts := creds.TokenSource
if settings.ImpersonationConfig == nil && settings.TokenSource != nil {
Expand All @@ -101,6 +109,15 @@ func newTransport(ctx context.Context, base http.RoundTripper, settings *interna
return trans, nil
}

func errUniverseNotMatch(settingsUD, credsUD string) error {
return fmt.Errorf(
"the configured universe domain (%q) does not match the universe "+
"domain found in the credentials (%q). If you haven't configured "+
"WithUniverseDomain explicitly, googleapis.com is the default",
settingsUD,
credsUD)
}

func newSettings(opts []option.ClientOption) (*internal.DialSettings, error) {
var o internal.DialSettings
for _, opt := range opts {
Expand Down
16 changes: 16 additions & 0 deletions transport/http/dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

"go.opencensus.io/plugin/ochttp"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
)

func TestNewClient(t *testing.T) {
Expand All @@ -37,3 +39,17 @@ func TestNewClient(t *testing.T) {
t.Fatalf("got %s, want: %s", got, want)
}
}

func TestNewClient_MismatchedUniverseDomainCreds(t *testing.T) {
rootTokenScope := "https://www.googleapis.com/auth/cloud-platform"
universeDomain := "example.com"
universeDomainDefault := "googleapis.com"
creds := &google.Credentials{} // universeDomainDefault
wantErr := errUniverseNotMatch(universeDomain, universeDomainDefault)
_, _, err := NewClient(context.Background(), option.WithUniverseDomain(universeDomain),
option.WithCredentials(creds), option.WithScopes(rootTokenScope))

if err.Error() != wantErr.Error() {
t.Fatalf("got: %v, want: %v", err, wantErr)
}
}

0 comments on commit 69626e3

Please sign in to comment.