refactor: split BMC data structures into pkg/bmc package

This commit is contained in:
Devon Bautista 2025-04-16 16:29:20 -06:00 committed by David Allen
parent e62a38183f
commit 93010587c6
Signed by: towk
GPG key ID: 0430CDBE22619155
4 changed files with 78 additions and 51 deletions

45
pkg/bmc/bmc.go Normal file
View file

@ -0,0 +1,45 @@
package bmc
import (
"encoding/json"
"github.com/OpenCHAMI/magellan/pkg/secrets"
"github.com/rs/zerolog/log"
)
type BMCCredentials struct {
Username string `json:"username"`
Password string `json:"password"`
}
func GetBMCCredentials(store secrets.SecretStore, id string) (BMCCredentials, error) {
var creds BMCCredentials
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")
}
}
} 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")
}
}
return creds, nil
}

View file

@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"github.com/OpenCHAMI/magellan/pkg/bmc"
"github.com/OpenCHAMI/magellan/pkg/secrets"
"github.com/rs/zerolog/log"
"github.com/stmcginnis/gofish"
@ -18,15 +19,10 @@ type CrawlerConfig struct {
UseDefault bool
}
func (cc *CrawlerConfig) GetUserPass() (BMCUsernamePassword, error) {
func (cc *CrawlerConfig) GetUserPass() (bmc.BMCCredentials, error) {
return loadBMCCreds(*cc)
}
type BMCUsernamePassword struct {
Username string `json:"username"`
Password string `json:"password"`
}
type EthernetInterface struct {
URI string `json:"uri,omitempty"` // URI of the interface
MAC string `json:"mac,omitempty"` // MAC address of the interface
@ -373,10 +369,10 @@ func walkManagers(rf_managers []*redfish.Manager, baseURI string) ([]Manager, er
return managers, nil
}
func loadBMCCreds(config CrawlerConfig) (BMCUsernamePassword, error) {
func loadBMCCreds(config CrawlerConfig) (bmc.BMCCredentials, error) {
// NOTE: it is possible for the SecretStore to be nil, so we need a check
if config.CredentialStore == nil {
return BMCUsernamePassword{}, fmt.Errorf("credential store is invalid")
return bmc.BMCCredentials{}, fmt.Errorf("credential store is invalid")
}
creds, err := config.CredentialStore.GetSecretByID(config.URI)
if err != nil {
@ -391,19 +387,19 @@ func loadBMCCreds(config CrawlerConfig) (BMCUsernamePassword, error) {
event := log.Error()
event.Err(err)
event.Msg("failed to get default credentials from secret store")
return BMCUsernamePassword{}, err
return bmc.BMCCredentials{}, err
}
} else {
return BMCUsernamePassword{}, err
return bmc.BMCCredentials{}, err
}
}
var bmc_creds BMCUsernamePassword
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 BMCUsernamePassword{}, err
return bmc.BMCCredentials{}, err
}
return bmc_creds, nil
}

View file

@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"github.com/OpenCHAMI/magellan/pkg/bmc"
"github.com/stmcginnis/gofish"
"github.com/stmcginnis/gofish/redfish"
)
@ -34,8 +35,14 @@ func UpdateFirmwareRemote(q *UpdateParams) error {
return fmt.Errorf("failed to parse URI: %w", err)
}
// Get BMC credentials from secret store in update parameters
bmcCreds, err := bmc.GetBMCCredentials(q.SecretStore, q.URI)
if err != nil {
return fmt.Errorf("failed to get BMC credentials: %w", err)
}
// Connect to the Redfish service using gofish
client, err := gofish.Connect(gofish.ClientConfig{Endpoint: uri.String(), Username: q.Username, Password: q.Password, Insecure: q.Insecure})
client, err := gofish.Connect(gofish.ClientConfig{Endpoint: uri.String(), Username: bmcCreds.Username, Password: bmcCreds.Password, Insecure: q.Insecure})
if err != nil {
return fmt.Errorf("failed to connect to Redfish service: %w", err)
}
@ -69,8 +76,14 @@ func GetUpdateStatus(q *UpdateParams) error {
return fmt.Errorf("failed to parse URI: %w", err)
}
// Get BMC credentials from secret store in update parameters
bmcCreds, err := bmc.GetBMCCredentials(q.SecretStore, q.URI)
if err != nil {
return fmt.Errorf("failed to get BMC credentials: %w", err)
}
// Connect to the Redfish service using gofish
client, err := gofish.Connect(gofish.ClientConfig{Endpoint: uri.String(), Username: q.Username, Password: q.Password, Insecure: q.Insecure})
client, err := gofish.Connect(gofish.ClientConfig{Endpoint: uri.String(), Username: bmcCreds.Username, Password: bmcCreds.Password, Insecure: q.Insecure})
if err != nil {
return fmt.Errorf("failed to connect to Redfish service: %w", err)
}