mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 11:37:01 -07:00
fix: move BMC credentials getter that logs to util func
This commit is contained in:
parent
939be12da7
commit
5d9afebcb1
5 changed files with 97 additions and 69 deletions
|
|
@ -85,15 +85,17 @@ var CollectCmd = &cobra.Command{
|
||||||
switch s := store.(type) {
|
switch s := store.(type) {
|
||||||
case *secrets.StaticStore:
|
case *secrets.StaticStore:
|
||||||
if username != "" {
|
if username != "" {
|
||||||
|
log.Info().Msg("--username passed, overriding all usernames with value")
|
||||||
s.Username = username
|
s.Username = username
|
||||||
}
|
}
|
||||||
if password != "" {
|
if password != "" {
|
||||||
|
log.Info().Msg("--password passed, overriding all passwords with value")
|
||||||
s.Password = password
|
s.Password = password
|
||||||
}
|
}
|
||||||
case *secrets.LocalSecretStore:
|
case *secrets.LocalSecretStore:
|
||||||
for k, _ := range s.Secrets {
|
for k, _ := range s.Secrets {
|
||||||
if creds, err := bmc.GetBMCCredentials(store, k); err != nil {
|
if creds, err := bmc.GetBMCCredentials(store, k); err != nil {
|
||||||
log.Error().Str("id", k).Err(err).Msg("failed to get BMC credentials from secret store")
|
log.Error().Str("id", k).Err(err).Msg("failed to override BMC credentials")
|
||||||
} else {
|
} else {
|
||||||
if username != "" {
|
if username != "" {
|
||||||
creds.Username = username
|
creds.Username = username
|
||||||
|
|
@ -103,7 +105,7 @@ var CollectCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
if newCreds, err := json.Marshal(creds); err != nil {
|
if newCreds, err := json.Marshal(creds); err != nil {
|
||||||
log.Error().Str("id", k).Err(err).Msg("failed to marshal updated BMC credentials")
|
log.Error().Str("id", k).Err(err).Msg("failed to override BMC credentials: marshal error")
|
||||||
} else {
|
} else {
|
||||||
s.StoreSecretByID(k, string(newCreds))
|
s.StoreSecretByID(k, string(newCreds))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
47
internal/util/bmc.go
Normal file
47
internal/util/bmc.go
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/OpenCHAMI/magellan/pkg/bmc"
|
||||||
|
"github.com/OpenCHAMI/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
|
||||||
|
}
|
||||||
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/OpenCHAMI/magellan/pkg/secrets"
|
"github.com/OpenCHAMI/magellan/pkg/secrets"
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type BMCCredentials struct {
|
type BMCCredentials struct {
|
||||||
|
|
@ -13,51 +12,54 @@ type BMCCredentials struct {
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetBMCCredentials(store secrets.SecretStore, id string) (BMCCredentials, error) {
|
func GetBMCCredentialsDefault(store secrets.SecretStore) (BMCCredentials, error) {
|
||||||
var creds BMCCredentials
|
var creds BMCCredentials
|
||||||
if id == secrets.DEFAULT_KEY {
|
if strCreds, err := store.GetSecretByID(secrets.DEFAULT_KEY); err != nil {
|
||||||
log.Info().Msg("fetching default credentials")
|
return creds, fmt.Errorf("get default BMC credentials from secret store: %w", err)
|
||||||
if uriCreds, err := store.GetSecretByID(id); err != nil {
|
|
||||||
log.Warn().Err(err).Msg("failed to get default credentials")
|
|
||||||
return creds, fmt.Errorf("get default credentials: %w", err)
|
|
||||||
} else {
|
} else {
|
||||||
if err := json.Unmarshal([]byte(uriCreds), &creds); err != nil {
|
// Default URI credentials found, use them.
|
||||||
log.Error().Err(err).Msg("failed to unmarshal default credentials")
|
if err = json.Unmarshal([]byte(strCreds), &creds); err != nil {
|
||||||
return creds, fmt.Errorf("unmarshal default credentials: %w", err)
|
return creds, fmt.Errorf("get default BMC credentials from secret store: failed to unmarshal: %w", err)
|
||||||
} else {
|
|
||||||
log.Info().Msg("default credentials found, using")
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return creds, nil
|
return creds, nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if uriCreds, err := store.GetSecretByID(id); err != nil {
|
func GetBMCCredentials(store secrets.SecretStore, id string) (BMCCredentials, error) {
|
||||||
// Specific credentials for URI not found, fetch default.
|
var creds BMCCredentials
|
||||||
log.Warn().Str("id", id).Msg("specific credentials not found, falling back to default")
|
if strCreds, err := store.GetSecretByID(id); err != nil {
|
||||||
defaultSecret, err := store.GetSecretByID(secrets.DEFAULT_KEY)
|
return creds, fmt.Errorf("get BMC credentials from secret store: %w", err)
|
||||||
if 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.
|
|
||||||
if err = json.Unmarshal([]byte(defaultSecret), &creds); err != nil {
|
|
||||||
log.Warn().Str("id", id).Err(err).Msg("failed to unmarshal default secrets store credentials")
|
|
||||||
return creds, err
|
|
||||||
} else {
|
|
||||||
log.Info().Str("id", id).Msg("default credentials found, using")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Specific URI credentials found, use them.
|
// Specific URI credentials found, use them.
|
||||||
if err = json.Unmarshal([]byte(uriCreds), &creds); err != nil {
|
if err = json.Unmarshal([]byte(strCreds), &creds); err != nil {
|
||||||
log.Warn().Str("id", id).Err(err).Msg("failed to unmarshal specific credentials")
|
return creds, fmt.Errorf("get BMC credentials from secret store: failed to unmarshal: %w", err)
|
||||||
return creds, err
|
|
||||||
} else {
|
|
||||||
log.Info().Str("id", id).Msg("specific credentials found, using")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return creds, nil
|
return creds, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetBMCCredentialsOrDefault(store secrets.SecretStore, id string) BMCCredentials {
|
||||||
|
var (
|
||||||
|
creds BMCCredentials
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
if id == "" {
|
||||||
|
return creds
|
||||||
|
}
|
||||||
|
|
||||||
|
if id == secrets.DEFAULT_KEY {
|
||||||
|
creds, _ = GetBMCCredentialsDefault(store)
|
||||||
|
return creds
|
||||||
|
}
|
||||||
|
|
||||||
|
if creds, err = GetBMCCredentials(store, id); err != nil {
|
||||||
|
if defaultSecret, err := GetBMCCredentialsDefault(store); err == nil {
|
||||||
|
// Default credentials found, use them.
|
||||||
|
creds = defaultSecret
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return creds
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -141,8 +141,8 @@ func CollectInventory(assets *[]RemoteAsset, params *CollectParams) ([]map[strin
|
||||||
}
|
}
|
||||||
|
|
||||||
// get BMC username to send
|
// get BMC username to send
|
||||||
bmcCreds, err := bmc.GetBMCCredentials(params.SecretStore, config.URI)
|
bmcCreds := bmc.GetBMCCredentialsOrDefault(params.SecretStore, config.URI)
|
||||||
if err != nil {
|
if bmcCreds == (bmc.BMCCredentials{}) {
|
||||||
log.Error().Str("id", config.URI).Msg("username will be blank")
|
log.Error().Str("id", config.URI).Msg("username will be blank")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
package crawler
|
package crawler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/OpenCHAMI/magellan/internal/util"
|
||||||
"github.com/OpenCHAMI/magellan/pkg/bmc"
|
"github.com/OpenCHAMI/magellan/pkg/bmc"
|
||||||
"github.com/OpenCHAMI/magellan/pkg/secrets"
|
"github.com/OpenCHAMI/magellan/pkg/secrets"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
@ -374,32 +374,9 @@ func loadBMCCreds(config CrawlerConfig) (bmc.BMCCredentials, error) {
|
||||||
if config.CredentialStore == nil {
|
if config.CredentialStore == nil {
|
||||||
return bmc.BMCCredentials{}, fmt.Errorf("credential store is invalid")
|
return bmc.BMCCredentials{}, fmt.Errorf("credential store is invalid")
|
||||||
}
|
}
|
||||||
creds, err := config.CredentialStore.GetSecretByID(config.URI)
|
if creds := util.GetBMCCredentials(config.CredentialStore, config.URI); creds == (bmc.BMCCredentials{}) {
|
||||||
if err != nil {
|
return creds, fmt.Errorf("%s: credentials blank for BNC", config.URI)
|
||||||
event := log.Error()
|
|
||||||
event.Err(err)
|
|
||||||
event.Msg("failed to get credentials from secret store")
|
|
||||||
// try to get default if parameter is set
|
|
||||||
if config.UseDefault {
|
|
||||||
creds, err = config.CredentialStore.GetSecretByID(secrets.DEFAULT_KEY)
|
|
||||||
// no default credentials
|
|
||||||
if err != nil {
|
|
||||||
event := log.Error()
|
|
||||||
event.Err(err)
|
|
||||||
event.Msg("failed to get default credentials from secret store")
|
|
||||||
return bmc.BMCCredentials{}, err
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return bmc.BMCCredentials{}, err
|
return creds, nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
var bmc_creds bmc.BMCCredentials
|
|
||||||
err = json.Unmarshal([]byte(creds), &bmc_creds)
|
|
||||||
if err != nil {
|
|
||||||
event := log.Error()
|
|
||||||
event.Err(err)
|
|
||||||
event.Msg("failed to unmarshal credentials")
|
|
||||||
return bmc.BMCCredentials{}, err
|
|
||||||
}
|
|
||||||
return bmc_creds, nil
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue