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
|
|
@ -5,7 +5,6 @@ import (
|
|||
"fmt"
|
||||
|
||||
"github.com/OpenCHAMI/magellan/pkg/secrets"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type BMCCredentials struct {
|
||||
|
|
@ -13,51 +12,54 @@ type BMCCredentials struct {
|
|||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func GetBMCCredentials(store secrets.SecretStore, id string) (BMCCredentials, error) {
|
||||
func GetBMCCredentialsDefault(store secrets.SecretStore) (BMCCredentials, error) {
|
||||
var creds BMCCredentials
|
||||
if id == secrets.DEFAULT_KEY {
|
||||
log.Info().Msg("fetching default credentials")
|
||||
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 {
|
||||
if err := json.Unmarshal([]byte(uriCreds), &creds); err != nil {
|
||||
log.Error().Err(err).Msg("failed to unmarshal default credentials")
|
||||
return creds, fmt.Errorf("unmarshal default credentials: %w", err)
|
||||
} else {
|
||||
log.Info().Msg("default credentials found, using")
|
||||
}
|
||||
if strCreds, err := store.GetSecretByID(secrets.DEFAULT_KEY); err != nil {
|
||||
return creds, fmt.Errorf("get default BMC credentials from secret store: %w", err)
|
||||
} else {
|
||||
// Default URI credentials found, use them.
|
||||
if err = json.Unmarshal([]byte(strCreds), &creds); err != nil {
|
||||
return creds, fmt.Errorf("get default BMC credentials from secret store: failed to unmarshal: %w", err)
|
||||
}
|
||||
|
||||
return creds, nil
|
||||
}
|
||||
}
|
||||
|
||||
if uriCreds, err := store.GetSecretByID(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")
|
||||
defaultSecret, err := store.GetSecretByID(secrets.DEFAULT_KEY)
|
||||
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")
|
||||
}
|
||||
}
|
||||
func GetBMCCredentials(store secrets.SecretStore, id string) (BMCCredentials, error) {
|
||||
var creds BMCCredentials
|
||||
if strCreds, err := store.GetSecretByID(id); err != nil {
|
||||
return creds, fmt.Errorf("get BMC credentials from secret store: %w", err)
|
||||
} else {
|
||||
// Specific URI credentials found, use them.
|
||||
if err = json.Unmarshal([]byte(uriCreds), &creds); err != nil {
|
||||
log.Warn().Str("id", id).Err(err).Msg("failed to unmarshal specific credentials")
|
||||
return creds, err
|
||||
} else {
|
||||
log.Info().Str("id", id).Msg("specific credentials found, using")
|
||||
if err = json.Unmarshal([]byte(strCreds), &creds); err != nil {
|
||||
return creds, fmt.Errorf("get BMC credentials from secret store: failed to unmarshal: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue