diff --git a/cmd/sessions.go b/cmd/sessions.go index d98a6d1..8017b98 100644 --- a/cmd/sessions.go +++ b/cmd/sessions.go @@ -139,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) } }, @@ -226,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", @@ -283,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) } @@ -311,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) } } 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]),