chore: small changes, fixes, and refactor

This commit is contained in:
David Allen 2025-05-01 23:29:45 -06:00
parent 2491c6bd1c
commit a62f74dbdd
Signed by: towk
GPG key ID: 0430CDBE22619155
11 changed files with 105 additions and 125 deletions

View file

@ -7,7 +7,7 @@ import (
"github.com/cznic/mathutil"
"github.com/davidallendj/magellan/internal/cache/sqlite"
urlx "github.com/davidallendj/magellan/internal/url"
urlx "github.com/davidallendj/magellan/internal/urlx"
magellan "github.com/davidallendj/magellan/pkg"
"github.com/davidallendj/magellan/pkg/auth"
"github.com/davidallendj/magellan/pkg/bmc"

View file

@ -6,7 +6,7 @@ import (
"github.com/rs/zerolog/log"
urlx "github.com/davidallendj/magellan/internal/url"
urlx "github.com/davidallendj/magellan/internal/urlx"
"github.com/davidallendj/magellan/pkg/bmc"
"github.com/davidallendj/magellan/pkg/crawler"
"github.com/davidallendj/magellan/pkg/secrets"
@ -101,7 +101,7 @@ func init() {
CrawlCmd.Flags().StringVarP(&username, "username", "u", "", "Set the username for the BMC")
CrawlCmd.Flags().StringVarP(&password, "password", "p", "", "Set the password for the BMC")
CrawlCmd.Flags().BoolVarP(&insecure, "insecure", "i", false, "Ignore SSL errors")
CrawlCmd.Flags().StringVarP(&secretsFile, "file", "f", "nodes.json", "set the secrets file with BMC credentials")
CrawlCmd.Flags().StringVarP(&secretsFile, "secrets-file", "f", "secrets.json", "set the secrets file with BMC credentials")
checkBindFlagError(viper.BindPFlag("crawl.insecure", CrawlCmd.Flags().Lookup("insecure")))

View file

@ -21,6 +21,8 @@ import (
"os/user"
magellan "github.com/davidallendj/magellan/internal"
"github.com/davidallendj/magellan/pkg/bmc"
"github.com/davidallendj/magellan/pkg/secrets"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -147,3 +149,44 @@ func SetDefaults() {
viper.SetDefault("update.status", false)
}
func initSecretsStore(uri string) secrets.SecretStore {
var (
store secrets.SecretStore
err error
)
if username != "" && password != "" {
// First, try and load credentials from --username and --password if both are set.
log.Debug().Str("id", uri).Msgf("--username and --password specified, using them for BMC credentials")
store = secrets.NewStaticStore(username, password)
} else {
// Alternatively, locate specific credentials (falling back to default) and override those
// with --username or --password if either are passed.
log.Debug().Str("id", uri).Msgf("one or both of --username and --password NOT passed, attempting to obtain missing credentials from secret store at %s", secretsFile)
if store, err = secrets.OpenStore(secretsFile); err != nil {
log.Error().Str("id", uri).Err(err).Msg("failed to open local secrets store")
}
// Either none of the flags were passed or only one of them were; get
// credentials from secrets store to fill in the gaps.
bmcCreds, _ := bmc.GetBMCCredentials(store, uri)
nodeCreds := secrets.StaticStore{
Username: bmcCreds.Username,
Password: bmcCreds.Password,
}
// If either of the flags were passed, override the fetched
// credentials with them.
if username != "" {
log.Info().Str("id", uri).Msg("--username was set, overriding username for this BMC")
nodeCreds.Username = username
}
if password != "" {
log.Info().Str("id", uri).Msg("--password was set, overriding password for this BMC")
nodeCreds.Password = password
}
store = &nodeCreds
}
return store
}

View file

@ -12,7 +12,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/cznic/mathutil"
urlx "github.com/davidallendj/magellan/internal/url"
urlx "github.com/davidallendj/magellan/internal/urlx"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

View file

@ -128,9 +128,7 @@ func init() {
checkBindFlagError(viper.BindPFlag("update.username", UpdateCmd.Flags().Lookup("username")))
checkBindFlagError(viper.BindPFlag("update.password", UpdateCmd.Flags().Lookup("password")))
checkBindFlagError(viper.BindPFlag("update.scheme", UpdateCmd.Flags().Lookup("scheme")))
checkBindFlagError(viper.BindPFlag("update.firmware-url", UpdateCmd.Flags().Lookup("firmware-url")))
checkBindFlagError(viper.BindPFlag("update.firmware-version", UpdateCmd.Flags().Lookup("firmware-version")))
checkBindFlagError(viper.BindPFlag("update.component", UpdateCmd.Flags().Lookup("component")))
checkBindFlagError(viper.BindPFlag("update.firmware-uri", UpdateCmd.Flags().Lookup("firmware-uri")))
checkBindFlagError(viper.BindPFlag("update.status", UpdateCmd.Flags().Lookup("status")))
rootCmd.AddCommand(UpdateCmd)