-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(COR--981): add admin jwt command
- Loading branch information
Showing
5 changed files
with
242 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
) | ||
|
||
var ( | ||
jwtKid string | ||
clusterId string | ||
projectId string | ||
lockReason string | ||
|
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,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) | ||
} |
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,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() | ||
} |
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,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) | ||
} | ||
} |
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,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() | ||
} |