From 0117f0355ad8eb1ca26e33d0205017f399424696 Mon Sep 17 00:00:00 2001 From: David Allen Date: Sun, 1 Jun 2025 14:41:06 -0600 Subject: [PATCH] chore: made changes to build and fix issues --- cmd/list.go | 11 ++++++++--- cmd/root.go | 36 ++++++++++++++++----------------- cmd/send.go | 4 ++-- cmd/sessions.go | 29 ++++++++++++++------------- internal/util/bmc.go | 47 ++++++++++++++++++++++++++++++++++++++++++++ pkg/collect.go | 8 +------- 6 files changed, 91 insertions(+), 44 deletions(-) create mode 100644 internal/util/bmc.go diff --git a/cmd/list.go b/cmd/list.go index ab80f6e..e36ae43 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -1,6 +1,7 @@ package cmd import ( + "encoding/json" "fmt" "os" "strings" @@ -19,7 +20,10 @@ import ( var ( showCache bool + showCacheInfo bool listOutputFormat string + listUsername string + listPassword string ) // The `list` command provides an easy way to show what was found @@ -40,7 +44,7 @@ var ListCmd = &cobra.Command{ if showCacheInfo { magellan.PrintMapWithFormat(map[string]any{ "path": cachePath, - }, format) + }, listOutputFormat) os.Exit(0) } @@ -69,9 +73,10 @@ var ListCmd = &cobra.Command{ default: log.Error().Msg("unrecognized format") os.Exit(1) + } // print cache data in specified format - magellan.PrintRemoteAssets(scannedResults, format) + magellan.PrintRemoteAssets(scannedResults, listOutputFormat) }, } @@ -113,7 +118,7 @@ var listDrivesCmd = &cobra.Command{ } 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'") listDrivesCmd.Flags().StringVarP(&listUsername, "username", "u", "", "Set the username for BMC login") diff --git a/cmd/root.go b/cmd/root.go index 842c602..052b6c9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -37,23 +37,24 @@ const ( // CLI arguments as variables to not fiddle with error-prone strings var ( - currentUser *user.User - accessToken string - timeout int - concurrency int - ports []int - protocol string - cacertPath string - username string - password string - cachePath string - outputPath string - outputDir string - configPath string - verbose bool - debug bool - forceUpdate bool - insecure bool + currentUser *user.User + accessToken string + accessTokenPath string + timeout int + concurrency int + ports []int + protocol string + cacertPath string + username string + password string + cachePath string + outputPath string + outputDir string + configPath string + verbose bool + debug bool + forceUpdate bool + insecure bool ) // The `root` command doesn't do anything on it's own except display @@ -137,7 +138,6 @@ func SetDefaults() { viper.SetDefault("scan.subnet-masks", []net.IP{}) viper.SetDefault("scan.disable-probing", false) viper.SetDefault("scan.disable-cache", false) - viper.SetDefault("collect.host", host) viper.SetDefault("collect.username", "") viper.SetDefault("collect.password", "") viper.SetDefault("collect.protocol", "tcp") diff --git a/cmd/send.go b/cmd/send.go index ca1a0de..ec169cb 100644 --- a/cmd/send.go +++ b/cmd/send.go @@ -7,7 +7,7 @@ import ( "os" "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/client" "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 if accessToken == "" { var err error - accessToken, err = auth.LoadAccessToken(tokenPath) + accessToken, err = auth.LoadAccessToken(accessTokenPath) if err != nil && verbose { log.Warn().Err(err).Msgf("could not load access token") } else if debug && accessToken != "" { diff --git a/cmd/sessions.go b/cmd/sessions.go index 0a27d37..fb355a3 100644 --- a/cmd/sessions.go +++ b/cmd/sessions.go @@ -15,10 +15,11 @@ import ( ) var ( - sessionID string - sessionToken string - sessionTokenPath string - storeToken bool + sessionID string + sessionToken string + sessionTokenPath string + sessionOutputFormat string + storeToken bool ) var sessionCmd = &cobra.Command{ @@ -128,7 +129,7 @@ var sessionLoginCmd = &cobra.Command{ } // print the session IDs for created sessions - switch format { + switch sessionOutputFormat { case "list": for _, session := range newSessions { fmt.Println(session.ID) @@ -138,7 +139,7 @@ var sessionLoginCmd = &cobra.Command{ case "yaml": util.PrintYAML(newSessions) default: - log.Error().Msg("unrecognized output format") + log.Error().Msg("unrecognized output sessionOutputFormat") os.Exit(1) } }, @@ -225,7 +226,7 @@ var sessionLogoutCmd = &cobra.Command{ var sessionStatusCmd = &cobra.Command{ 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`, Args: cobra.MinimumNArgs(1), Short: "Show the status of the session service", @@ -239,8 +240,8 @@ var sessionStatusCmd = &cobra.Command{ os.Exit(1) } - // show the service status in given format - switch format { + // show the service status in given sessionOutputFormat + switch sessionOutputFormat { case FORMAT_LIST: fmt.Printf("%s: %v (%d secs)", host, status.Enabled, status.Timeout) case FORMAT_JSON: @@ -274,7 +275,7 @@ var sessionListCmd = &cobra.Command{ continue } - switch format { + switch sessionOutputFormat { case FORMAT_LIST: fallthrough case FORMAT_JSON: @@ -282,7 +283,7 @@ var sessionListCmd = &cobra.Command{ case FORMAT_YAML: util.PrintYAML(session) default: - log.Error().Msg("unrecognized output format") + log.Error().Msg("unrecognized output sessionOutputFormat") os.Exit(1) } @@ -302,7 +303,7 @@ var sessionListCmd = &cobra.Command{ continue } - switch format { + switch sessionOutputFormat { case FORMAT_LIST: fmt.Println(strings.Join(sessionIDs, "\n")) case FORMAT_JSON: @@ -310,7 +311,7 @@ var sessionListCmd = &cobra.Command{ case FORMAT_YAML: util.PrintYAML(sessionIDs) default: - log.Error().Msg("unrecognized output format") + log.Error().Msg("unrecognized output sessionOutputFormat") os.Exit(1) } } @@ -338,7 +339,7 @@ func init() { 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(&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.MarkFlagRequired("username") diff --git a/internal/util/bmc.go b/internal/util/bmc.go new file mode 100644 index 0000000..d2aaebf --- /dev/null +++ b/internal/util/bmc.go @@ -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 +} diff --git a/pkg/collect.go b/pkg/collect.go index dd4ff1a..9f459a6 100644 --- a/pkg/collect.go +++ b/pkg/collect.go @@ -51,7 +51,7 @@ type CollectParams struct { // // Requests can be made to several of the nodes using a goroutine by setting the q.Concurrency // 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 if assets == nil { 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") } - // 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 := map[string]any{ "ID": fmt.Sprintf("%v", node.String()[:len(node.String())-2]),