Skip to content

Commit

Permalink
feat(auth/credentials/externalaccount): add default TokenURL (#9700)
Browse files Browse the repository at this point in the history
  • Loading branch information
quartzmo authored Apr 4, 2024
1 parent b3132c1 commit 81830e6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
19 changes: 18 additions & 1 deletion auth/credentials/internal/externalaccount/externalaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/http"
"regexp"
"strconv"
"strings"
"time"

"cloud.google.com/go/auth"
Expand All @@ -32,6 +33,10 @@ import (
const (
timeoutMinimum = 5 * time.Second
timeoutMaximum = 120 * time.Second

universeDomainPlaceholder = "UNIVERSE_DOMAIN"
defaultTokenURL = "https://sts.UNIVERSE_DOMAIN/v1/token"
defaultUniverseDomain = "googleapis.com"
)

var (
Expand Down Expand Up @@ -176,12 +181,25 @@ func (o *Options) validate() error {
return nil
}

// resolveTokenURL sets the default STS token endpoint with the configured
// universe domain.
func (o *Options) resolveTokenURL() {
if o.TokenURL != "" {
return
} else if o.UniverseDomain != "" {
o.TokenURL = strings.Replace(defaultTokenURL, universeDomainPlaceholder, o.UniverseDomain, 1)
} else {
o.TokenURL = strings.Replace(defaultTokenURL, universeDomainPlaceholder, defaultUniverseDomain, 1)
}
}

// NewTokenProvider returns a [cloud.google.com/go/auth.TokenProvider]
// configured with the provided options.
func NewTokenProvider(opts *Options) (auth.TokenProvider, error) {
if err := opts.validate(); err != nil {
return nil, err
}
opts.resolveTokenURL()
stp, err := newSubjectTokenProvider(opts)
if err != nil {
return nil, err
Expand Down Expand Up @@ -282,7 +300,6 @@ func (tp *tokenProvider) Token(ctx context.Context) (*auth.Token, error) {
// subjectTokenProvider
func newSubjectTokenProvider(o *Options) (subjectTokenProvider, error) {
reqOpts := &RequestOptions{Audience: o.Audience, SubjectTokenType: o.SubjectTokenType}

if o.AwsSecurityCredentialsProvider != nil {
return &awsSubjectProvider{
securityCredentialsProvider: o.AwsSecurityCredentialsProvider,
Expand Down
44 changes: 44 additions & 0 deletions auth/credentials/internal/externalaccount/externalaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,47 @@ func TestOptionsValidate(t *testing.T) {
})
}
}

func TestOptionsResolveTokenURL(t *testing.T) {
tests := []struct {
name string
o *Options
want string
}{
{
name: "default",
o: &Options{},
want: "https://sts.googleapis.com/v1/token",
},
{
name: "Options TokenURL",
o: &Options{
TokenURL: "http://localhost:8080/v1/token",
},
want: "http://localhost:8080/v1/token",
},
{
name: "Options UniverseDomain",
o: &Options{
UniverseDomain: "example.com",
},
want: "https://sts.example.com/v1/token",
},
{
name: "Options TokenURL overrides UniverseDomain",
o: &Options{
TokenURL: "http://localhost:8080/v1/token",
UniverseDomain: "example.com",
},
want: "http://localhost:8080/v1/token",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.o.resolveTokenURL()
if tc.o.TokenURL != tc.want {
t.Errorf("got %s, want %s", tc.o.TokenURL, tc.want)
}
})
}
}

0 comments on commit 81830e6

Please sign in to comment.