diff --git a/cmd/collect.go b/cmd/collect.go index 472b32d..ac758b8 100644 --- a/cmd/collect.go +++ b/cmd/collect.go @@ -3,10 +3,10 @@ package cmd import ( "encoding/json" "fmt" - "os/user" "github.com/OpenCHAMI/magellan/internal/cache/sqlite" urlx "github.com/OpenCHAMI/magellan/internal/url" + "github.com/OpenCHAMI/magellan/internal/util" magellan "github.com/OpenCHAMI/magellan/pkg" "github.com/OpenCHAMI/magellan/pkg/auth" "github.com/OpenCHAMI/magellan/pkg/bmc" @@ -141,14 +141,13 @@ var CollectCmd = &cobra.Command{ } func init() { - currentUser, _ = user.Current() CollectCmd.Flags().StringVar(&host, "host", "", "Set the URI to the SMD root endpoint") CollectCmd.Flags().StringVarP(&username, "username", "u", "", "Set the master BMC username") CollectCmd.Flags().StringVarP(&password, "password", "p", "", "Set the master BMC password") CollectCmd.Flags().StringVar(&secretsFile, "secrets-file", "", "Set path to the node secrets file") CollectCmd.Flags().StringVar(&scheme, "scheme", "https", "Set the default scheme used to query when not included in URI") CollectCmd.Flags().StringVar(&protocol, "protocol", "tcp", "Set the protocol used to query") - CollectCmd.Flags().StringVarP(&outputPath, "output", "o", fmt.Sprintf("/tmp/%smagellan/inventory/", currentUser.Username+"/"), "Set the path to store collection data") + CollectCmd.Flags().StringVarP(&outputPath, "output", "o", fmt.Sprintf("/tmp/%smagellan/inventory/", util.GetCurrentUsername()+"/"), "Set the path to store collection data") CollectCmd.Flags().BoolVar(&forceUpdate, "force-update", false, "Set flag to force update data sent to SMD") CollectCmd.Flags().StringVar(&cacertPath, "cacert", "", "Set the path to CA cert file. (defaults to system CAs when blank)") CollectCmd.Flags().StringVarP(&format, "format", "F", "hive", "Set the output format (json|yaml)") diff --git a/pkg/client/client.go b/pkg/client/client.go index 6b455c6..aa3b26c 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -36,7 +36,21 @@ func NewClient[T Client](opts ...func(T)) T { return *client } -func WithCertPool(client Client, certPool *x509.CertPool) error { +func LoadCertificateFromPath(client Client, path string) error { + cacert, err := os.ReadFile(path) + if err != nil { + return fmt.Errorf("failed to read certificate at path: %s", path) + } + certPool := x509.NewCertPool() + certPool.AppendCertsFromPEM(cacert) + err = LoadCertificateFromPool(client, certPool) + if err != nil { + return fmt.Errorf("could not initialize certificate from pool: %v", err) + } + return nil +} + +func LoadCertificateFromPool(client Client, certPool *x509.CertPool) error { // make sure we have a valid cert pool if certPool == nil { return fmt.Errorf("invalid cert pool") @@ -63,16 +77,6 @@ func WithCertPool(client Client, certPool *x509.CertPool) error { return nil } -func WithSecureTLS(client Client, certPath string) error { - cacert, err := os.ReadFile(certPath) - if err != nil { - return fmt.Errorf("failed to read certificate from path '%s': %v", certPath, err) - } - certPool := x509.NewCertPool() - certPool.AppendCertsFromPEM(cacert) - return WithCertPool(client, certPool) -} - // Post() is a simplified wrapper function that packages all of the // that marshals a mapper into a JSON-formatted byte array, and then performs // a request to the specified URL. diff --git a/pkg/collect.go b/pkg/collect.go index 7833b4f..bf5a726 100644 --- a/pkg/collect.go +++ b/pkg/collect.go @@ -205,26 +205,19 @@ func CollectInventory(assets *[]RemoteAsset, params *CollectParams) ([]map[strin // write data to file if output path is set using set format if outputPath != "" { - switch params.Format { - case "hive": - var ( - finalPath = fmt.Sprintf("./%s/%s/%d.json", outputPath, data["ID"], time.Now().Unix()) - finalDir = filepath.Dir(finalPath) - ) - // if it doesn't, make the directory and write file - err = os.MkdirAll(finalDir, 0o777) - if err == nil { // no error - err = os.WriteFile(path.Clean(finalPath), body, os.ModePerm) - if err != nil { - log.Error().Err(err).Msgf("failed to write collect output to file") - } - } else { // error is set - log.Error().Err(err).Msg("failed to make directory for collect output") + var ( + finalPath = fmt.Sprintf("./%s/%s/%d.json", outputPath, data["ID"], time.Now().Unix()) + finalDir = filepath.Dir(finalPath) + ) + // if it doesn't, make the directory and write file + err = os.MkdirAll(finalDir, 0o777) + if err == nil { // no error + err = os.WriteFile(path.Clean(finalPath), body, os.ModePerm) + if err != nil { + log.Error().Err(err).Msgf("failed to write collect output to file") } - case "json": - case "yaml": - - default: + } else { // error is set + log.Error().Err(err).Msg("failed to make directory for collect output") } }