in pkg/cloud_provider/auth/token_sources.go [111:141]
func (ts *GCPTokenSource) fetchIdentityBindingToken(ctx context.Context, k8sSAToken *oauth2.Token) (*oauth2.Token, error) {
stsService, err := sts.NewService(ctx, option.WithHTTPClient(&http.Client{}))
if err != nil {
return nil, fmt.Errorf("new STS service error: %w", err)
}
audience := fmt.Sprintf(
"identitynamespace:%s:%s",
ts.meta.GetIdentityPool(),
ts.meta.GetIdentityProvider(),
)
stsRequest := &sts.GoogleIdentityStsV1ExchangeTokenRequest{
Audience: audience,
GrantType: "urn:ietf:params:oauth:grant-type:token-exchange",
Scope: credentials.DefaultAuthScopes()[0],
RequestedTokenType: "urn:ietf:params:oauth:token-type:access_token",
SubjectTokenType: "urn:ietf:params:oauth:token-type:jwt",
SubjectToken: k8sSAToken.AccessToken,
}
stsResponse, err := stsService.V1.Token(stsRequest).Do()
if err != nil {
return nil, fmt.Errorf("IdentityBindingToken exchange error with audience %q: %w", audience, err)
}
return &oauth2.Token{
AccessToken: stsResponse.AccessToken,
TokenType: stsResponse.TokenType,
Expiry: time.Now().Add(time.Second * time.Duration(stsResponse.ExpiresIn)),
}, nil
}