refactor: added optional secrets file parameter and lookup in collect

This commit is contained in:
David Allen 2025-03-17 10:34:53 -06:00
parent c3a7ebf975
commit 5ecc051fef
Signed by: towk
GPG key ID: 793B2924A49B3A3F

View file

@ -41,6 +41,7 @@ type CollectParams struct {
OutputPath string // set the path to save output with 'output' flag OutputPath string // set the path to save output with 'output' flag
ForceUpdate bool // set whether to force updating SMD with 'force-update' flag ForceUpdate bool // set whether to force updating SMD with 'force-update' flag
AccessToken string // set the access token to include in request with 'access-token' flag AccessToken string // set the access token to include in request with 'access-token' flag
SecretsFile string // set the path to secrets file
} }
// This is the main function used to collect information from the BMC nodes via Redfish. // This is the main function used to collect information from the BMC nodes via Redfish.
@ -68,7 +69,10 @@ func CollectInventory(assets *[]RemoteAsset, params *CollectParams, store secret
chanAssets = make(chan RemoteAsset, params.Concurrency+1) chanAssets = make(chan RemoteAsset, params.Concurrency+1)
outputPath = path.Clean(params.OutputPath) outputPath = path.Clean(params.OutputPath)
smdClient = &client.SmdClient{Client: &http.Client{}} smdClient = &client.SmdClient{Client: &http.Client{}}
initialStore secrets.SecretStore = store
err error
) )
// set the client's params from CLI // set the client's params from CLI
// NOTE: temporary solution until client.NewClient() is fixed // NOTE: temporary solution until client.NewClient() is fixed
smdClient.URI = params.URI smdClient.URI = params.URI
@ -103,27 +107,46 @@ func CollectInventory(assets *[]RemoteAsset, params *CollectParams, store secret
return return
} }
// use initial store to check for creds for specific node
store = initialStore
// generate custom xnames for bmcs // generate custom xnames for bmcs
// TODO: add xname customization via CLI // TODO: add xname customization via CLI
node := xnames.Node{ var (
uri = fmt.Sprintf("%s:%d", sr.Host, sr.Port)
node = xnames.Node{
Cabinet: 1000, Cabinet: 1000,
Chassis: 1, Chassis: 1,
ComputeModule: 7, ComputeModule: 7,
NodeBMC: offset, NodeBMC: offset,
} }
)
offset += 1 offset += 1
// determine if local store exists and has credentials for
// the provided secretID...
// if it does not, create a static store and use the username
// and password provided instead
_, err = store.GetSecretByID(uri)
if store == nil || err != nil {
log.Warn().Err(err).Msgf("could not retrieve secrets for %s...falling back to default provided credentials", uri)
store = secrets.NewStaticStore(params.Username, params.Password)
}
// crawl BMC node to fetch inventory data via Redfish // crawl BMC node to fetch inventory data via Redfish
var ( var (
systems []crawler.InventoryDetail systems []crawler.InventoryDetail
managers []crawler.Manager managers []crawler.Manager
config = crawler.CrawlerConfig{ config = crawler.CrawlerConfig{
URI: fmt.Sprintf("%s:%d", sr.Host, sr.Port), URI: uri,
CredentialStore: store, CredentialStore: store,
Insecure: true, Insecure: true,
} }
err error
) )
systems, err := crawler.CrawlBMCForSystems(config)
// crawl for node and BMC information
systems, err = crawler.CrawlBMCForSystems(config)
if err != nil { if err != nil {
log.Error().Err(err).Msg("failed to crawl BMC for systems") log.Error().Err(err).Msg("failed to crawl BMC for systems")
} }