diff --git a/cmd/collect.go b/cmd/collect.go index a9c831d..0dccd70 100644 --- a/cmd/collect.go +++ b/cmd/collect.go @@ -27,7 +27,7 @@ var collectCmd = &cobra.Command{ // get probe states stored in db from scan probeStates, err := sqlite.GetProbeResults(dbpath) if err != nil { - l.Log.Errorf("could not get states: %v", err) + l.Log.Errorf("failed toget states: %v", err) } // try to load access token either from env var, file, or config if var not set @@ -63,10 +63,10 @@ var collectCmd = &cobra.Command{ } magellan.CollectAll(&probeStates, l, q) - // confirm the inventories were added - err = smd.GetRedfishEndpoints() - if err != nil { - l.Log.Errorf("could not get redfish endpoints: %v", err) + // add necessary headers for final request (like token) + headers := make(map[string]string) + if q.AccessToken != "" { + headers["Authorization"] = "Bearer " + q.AccessToken } }, } @@ -77,7 +77,7 @@ func init() { collectCmd.PersistentFlags().IntVarP(&smd.Port, "port", "p", smd.Port, "set the port to the smd API") collectCmd.PersistentFlags().StringVar(&user, "user", "", "set the BMC user") collectCmd.PersistentFlags().StringVar(&pass, "pass", "", "set the BMC password") - collectCmd.PersistentFlags().StringVar(&protocol, "protocol", "https", "set the Redfish protocol") + collectCmd.PersistentFlags().StringVar(&protocol, "protocol", "https", "set the protocol used to query") collectCmd.PersistentFlags().StringVarP(&outputPath, "output", "o", "/tmp/magellan/data/", "set the path to store collection data") collectCmd.PersistentFlags().BoolVar(&forceUpdate, "force-update", false, "set flag to force update data sent to SMD ") collectCmd.PersistentFlags().StringVar(&preferredDriver, "preferred-driver", "ipmi", "set the preferred driver to use") diff --git a/cmd/list.go b/cmd/list.go index 9ad54bb..c5ce1c4 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -15,7 +15,7 @@ var listCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { probeResults, err := sqlite.GetProbeResults(dbpath) if err != nil { - logrus.Errorf("could not get probe results: %v\n", err) + logrus.Errorf("failed toget probe results: %v\n", err) } for _, r := range probeResults { fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol) diff --git a/cmd/root.go b/cmd/root.go index 2223ea1..bb38247 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -56,7 +56,7 @@ func Execute() { func LoadAccessToken() (string, error) { // try to load token from env var - testToken := os.Getenv("OCHAMI_ACCESS_TOKEN") + testToken := os.Getenv("MAGELLAN_ACCESS_TOKEN") if testToken != "" { return testToken, nil } @@ -72,7 +72,7 @@ func LoadAccessToken() (string, error) { if testToken != "" { return testToken, nil } - return "", fmt.Errorf("could not load token from environment variable, file, or config") + return "", fmt.Errorf("failed toload token from environment variable, file, or config") } func init() { @@ -80,7 +80,7 @@ func init() { rootCmd.PersistentFlags().IntVar(&threads, "threads", -1, "set the number of threads") rootCmd.PersistentFlags().IntVar(&timeout, "timeout", 30, "set the timeout") rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "set the config file path") - rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", true, "set verbose flag") + rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "set verbose flag") rootCmd.PersistentFlags().StringVar(&accessToken, "access-token", "", "set the access token") rootCmd.PersistentFlags().StringVar(&dbpath, "db.path", "/tmp/magellan/magellan.db", "set the probe storage path") @@ -103,7 +103,7 @@ func SetDefaults() { viper.SetDefault("threads", 1) viper.SetDefault("timeout", 30) viper.SetDefault("config", "") - viper.SetDefault("verbose", true) + viper.SetDefault("verbose", false) viper.SetDefault("db.path", "/tmp/magellan/magellan.db") viper.SetDefault("scan.hosts", []string{}) viper.SetDefault("scan.ports", []int{}) diff --git a/cmd/scan.go b/cmd/scan.go index f190d21..ca2fdd0 100644 --- a/cmd/scan.go +++ b/cmd/scan.go @@ -58,14 +58,16 @@ var scanCmd = &cobra.Command{ threads = mathutil.Clamp(len(hostsToScan), 1, 255) } probeStates := magellan.ScanForAssets(hostsToScan, portsToScan, threads, timeout, disableProbing) - for _, r := range probeStates { - fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol) + if verbose { + for _, r := range probeStates { + fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol) + } } // make the dbpath dir if needed err := os.MkdirAll(path.Dir(dbpath), 0766) if err != nil { - fmt.Printf("could not make database directory: %v", err) + fmt.Printf("failed tomake database directory: %v", err) } sqlite.InsertProbeResults(dbpath, &probeStates) @@ -86,6 +88,6 @@ func init() { viper.BindPFlag("scan.subnets", scanCmd.Flags().Lookup("subnet")) viper.BindPFlag("scan.subnet-masks", scanCmd.Flags().Lookup("subnet-mask")) viper.BindPFlag("scan.disable-probing", scanCmd.Flags().Lookup("disable-probing")) - + rootCmd.AddCommand(scanCmd) } diff --git a/cmd/update.go b/cmd/update.go index bd64986..c099fe0 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -49,19 +49,19 @@ var updateCmd = &cobra.Command{ if status { err := magellan.GetUpdateStatus(q) if err != nil { - l.Log.Errorf("could not get update status: %v", err) + l.Log.Errorf("failed toget update status: %v", err) } return } // client, err := magellan.NewClient(l, &q.QueryParams) // if err != nil { - // l.Log.Errorf("could not make client: %v", err) + // l.Log.Errorf("failed tomake client: %v", err) // } // err = magellan.UpdateFirmware(client, l, q) err := magellan.UpdateFirmwareRemote(q) if err != nil { - l.Log.Errorf("could not update firmware: %v", err) + l.Log.Errorf("failed toupdate firmware: %v", err) } }, } diff --git a/internal/api/dora/dora.go b/internal/api/dora/dora.go index 880ac7b..b113cf6 100644 --- a/internal/api/dora/dora.go +++ b/internal/api/dora/dora.go @@ -43,15 +43,15 @@ func ScanForAssets() error { func QueryScannedPorts() error { // Perform scan and collect from dora server url := makeEndpointUrl("/scanned_ports") - _, body, err := util.MakeRequest(url, "GET", nil, nil) + _, body, err := util.MakeRequest(nil, url, "GET", nil, nil) if err != nil { - return fmt.Errorf("could not discover assets: %v", err) + return fmt.Errorf("failed todiscover assets: %v", err) } // get data from JSON var res map[string]any if err := json.Unmarshal(body, &res); err != nil { - return fmt.Errorf("could not unmarshal response body: %v", err) + return fmt.Errorf("failed tounmarshal response body: %v", err) } data := res["data"] diff --git a/internal/api/smd/smd.go b/internal/api/smd/smd.go index ca0a606..370841d 100644 --- a/internal/api/smd/smd.go +++ b/internal/api/smd/smd.go @@ -1,13 +1,18 @@ package smd // See ref for API docs: -// https://github.com/Cray-HPE/hms-smd/blob/master/docs/examples.adoc -// https://github.com/alexlovelltroy/hms-smd +// https://github.com/OpenCHAMI/hms-smd/blob/master/docs/examples.adoc +// https://github.com/OpenCHAMI/hms-smd import ( + "crypto/tls" + "crypto/x509" "fmt" + "net" + "net/http" + "os" + "time" "github.com/OpenCHAMI/magellan/internal/util" - // hms "github.com/alexlovelltroy/hms-smd" ) var ( @@ -16,40 +21,90 @@ var ( Port = 27779 ) -func makeEndpointUrl(endpoint string) string { - return Host + ":" + fmt.Sprint(Port) + BaseEndpoint + endpoint +type Option func(*Client) + +type Client struct { + *http.Client + CACertPool *x509.CertPool } -func GetRedfishEndpoints() error { +func NewClient(opts ...Option) *Client { + client := &Client{ + Client: http.DefaultClient, + } + for _, opt := range opts { + opt(client) + } + return client +} + +func WithHttpClient(httpClient *http.Client) Option { + return func(c *Client) { + c.Client = httpClient + } +} + +// This MakeRequest function is a wrapper around the util.MakeRequest function +// with a couple of niceties with using a smd.Client +func (c *Client) MakeRequest(url string, method string, body []byte, headers map[string]string) (*http.Response, []byte, error) { + return util.MakeRequest(c.Client, url, method, body, headers) +} + +func WithCertPool(certPool *x509.CertPool) Option { + return func(c *Client) { + c.Client.Transport = &http.Transport{ + TLSClientConfig: &tls.Config{ + RootCAs: certPool, + InsecureSkipVerify: true, + }, + DisableKeepAlives: true, + Dial: (&net.Dialer{ + Timeout: 120 * time.Second, + KeepAlive: 120 * time.Second, + }).Dial, + TLSHandshakeTimeout: 120 * time.Second, + ResponseHeaderTimeout: 120 * time.Second, + } + } +} + +func WithSecureTLS(certPath string) Option { + cacert, _ := os.ReadFile(certPath) + certPool := x509.NewCertPool() + certPool.AppendCertsFromPEM(cacert) + return WithCertPool(certPool) +} + +func (c *Client) GetRedfishEndpoints(headers map[string]string, opts ...Option) error { url := makeEndpointUrl("/Inventory/RedfishEndpoints") - _, body, err := util.MakeRequest(url, "GET", nil, nil) + _, body, err := c.MakeRequest(url, "GET", nil, headers) if err != nil { - return fmt.Errorf("could not get endpoint: %v", err) + return fmt.Errorf("failed toget endpoint: %v", err) } // fmt.Println(res) fmt.Println(string(body)) return nil } -func GetComponentEndpoint(xname string) error { +func (c *Client) GetComponentEndpoint(xname string) error { url := makeEndpointUrl("/Inventory/ComponentsEndpoints/" + xname) - res, body, err := util.MakeRequest(url, "GET", nil, nil) + res, body, err := c.MakeRequest(url, "GET", nil, nil) if err != nil { - return fmt.Errorf("could not get endpoint: %v", err) + return fmt.Errorf("failed toget endpoint: %v", err) } fmt.Println(res) fmt.Println(string(body)) return nil } -func AddRedfishEndpoint(data []byte, headers map[string]string) error { +func (c *Client) AddRedfishEndpoint(data []byte, headers map[string]string) error { if data == nil { - return fmt.Errorf("could not add redfish endpoint: no data found") + return fmt.Errorf("failed toadd redfish endpoint: no data found") } // Add redfish endpoint via POST `/hsm/v2/Inventory/RedfishEndpoints` endpoint url := makeEndpointUrl("/Inventory/RedfishEndpoints") - res, body, err := util.MakeRequest(url, "POST", data, headers) + res, body, err := c.MakeRequest(url, "POST", data, headers) if res != nil { statusOk := res.StatusCode >= 200 && res.StatusCode < 300 if !statusOk { @@ -60,19 +115,23 @@ func AddRedfishEndpoint(data []byte, headers map[string]string) error { return err } -func UpdateRedfishEndpoint(xname string, data []byte, headers map[string]string) error { +func (c *Client) UpdateRedfishEndpoint(xname string, data []byte, headers map[string]string) error { if data == nil { - return fmt.Errorf("could not add redfish endpoint: no data found") + return fmt.Errorf("failed to add redfish endpoint: no data found") } // Update redfish endpoint via PUT `/hsm/v2/Inventory/RedfishEndpoints` endpoint url := makeEndpointUrl("/Inventory/RedfishEndpoints/" + xname) - res, body, err := util.MakeRequest(url, "PUT", data, headers) + res, body, err := c.MakeRequest(url, "PUT", data, headers) fmt.Printf("%v (%v)\n%s\n", url, res.Status, string(body)) if res != nil { statusOk := res.StatusCode >= 200 && res.StatusCode < 300 if !statusOk { - return fmt.Errorf("could not update redfish endpoint") + return fmt.Errorf("failed to update redfish endpoint (returned %s)", res.Status) } } return err } + +func makeEndpointUrl(endpoint string) string { + return Host + ":" + fmt.Sprint(Port) + BaseEndpoint + endpoint +} diff --git a/internal/collect.go b/internal/collect.go index 4a2189a..d97b6e6 100644 --- a/internal/collect.go +++ b/internal/collect.go @@ -3,7 +3,6 @@ package magellan import ( "context" "crypto/tls" - "crypto/x509" "encoding/json" "fmt" "net/http" @@ -19,7 +18,6 @@ import ( "github.com/Cray-HPE/hms-xname/xnames" bmclib "github.com/bmc-toolbox/bmclib/v2" - "github.com/jacobweinstock/registrar" _ "github.com/mattn/go-sqlite3" "github.com/stmcginnis/gofish" _ "github.com/stmcginnis/gofish" @@ -52,56 +50,6 @@ type QueryParams struct { AccessToken string } -func NewClient(l *log.Logger, q *QueryParams) (*bmclib.Client, error) { - - tr := &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - } - httpClient := http.Client{ - Transport: tr, - } - - // init client - clientOpts := []bmclib.Option{ - // bmclib.WithSecureTLS(nil), - bmclib.WithHTTPClient(&httpClient), - // bmclib.WithLogger(), - bmclib.WithRedfishHTTPClient(&httpClient), - // bmclib.WithDellRedfishUseBasicAuth(true), - bmclib.WithRedfishPort(fmt.Sprint(q.Port)), - bmclib.WithRedfishUseBasicAuth(true), - bmclib.WithIpmitoolPort(fmt.Sprint(IPMI_PORT)), - bmclib.WithIpmitoolPath(q.IpmitoolPath), - } - - // only work if valid cert is provided - if q.CaCertPath != "" { - pool := x509.NewCertPool() - data, err := os.ReadFile(q.CaCertPath) - if err != nil { - return nil, fmt.Errorf("could not read cert pool file: %v", err) - } - pool.AppendCertsFromPEM(data) - // a nil pool uses the system certs - clientOpts = append(clientOpts, bmclib.WithSecureTLS(pool)) - } - url := "" - fmt.Println(url) - if q.User != "" && q.Pass != "" { - url += fmt.Sprintf("%s://%s:%s@%s", q.Protocol, q.User, q.Pass, q.Host) - } else { - url += q.Host - } - client := bmclib.NewClient(url, q.User, q.Pass, clientOpts...) - ds := registrar.Drivers{} - for _, driver := range q.Drivers { - ds = append(ds, client.Registry.Using(driver)...) // ipmi, gofish, redfish - } - client.Registry.Drivers = ds - - return client, nil -} - func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) error { // check for available probe states if probeStates == nil { @@ -115,16 +63,20 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err outputPath := path.Clean(q.OutputPath) outputPath, err := util.MakeOutputDirectory(outputPath) if err != nil { - l.Log.Errorf("could not make output directory: %v", err) + l.Log.Errorf("failed to make output directory: %v", err) } - found := make([]string, 0, len(*probeStates)) - done := make(chan struct{}, q.Threads+1) - chanProbeState := make(chan ScannedResult, q.Threads+1) - // collect bmc information asynchronously - var offset = 0 - var wg sync.WaitGroup + var ( + offset = 0 + wg sync.WaitGroup + found = make([]string, 0, len(*probeStates)) + done = make(chan struct{}, q.Threads+1) + chanProbeState = make(chan ScannedResult, q.Threads+1) + client = smd.NewClient( + smd.WithSecureTLS(q.CaCertPath), + ) + ) wg.Add(q.Threads) for i := 0; i < q.Threads; i++ { go func() { @@ -146,14 +98,9 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err } offset += 1 - // bmclibClient, err := NewClient(l, q) - // if err != nil { - // l.Log.Errorf("could not make client: %v", err) - // } - gofishClient, err := connectGofish(q) if err != nil { - l.Log.Errorf("could not connect to bmc (%v:%v): %v", q.Host, q.Port, err) + l.Log.Errorf("failed to connect to bmc (%v:%v): %v", q.Host, q.Port, err) } // data to be sent to smd @@ -171,21 +118,11 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err // unmarshal json to send in correct format var rm map[string]json.RawMessage - // inventories - // if bmclibClient != nil { - // inventory, err := CollectInventory(bmclibClient, q) - // if err != nil { - // l.Log.Errorf("could not query inventory (%v:%v): %v", q.Host, q.Port, err) - // } - // json.Unmarshal(inventory, &rm) - // data["Inventory"] = rm["Inventory"] - // } - // chassis if gofishClient != nil { chassis, err := CollectChassis(gofishClient, q) if err != nil { - l.Log.Errorf("could not query chassis: %v", err) + l.Log.Errorf("failed to query chassis: %v", err) continue } json.Unmarshal(chassis, &rm) @@ -194,7 +131,7 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err // systems systems, err := CollectSystems(gofishClient, q) if err != nil { - l.Log.Errorf("could not query systems: %v", err) + l.Log.Errorf("failed to query systems: %v", err) } json.Unmarshal(systems, &rm) data["Systems"] = rm["Systems"] @@ -217,27 +154,29 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err body, err := json.MarshalIndent(data, "", " ") if err != nil { - l.Log.Errorf("could not marshal JSON: %v", err) + l.Log.Errorf("failed to marshal JSON: %v", err) } if q.Verbose { fmt.Printf("%v\n", string(body)) } - // write JSON data to file - err = os.WriteFile(path.Clean(outputPath+"/"+q.Host+".json"), body, os.ModePerm) - if err != nil { - l.Log.Errorf("could not write data to file: %v", err) + // write JSON data to file if output path is set + if outputPath != "" { + err = os.WriteFile(path.Clean(outputPath+"/"+q.Host+".json"), body, os.ModePerm) + if err != nil { + l.Log.Errorf("failed to write data to file: %v", err) + } } // add all endpoints to smd - err = smd.AddRedfishEndpoint(body, headers) + err = client.AddRedfishEndpoint(body, headers) if err != nil { l.Log.Error(err) // try updating instead if q.ForceUpdate { - err = smd.UpdateRedfishEndpoint(data["ID"].(string), body, headers) + err = client.UpdateRedfishEndpoint(data["ID"].(string), body, headers) if err != nil { l.Log.Error(err) } @@ -274,19 +213,18 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err close(chanProbeState) wg.Wait() close(done) + return nil } func CollectMetadata(client *bmclib.Client, q *QueryParams) ([]byte, error) { - // client, err := NewClient(l, q) - // open BMC session and update driver registry ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(q.Timeout)) client.Registry.FilterForCompatible(ctx) err := client.Open(ctx) if err != nil { ctxCancel() - return nil, fmt.Errorf("could not connect to bmc: %v", err) + return nil, fmt.Errorf("failed to connect to bmc: %v", err) } defer client.Close(ctx) @@ -294,14 +232,14 @@ func CollectMetadata(client *bmclib.Client, q *QueryParams) ([]byte, error) { metadata := client.GetMetadata() if err != nil { ctxCancel() - return nil, fmt.Errorf("could not get metadata: %v", err) + return nil, fmt.Errorf("failed to get metadata: %v", err) } // retrieve inventory data b, err := json.MarshalIndent(metadata, "", " ") if err != nil { ctxCancel() - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } ctxCancel() @@ -315,13 +253,13 @@ func CollectInventory(client *bmclib.Client, q *QueryParams) ([]byte, error) { err := client.PreferProvider(q.Preferred).Open(ctx) if err != nil { ctxCancel() - return nil, fmt.Errorf("could not open client: %v", err) + return nil, fmt.Errorf("failed to open client: %v", err) } inventory, err := client.Inventory(ctx) if err != nil { ctxCancel() - return nil, fmt.Errorf("could not get inventory: %v", err) + return nil, fmt.Errorf("failed to get inventory: %v", err) } // retrieve inventory data @@ -329,7 +267,7 @@ func CollectInventory(client *bmclib.Client, q *QueryParams) ([]byte, error) { b, err := json.MarshalIndent(data, "", " ") if err != nil { ctxCancel() - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } ctxCancel() @@ -342,13 +280,13 @@ func CollectPowerState(client *bmclib.Client, q *QueryParams) ([]byte, error) { err := client.PreferProvider(q.Preferred).Open(ctx) if err != nil { ctxCancel() - return nil, fmt.Errorf("could not open client: %v", err) + return nil, fmt.Errorf("failed to open client: %v", err) } powerState, err := client.GetPowerState(ctx) if err != nil { ctxCancel() - return nil, fmt.Errorf("could not get inventory: %v", err) + return nil, fmt.Errorf("failed to get inventory: %v", err) } // retrieve inventory data @@ -356,7 +294,7 @@ func CollectPowerState(client *bmclib.Client, q *QueryParams) ([]byte, error) { b, err := json.MarshalIndent(data, "", " ") if err != nil { ctxCancel() - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } ctxCancel() @@ -371,7 +309,7 @@ func CollectUsers(client *bmclib.Client, q *QueryParams) ([]byte, error) { err := client.Open(ctx) if err != nil { ctxCancel() - return nil, fmt.Errorf("could not connect to bmc: %v", err) + return nil, fmt.Errorf("failed to connect to bmc: %v", err) } defer client.Close(ctx) @@ -379,7 +317,7 @@ func CollectUsers(client *bmclib.Client, q *QueryParams) ([]byte, error) { users, err := client.ReadUsers(ctx) if err != nil { ctxCancel() - return nil, fmt.Errorf("could not get users: %v", err) + return nil, fmt.Errorf("failed to get users: %v", err) } // retrieve inventory data @@ -387,7 +325,7 @@ func CollectUsers(client *bmclib.Client, q *QueryParams) ([]byte, error) { b, err := json.MarshalIndent(data, "", " ") if err != nil { ctxCancel() - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } ctxCancel() @@ -397,7 +335,7 @@ func CollectUsers(client *bmclib.Client, q *QueryParams) ([]byte, error) { func CollectBios(client *bmclib.Client, q *QueryParams) ([]byte, error) { // client, err := NewClient(l, q) // if err != nil { - // return nil, fmt.Errorf("could not make query: %v", err) + // return nil, fmt.Errorf("failed to make query: %v", err) // } b, err := makeRequest(client, client.GetBiosConfiguration, q.Timeout) return b, err @@ -406,7 +344,7 @@ func CollectBios(client *bmclib.Client, q *QueryParams) ([]byte, error) { func CollectEthernetInterfaces(c *gofish.APIClient, q *QueryParams, systemID string) ([]byte, error) { systems, err := c.Service.Systems() if err != nil { - return nil, fmt.Errorf("could not query storage systems (%v:%v): %v", q.Host, q.Port, err) + return nil, fmt.Errorf("failed to query storage systems (%v:%v): %v", q.Host, q.Port, err) } var interfaces []*redfish.EthernetInterface @@ -419,13 +357,13 @@ func CollectEthernetInterfaces(c *gofish.APIClient, q *QueryParams, systemID str } if len(interfaces) <= 0 { - return nil, fmt.Errorf("could not get ethernet interfaces: %v", err) + return nil, fmt.Errorf("failed to get ethernet interfaces: %v", err) } data := map[string]any{"EthernetInterfaces": interfaces} b, err := json.MarshalIndent(data, "", " ") if err != nil { - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } return b, nil @@ -434,13 +372,13 @@ func CollectEthernetInterfaces(c *gofish.APIClient, q *QueryParams, systemID str func CollectChassis(c *gofish.APIClient, q *QueryParams) ([]byte, error) { chassis, err := c.Service.Chassis() if err != nil { - return nil, fmt.Errorf("could not query chassis (%v:%v): %v", q.Host, q.Port, err) + return nil, fmt.Errorf("failed to query chassis (%v:%v): %v", q.Host, q.Port, err) } data := map[string]any{"Chassis": chassis} b, err := json.MarshalIndent(data, "", " ") if err != nil { - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } return b, nil @@ -449,12 +387,12 @@ func CollectChassis(c *gofish.APIClient, q *QueryParams) ([]byte, error) { func CollectStorage(c *gofish.APIClient, q *QueryParams) ([]byte, error) { systems, err := c.Service.StorageSystems() if err != nil { - return nil, fmt.Errorf("could not query storage systems (%v:%v): %v", q.Host, q.Port, err) + return nil, fmt.Errorf("failed to query storage systems (%v:%v): %v", q.Host, q.Port, err) } services, err := c.Service.StorageServices() if err != nil { - return nil, fmt.Errorf("could not query storage services (%v:%v): %v", q.Host, q.Port, err) + return nil, fmt.Errorf("failed to query storage services (%v:%v): %v", q.Host, q.Port, err) } data := map[string]any{ @@ -465,7 +403,7 @@ func CollectStorage(c *gofish.APIClient, q *QueryParams) ([]byte, error) { } b, err := json.MarshalIndent(data, "", " ") if err != nil { - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } return b, nil @@ -474,7 +412,7 @@ func CollectStorage(c *gofish.APIClient, q *QueryParams) ([]byte, error) { func CollectSystems(c *gofish.APIClient, q *QueryParams) ([]byte, error) { systems, err := c.Service.Systems() if err != nil { - return nil, fmt.Errorf("could not query systems (%v:%v): %v", q.Host, q.Port, err) + return nil, fmt.Errorf("failed to query systems (%v:%v): %v", q.Host, q.Port, err) } // query the system's ethernet interfaces @@ -495,7 +433,7 @@ func CollectSystems(c *gofish.APIClient, q *QueryParams) ([]byte, error) { data := map[string]any{"Systems": temp} b, err := json.MarshalIndent(data, "", " ") if err != nil { - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } return b, nil @@ -504,13 +442,13 @@ func CollectSystems(c *gofish.APIClient, q *QueryParams) ([]byte, error) { func CollectRegisteries(c *gofish.APIClient, q *QueryParams) ([]byte, error) { registries, err := c.Service.Registries() if err != nil { - return nil, fmt.Errorf("could not query storage systems (%v:%v): %v", q.Host, q.Port, err) + return nil, fmt.Errorf("failed to query storage systems (%v:%v): %v", q.Host, q.Port, err) } data := map[string]any{"Registries": registries} b, err := json.MarshalIndent(data, "", " ") if err != nil { - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } return b, nil @@ -518,7 +456,7 @@ func CollectRegisteries(c *gofish.APIClient, q *QueryParams) ([]byte, error) { func CollectProcessors(q *QueryParams) ([]byte, error) { url := baseRedfishUrl(q) + "/Systems" - res, body, err := util.MakeRequest(url, "GET", nil, nil) + res, body, err := util.MakeRequest(nil, url, "GET", nil, nil) if err != nil { return nil, fmt.Errorf("something went wrong: %v", err) } else if res == nil { @@ -537,7 +475,7 @@ func CollectProcessors(q *QueryParams) ([]byte, error) { for _, member := range members { var oid = member["@odata.id"].(string) var infoUrl = url + oid - res, _, err := util.MakeRequest(infoUrl, "GET", nil, nil) + res, _, err := util.MakeRequest(nil, infoUrl, "GET", nil, nil) if err != nil { return nil, fmt.Errorf("something went wrong: %v", err) } else if res == nil { @@ -550,7 +488,7 @@ func CollectProcessors(q *QueryParams) ([]byte, error) { data := map[string]any{"Processors": procs} b, err := json.MarshalIndent(data, "", " ") if err != nil { - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } return b, nil @@ -563,7 +501,7 @@ func connectGofish(q *QueryParams) (*gofish.APIClient, error) { } c, err := gofish.Connect(config) if err != nil { - return nil, fmt.Errorf("could not connect to redfish endpoint: %v", err) + return nil, fmt.Errorf("failed to connect to redfish endpoint: %v", err) } if c != nil { c.Service.ProtocolFeaturesSupported = gofish.ProtocolFeaturesSupported{ @@ -578,39 +516,23 @@ func connectGofish(q *QueryParams) (*gofish.APIClient, error) { func makeGofishConfig(q *QueryParams) (gofish.ClientConfig, error) { var ( - client = &http.Client{} - url = baseRedfishUrl(q) - config = gofish.ClientConfig{ - Endpoint: url, - Username: q.User, - Password: q.Pass, - Insecure: q.CaCertPath == "", - TLSHandshakeTimeout: q.Timeout, - HTTPClient: client, - // MaxConcurrentRequests: int64(q.Threads), // NOTE: this was added in latest gofish - } - ) - if q.CaCertPath != "" { - cacert, err := os.ReadFile(q.CaCertPath) - if err != nil { - return config, fmt.Errorf("failed to read CA cert file: %v", err) - } - certPool := x509.NewCertPool() - certPool.AppendCertsFromPEM(cacert) - client.Transport = &http.Transport{ - TLSClientConfig: &tls.Config{ - RootCAs: certPool, + client = &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + }, }, } - } + url = baseRedfishUrl(q) + ) return gofish.ClientConfig{ Endpoint: url, Username: q.User, Password: q.Pass, - Insecure: q.CaCertPath == "", + Insecure: true, TLSHandshakeTimeout: q.Timeout, HTTPClient: client, - // MaxConcurrentRequests: int64(q.Threads), // NOTE: this was added in latest gofish + // MaxConcurrentRequests: int64(q.Threads), // NOTE: this was added in latest version of gofish }, nil } @@ -620,7 +542,7 @@ func makeRequest[T any](client *bmclib.Client, fn func(context.Context) (T, erro err := client.Open(ctx) if err != nil { ctxCancel() - return nil, fmt.Errorf("could not open client: %v", err) + return nil, fmt.Errorf("failed to open client: %v", err) } defer client.Close(ctx) @@ -628,7 +550,7 @@ func makeRequest[T any](client *bmclib.Client, fn func(context.Context) (T, erro response, err := fn(ctx) if err != nil { ctxCancel() - return nil, fmt.Errorf("could not get response: %v", err) + return nil, fmt.Errorf("failed to get response: %v", err) } ctxCancel() @@ -638,7 +560,7 @@ func makeRequest[T any](client *bmclib.Client, fn func(context.Context) (T, erro func makeJson(object any) ([]byte, error) { b, err := json.MarshalIndent(object, "", " ") if err != nil { - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } return []byte(b), nil } diff --git a/internal/config.go b/internal/config.go index 74df39b..82df93c 100644 --- a/internal/config.go +++ b/internal/config.go @@ -21,7 +21,7 @@ func LoadConfig(path string) error { if _, ok := err.(viper.ConfigFileNotFoundError); ok { return fmt.Errorf("config file not found: %w", err) } else { - return fmt.Errorf("could not load config file: %w", err) + return fmt.Errorf("failed toload config file: %w", err) } } diff --git a/internal/db/sqlite/sqlite.go b/internal/db/sqlite/sqlite.go index 564fd0d..213b2f3 100644 --- a/internal/db/sqlite/sqlite.go +++ b/internal/db/sqlite/sqlite.go @@ -21,7 +21,7 @@ func CreateProbeResultsIfNotExists(path string) (*sqlx.DB, error) { // TODO: it may help with debugging to check for file permissions here first db, err := sqlx.Open("sqlite3", path) if err != nil { - return nil, fmt.Errorf("could not open database: %v", err) + return nil, fmt.Errorf("failed toopen database: %v", err) } db.MustExec(schema) return db, nil @@ -45,12 +45,12 @@ func InsertProbeResults(path string, states *[]magellan.ScannedResult) error { VALUES (:host, :port, :protocol, :state);` _, err := tx.NamedExec(sql, &state) if err != nil { - fmt.Printf("could not execute transaction: %v\n", err) + fmt.Printf("failed toexecute transaction: %v\n", err) } } err = tx.Commit() if err != nil { - return fmt.Errorf("could not commit transaction: %v", err) + return fmt.Errorf("failed tocommit transaction: %v", err) } return nil } @@ -61,20 +61,20 @@ func DeleteProbeResults(path string, results *[]magellan.ScannedResult) error { } db, err := sqlx.Open("sqlite3", path) if err != nil { - return fmt.Errorf("could not open database: %v", err) + return fmt.Errorf("failed toopen database: %v", err) } tx := db.MustBegin() for _, state := range *results { sql := `DELETE FROM magellan_scanned_ports WHERE host = :host, port = :port;` _, err := tx.NamedExec(sql, &state) if err != nil { - fmt.Printf("could not execute transaction: %v\n", err) + fmt.Printf("failed toexecute transaction: %v\n", err) } } err = tx.Commit() if err != nil { - return fmt.Errorf("could not commit transaction: %v", err) + return fmt.Errorf("failed tocommit transaction: %v", err) } return nil } @@ -82,13 +82,13 @@ func DeleteProbeResults(path string, results *[]magellan.ScannedResult) error { func GetProbeResults(path string) ([]magellan.ScannedResult, error) { db, err := sqlx.Open("sqlite3", path) if err != nil { - return nil, fmt.Errorf("could not open database: %v", err) + return nil, fmt.Errorf("failed toopen database: %v", err) } results := []magellan.ScannedResult{} err = db.Select(&results, "SELECT * FROM magellan_scanned_ports ORDER BY host ASC, port ASC;") if err != nil { - return nil, fmt.Errorf("could not retrieve probes: %v", err) + return nil, fmt.Errorf("failed toretrieve probes: %v", err) } return results, nil } diff --git a/internal/scan.go b/internal/scan.go index 68e8f4f..2b8c14e 100644 --- a/internal/scan.go +++ b/internal/scan.go @@ -116,7 +116,7 @@ func ScanForAssets(hosts []string, ports []int, threads int, timeout int, disabl probeResults := []ScannedResult{} for _, result := range scannedResults { url := fmt.Sprintf("https://%s:%d/redfish/v1/", result.Host, result.Port) - res, _, err := util.MakeRequest(url, "GET", nil, nil) + res, _, err := util.MakeRequest(nil, url, "GET", nil, nil) if err != nil || res == nil { continue } else if res.StatusCode != http.StatusOK { diff --git a/internal/update.go b/internal/update.go index ce5c544..e6ed6b3 100644 --- a/internal/update.go +++ b/internal/update.go @@ -39,7 +39,7 @@ func UpdateFirmware(client *bmclib.Client, l *log.Logger, q *UpdateParams) error err := client.Open(ctx) if err != nil { ctxCancel() - return fmt.Errorf("could not connect to bmc: %v", err) + return fmt.Errorf("failed toconnect to bmc: %v", err) } defer client.Close(ctx) @@ -47,7 +47,7 @@ func UpdateFirmware(client *bmclib.Client, l *log.Logger, q *UpdateParams) error file, err := os.Open(q.FirmwarePath) if err != nil { ctxCancel() - return fmt.Errorf("could not open firmware path: %v", err) + return fmt.Errorf("failed toopen firmware path: %v", err) } defer file.Close() @@ -55,7 +55,7 @@ func UpdateFirmware(client *bmclib.Client, l *log.Logger, q *UpdateParams) error taskId, err := client.FirmwareInstall(ctx, q.Component, constants.FirmwareApplyOnReset, true, file) if err != nil { ctxCancel() - return fmt.Errorf("could not install firmware: %v", err) + return fmt.Errorf("failed toinstall firmware: %v", err) } for { @@ -137,9 +137,9 @@ func UpdateFirmwareRemote(q *UpdateParams) error { } data, err := json.Marshal(b) if err != nil { - return fmt.Errorf("could not marshal data: %v", err) + return fmt.Errorf("failed tomarshal data: %v", err) } - res, body, err := util.MakeRequest(url, "POST", data, headers) + res, body, err := util.MakeRequest(nil, url, "POST", data, headers) if err != nil { return fmt.Errorf("something went wrong: %v", err) } else if res == nil { @@ -153,7 +153,7 @@ func UpdateFirmwareRemote(q *UpdateParams) error { func GetUpdateStatus(q *UpdateParams) error { url := baseRedfishUrl(&q.QueryParams) + "/redfish/v1/UpdateService" - res, body, err := util.MakeRequest(url, "GET", nil, nil) + res, body, err := util.MakeRequest(nil, url, "GET", nil, nil) if err != nil { return fmt.Errorf("something went wrong: %v", err) } else if res == nil { @@ -180,7 +180,7 @@ func GetUpdateStatus(q *UpdateParams) error { // // load file from disk // file, err := os.ReadFile(q.FirmwarePath) // if err != nil { -// return fmt.Errorf("could not read file: %v", err) +// return fmt.Errorf("failed toread file: %v", err) // } // switch q.TransferProtocol { diff --git a/internal/util/util.go b/internal/util/util.go index bed13a7..f385746 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -15,8 +15,12 @@ import ( func PathExists(path string) (bool, error) { _, err := os.Stat(path) - if err == nil { return true, nil } - if os.IsNotExist(err) { return false, nil } + if err == nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } return false, err } @@ -36,24 +40,31 @@ func GetNextIP(ip *net.IP, inc uint) *net.IP { return &r } -func MakeRequest(url string, httpMethod string, body []byte, headers map[string]string) (*http.Response, []byte, error) { - http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} +// Generic convenience function used to make HTTP requests. +func MakeRequest(client *http.Client, url string, httpMethod string, body []byte, headers map[string]string) (*http.Response, []byte, error) { + // use defaults if no client provided + if client == nil { + client = http.DefaultClient + client.Transport = &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + } req, err := http.NewRequest(httpMethod, url, bytes.NewBuffer(body)) if err != nil { - return nil, nil, fmt.Errorf("could not create new HTTP request: %v", err) + return nil, nil, fmt.Errorf("failed to create new HTTP request: %v", err) } req.Header.Add("User-Agent", "magellan") for k, v := range headers { req.Header.Add(k, v) } - res, err := http.DefaultClient.Do(req) + res, err := client.Do(req) if err != nil { - return nil, nil, fmt.Errorf("could not make request: %v", err) + return nil, nil, fmt.Errorf("failed to make request: %v", err) } b, err := io.ReadAll(res.Body) res.Body.Close() if err != nil { - return nil, nil, fmt.Errorf("could not read response body: %v", err) + return nil, nil, fmt.Errorf("failed to read response body: %v", err) } return res, b, err } @@ -65,19 +76,19 @@ func MakeOutputDirectory(path string) (string, error) { final := path + "/" + dirname // check if path is valid and directory - pathExists, err := PathExists(final); + pathExists, err := PathExists(final) if err != nil { - return final, fmt.Errorf("could not check for existing path: %v", err) + return final, fmt.Errorf("failed to check for existing path: %v", err) } if pathExists { // make sure it is directory with 0o644 permissions return final, fmt.Errorf("found existing path: %v", final) } - + // create directory with data + time err = os.MkdirAll(final, 0766) if err != nil { - return final, fmt.Errorf("could not make directory: %v", err) + return final, fmt.Errorf("failed to make directory: %v", err) } return final, nil }