-
Notifications
You must be signed in to change notification settings - Fork 23
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
Signed-off-by: Anuj Chaudhari <[email protected]>
- Loading branch information
Showing
7 changed files
with
140 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
## tanzu api-token | ||
|
||
API Token Generation for the Tanzu Platform | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for api-token | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [tanzu](tanzu.md) - The Tanzu CLI | ||
* [tanzu api-token create](tanzu_api-token_create.md) - Create new API Token | ||
|
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,18 @@ | ||
## tanzu api-token create | ||
|
||
Create new API Token | ||
|
||
``` | ||
tanzu api-token create [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for create | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [tanzu api-token](tanzu_api-token.md) - API Token Generation for the Tanzu Platform | ||
|
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,88 @@ | ||
// 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" | ||
|
||
"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" | ||
|
||
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" | ||
) | ||
|
||
func newAPITokenCmd() *cobra.Command { | ||
apiTokenCmd := &cobra.Command{ | ||
Use: "api-token", | ||
Short: "API Token Generation 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, _ []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 | ||
// If user chooses to use a specific local listener port, use it | ||
// Also specify the client ID to use for token generation | ||
loginOptions := []commonauth.LoginOption{ | ||
commonauth.WithListenerPortFromEnv(constants.TanzuCLIOAuthLocalListenerPort), | ||
commonauth.WithClientID(uaa.TanzuCLIClientIDExtended), | ||
} | ||
|
||
token, err = uaa.TanzuLogin(c.GlobalOpts.Auth.Issuer, loginOptions...) | ||
if err != nil { | ||
return errors.Wrap(err, "unable to login") | ||
} | ||
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