From db210f1832c6e572cc63240c8fcae18746d5af28 Mon Sep 17 00:00:00 2001 From: Pierre Gerbelot Date: Fri, 8 Nov 2024 17:32:44 +0100 Subject: [PATCH] feat(COR--981): add admin jwt command --- cmd/admin.go | 1 + cmd/admin_jwt.go | 28 +++++++++++++++ cmd/admin_jwt_create.go | 77 +++++++++++++++++++++++++++++++++++++++ cmd/admin_jwt_delete.go | 56 +++++++++++++++++++++++++++++ cmd/admin_jwt_list.go | 80 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 242 insertions(+) create mode 100644 cmd/admin_jwt.go create mode 100644 cmd/admin_jwt_create.go create mode 100644 cmd/admin_jwt_delete.go create mode 100644 cmd/admin_jwt_list.go diff --git a/cmd/admin.go b/cmd/admin.go index b63029b2..867838ba 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -5,6 +5,7 @@ import ( ) var ( + jwtKid string clusterId string projectId string lockReason string diff --git a/cmd/admin_jwt.go b/cmd/admin_jwt.go new file mode 100644 index 00000000..ef92aba7 --- /dev/null +++ b/cmd/admin_jwt.go @@ -0,0 +1,28 @@ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var ( + adminJwtCmd = &cobra.Command{ + Use: "jwt", + Short: "Manage clusters", + Run: func(cmd *cobra.Command, args []string) { + utils.Capture(cmd) + + if len(args) == 0 { + _ = cmd.Help() + os.Exit(0) + } + }, + } +) + +func init() { + adminCmd.AddCommand(adminJwtCmd) +} diff --git a/cmd/admin_jwt_create.go b/cmd/admin_jwt_create.go new file mode 100644 index 00000000..8392d84e --- /dev/null +++ b/cmd/admin_jwt_create.go @@ -0,0 +1,77 @@ +package cmd + +import ( + "bytes" + "fmt" + "github.com/go-jose/go-jose/v4/json" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "io" + "net/http" + "os" + "text/tabwriter" + + "github.com/qovery/qovery-cli/utils" +) + +var ( + adminJwtCreateCmd = &cobra.Command{ + Use: "create", + Short: "Create a Jwt for a cluster", + Run: func(cmd *cobra.Command, args []string) { + createJwt() + }, + } +) + +func init() { + adminJwtCreateCmd.Flags().StringVarP(&clusterId, "cluster", "c", "", "Cluster's id") + + adminJwtCmd.AddCommand(adminJwtCreateCmd) + +} + +func createJwt() { + utils.CheckAdminUrl() + + tokenType, token, err := utils.GetAccessToken() + if err != nil { + utils.PrintlnError(err) + os.Exit(0) + } + + url := fmt.Sprintf("%s/clusters/%s/jwts", utils.AdminUrl, clusterId) + req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte("{ }"))) + if err != nil { + log.Fatal(err) + } + req.Header.Set("Authorization", utils.GetAuthorizationHeaderValue(tokenType, token)) + req.Header.Set("Content-Type", "application/json") + + res, err := http.DefaultClient.Do(req) + if err != nil { + log.Fatal(err) + } + + body, _ := io.ReadAll(res.Body) + if res.StatusCode != http.StatusOK { + utils.PrintlnError(fmt.Errorf("error uploading debug logs: %s %s", res.Status, body)) + return + } + + jwt := struct { + KeyId string `json:"key_id"` + ClusterId string `json:"cluster_id"` + CreatedAt string `json:"created_at"` + }{} + + if err := json.Unmarshal(body, &jwt); err != nil { + log.Fatal(err) + } + + w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0) + format := "%s\t | %s\t | %s\t | %s\n" + fmt.Fprintf(w, format, "", "cluster_id", "key_id", "created_at") + fmt.Fprintf(w, format, fmt.Sprintf("%d", 1), jwt.ClusterId, jwt.KeyId, jwt.CreatedAt) + w.Flush() +} diff --git a/cmd/admin_jwt_delete.go b/cmd/admin_jwt_delete.go new file mode 100644 index 00000000..1f376e77 --- /dev/null +++ b/cmd/admin_jwt_delete.go @@ -0,0 +1,56 @@ +package cmd + +import ( + "fmt" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "net/http" + "os" + + "github.com/qovery/qovery-cli/utils" +) + +var ( + adminJwtDeleteCmd = &cobra.Command{ + Use: "delete", + Short: "Delete a Jwt", + Run: func(cmd *cobra.Command, args []string) { + deleteJwt() + }, + } +) + +func init() { + adminJwtDeleteCmd.Flags().StringVarP(&jwtKid, "kid", "", "", "Cluster's id") + + adminJwtCmd.AddCommand(adminJwtDeleteCmd) + +} + +func deleteJwt() { + utils.CheckAdminUrl() + + tokenType, token, err := utils.GetAccessToken() + if err != nil { + utils.PrintlnError(err) + os.Exit(0) + } + + url := fmt.Sprintf("%s/clusters/jwts/%s", utils.AdminUrl, jwtKid) + req, err := http.NewRequest(http.MethodDelete, url, nil) + if err != nil { + log.Fatal(err) + } + req.Header.Set("Authorization", utils.GetAuthorizationHeaderValue(tokenType, token)) + req.Header.Set("Content-Type", "application/json") + + res, err := http.DefaultClient.Do(req) + if res.StatusCode != http.StatusNoContent { + utils.PrintlnError(fmt.Errorf("error: %s", res.Status)) + return + } + + if err != nil { + log.Fatal(err) + } +} diff --git a/cmd/admin_jwt_list.go b/cmd/admin_jwt_list.go new file mode 100644 index 00000000..0aec9b43 --- /dev/null +++ b/cmd/admin_jwt_list.go @@ -0,0 +1,80 @@ +package cmd + +import ( + "encoding/json" + "fmt" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "io" + "net/http" + "os" + "text/tabwriter" + + "github.com/qovery/qovery-cli/utils" +) + +var ( + adminJwtListCmd = &cobra.Command{ + Use: "list", + Short: "List Jwt of a cluster", + Run: func(cmd *cobra.Command, args []string) { + listJwts() + }, + } +) + +func init() { + adminJwtListCmd.Flags().StringVarP(&clusterId, "cluster", "c", "", "Cluster's id") + + adminJwtCmd.AddCommand(adminJwtListCmd) + +} + +func listJwts() { + utils.CheckAdminUrl() + + tokenType, token, err := utils.GetAccessToken() + if err != nil { + utils.PrintlnError(err) + os.Exit(0) + } + + url := fmt.Sprintf("%s/clusters/%s/jwts", utils.AdminUrl, clusterId) + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + log.Fatal(err) + } + req.Header.Set("Authorization", utils.GetAuthorizationHeaderValue(tokenType, token)) + req.Header.Set("Content-Type", "application/json") + + res, err := http.DefaultClient.Do(req) + if err != nil { + log.Fatal(err) + } + + body, _ := io.ReadAll(res.Body) + if res.StatusCode != http.StatusOK { + utils.PrintlnError(fmt.Errorf("error uploading debug logs: %s %s", res.Status, body)) + return + } + + resp := struct { + Results []struct { + ClusterId string `json:"cluster_id"` + KeyId string `json:"key_id"` + CreatedAt string `json:"created_at"` + } `json:"results"` + }{} + + if err := json.Unmarshal(body, &resp); err != nil { + log.Fatal(err) + } + + w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0) + format := "%s\t | %s\t | %s\t | %s\n" + fmt.Fprintf(w, format, "", "cluster_id", "key_id", "created_at") + for idx, jwt := range resp.Results { + fmt.Fprintf(w, format, fmt.Sprintf("%d", idx+1), jwt.ClusterId, jwt.KeyId, jwt.CreatedAt) + } + w.Flush() +}