-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to generate API token with
tz api-token create
- Loading branch information
Showing
4 changed files
with
102 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2024 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package command | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/fatih/color" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
commonauth "github.com/vmware-tanzu/tanzu-cli/pkg/auth/common" | ||
"github.com/vmware-tanzu/tanzu-cli/pkg/auth/uaa" | ||
"github.com/vmware-tanzu/tanzu-cli/pkg/cli" | ||
"github.com/vmware-tanzu/tanzu-cli/pkg/constants" | ||
"github.com/vmware-tanzu/tanzu-plugin-runtime/config" | ||
"github.com/vmware-tanzu/tanzu-plugin-runtime/config/types" | ||
"github.com/vmware-tanzu/tanzu-plugin-runtime/plugin" | ||
) | ||
|
||
func newAPITokenCmd() *cobra.Command { | ||
apiTokenCmd := &cobra.Command{ | ||
Use: "api-token", | ||
Short: "API Token operations for the Tanzu Platform", | ||
Aliases: []string{"apitoken"}, | ||
Annotations: map[string]string{ | ||
"group": string(plugin.SystemCmdGroup), | ||
}, | ||
} | ||
|
||
apiTokenCmd.SetUsageFunc(cli.SubCmdUsageFunc) | ||
apiTokenCmd.AddCommand( | ||
newAPITokenCreateCmd(), | ||
) | ||
|
||
return apiTokenCmd | ||
} | ||
|
||
func newAPITokenCreateCmd() *cobra.Command { | ||
createCmd := &cobra.Command{ | ||
Use: "create", | ||
Short: "Create new API Token", | ||
Aliases: []string{}, | ||
RunE: createAPIToken, | ||
} | ||
|
||
return createCmd | ||
} | ||
|
||
func createAPIToken(cmd *cobra.Command, args []string) (err error) { | ||
c, err := config.GetActiveContext(types.ContextTypeTanzu) | ||
if err != nil { | ||
return errors.New("No active context of type `tanzu`. Please login to Tanzu Platform first to generate the API token") | ||
} | ||
if c == nil || c.GlobalOpts == nil || c.GlobalOpts.Auth.Issuer == "" { | ||
return errors.New("Invalid active context of type `tanzu`. Please login to Tanzu Platform first to generate the API token") | ||
} | ||
// Make sure it is of type tanzu with tanzuIdpType as `uaa` else return error | ||
if idpType, exist := c.AdditionalMetadata[config.TanzuIdpTypeKey]; !exist || idpType != "uaa" { | ||
return errors.New("invalid IDP type for the active context. Only UAA IDP type is supported for generating API token") | ||
} | ||
|
||
var token *commonauth.Token | ||
var loginOptions []commonauth.LoginOption | ||
// If user chooses to use a specific local listener port, use it | ||
loginOptions = append(loginOptions, commonauth.WithListenerPortFromEnv(constants.TanzuCLIOAuthLocalListenerPort)) | ||
|
||
loginOptions = append(loginOptions, commonauth.WithClientID(uaa.TanzuCLIClientIDExtended)) | ||
token, err = uaa.TanzuLogin(c.GlobalOpts.Auth.Issuer, loginOptions...) | ||
if err != nil { | ||
return err // TODO (Update error) | ||
} | ||
cyanBold := color.New(color.FgCyan).Add(color.Bold) | ||
bold := color.New(color.Bold) | ||
|
||
fmt.Fprint(cmd.OutOrStdout(), bold.Sprint("==\n\n")) | ||
fmt.Fprintf(cmd.OutOrStdout(), "%s Your generated API token is: %s\n\n", bold.Sprint("API Token Generation Successful!"), cyanBold.Sprint(token.RefreshToken)) | ||
fmt.Fprintf(cmd.OutOrStdout(), "%s The token will expire on %s.\n", bold.Sprint("Token Expiration:"), bold.Sprint(time.Now().Local().Add(time.Second*time.Duration(token.ExpiresIn)))) | ||
fmt.Fprint(cmd.OutOrStdout(), "Please copy and save your token securely. Note that you will need to regenerate a new token before expiration time and login again to continue using the CLI.\n\n") | ||
fmt.Fprintf(cmd.OutOrStdout(), "For non-interactive login use the API token as follow: %s\n", cyanBold.Sprint("TANZU_API_TOKEN=<token> tanzu login --endpoint <tanzu-platform-endpoint>")) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters