Skip to content

Commit 81830e6

Browse files
authored
feat(auth/credentials/externalaccount): add default TokenURL (#9700)
1 parent b3132c1 commit 81830e6

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

auth/credentials/internal/externalaccount/externalaccount.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net/http"
2222
"regexp"
2323
"strconv"
24+
"strings"
2425
"time"
2526

2627
"cloud.google.com/go/auth"
@@ -32,6 +33,10 @@ import (
3233
const (
3334
timeoutMinimum = 5 * time.Second
3435
timeoutMaximum = 120 * time.Second
36+
37+
universeDomainPlaceholder = "UNIVERSE_DOMAIN"
38+
defaultTokenURL = "https://sts.UNIVERSE_DOMAIN/v1/token"
39+
defaultUniverseDomain = "googleapis.com"
3540
)
3641

3742
var (
@@ -176,12 +181,25 @@ func (o *Options) validate() error {
176181
return nil
177182
}
178183

184+
// resolveTokenURL sets the default STS token endpoint with the configured
185+
// universe domain.
186+
func (o *Options) resolveTokenURL() {
187+
if o.TokenURL != "" {
188+
return
189+
} else if o.UniverseDomain != "" {
190+
o.TokenURL = strings.Replace(defaultTokenURL, universeDomainPlaceholder, o.UniverseDomain, 1)
191+
} else {
192+
o.TokenURL = strings.Replace(defaultTokenURL, universeDomainPlaceholder, defaultUniverseDomain, 1)
193+
}
194+
}
195+
179196
// NewTokenProvider returns a [cloud.google.com/go/auth.TokenProvider]
180197
// configured with the provided options.
181198
func NewTokenProvider(opts *Options) (auth.TokenProvider, error) {
182199
if err := opts.validate(); err != nil {
183200
return nil, err
184201
}
202+
opts.resolveTokenURL()
185203
stp, err := newSubjectTokenProvider(opts)
186204
if err != nil {
187205
return nil, err
@@ -282,7 +300,6 @@ func (tp *tokenProvider) Token(ctx context.Context) (*auth.Token, error) {
282300
// subjectTokenProvider
283301
func newSubjectTokenProvider(o *Options) (subjectTokenProvider, error) {
284302
reqOpts := &RequestOptions{Audience: o.Audience, SubjectTokenType: o.SubjectTokenType}
285-
286303
if o.AwsSecurityCredentialsProvider != nil {
287304
return &awsSubjectProvider{
288305
securityCredentialsProvider: o.AwsSecurityCredentialsProvider,

auth/credentials/internal/externalaccount/externalaccount_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,47 @@ func TestOptionsValidate(t *testing.T) {
462462
})
463463
}
464464
}
465+
466+
func TestOptionsResolveTokenURL(t *testing.T) {
467+
tests := []struct {
468+
name string
469+
o *Options
470+
want string
471+
}{
472+
{
473+
name: "default",
474+
o: &Options{},
475+
want: "https://sts.googleapis.com/v1/token",
476+
},
477+
{
478+
name: "Options TokenURL",
479+
o: &Options{
480+
TokenURL: "http://localhost:8080/v1/token",
481+
},
482+
want: "http://localhost:8080/v1/token",
483+
},
484+
{
485+
name: "Options UniverseDomain",
486+
o: &Options{
487+
UniverseDomain: "example.com",
488+
},
489+
want: "https://sts.example.com/v1/token",
490+
},
491+
{
492+
name: "Options TokenURL overrides UniverseDomain",
493+
o: &Options{
494+
TokenURL: "http://localhost:8080/v1/token",
495+
UniverseDomain: "example.com",
496+
},
497+
want: "http://localhost:8080/v1/token",
498+
},
499+
}
500+
for _, tc := range tests {
501+
t.Run(tc.name, func(t *testing.T) {
502+
tc.o.resolveTokenURL()
503+
if tc.o.TokenURL != tc.want {
504+
t.Errorf("got %s, want %s", tc.o.TokenURL, tc.want)
505+
}
506+
})
507+
}
508+
}

0 commit comments

Comments
 (0)