chore: made changes to build and fix issues

This commit is contained in:
David Allen 2025-06-01 14:41:06 -06:00
parent adec553c1b
commit 95f0cd41e6
Signed by: towk
GPG key ID: 0430CDBE22619155
6 changed files with 91 additions and 44 deletions

View file

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"encoding/json"
"fmt" "fmt"
"os" "os"
"strings" "strings"
@ -19,7 +20,10 @@ import (
var ( var (
showCache bool showCache bool
showCacheInfo bool
listOutputFormat string listOutputFormat string
listUsername string
listPassword string
) )
// The `list` command provides an easy way to show what was found // The `list` command provides an easy way to show what was found
@ -40,7 +44,7 @@ var ListCmd = &cobra.Command{
if showCacheInfo { if showCacheInfo {
magellan.PrintMapWithFormat(map[string]any{ magellan.PrintMapWithFormat(map[string]any{
"path": cachePath, "path": cachePath,
}, format) }, listOutputFormat)
os.Exit(0) os.Exit(0)
} }
@ -69,9 +73,10 @@ var ListCmd = &cobra.Command{
default: default:
log.Error().Msg("unrecognized format") log.Error().Msg("unrecognized format")
os.Exit(1) os.Exit(1)
}
// print cache data in specified format // print cache data in specified format
magellan.PrintRemoteAssets(scannedResults, format) magellan.PrintRemoteAssets(scannedResults, listOutputFormat)
}, },
} }
@ -113,7 +118,7 @@ var listDrivesCmd = &cobra.Command{
} }
func init() { func init() {
ListCmd.Flags().StringVarP(&format, "format", "F", "none", "Set the output format (list|json|yaml)") ListCmd.Flags().StringVarP(&listOutputFormat, "format", "F", "none", "Set the output format (list|json|yaml)")
ListCmd.Flags().BoolVar(&showCacheInfo, "cache-info", false, "Alias for 'magellan cache info'") ListCmd.Flags().BoolVar(&showCacheInfo, "cache-info", false, "Alias for 'magellan cache info'")
listDrivesCmd.Flags().StringVarP(&listUsername, "username", "u", "", "Set the username for BMC login") listDrivesCmd.Flags().StringVarP(&listUsername, "username", "u", "", "Set the username for BMC login")

View file

@ -39,6 +39,7 @@ const (
var ( var (
currentUser *user.User currentUser *user.User
accessToken string accessToken string
accessTokenPath string
timeout int timeout int
concurrency int concurrency int
ports []int ports []int
@ -137,7 +138,6 @@ func SetDefaults() {
viper.SetDefault("scan.subnet-masks", []net.IP{}) viper.SetDefault("scan.subnet-masks", []net.IP{})
viper.SetDefault("scan.disable-probing", false) viper.SetDefault("scan.disable-probing", false)
viper.SetDefault("scan.disable-cache", false) viper.SetDefault("scan.disable-cache", false)
viper.SetDefault("collect.host", host)
viper.SetDefault("collect.username", "") viper.SetDefault("collect.username", "")
viper.SetDefault("collect.password", "") viper.SetDefault("collect.password", "")
viper.SetDefault("collect.protocol", "tcp") viper.SetDefault("collect.protocol", "tcp")

View file

@ -7,7 +7,7 @@ import (
"os" "os"
"strings" "strings"
urlx "github.com/davidallendj/magellan/internal/url" urlx "github.com/davidallendj/magellan/internal/urlx"
"github.com/davidallendj/magellan/pkg/auth" "github.com/davidallendj/magellan/pkg/auth"
"github.com/davidallendj/magellan/pkg/client" "github.com/davidallendj/magellan/pkg/client"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -40,7 +40,7 @@ var sendCmd = &cobra.Command{
// try to load access token either from env var, file, or config if var not set // try to load access token either from env var, file, or config if var not set
if accessToken == "" { if accessToken == "" {
var err error var err error
accessToken, err = auth.LoadAccessToken(tokenPath) accessToken, err = auth.LoadAccessToken(accessTokenPath)
if err != nil && verbose { if err != nil && verbose {
log.Warn().Err(err).Msgf("could not load access token") log.Warn().Err(err).Msgf("could not load access token")
} else if debug && accessToken != "" { } else if debug && accessToken != "" {

View file

@ -18,6 +18,7 @@ var (
sessionID string sessionID string
sessionToken string sessionToken string
sessionTokenPath string sessionTokenPath string
sessionOutputFormat string
storeToken bool storeToken bool
) )
@ -128,7 +129,7 @@ var sessionLoginCmd = &cobra.Command{
} }
// print the session IDs for created sessions // print the session IDs for created sessions
switch format { switch sessionOutputFormat {
case "list": case "list":
for _, session := range newSessions { for _, session := range newSessions {
fmt.Println(session.ID) fmt.Println(session.ID)
@ -138,7 +139,7 @@ var sessionLoginCmd = &cobra.Command{
case "yaml": case "yaml":
util.PrintYAML(newSessions) util.PrintYAML(newSessions)
default: default:
log.Error().Msg("unrecognized output format") log.Error().Msg("unrecognized output sessionOutputFormat")
os.Exit(1) os.Exit(1)
} }
}, },
@ -225,7 +226,7 @@ var sessionLogoutCmd = &cobra.Command{
var sessionStatusCmd = &cobra.Command{ var sessionStatusCmd = &cobra.Command{
Use: "status", Use: "status",
Example: ` // show the host's session service status in YAML format Example: ` // show the host's session service status in YAML sessionOutputFormat
magellan sessions status https://172.21.0.2:5000 -u $bmc_username -p $bmc_password -i -F yaml`, magellan sessions status https://172.21.0.2:5000 -u $bmc_username -p $bmc_password -i -F yaml`,
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
Short: "Show the status of the session service", Short: "Show the status of the session service",
@ -239,8 +240,8 @@ var sessionStatusCmd = &cobra.Command{
os.Exit(1) os.Exit(1)
} }
// show the service status in given format // show the service status in given sessionOutputFormat
switch format { switch sessionOutputFormat {
case FORMAT_LIST: case FORMAT_LIST:
fmt.Printf("%s: %v (%d secs)", host, status.Enabled, status.Timeout) fmt.Printf("%s: %v (%d secs)", host, status.Enabled, status.Timeout)
case FORMAT_JSON: case FORMAT_JSON:
@ -274,7 +275,7 @@ var sessionListCmd = &cobra.Command{
continue continue
} }
switch format { switch sessionOutputFormat {
case FORMAT_LIST: case FORMAT_LIST:
fallthrough fallthrough
case FORMAT_JSON: case FORMAT_JSON:
@ -282,7 +283,7 @@ var sessionListCmd = &cobra.Command{
case FORMAT_YAML: case FORMAT_YAML:
util.PrintYAML(session) util.PrintYAML(session)
default: default:
log.Error().Msg("unrecognized output format") log.Error().Msg("unrecognized output sessionOutputFormat")
os.Exit(1) os.Exit(1)
} }
@ -302,7 +303,7 @@ var sessionListCmd = &cobra.Command{
continue continue
} }
switch format { switch sessionOutputFormat {
case FORMAT_LIST: case FORMAT_LIST:
fmt.Println(strings.Join(sessionIDs, "\n")) fmt.Println(strings.Join(sessionIDs, "\n"))
case FORMAT_JSON: case FORMAT_JSON:
@ -310,7 +311,7 @@ var sessionListCmd = &cobra.Command{
case FORMAT_YAML: case FORMAT_YAML:
util.PrintYAML(sessionIDs) util.PrintYAML(sessionIDs)
default: default:
log.Error().Msg("unrecognized output format") log.Error().Msg("unrecognized output sessionOutputFormat")
os.Exit(1) os.Exit(1)
} }
} }
@ -338,7 +339,7 @@ func init() {
sessionCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "Set the password for BMC login") sessionCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "Set the password for BMC login")
sessionCmd.PersistentFlags().StringVar(&sessionTokenPath, "session-token-file", "", "Set the session token from a file") sessionCmd.PersistentFlags().StringVar(&sessionTokenPath, "session-token-file", "", "Set the session token from a file")
sessionCmd.PersistentFlags().StringVar(&sessionToken, "session-token", "", "Set the session token") sessionCmd.PersistentFlags().StringVar(&sessionToken, "session-token", "", "Set the session token")
sessionCmd.PersistentFlags().StringVarP(&format, "format", "F", "list", "Set the output format (list|json|yaml)") sessionCmd.PersistentFlags().StringVarP(&sessionOutputFormat, "sessionOutputFormat", "F", "list", "Set the output sessionOutputFormat (list|json|yaml)")
sessionCmd.PersistentFlags().StringVarP(&secretsFile, "secrets-file", "f", "secrets.json", "Set the path to secrets store file to store credentials") sessionCmd.PersistentFlags().StringVarP(&secretsFile, "secrets-file", "f", "secrets.json", "Set the path to secrets store file to store credentials")
sessionCmd.MarkFlagRequired("username") sessionCmd.MarkFlagRequired("username")

47
internal/util/bmc.go Normal file
View file

@ -0,0 +1,47 @@
package util
import (
"github.com/davidallendj/magellan/pkg/bmc"
"github.com/davidallendj/magellan/pkg/secrets"
"github.com/rs/zerolog/log"
)
func GetBMCCredentials(store secrets.SecretStore, id string) bmc.BMCCredentials {
var (
creds bmc.BMCCredentials
err error
)
if id == "" {
log.Error().Msg("failed to get BMC credentials: id was empty")
return creds
}
if id == secrets.DEFAULT_KEY {
log.Info().Msg("fetching default credentials")
if creds, err = bmc.GetBMCCredentialsDefault(store); err != nil {
log.Warn().Err(err).Msg("failed to get default credentials")
} else {
log.Info().Msg("default credentials found, using")
}
return creds
}
if creds, err = bmc.GetBMCCredentials(store, id); err != nil {
// Specific credentials for URI not found, fetch default.
log.Warn().Str("id", id).Msg("specific credentials not found, falling back to default")
if defaultSecret, err := bmc.GetBMCCredentialsDefault(store); err != nil {
// We've exhausted all options, the credentials will be blank unless
// overridden by a CLI flag.
log.Warn().Str("id", id).Err(err).Msg("no default credentials were set, they will be blank unless overridden by CLI flags")
} else {
// Default credentials found, use them.
log.Info().Str("id", id).Msg("default credentials found, using")
creds = defaultSecret
}
} else {
log.Info().Str("id", id).Msg("specific credentials found, using")
}
return creds
}

View file

@ -51,7 +51,7 @@ type CollectParams struct {
// //
// Requests can be made to several of the nodes using a goroutine by setting the q.Concurrency // Requests can be made to several of the nodes using a goroutine by setting the q.Concurrency
// property value between 1 and 10000. // property value between 1 and 10000.
func CollectInventory(assets *[]RemoteAsset, params *CollectParams, store secrets.SecretStore) ([]map[string]any, error) { func CollectInventory(assets *[]RemoteAsset, params *CollectParams) ([]map[string]any, error) {
// check for available remote assets found from scan // check for available remote assets found from scan
if assets == nil { if assets == nil {
return nil, fmt.Errorf("no assets found") return nil, fmt.Errorf("no assets found")
@ -134,12 +134,6 @@ func CollectInventory(assets *[]RemoteAsset, params *CollectParams, store secret
log.Warn().Str("id", config.URI).Msg("username will be blank") log.Warn().Str("id", config.URI).Msg("username will be blank")
} }
// get BMC username to send
bmcCreds, err := bmc.GetBMCCredentials(params.SecretStore, config.URI)
if err != nil {
log.Error().Str("id", config.URI).Msg("username will be blank")
}
// data to be sent to smd // data to be sent to smd
data := map[string]any{ data := map[string]any{
"ID": fmt.Sprintf("%v", node.String()[:len(node.String())-2]), "ID": fmt.Sprintf("%v", node.String()[:len(node.String())-2]),