Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(option): add support for env var GOOGLE_CLOUD_UNIVERSE_DOMAIN #2509

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions internal/cba_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func TestGetEndpoint(t *testing.T) {
Endpoint: tc.UserEndpoint,
DefaultEndpoint: tc.DefaultEndpoint,
DefaultEndpointTemplate: tc.DefaultEndpointTemplate,
DefaultUniverseDomain: "googleapis.com",
}, nil)
if tc.WantErr && err == nil {
t.Errorf("want err, got nil err")
Expand Down Expand Up @@ -121,10 +120,9 @@ func TestGetEndpointWithClientCertSource(t *testing.T) {

for _, tc := range testCases {
got, err := getEndpoint(&DialSettings{
Endpoint: tc.UserEndpoint,
DefaultEndpoint: tc.DefaultEndpoint,
DefaultMTLSEndpoint: tc.DefaultMTLSEndpoint,
DefaultUniverseDomain: "googleapis.com",
Endpoint: tc.UserEndpoint,
DefaultEndpoint: tc.DefaultEndpoint,
DefaultMTLSEndpoint: tc.DefaultMTLSEndpoint,
}, dummyClientCertSource)
if tc.WantErr && err == nil {
t.Errorf("want err, got nil err")
Expand Down Expand Up @@ -201,7 +199,6 @@ func TestGetGRPCTransportConfigAndEndpoint(t *testing.T) {
DefaultMTLSEndpoint: testMTLSEndpoint,
DefaultEndpointTemplate: testEndpointTemplate,
Endpoint: testOverrideEndpoint,
DefaultUniverseDomain: "googleapis.com",
},
validConfigResp,
testOverrideEndpoint,
Expand All @@ -211,7 +208,6 @@ func TestGetGRPCTransportConfigAndEndpoint(t *testing.T) {
&DialSettings{
DefaultMTLSEndpoint: "",
DefaultEndpointTemplate: testEndpointTemplate,
DefaultUniverseDomain: "googleapis.com",
},
validConfigResp,
testRegularEndpoint,
Expand Down Expand Up @@ -384,7 +380,6 @@ func TestGetHTTPTransportConfigAndEndpoint_UniverseDomain(t *testing.T) {
DefaultEndpoint: testRegularEndpoint,
DefaultEndpointTemplate: testEndpointTemplate,
DefaultMTLSEndpoint: testMTLSEndpoint,
DefaultUniverseDomain: "googleapis.com",
},
wantEndpoint: testRegularEndpoint,
},
Expand All @@ -395,7 +390,6 @@ func TestGetHTTPTransportConfigAndEndpoint_UniverseDomain(t *testing.T) {
DefaultEndpointTemplate: testEndpointTemplate,
DefaultMTLSEndpoint: testMTLSEndpoint,
ClientCertSource: dummyClientCertSource,
DefaultUniverseDomain: "googleapis.com",
},
wantEndpoint: testMTLSEndpoint,
},
Expand All @@ -406,7 +400,6 @@ func TestGetHTTPTransportConfigAndEndpoint_UniverseDomain(t *testing.T) {
DefaultEndpointTemplate: testEndpointTemplate,
DefaultMTLSEndpoint: testMTLSEndpoint,
UniverseDomain: testUniverseDomain,
DefaultUniverseDomain: "googleapis.com",
},
wantEndpoint: testUniverseDomainEndpoint,
},
Expand All @@ -418,7 +411,6 @@ func TestGetHTTPTransportConfigAndEndpoint_UniverseDomain(t *testing.T) {
DefaultMTLSEndpoint: testMTLSEndpoint,
UniverseDomain: testUniverseDomain,
ClientCertSource: dummyClientCertSource,
DefaultUniverseDomain: "googleapis.com",
},
wantEndpoint: testUniverseDomainEndpoint,
wantErr: errUniverseNotSupportedMTLS,
Expand Down Expand Up @@ -457,7 +449,6 @@ func TestGetGRPCTransportConfigAndEndpoint_UniverseDomain(t *testing.T) {
DefaultEndpoint: testRegularEndpoint,
DefaultEndpointTemplate: testEndpointTemplate,
DefaultMTLSEndpoint: testMTLSEndpoint,
DefaultUniverseDomain: "googleapis.com",
},
wantEndpoint: testRegularEndpoint,
},
Expand All @@ -468,7 +459,6 @@ func TestGetGRPCTransportConfigAndEndpoint_UniverseDomain(t *testing.T) {
DefaultEndpointTemplate: testEndpointTemplate,
DefaultMTLSEndpoint: testMTLSEndpoint,
Endpoint: testOverrideEndpoint,
DefaultUniverseDomain: "googleapis.com",
},
wantEndpoint: testOverrideEndpoint,
},
Expand All @@ -479,7 +469,6 @@ func TestGetGRPCTransportConfigAndEndpoint_UniverseDomain(t *testing.T) {
DefaultEndpointTemplate: testEndpointTemplate,
DefaultMTLSEndpoint: testMTLSEndpoint,
ClientCertSource: dummyClientCertSource,
DefaultUniverseDomain: "googleapis.com",
},
wantEndpoint: testMTLSEndpoint,
},
Expand All @@ -491,7 +480,6 @@ func TestGetGRPCTransportConfigAndEndpoint_UniverseDomain(t *testing.T) {
DefaultMTLSEndpoint: testMTLSEndpoint,
ClientCertSource: dummyClientCertSource,
Endpoint: testOverrideEndpoint,
DefaultUniverseDomain: "googleapis.com",
},
wantEndpoint: testOverrideEndpoint,
},
Expand All @@ -502,7 +490,6 @@ func TestGetGRPCTransportConfigAndEndpoint_UniverseDomain(t *testing.T) {
DefaultEndpointTemplate: testEndpointTemplate,
DefaultMTLSEndpoint: testMTLSEndpoint,
UniverseDomain: testUniverseDomain,
DefaultUniverseDomain: "googleapis.com",
},
wantEndpoint: testUniverseDomainEndpoint,
},
Expand All @@ -514,7 +501,6 @@ func TestGetGRPCTransportConfigAndEndpoint_UniverseDomain(t *testing.T) {
DefaultMTLSEndpoint: testMTLSEndpoint,
UniverseDomain: testUniverseDomain,
Endpoint: testOverrideEndpoint,
DefaultUniverseDomain: "googleapis.com",
},
wantEndpoint: testOverrideEndpoint,
},
Expand All @@ -526,7 +512,6 @@ func TestGetGRPCTransportConfigAndEndpoint_UniverseDomain(t *testing.T) {
DefaultMTLSEndpoint: testMTLSEndpoint,
UniverseDomain: testUniverseDomain,
ClientCertSource: dummyClientCertSource,
DefaultUniverseDomain: "googleapis.com",
},
wantErr: errUniverseNotSupportedMTLS,
},
Expand All @@ -539,7 +524,6 @@ func TestGetGRPCTransportConfigAndEndpoint_UniverseDomain(t *testing.T) {
UniverseDomain: testUniverseDomain,
ClientCertSource: dummyClientCertSource,
Endpoint: testOverrideEndpoint,
DefaultUniverseDomain: "googleapis.com",
},
wantEndpoint: testOverrideEndpoint,
},
Expand Down
49 changes: 29 additions & 20 deletions internal/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"os"
"strconv"
"sync"
"time"

"golang.org/x/oauth2"
Expand All @@ -20,8 +21,9 @@ import (
)

const (
newAuthLibEnVar = "GOOGLE_API_GO_EXPERIMENTAL_USE_NEW_AUTH_LIB"
universeDomainDefault = "googleapis.com"
newAuthLibEnvVar = "GOOGLE_API_GO_EXPERIMENTAL_USE_NEW_AUTH_LIB"
universeDomainEnvVar = "GOOGLE_CLOUD_UNIVERSE_DOMAIN"
defaultUniverseDomain = "googleapis.com"
)

// DialSettings holds information needed to establish a connection with a
Expand Down Expand Up @@ -58,9 +60,10 @@ type DialSettings struct {
EnableDirectPathXds bool
EnableNewAuthLibrary bool
AllowNonDefaultServiceAccount bool
UniverseDomain string
DefaultUniverseDomain string

udMu sync.Mutex // guards universeDomain
UniverseDomain string
// Google API system parameters. For more information please read:
// https://cloud.google.com/apis/docs/system-parameters
QuotaProject string
Expand Down Expand Up @@ -94,7 +97,7 @@ func (ds *DialSettings) IsNewAuthLibraryEnabled() bool {
if ds.EnableNewAuthLibrary {
return true
}
if b, err := strconv.ParseBool(os.Getenv(newAuthLibEnVar)); err == nil {
if b, err := strconv.ParseBool(os.Getenv(newAuthLibEnvVar)); err == nil {
return b
}
return false
Expand Down Expand Up @@ -165,31 +168,37 @@ func (ds *DialSettings) Validate() error {
return nil
}

// GetDefaultUniverseDomain returns the default service domain for a given Cloud
// universe, as configured with internaloption.WithDefaultUniverseDomain.
// The default value is "googleapis.com".
// GetDefaultUniverseDomain returns the Google default universe domain
// ("googleapis.com").
func (ds *DialSettings) GetDefaultUniverseDomain() string {
if ds.DefaultUniverseDomain == "" {
return universeDomainDefault
}
return ds.DefaultUniverseDomain
return defaultUniverseDomain
quartzmo marked this conversation as resolved.
Show resolved Hide resolved
}

// GetUniverseDomain returns the default service domain for a given Cloud
// universe, as configured with option.WithUniverseDomain.
// The default value is the value of GetDefaultUniverseDomain, as configured
// with internaloption.WithDefaultUniverseDomain.
// universe, with the following precedence:
//
// 1. A non-empty option.WithUniverseDomain.
// 2. A non-empty environment variable GOOGLE_CLOUD_UNIVERSE_DOMAIN.
// 3. The default value "googleapis.com".
func (ds *DialSettings) GetUniverseDomain() string {
if ds.UniverseDomain == "" {
return ds.GetDefaultUniverseDomain()
ds.udMu.Lock()
quartzmo marked this conversation as resolved.
Show resolved Hide resolved
defer ds.udMu.Unlock()
if ds.UniverseDomain != "" {
return ds.UniverseDomain
}
envVarUniverseDomain := os.Getenv(universeDomainEnvVar)
if envVarUniverseDomain != "" {
ds.UniverseDomain = envVarUniverseDomain
} else {
ds.UniverseDomain = defaultUniverseDomain
}
return ds.UniverseDomain
}

// IsUniverseDomainGDU returns true if the universe domain is the default Google
// universe.
// universe ("googleapis.com").
func (ds *DialSettings) IsUniverseDomainGDU() bool {
return ds.GetUniverseDomain() == ds.GetDefaultUniverseDomain()
return ds.GetUniverseDomain() == defaultUniverseDomain
}

// GetUniverseDomain returns the default service domain for a given Cloud
Expand All @@ -215,7 +224,7 @@ func GetUniverseDomain(creds *google.Credentials) (string, error) {
case <-errors:
// An error that is returned before the timer expires is likely to be
// connection refused. Temporarily (2024-03-21) return the GDU domain.
return universeDomainDefault, nil
return defaultUniverseDomain, nil
case res := <-results:
return res, nil
case <-timer.C: // Timer is expired.
Expand All @@ -227,6 +236,6 @@ func GetUniverseDomain(creds *google.Credentials) (string, error) {
// calls to creds.GetUniverseDomain() in grpc/dial.go and http/dial.go
// and remove this method to close
// https://github.com/googleapis/google-api-go-client/issues/2399.
return universeDomainDefault, nil
return defaultUniverseDomain, nil
}
}
71 changes: 71 additions & 0 deletions internal/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,74 @@ func TestSettingsValidate(t *testing.T) {
type dummyTS struct{}

func (dummyTS) Token() (*oauth2.Token, error) { return nil, nil }

func TestGetUniverseDomain(t *testing.T) {
testCases := []struct {
name string
ds *DialSettings
universeDomainEnvVar string
want string
}{
{
name: "none",
ds: &DialSettings{},
want: "googleapis.com",
},
{
name: "settings",
ds: &DialSettings{
UniverseDomain: "settings-example.goog",
},
want: "settings-example.goog",
},
{
name: "env var",
ds: &DialSettings{},
universeDomainEnvVar: "env-example.goog",
want: "env-example.goog",
},
{
name: "both",
ds: &DialSettings{
UniverseDomain: "settings-example.goog",
},
universeDomainEnvVar: "env-example.goog",
want: "settings-example.goog",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.universeDomainEnvVar != "" {
t.Setenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN", tc.universeDomainEnvVar)
}

if got := tc.ds.GetUniverseDomain(); got != tc.want {
t.Errorf("got %s, want %s", got, tc.want)
}
if got, want := tc.ds.GetDefaultUniverseDomain(), "googleapis.com"; got != want {
t.Errorf("got %s, want %s", got, want)
}
})
}
}

func TestGetUniverseDomain_Race(t *testing.T) {
want := "example.com"
t.Setenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN", want)
creds := &DialSettings{}
c := make(chan bool)
go func() {
got := creds.GetUniverseDomain() // First conflicting access.
if got != want {
t.Errorf("got %q, want %q", got, want)
}
c <- true
}()
got := creds.GetUniverseDomain() // Second conflicting access.
<-c
if got != want {
t.Errorf("got %q, want %q", got, want)
}

}
9 changes: 7 additions & 2 deletions option/internaloption/internaloption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ func TestDefaultApply(t *testing.T) {
DefaultAudience: "audience",
DefaultMTLSEndpoint: "http://mtls.example.com:445",
}
if !cmp.Equal(got, want, cmpopts.IgnoreUnexported(grpc.ClientConn{}), cmpopts.IgnoreFields(google.Credentials{}, "udMu", "universeDomain")) {
t.Errorf(cmp.Diff(got, want, cmpopts.IgnoreUnexported(grpc.ClientConn{}), cmpopts.IgnoreFields(google.Credentials{}, "udMu", "universeDomain")))
ignore := []cmp.Option{
cmpopts.IgnoreUnexported(grpc.ClientConn{}),
cmpopts.IgnoreFields(internal.DialSettings{}, "udMu"),
cmpopts.IgnoreFields(google.Credentials{}, "udMu", "universeDomain"),
}
if !cmp.Equal(got, want, ignore...) {
t.Errorf(cmp.Diff(got, want, ignore...))
}
}
9 changes: 7 additions & 2 deletions option/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,13 @@ func TestApply(t *testing.T) {
TelemetryDisabled: true,
UniverseDomain: "universe.com",
}
if !cmp.Equal(got, want, cmpopts.IgnoreUnexported(grpc.ClientConn{}), cmpopts.IgnoreFields(google.Credentials{}, "udMu", "universeDomain")) {
t.Errorf(cmp.Diff(got, want, cmpopts.IgnoreUnexported(grpc.ClientConn{}), cmpopts.IgnoreFields(google.Credentials{}, "udMu", "universeDomain")))
ignore := []cmp.Option{
cmpopts.IgnoreUnexported(grpc.ClientConn{}),
cmpopts.IgnoreFields(internal.DialSettings{}, "udMu"),
cmpopts.IgnoreFields(google.Credentials{}, "udMu", "universeDomain"),
}
if !cmp.Equal(got, want, ignore...) {
t.Errorf(cmp.Diff(got, want, ignore...))
}
}

Expand Down