mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 03:27:03 -07:00
Added checks for access token when running command
This commit is contained in:
parent
7760857ae5
commit
09c5af44b9
3 changed files with 38 additions and 10 deletions
|
|
@ -27,6 +27,16 @@ var collectCmd = &cobra.Command{
|
||||||
l.Log.Errorf("could not get states: %v", err)
|
l.Log.Errorf("could not get states: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// try to load access token either from env var, file, or config if var not set
|
||||||
|
if accessToken == "" {
|
||||||
|
var err error
|
||||||
|
accessToken, err = LoadAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
l.Log.Errorf("failed to load access token: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
if threads <= 0 {
|
if threads <= 0 {
|
||||||
threads = mathutil.Clamp(len(probeStates), 1, 255)
|
threads = mathutil.Clamp(len(probeStates), 1, 255)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
20
cmd/login.go
20
cmd/login.go
|
|
@ -7,7 +7,9 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
magellan "github.com/OpenCHAMI/magellan/internal"
|
magellan "github.com/OpenCHAMI/magellan/internal"
|
||||||
|
"github.com/OpenCHAMI/magellan/internal/log"
|
||||||
"github.com/lestrrat-go/jwx/jwt"
|
"github.com/lestrrat-go/jwx/jwt"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -25,21 +27,19 @@ var loginCmd = &cobra.Command{
|
||||||
Short: "Log in with identity provider for access token",
|
Short: "Log in with identity provider for access token",
|
||||||
Long: "",
|
Long: "",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
// make application logger
|
||||||
|
l := log.NewLogger(logrus.New(), logrus.DebugLevel)
|
||||||
|
|
||||||
// check if we have a valid JWT before starting login
|
// check if we have a valid JWT before starting login
|
||||||
if !forceLogin {
|
if !forceLogin {
|
||||||
// try getting the access token from env var
|
// try getting the access token from env var
|
||||||
testToken := []byte(os.Getenv("OCHAMI_ACCESS_TOKEN"))
|
testToken, err := LoadAccessToken()
|
||||||
if testToken == nil {
|
if err != nil {
|
||||||
// try reading access token from a file
|
l.Log.Errorf("failed to load access token: %v", err)
|
||||||
b, err := os.ReadFile(tokenPath)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("failed to read access token from file: %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
testToken = b
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse into jwt.Token to validate
|
// parse into jwt.Token to validate
|
||||||
token, err := jwt.Parse(testToken)
|
token, err := jwt.Parse([]byte(testToken))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("failed to parse access token contents: %v\n", err)
|
fmt.Printf("failed to parse access token contents: %v\n", err)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
18
cmd/root.go
18
cmd/root.go
|
|
@ -50,9 +50,27 @@ func Execute() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LoadAccessToken() (string, error) {
|
||||||
|
// try to load token from env var
|
||||||
|
testToken := os.Getenv("OCHAMI_ACCESS_TOKEN")
|
||||||
|
if testToken != "" {
|
||||||
|
return testToken, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// try reading access token from a file
|
||||||
|
b, err := os.ReadFile(tokenPath)
|
||||||
|
if err == nil {
|
||||||
|
return string(b), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: try to load token from config
|
||||||
|
return "", fmt.Errorf("could not load from environment variable or file")
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.PersistentFlags().IntVar(&threads, "threads", -1, "set the number of threads")
|
rootCmd.PersistentFlags().IntVar(&threads, "threads", -1, "set the number of threads")
|
||||||
rootCmd.PersistentFlags().IntVar(&timeout, "timeout", 30, "set the timeout")
|
rootCmd.PersistentFlags().IntVar(&timeout, "timeout", 30, "set the timeout")
|
||||||
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", true, "set verbose flag")
|
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", true, "set verbose flag")
|
||||||
|
rootCmd.PersistentFlags().StringVar(&accessToken, "access-token", "", "set the access token")
|
||||||
rootCmd.PersistentFlags().StringVar(&dbpath, "db.path", "/tmp/magellan/magellan.db", "set the probe storage path")
|
rootCmd.PersistentFlags().StringVar(&dbpath, "db.path", "/tmp/magellan/magellan.db", "set the probe storage path")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue