Skip to content

Commit 34a7afa

Browse files
quartzmocodyoss
authored andcommitted
google/externalaccount: add Config.UniverseDomain
Change-Id: Ia1caee246da68c01addd06e1367ed1e43645826b Reviewed-on: https://go-review.googlesource.com/c/oauth2/+/568216 Reviewed-by: Alex Eitzman <eitzman@google.com> Reviewed-by: Cody Oss <codyoss@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 95bec95 commit 34a7afa

File tree

4 files changed

+76
-12
lines changed

4 files changed

+76
-12
lines changed

google/default.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
const (
2424
adcSetupURL = "https://cloud.google.com/docs/authentication/external/set-up-adc"
25-
universeDomainDefault = "googleapis.com"
25+
defaultUniverseDomain = "googleapis.com"
2626
)
2727

2828
// Credentials holds Google credentials, including "Application Default Credentials".
@@ -58,7 +58,7 @@ type Credentials struct {
5858
// See also [The attached service account](https://cloud.google.com/docs/authentication/application-default-credentials#attached-sa).
5959
func (c *Credentials) UniverseDomain() string {
6060
if c.universeDomain == "" {
61-
return universeDomainDefault
61+
return defaultUniverseDomain
6262
}
6363
return c.universeDomain
6464
}
@@ -89,7 +89,7 @@ func (c *Credentials) GetUniverseDomain() (string, error) {
8989
// computeUniverseDomain that did not set universeDomain, set the default
9090
// universe domain.
9191
if c.universeDomain == "" {
92-
c.universeDomain = universeDomainDefault
92+
c.universeDomain = defaultUniverseDomain
9393
}
9494
return c.universeDomain, nil
9595
}
@@ -103,7 +103,7 @@ func (c *Credentials) computeUniverseDomain() error {
103103
if err != nil {
104104
if _, ok := err.(metadata.NotDefinedError); ok {
105105
// http.StatusNotFound (404)
106-
c.universeDomain = universeDomainDefault
106+
c.universeDomain = defaultUniverseDomain
107107
return nil
108108
} else {
109109
return err
@@ -287,7 +287,7 @@ func CredentialsFromJSONWithParams(ctx context.Context, jsonData []byte, params
287287
}
288288
// Authorized user credentials are only supported in the googleapis.com universe.
289289
if f.Type == userCredentialsKey {
290-
universeDomain = universeDomainDefault
290+
universeDomain = defaultUniverseDomain
291291
}
292292

293293
ts, err := f.tokenSource(ctx, params)

google/downscope/downscoping.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import (
5151
const (
5252
universeDomainPlaceholder = "UNIVERSE_DOMAIN"
5353
identityBindingEndpointTemplate = "https://sts.UNIVERSE_DOMAIN/v1/token"
54-
universeDomainDefault = "googleapis.com"
54+
defaultUniverseDomain = "googleapis.com"
5555
)
5656

5757
type accessBoundary struct {
@@ -117,7 +117,7 @@ type DownscopingConfig struct {
117117
// configured universe domain.
118118
func (dc *DownscopingConfig) identityBindingEndpoint() string {
119119
if dc.UniverseDomain == "" {
120-
return strings.Replace(identityBindingEndpointTemplate, universeDomainPlaceholder, universeDomainDefault, 1)
120+
return strings.Replace(identityBindingEndpointTemplate, universeDomainPlaceholder, defaultUniverseDomain, 1)
121121
}
122122
return strings.Replace(identityBindingEndpointTemplate, universeDomainPlaceholder, dc.UniverseDomain, 1)
123123
}

google/externalaccount/basecredentials.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,20 @@ import (
113113
"net/http"
114114
"regexp"
115115
"strconv"
116+
"strings"
116117
"time"
117118

118119
"golang.org/x/oauth2"
119120
"golang.org/x/oauth2/google/internal/impersonate"
120121
"golang.org/x/oauth2/google/internal/stsexchange"
121122
)
122123

124+
const (
125+
universeDomainPlaceholder = "UNIVERSE_DOMAIN"
126+
defaultTokenURL = "https://sts.UNIVERSE_DOMAIN/v1/token"
127+
defaultUniverseDomain = "googleapis.com"
128+
)
129+
123130
// now aliases time.Now for testing
124131
var now = func() time.Time {
125132
return time.Now().UTC()
@@ -139,7 +146,9 @@ type Config struct {
139146
// Required.
140147
SubjectTokenType string
141148
// TokenURL is the STS token exchange endpoint. If not provided, will default to
142-
// https://sts.googleapis.com/v1/token. Optional.
149+
// https://sts.UNIVERSE_DOMAIN/v1/token, with UNIVERSE_DOMAIN set to the
150+
// default service domain googleapis.com unless UniverseDomain is set.
151+
// Optional.
143152
TokenURL string
144153
// TokenInfoURL is the token_info endpoint used to retrieve the account related information (
145154
// user attributes like account identifier, eg. email, username, uid, etc). This is
@@ -177,6 +186,10 @@ type Config struct {
177186
// AwsSecurityCredentialsSupplier is an AWS Security Credential supplier for AWS credentials.
178187
// One of SubjectTokenSupplier, AWSSecurityCredentialSupplier or CredentialSource must be provided. Optional.
179188
AwsSecurityCredentialsSupplier AwsSecurityCredentialsSupplier
189+
// UniverseDomain is the default service domain for a given Cloud universe.
190+
// This value will be used in the default STS token URL. The default value
191+
// is "googleapis.com". It will not be used if TokenURL is set. Optional.
192+
UniverseDomain string
180193
}
181194

182195
var (
@@ -246,9 +259,8 @@ func (c *Config) tokenSource(ctx context.Context, scheme string) (oauth2.TokenSo
246259

247260
// Subject token file types.
248261
const (
249-
fileTypeText = "text"
250-
fileTypeJSON = "json"
251-
defaultTokenUrl = "https://sts.googleapis.com/v1/token"
262+
fileTypeText = "text"
263+
fileTypeJSON = "json"
252264
)
253265

254266
// Format contains information needed to retireve a subject token for URL or File sourced credentials.
@@ -336,11 +348,20 @@ type SupplierOptions struct {
336348
SubjectTokenType string
337349
}
338350

351+
// tokenURL returns the default STS token endpoint with the configured universe
352+
// domain.
353+
func (c *Config) tokenURL() string {
354+
if c.UniverseDomain == "" {
355+
return strings.Replace(defaultTokenURL, universeDomainPlaceholder, defaultUniverseDomain, 1)
356+
}
357+
return strings.Replace(defaultTokenURL, universeDomainPlaceholder, c.UniverseDomain, 1)
358+
}
359+
339360
// parse determines the type of CredentialSource needed.
340361
func (c *Config) parse(ctx context.Context) (baseCredentialSource, error) {
341362
//set Defaults
342363
if c.TokenURL == "" {
343-
c.TokenURL = defaultTokenUrl
364+
c.TokenURL = c.tokenURL()
344365
}
345366
supplierOptions := SupplierOptions{Audience: c.Audience, SubjectTokenType: c.SubjectTokenType}
346367

google/externalaccount/basecredentials_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,46 @@ func TestNewToken(t *testing.T) {
454454
})
455455
}
456456
}
457+
458+
func TestConfig_TokenURL(t *testing.T) {
459+
tests := []struct {
460+
tokenURL string
461+
universeDomain string
462+
want string
463+
}{
464+
{
465+
tokenURL: "https://sts.googleapis.com/v1/token",
466+
universeDomain: "",
467+
want: "https://sts.googleapis.com/v1/token",
468+
},
469+
{
470+
tokenURL: "",
471+
universeDomain: "",
472+
want: "https://sts.googleapis.com/v1/token",
473+
},
474+
{
475+
tokenURL: "",
476+
universeDomain: "googleapis.com",
477+
want: "https://sts.googleapis.com/v1/token",
478+
},
479+
{
480+
tokenURL: "",
481+
universeDomain: "example.com",
482+
want: "https://sts.example.com/v1/token",
483+
},
484+
}
485+
for _, tt := range tests {
486+
config := &Config{
487+
Audience: "//iam.googleapis.com/locations/eu/workforcePools/pool-id/providers/provider-id",
488+
SubjectTokenType: "urn:ietf:params:oauth:token-type:id_token",
489+
CredentialSource: &testBaseCredSource,
490+
Scopes: []string{"https://www.googleapis.com/auth/devstorage.full_control"},
491+
}
492+
config.TokenURL = tt.tokenURL
493+
config.UniverseDomain = tt.universeDomain
494+
config.parse(context.Background())
495+
if got := config.TokenURL; got != tt.want {
496+
t.Errorf("got %q, want %q", got, tt.want)
497+
}
498+
}
499+
}

0 commit comments

Comments
 (0)