refactor: added check for secretID in secrets store

This commit is contained in:
David Allen 2025-03-24 15:32:47 -06:00
parent cc112e72e4
commit 2b0245e17b
Signed by: towk
GPG key ID: 0430CDBE22619155

View file

@ -37,28 +37,37 @@ var CrawlCmd = &cobra.Command{
return nil return nil
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var (
uri = args[0]
store secrets.SecretStore
err error
)
// try and load credentials from local store first // try and load credentials from local store first
store, err := secrets.OpenStore(secretsFile) store, err = secrets.OpenStore(secretsFile)
if err != nil { if err != nil {
log.Error().Err(err).Msg("failed to open store") log.Warn().Err(err).Msg("failed to open local store...falling back to default provided arguments")
// try and use the `username` and `password` arguments instead // try and use the `username` and `password` arguments instead
store = &secrets.StaticStore{ store = secrets.NewStaticStore(username, password)
Username: username,
Password: password,
}
} }
// found the store so try to load the creds
_, err = store.GetSecretByID(uri)
if err != nil {
store = secrets.NewStaticStore(username, password)
}
systems, err := crawler.CrawlBMCForSystems(crawler.CrawlerConfig{ systems, err := crawler.CrawlBMCForSystems(crawler.CrawlerConfig{
URI: args[0], URI: uri,
CredentialStore: store, CredentialStore: store,
Insecure: insecure, Insecure: insecure,
}) })
if err != nil { if err != nil {
log.Error().Err(err).Msg("error crawling BMC") log.Error().Err(err).Msg("failed to crawl BMC")
} }
// Marshal the inventory details to JSON // Marshal the inventory details to JSON
jsonData, err := json.MarshalIndent(systems, "", " ") jsonData, err := json.MarshalIndent(systems, "", " ")
if err != nil { if err != nil {
log.Error().Err(err).Msg("error marshalling to JSON:") log.Error().Err(err).Msg("failed to marshal JSON")
return return
} }