Skip to content

Commit 1e6999b

Browse files
quartzmocodyoss
authored andcommitted
google: add UniverseDomain to CredentialsParams
Change-Id: I7925b8341e1f047d0115acd7a01a34679a489ee0 Reviewed-on: https://go-review.googlesource.com/c/oauth2/+/552716 Reviewed-by: Cody Oss <[email protected]> Run-TryBot: Cody Oss <[email protected]> Reviewed-by: Viacheslav Rostovtsev <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 6e9ec93 commit 1e6999b

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

google/default.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ type CredentialsParams struct {
9191
// Note: This option is currently only respected when using credentials
9292
// fetched from the GCE metadata server.
9393
EarlyTokenRefresh time.Duration
94+
95+
// UniverseDomain is the default service domain for a given Cloud universe.
96+
// Only supported in authentication flows that support universe domains.
97+
// This value takes precedence over a universe domain explicitly specified
98+
// in a credentials config file or by the GCE metadata server. Optional.
99+
UniverseDomain string
94100
}
95101

96102
func (params CredentialsParams) deepCopy() CredentialsParams {
@@ -175,8 +181,9 @@ func FindDefaultCredentialsWithParams(ctx context.Context, params CredentialsPar
175181
if metadata.OnGCE() {
176182
id, _ := metadata.ProjectID()
177183
return &Credentials{
178-
ProjectID: id,
179-
TokenSource: computeTokenSource("", params.EarlyTokenRefresh, params.Scopes...),
184+
ProjectID: id,
185+
TokenSource: computeTokenSource("", params.EarlyTokenRefresh, params.Scopes...),
186+
universeDomain: params.UniverseDomain,
180187
}, nil
181188
}
182189

@@ -217,6 +224,9 @@ func CredentialsFromJSONWithParams(ctx context.Context, jsonData []byte, params
217224
}
218225

219226
universeDomain := f.UniverseDomain
227+
if params.UniverseDomain != "" {
228+
universeDomain = params.UniverseDomain
229+
}
220230
// Authorized user credentials are only supported in the googleapis.com universe.
221231
if f.Type == userCredentialsKey {
222232
universeDomain = universeDomainDefault

google/default_test.go

+80-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ var userJSONUniverseDomain = []byte(`{
5353
"universe_domain": "example.com"
5454
}`)
5555

56+
var universeDomain = "example.com"
57+
58+
var universeDomain2 = "apis-tpclp.goog"
59+
5660
func TestCredentialsFromJSONWithParams_SA(t *testing.T) {
5761
ctx := context.Background()
5862
scope := "https://www.googleapis.com/auth/cloud-platform"
@@ -72,6 +76,26 @@ func TestCredentialsFromJSONWithParams_SA(t *testing.T) {
7276
}
7377
}
7478

79+
func TestCredentialsFromJSONWithParams_SA_Params_UniverseDomain(t *testing.T) {
80+
ctx := context.Background()
81+
scope := "https://www.googleapis.com/auth/cloud-platform"
82+
params := CredentialsParams{
83+
Scopes: []string{scope},
84+
UniverseDomain: universeDomain2,
85+
}
86+
creds, err := CredentialsFromJSONWithParams(ctx, saJSONJWT, params)
87+
if err != nil {
88+
t.Fatal(err)
89+
}
90+
91+
if want := "fake_project"; creds.ProjectID != want {
92+
t.Fatalf("got %q, want %q", creds.ProjectID, want)
93+
}
94+
if creds.UniverseDomain() != universeDomain2 {
95+
t.Fatalf("got %q, want %q", creds.UniverseDomain(), universeDomain2)
96+
}
97+
}
98+
7599
func TestCredentialsFromJSONWithParams_SA_UniverseDomain(t *testing.T) {
76100
ctx := context.Background()
77101
scope := "https://www.googleapis.com/auth/cloud-platform"
@@ -86,8 +110,28 @@ func TestCredentialsFromJSONWithParams_SA_UniverseDomain(t *testing.T) {
86110
if want := "fake_project"; creds.ProjectID != want {
87111
t.Fatalf("got %q, want %q", creds.ProjectID, want)
88112
}
89-
if want := "example.com"; creds.UniverseDomain() != want {
90-
t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
113+
if creds.UniverseDomain() != universeDomain {
114+
t.Fatalf("got %q, want %q", creds.UniverseDomain(), universeDomain)
115+
}
116+
}
117+
118+
func TestCredentialsFromJSONWithParams_SA_UniverseDomain_Params_UniverseDomain(t *testing.T) {
119+
ctx := context.Background()
120+
scope := "https://www.googleapis.com/auth/cloud-platform"
121+
params := CredentialsParams{
122+
Scopes: []string{scope},
123+
UniverseDomain: universeDomain2,
124+
}
125+
creds, err := CredentialsFromJSONWithParams(ctx, saJSONJWTUniverseDomain, params)
126+
if err != nil {
127+
t.Fatal(err)
128+
}
129+
130+
if want := "fake_project"; creds.ProjectID != want {
131+
t.Fatalf("got %q, want %q", creds.ProjectID, want)
132+
}
133+
if creds.UniverseDomain() != universeDomain2 {
134+
t.Fatalf("got %q, want %q", creds.UniverseDomain(), universeDomain2)
91135
}
92136
}
93137

@@ -107,6 +151,23 @@ func TestCredentialsFromJSONWithParams_User(t *testing.T) {
107151
}
108152
}
109153

154+
func TestCredentialsFromJSONWithParams_User_Params_UniverseDomain(t *testing.T) {
155+
ctx := context.Background()
156+
scope := "https://www.googleapis.com/auth/cloud-platform"
157+
params := CredentialsParams{
158+
Scopes: []string{scope},
159+
UniverseDomain: universeDomain2,
160+
}
161+
creds, err := CredentialsFromJSONWithParams(ctx, userJSON, params)
162+
if err != nil {
163+
t.Fatal(err)
164+
}
165+
166+
if want := "googleapis.com"; creds.UniverseDomain() != want {
167+
t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
168+
}
169+
}
170+
110171
func TestCredentialsFromJSONWithParams_User_UniverseDomain(t *testing.T) {
111172
ctx := context.Background()
112173
scope := "https://www.googleapis.com/auth/cloud-platform"
@@ -122,3 +183,20 @@ func TestCredentialsFromJSONWithParams_User_UniverseDomain(t *testing.T) {
122183
t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
123184
}
124185
}
186+
187+
func TestCredentialsFromJSONWithParams_User_UniverseDomain_Params_UniverseDomain(t *testing.T) {
188+
ctx := context.Background()
189+
scope := "https://www.googleapis.com/auth/cloud-platform"
190+
params := CredentialsParams{
191+
Scopes: []string{scope},
192+
UniverseDomain: universeDomain2,
193+
}
194+
creds, err := CredentialsFromJSONWithParams(ctx, userJSONUniverseDomain, params)
195+
if err != nil {
196+
t.Fatal(err)
197+
}
198+
199+
if want := "googleapis.com"; creds.UniverseDomain() != want {
200+
t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
201+
}
202+
}

0 commit comments

Comments
 (0)