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(auth/credentials/externalaccount): add default TokenURL #9700

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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)
}
})
}
}