From aab92ce88dfc417ad5745d8cbf2a4fcc3ca02981 Mon Sep 17 00:00:00 2001 From: Anish Mukherjee Date: Fri, 18 Nov 2022 19:24:35 +0530 Subject: [PATCH] add access key subcommand --- cli/cmd/keys/create.go | 38 +++++++++++++++++++++++++++++++++ cli/cmd/keys/delete.go | 24 +++++++++++++++++++++ cli/cmd/keys/list.go | 20 +++++++++++++++++ cli/cmd/keys/root.go | 37 ++++++++++++++++++++++++++++++++ cli/cmd/network/refresh_keys.go | 20 +++++++++++++++++ cli/cmd/root.go | 2 ++ cli/functions/http_client.go | 3 ++- cli/functions/keys.go | 23 ++++++++++++++++++++ cli/functions/network.go | 5 +++++ 9 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 cli/cmd/keys/create.go create mode 100644 cli/cmd/keys/delete.go create mode 100644 cli/cmd/keys/list.go create mode 100644 cli/cmd/keys/root.go create mode 100644 cli/cmd/network/refresh_keys.go create mode 100644 cli/functions/keys.go diff --git a/cli/cmd/keys/create.go b/cli/cmd/keys/create.go new file mode 100644 index 00000000..bcdca4b6 --- /dev/null +++ b/cli/cmd/keys/create.go @@ -0,0 +1,38 @@ +package keys + +import ( + "fmt" + "log" + "strconv" + + "github.com/gravitl/netmaker/cli/functions" + "github.com/gravitl/netmaker/models" + "github.com/spf13/cobra" +) + +const FlagKeyName = "name" + +var keyName string + +var keysCreateCmd = &cobra.Command{ + Use: fmt.Sprintf("create [NETWORK NAME] [NUM USES] [--%s=test_key]", FlagKeyName), + Args: cobra.ExactArgs(2), + Short: "Create an access key", + Long: `Create an access key`, + Run: func(cmd *cobra.Command, args []string) { + keyUses, err := strconv.ParseInt(args[1], 10, 64) + if err != nil { + log.Fatal(err) + } + key := &models.AccessKey{Uses: int(keyUses)} + if keyName != "" { + key.Name = keyName + } + functions.PrettyPrint(functions.CreateKey(args[0], key)) + }, +} + +func init() { + keysCreateCmd.Flags().StringVar(&keyName, FlagKeyName, "", "Name of the key") + rootCmd.AddCommand(keysCreateCmd) +} diff --git a/cli/cmd/keys/delete.go b/cli/cmd/keys/delete.go new file mode 100644 index 00000000..d7c0a8d5 --- /dev/null +++ b/cli/cmd/keys/delete.go @@ -0,0 +1,24 @@ +package keys + +import ( + "fmt" + + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var keysDeleteCmd = &cobra.Command{ + Use: "delete [NETWORK NAME] [KEY NAME]", + Args: cobra.ExactArgs(2), + Short: "Delete a key", + Long: `Delete a key`, + Run: func(cmd *cobra.Command, args []string) { + if functions.DeleteKey(args[0], args[1]) == nil { + fmt.Println("Success") + } + }, +} + +func init() { + rootCmd.AddCommand(keysDeleteCmd) +} diff --git a/cli/cmd/keys/list.go b/cli/cmd/keys/list.go new file mode 100644 index 00000000..c1550017 --- /dev/null +++ b/cli/cmd/keys/list.go @@ -0,0 +1,20 @@ +package keys + +import ( + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var keysListCmd = &cobra.Command{ + Use: "list [NETWORK NAME]", + Args: cobra.ExactArgs(1), + Short: "List all keys associated with a network", + Long: `List all keys associated with a network`, + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint(functions.GetKeys(args[0])) + }, +} + +func init() { + rootCmd.AddCommand(keysListCmd) +} diff --git a/cli/cmd/keys/root.go b/cli/cmd/keys/root.go new file mode 100644 index 00000000..add2610d --- /dev/null +++ b/cli/cmd/keys/root.go @@ -0,0 +1,37 @@ +package keys + +import ( + "os" + + "github.com/spf13/cobra" +) + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "keys", + Short: "Manage access keys associated with a network", + Long: `Manage access keys associated with a network`, + // Run: func(cmd *cobra.Command, args []string) { }, +} + +func GetRoot() *cobra.Command { + return rootCmd +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application. + // Cobra also supports local flags, which will only run + // when this action is called directly. + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cli/cmd/network/refresh_keys.go b/cli/cmd/network/refresh_keys.go new file mode 100644 index 00000000..c7c5f686 --- /dev/null +++ b/cli/cmd/network/refresh_keys.go @@ -0,0 +1,20 @@ +package network + +import ( + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var networkRefreshKeysCmd = &cobra.Command{ + Use: "refresh_keys [NAME]", + Short: "Refresh public and private key pairs of a network", + Long: `Refresh public and private key pairs of a network`, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint(functions.RefreshKeys(args[0])) + }, +} + +func init() { + rootCmd.AddCommand(networkRefreshKeysCmd) +} diff --git a/cli/cmd/root.go b/cli/cmd/root.go index d1cc930d..ea8d511f 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -4,6 +4,7 @@ import ( "os" "github.com/gravitl/netmaker/cli/cmd/context" + "github.com/gravitl/netmaker/cli/cmd/keys" "github.com/gravitl/netmaker/cli/cmd/network" "github.com/spf13/cobra" ) @@ -45,4 +46,5 @@ func init() { // IMP: Bind subcommands here rootCmd.AddCommand(network.GetRoot()) rootCmd.AddCommand(context.GetRoot()) + rootCmd.AddCommand(keys.GetRoot()) } diff --git a/cli/functions/http_client.go b/cli/functions/http_client.go index 185dcb73..f466d74b 100644 --- a/cli/functions/http_client.go +++ b/cli/functions/http_client.go @@ -75,7 +75,8 @@ func request[T any](method, route string, payload any) *T { } body := new(T) if err := json.Unmarshal(resBodyBytes, body); err != nil { - log.Fatalf("Error unmarshalling JSON: %s", err) + // log.Printf("Error unmarshalling JSON: %s", err) + return nil } return body } diff --git a/cli/functions/keys.go b/cli/functions/keys.go new file mode 100644 index 00000000..5b2bc330 --- /dev/null +++ b/cli/functions/keys.go @@ -0,0 +1,23 @@ +package functions + +import ( + "fmt" + "net/http" + + "github.com/gravitl/netmaker/models" +) + +// GetKeys - fetch all access keys of a network +func GetKeys(networkName string) *[]models.AccessKey { + return request[[]models.AccessKey](http.MethodGet, fmt.Sprintf("/api/networks/%s/keys", networkName), nil) +} + +// CreateKey - create an access key +func CreateKey(networkName string, key *models.AccessKey) *models.AccessKey { + return request[models.AccessKey](http.MethodPost, fmt.Sprintf("/api/networks/%s/keys", networkName), key) +} + +// DeleteKey - delete an access key +func DeleteKey(networkName, keyName string) *string { + return request[string](http.MethodDelete, fmt.Sprintf("/api/networks/%s/keys/%s", networkName, keyName), nil) +} diff --git a/cli/functions/network.go b/cli/functions/network.go index 3d2f0ef2..ae74aa64 100644 --- a/cli/functions/network.go +++ b/cli/functions/network.go @@ -38,3 +38,8 @@ func GetNetwork(name string) *models.Network { func DeleteNetwork(name string) *string { return request[string](http.MethodDelete, "/api/networks/"+name, nil) } + +// RefreshKeys - refresh public and private key pairs for a network +func RefreshKeys(networkName string) *models.Network { + return request[models.Network](http.MethodPost, fmt.Sprintf("/api/networks/%s/keyupdate", networkName), nil) +}