Skip to content

Commit

Permalink
feat(COR--981): add admin jwt command
Browse files Browse the repository at this point in the history
  • Loading branch information
pggb25 committed Nov 14, 2024
1 parent ebab710 commit db210f1
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

var (
jwtKid string
clusterId string
projectId string
lockReason string
Expand Down
28 changes: 28 additions & 0 deletions cmd/admin_jwt.go
Original file line number Diff line number Diff line change
@@ -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)
}
77 changes: 77 additions & 0 deletions cmd/admin_jwt_create.go
Original file line number Diff line number Diff line change
@@ -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()
}
56 changes: 56 additions & 0 deletions cmd/admin_jwt_delete.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
80 changes: 80 additions & 0 deletions cmd/admin_jwt_list.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit db210f1

Please sign in to comment.