From 98c24238bfc1f805f3829f334a7dbdb0d2d25d2f Mon Sep 17 00:00:00 2001 From: Zain Asgar Date: Tue, 24 Aug 2021 17:59:15 -0700 Subject: [PATCH] PC-1145 - API key list method no longer shows the key Summary: An explicit `px api-key get ` command has been added. Test Plan: tested manually Reviewers: michelle, vihang Reviewed By: michelle JIRA Issues: PC-1145 Differential Revision: https://phab.corp.pixielabs.ai/D9561 GitOrigin-RevId: 8ffe4640a186635a323602d8c02e71374c918fb6 --- src/pixie_cli/pkg/cmd/api_key.go | 52 ++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/src/pixie_cli/pkg/cmd/api_key.go b/src/pixie_cli/pkg/cmd/api_key.go index 0723ba42e2a..44cb288bcad 100644 --- a/src/pixie_cli/pkg/cmd/api_key.go +++ b/src/pixie_cli/pkg/cmd/api_key.go @@ -39,6 +39,7 @@ func init() { APIKeyCmd.AddCommand(CreateAPIKeyCmd) APIKeyCmd.AddCommand(DeleteAPIKeyCmd) APIKeyCmd.AddCommand(ListAPIKeyCmd) + APIKeyCmd.AddCommand(GetAPIKeyCmd) CreateAPIKeyCmd.Flags().StringP("desc", "d", "", "A description for the API key") viper.BindPFlag("desc", CreateAPIKeyCmd.Flags().Lookup("desc")) @@ -105,7 +106,7 @@ var DeleteAPIKeyCmd = &cobra.Command{ // ListAPIKeyCmd is the List sub-command of APIKey. var ListAPIKeyCmd = &cobra.Command{ Use: "list", - Short: "List all API key for Pixie", + Short: "List all API key metadata", Run: func(cmd *cobra.Command, args []string) { cloudAddr := viper.GetString("cloud_addr") format, _ := cmd.Flags().GetString("output") @@ -121,12 +122,43 @@ var ListAPIKeyCmd = &cobra.Command{ defer w.Finish() w.SetHeader("api-keys", []string{"ID", "Key", "CreatedAt", "Description"}) for _, k := range keys { - _ = w.Write([]interface{}{utils2.UUIDFromProtoOrNil(k.ID), k.Key, k.CreatedAt, + _ = w.Write([]interface{}{utils2.UUIDFromProtoOrNil(k.ID), "", k.CreatedAt, k.Desc}) } }, } +// GetAPIKeyCmd is the List sub-command of APIKey. +var GetAPIKeyCmd = &cobra.Command{ + Use: "get", + Short: "Get API key details for a specific key", + Run: func(cmd *cobra.Command, args []string) { + cloudAddr := viper.GetString("cloud_addr") + format, _ := cmd.Flags().GetString("output") + format = strings.ToLower(format) + + if len(args) != 1 { + utils.Fatal("Expected a single argument 'key id'.") + } + + keyID, err := uuid.FromString(args[0]) + if err != nil { + utils.Fatal("Malformed Key ID. Expected a single argument 'key id'.") + } + k, err := getAPIKey(cloudAddr, keyID) + if err != nil { + // Using log.Fatal rather than CLI log in order to track this unexpected error in Sentry. + log.WithError(err).Fatal("Failed to fetch API key") + } + // Throw keys into table. + w := components.CreateStreamWriter(format, os.Stdout) + defer w.Finish() + w.SetHeader("api-keys", []string{"ID", "Key", "CreatedAt", "Description"}) + _ = w.Write([]interface{}{utils2.UUIDFromProtoOrNil(k.ID), k.Key, k.CreatedAt, + k.Desc}) + }, +} + func getAPIKeyClientAndContext(cloudAddr string) (cloudpb.APIKeyManagerClient, context.Context, error) { // Get grpc connection to cloud. cloudConn, err := utils.GetCloudClientConnection(cloudAddr) @@ -179,3 +211,19 @@ func listAPIKeys(cloudAddr string) ([]*cloudpb.APIKey, error) { return resp.Keys, nil } + +func getAPIKey(cloudAddr string, keyID uuid.UUID) (*cloudpb.APIKey, error) { + apiKeyMgr, ctxWithCreds, err := getAPIKeyClientAndContext(cloudAddr) + if err != nil { + return nil, err + } + + resp, err := apiKeyMgr.Get(ctxWithCreds, &cloudpb.GetAPIKeyRequest{ + ID: utils2.ProtoFromUUID(keyID), + }) + if err != nil { + return nil, err + } + + return resp.Key, nil +}