Merge branch 'main' into config-file

This commit is contained in:
David J. Allen 2024-05-07 14:57:34 -06:00
commit 2b421e8af5
No known key found for this signature in database
GPG key ID: 717C593FF60A2ACC
24 changed files with 618 additions and 298 deletions

View file

@ -1,10 +1,10 @@
package cmd
import (
magellan "github.com/bikeshack/magellan/internal"
"github.com/bikeshack/magellan/internal/api/smd"
"github.com/bikeshack/magellan/internal/db/sqlite"
"github.com/bikeshack/magellan/internal/log"
magellan "github.com/OpenCHAMI/magellan/internal"
"github.com/OpenCHAMI/magellan/internal/api/smd"
"github.com/OpenCHAMI/magellan/internal/db/sqlite"
"github.com/OpenCHAMI/magellan/internal/log"
"github.com/cznic/mathutil"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -28,21 +28,31 @@ var collectCmd = &cobra.Command{
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 {
threads = mathutil.Clamp(len(probeStates), 1, 255)
}
q := &magellan.QueryParams{
User: user,
Pass: pass,
Protocol: protocol,
Drivers: drivers,
Preferred: preferredDriver,
Timeout: timeout,
Threads: threads,
Verbose: verbose,
WithSecureTLS: withSecureTLS,
OutputPath: outputPath,
ForceUpdate: forceUpdate,
User: user,
Pass: pass,
Protocol: protocol,
Drivers: drivers,
Preferred: preferredDriver,
Timeout: timeout,
Threads: threads,
Verbose: verbose,
CaCertPath: cacertPath,
OutputPath: outputPath,
ForceUpdate: forceUpdate,
}
magellan.CollectAll(&probeStates, l, q)
@ -81,6 +91,5 @@ func init() {
viper.BindPFlag("collect.secure-tls", collectCmd.Flags().Lookup("secure-tls"))
viper.BindPFlag("collect.cert-pool", collectCmd.Flags().Lookup("cert-pool"))
rootCmd.AddCommand(collectCmd)
}

View file

@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"github.com/bikeshack/magellan/internal/db/sqlite"
"github.com/OpenCHAMI/magellan/internal/db/sqlite"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

84
cmd/login.go Normal file
View file

@ -0,0 +1,84 @@
package cmd
import (
"errors"
"fmt"
"net/http"
"os"
magellan "github.com/OpenCHAMI/magellan/internal"
"github.com/OpenCHAMI/magellan/internal/log"
"github.com/lestrrat-go/jwx/jwt"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
loginUrl string
targetHost string
targetPort int
tokenPath string
forceLogin bool
noBrowser bool
)
var loginCmd = &cobra.Command{
Use: "login",
Short: "Log in with identity provider for access token",
Long: "",
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
if !forceLogin {
// try getting the access token from env var
testToken, err := LoadAccessToken()
if err != nil {
l.Log.Errorf("failed to load access token: %v", err)
}
// parse into jwt.Token to validate
token, err := jwt.Parse([]byte(testToken))
if err != nil {
fmt.Printf("failed to parse access token contents: %v\n", err)
return
}
// check if the token is invalid and we need a new one
err = jwt.Validate(token)
if err != nil {
fmt.Printf("failed to validate access token...fetching a new one")
} else {
fmt.Printf("found a valid token...skipping login (use the '-f/--force' flag to login anyway)")
return
}
}
// start the login flow
var err error
accessToken, err = magellan.Login(loginUrl, targetHost, targetPort)
if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("\n=========================================\nServer closed.\n=========================================\n\n")
} else if err != nil {
fmt.Printf("failed to start server: %v\n", err)
}
// if we got a new token successfully, save it to the token path
if accessToken != "" && tokenPath != "" {
err := os.WriteFile(tokenPath, []byte(accessToken), os.ModePerm)
if err != nil {
fmt.Printf("failed to write access token to file: %v\n", err)
}
}
},
}
func init() {
loginCmd.Flags().StringVar(&loginUrl, "url", "http://127.0.0.1:3333/login", "set the login URL")
loginCmd.Flags().StringVar(&targetHost, "target-host", "127.0.0.1", "set the target host to return the access code")
loginCmd.Flags().IntVar(&targetPort, "target-port", 5000, "set the target host to return the access code")
loginCmd.Flags().BoolVarP(&forceLogin, "force", "f", false, "start the login process even with a valid token")
loginCmd.Flags().StringVar(&tokenPath, "token-path", ".ochami-token", "set the path the load/save the access token")
loginCmd.Flags().BoolVar(&noBrowser, "no-browser", false, "prevent the default browser from being opened automatically")
rootCmd.AddCommand(loginCmd)
}

View file

@ -12,21 +12,21 @@ import (
)
var (
accessToken string
timeout int
threads int
ports []int
hosts []string
protocol string
withSecureTLS bool
certPoolFile string
cacertPath string
user string
pass string
dbpath string
drivers []string
preferredDriver string
ipmitoolPath string
outputPath string
configPath string
outputPath string
configPath string
verbose bool
)
@ -54,14 +54,32 @@ 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() {
cobra.OnInitialize(InitializeConfig)
rootCmd.PersistentFlags().IntVar(&threads, "threads", -1, "set the number of threads")
rootCmd.PersistentFlags().IntVar(&timeout, "timeout", 30, "set the timeout")
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "set the config file path")
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")
// bind viper config flags with cobra
viper.BindPFlag("threads", rootCmd.Flags().Lookup("threads"))
viper.BindPFlag("timeout", rootCmd.Flags().Lookup("timeout"))
@ -112,4 +130,4 @@ func SetDefaults() {
viper.SetDefault("secure-tls", false)
viper.SetDefault("status", false)
}
}

View file

@ -6,8 +6,8 @@ import (
"os"
"path"
magellan "github.com/bikeshack/magellan/internal"
"github.com/bikeshack/magellan/internal/db/sqlite"
magellan "github.com/OpenCHAMI/magellan/internal"
"github.com/OpenCHAMI/magellan/internal/db/sqlite"
"github.com/cznic/mathutil"
"github.com/spf13/cobra"
@ -15,10 +15,10 @@ import (
)
var (
begin uint8
end uint8
subnets []string
subnetMasks []net.IP
begin uint8
end uint8
subnets []string
subnetMasks []net.IP
disableProbing bool
)
@ -37,10 +37,10 @@ var scanCmd = &cobra.Command{
return
}
if len(subnetMasks) < i + 1 {
if len(subnetMasks) < i+1 {
subnetMasks = append(subnetMasks, net.IP{255, 255, 255, 0})
}
hostsToScan = append(hostsToScan, magellan.GenerateHosts(subnet, &subnetMasks[i])...)
}
}

View file

@ -1,46 +1,45 @@
package cmd
import (
magellan "github.com/bikeshack/magellan/internal"
"github.com/bikeshack/magellan/internal/log"
magellan "github.com/OpenCHAMI/magellan/internal"
"github.com/OpenCHAMI/magellan/internal/log"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
host string
port int
firmwareUrl string
firmwareVersion string
component string
host string
port int
firmwareUrl string
firmwareVersion string
component string
transferProtocol string
status bool
status bool
)
var updateCmd = &cobra.Command{
Use: "update",
Use: "update",
Short: "Update BMC node firmware",
Run: func(cmd *cobra.Command, args []string) {
l := log.NewLogger(logrus.New(), logrus.DebugLevel)
q := &magellan.UpdateParams {
FirmwarePath: firmwareUrl,
FirmwareVersion: firmwareVersion,
Component: component,
q := &magellan.UpdateParams{
FirmwarePath: firmwareUrl,
FirmwareVersion: firmwareVersion,
Component: component,
TransferProtocol: transferProtocol,
QueryParams: magellan.QueryParams{
Drivers: []string{"redfish"},
Drivers: []string{"redfish"},
Preferred: "redfish",
Protocol: protocol,
Host: host,
User: user,
Pass: pass,
Timeout: timeout,
Port: port,
WithSecureTLS: withSecureTLS,
Protocol: protocol,
Host: host,
User: user,
Pass: pass,
Timeout: timeout,
Port: port,
},
}
// check if required params are set
if host == "" || user == "" || pass == "" {
l.Log.Fatal("requires host, user, and pass to be set")
@ -54,7 +53,7 @@ var updateCmd = &cobra.Command{
}
return
}
// client, err := magellan.NewClient(l, &q.QueryParams)
// if err != nil {
// l.Log.Errorf("could not make client: %v", err)
@ -77,7 +76,6 @@ func init() {
updateCmd.Flags().StringVar(&firmwareUrl, "firmware-url", "", "set the path to the firmware")
updateCmd.Flags().StringVar(&firmwareVersion, "firmware-version", "", "set the version of firmware to be installed")
updateCmd.Flags().StringVar(&component, "component", "", "set the component to upgrade")
updateCmd.Flags().BoolVar(&withSecureTLS, "secure-tls", false, "enable secure TLS")
updateCmd.Flags().BoolVar(&status, "status", false, "get the status of the update")
viper.BindPFlag("bmc-host", updateCmd.Flags().Lookup("bmc-host"))
@ -93,4 +91,4 @@ func init() {
viper.BindPFlag("status", updateCmd.Flags().Lookup("status"))
rootCmd.AddCommand(updateCmd)
}
}