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

Update UAA token refresh logic #826

Merged
merged 1 commit into from
Oct 10, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pkg/auth/common/login_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,14 @@ func WithCertInfo(tlsSkipVerify bool, caCertData string) LoginOption {
}
}

// WithClientID specifies a OAuth Client ID to use
func WithClientID(clientID string) LoginOption {
// WithClientIDAndSecret specifies a OAuth Client ID and secret to use
func WithClientIDAndSecret(clientID, clientSecret string) LoginOption {
return func(h *TanzuLoginHandler) error {
h.clientID = clientID
h.clientSecret = clientSecret
if h.oauthConfig != nil {
h.oauthConfig.ClientID = clientID
h.oauthConfig.ClientSecret = clientSecret
}
return nil
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/auth/uaa/tanzu.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ func getIssuerEndpoints(issuerURL string) common.IssuerEndPoints {
}
}

func GetClientSecret() string {
// Not really used as a secret, but specified in OAuth client to UAA in order
// to obtain the expected token refresh behavior.
secret := "tanzu_intentionally_not_a_secret"

if noClientSecret, _ := strconv.ParseBool(os.Getenv(constants.UAANoClientSecret)); noClientSecret {
secret = ""
}
return secret
}

func GetAlternateClientID() string {
// Default to use the same client id, even for non-interactive login use cases.
clientID := tanzuCLIClientID
Expand Down
2 changes: 1 addition & 1 deletion pkg/auth/uaa/uaa.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func GetTokens(refreshOrAPIToken, _, issuer, tokenType string) (*common.Token, e
if tokenType == common.APITokenType {
clientID = GetAlternateClientID()
}
loginOptions := []common.LoginOption{common.WithRefreshToken(refreshOrAPIToken), common.WithListenerPortFromEnv(constants.TanzuCLIOAuthLocalListenerPort), common.WithClientID(clientID)}
loginOptions := []common.LoginOption{common.WithRefreshToken(refreshOrAPIToken), common.WithListenerPortFromEnv(constants.TanzuCLIOAuthLocalListenerPort), common.WithClientIDAndSecret(clientID, GetClientSecret())}
if tokenType == common.APITokenType {
loginOptions = append(loginOptions, common.WithSuppressInteractive(true))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/command/apitoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func createAPIToken(cmd *cobra.Command, _ []string) (err error) {
// Also specify the client ID to use for token generation
loginOptions := []commonauth.LoginOption{
commonauth.WithListenerPortFromEnv(constants.TanzuCLIOAuthLocalListenerPort),
commonauth.WithClientID(uaa.GetAlternateClientID()),
commonauth.WithClientIDAndSecret(uaa.GetAlternateClientID(), uaa.GetClientSecret()),
}

token, err = uaa.TanzuLogin(c.GlobalOpts.Auth.Issuer, loginOptions...)
Expand Down
2 changes: 1 addition & 1 deletion pkg/command/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ func doUAAAPITokenAuthAndUpdateContext(c *configtypes.Context, uaaEndpoint, apiT
loginOptions := []commonauth.LoginOption{
commonauth.WithSuppressInteractive(true), // fail instead of falling back to interactive login
commonauth.WithRefreshToken(apiTokenValue),
commonauth.WithClientID(uaa.GetAlternateClientID()),
commonauth.WithClientIDAndSecret(uaa.GetAlternateClientID(), uaa.GetClientSecret()),
}

var endpointCACertData string
Expand Down
3 changes: 3 additions & 0 deletions pkg/constants/env_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ const (
// UAAUseAlternateClient allows use of an alternate UAA client for non-interactive logins
UAAUseAlternateClient = "TANZU_CLI_USE_ALTERNATE_UAA_CLIENT"

// UAANoClientSecret skips setting of OAuth Client Secret
UAANoClientSecret = "TANZU_CLI_NO_UAA_CLIENT_SECRET" //nolint:gosec

// TanzuCLIOAuthLocalListenerPort is the port to be used by local listener for OAuth authorization flow
TanzuCLIOAuthLocalListenerPort = "TANZU_CLI_OAUTH_LOCAL_LISTENER_PORT"

Expand Down
Loading