mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 03:27:03 -07:00
Reorganized adding cobra commands
This commit is contained in:
parent
25a885b18c
commit
c202469c50
9 changed files with 435 additions and 208 deletions
|
|
@ -6,14 +6,16 @@ package smd
|
||||||
import (
|
import (
|
||||||
"davidallendj/magellan/api"
|
"davidallendj/magellan/api"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
// hms "github.com/alexlovelltroy/hms-smd"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
var (
|
||||||
Host = "http://localhost"
|
Host = "http://localhost"
|
||||||
BaseEndpoint = "/hsm/v2"
|
BaseEndpoint = "/hsm/v2"
|
||||||
Port = 27779
|
Port = 27779
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func makeEndpointUrl(endpoint string) string {
|
func makeEndpointUrl(endpoint string) string {
|
||||||
return Host + ":" + fmt.Sprint(Port) + BaseEndpoint + endpoint
|
return Host + ":" + fmt.Sprint(Port) + BaseEndpoint + endpoint
|
||||||
}
|
}
|
||||||
|
|
@ -40,13 +42,16 @@ func GetComponentEndpoint(xname string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddRedfishEndpoint(inventory []byte) error {
|
func AddRedfishEndpoint(data []byte) error {
|
||||||
if inventory == nil {
|
if data == nil {
|
||||||
return fmt.Errorf("could not add redfish endpoint: no data found")
|
return fmt.Errorf("could not add redfish endpoint: no data found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// var ep hms.RedfishEP
|
||||||
|
// _ = ep
|
||||||
// Add redfish endpoint via POST `/hsm/v2/Inventory/RedfishEndpoints` endpoint
|
// Add redfish endpoint via POST `/hsm/v2/Inventory/RedfishEndpoints` endpoint
|
||||||
url := makeEndpointUrl("/Inventory/RedfishEndpoints")
|
url := makeEndpointUrl("/Inventory/RedfishEndpoints")
|
||||||
res, body, _ := api.MakeRequest(url, "POST", inventory)
|
res, body, _ := api.MakeRequest(url, "POST", data)
|
||||||
fmt.Println("smd url: ", url)
|
fmt.Println("smd url: ", url)
|
||||||
fmt.Println("res: ", res)
|
fmt.Println("res: ", res)
|
||||||
fmt.Println("body: ", string(body))
|
fmt.Println("body: ", string(body))
|
||||||
|
|
|
||||||
99
cmd/collect.go
Normal file
99
cmd/collect.go
Normal file
|
|
@ -0,0 +1,99 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"davidallendj/magellan/api/smd"
|
||||||
|
magellan "davidallendj/magellan/internal"
|
||||||
|
|
||||||
|
"github.com/bombsimon/logrusr/v2"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
var collectCmd = &cobra.Command{
|
||||||
|
Use: "collect",
|
||||||
|
Short: "Query information about BMC",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
// make application logger
|
||||||
|
l := logrus.New()
|
||||||
|
l.Level = logrus.DebugLevel
|
||||||
|
logger := logrusr.New(l)
|
||||||
|
|
||||||
|
// get probe states stored in db from scan
|
||||||
|
probeStates, err := magellan.GetStates(dbpath)
|
||||||
|
if err != nil {
|
||||||
|
l.Errorf("could not get states: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// use the found results to query bmc information
|
||||||
|
inventories := [][]byte{}
|
||||||
|
// users := [][]byte{}
|
||||||
|
for _, ps := range probeStates {
|
||||||
|
if !ps.State {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
logrus.Infof("querying %v\n", ps)
|
||||||
|
q := magellan.QueryParams{
|
||||||
|
Host: ps.Host,
|
||||||
|
Port: ps.Port,
|
||||||
|
User: user,
|
||||||
|
Pass: pass,
|
||||||
|
Drivers: drivers,
|
||||||
|
Timeout: timeout,
|
||||||
|
Verbose: true,
|
||||||
|
WithSecureTLS: withSecureTLS,
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := magellan.NewClient(&logger, &q)
|
||||||
|
if err != nil {
|
||||||
|
l.Errorf("could not make client: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// metadata
|
||||||
|
_, err = magellan.QueryMetadata(client, &logger, &q)
|
||||||
|
if err != nil {
|
||||||
|
l.Errorf("could not query metadata: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// inventories
|
||||||
|
inventory, err := magellan.QueryInventory(client, &logger, &q)
|
||||||
|
// inventory, err := magellan.QueryInventoryV2(q.Host, q.Port, q.User, q.Pass)
|
||||||
|
if err != nil {
|
||||||
|
l.Errorf("could not query inventory: %v\n", err)
|
||||||
|
}
|
||||||
|
inventories = append(inventories, inventory)
|
||||||
|
|
||||||
|
// users
|
||||||
|
// user, err := magellan.QueryUsers(client, &logger, &q)
|
||||||
|
// if err != nil {
|
||||||
|
// l.Errorf("could not query users: %v\n", err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // bios
|
||||||
|
// _, err = magellan.QueryBios(client, &logger, &q)
|
||||||
|
// if err != nil {
|
||||||
|
// l.Errorf("could not query bios: %v\n", err)
|
||||||
|
// }
|
||||||
|
// users = append(users, user)
|
||||||
|
}
|
||||||
|
|
||||||
|
// add all endpoints to smd
|
||||||
|
for _, inventory := range inventories {
|
||||||
|
err := smd.AddRedfishEndpoint(inventory)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorf("could not add redfish endpoint: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// confirm the inventories were added
|
||||||
|
err = smd.GetRedfishEndpoints()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorf("could not get redfish endpoints: %v\n", err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init(){
|
||||||
|
rootCmd.AddCommand(collectCmd)
|
||||||
|
}
|
||||||
26
cmd/list.go
Normal file
26
cmd/list.go
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
magellan "davidallendj/magellan/internal"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
var listCmd = &cobra.Command{
|
||||||
|
Use: "list",
|
||||||
|
Short: "List information from scan",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
probeResults, err := magellan.GetStates(dbpath)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorf("could not get probe results: %v\n", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("%v\n", probeResults)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(listCmd)
|
||||||
|
}
|
||||||
59
cmd/root.go
Normal file
59
cmd/root.go
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"davidallendj/magellan/api/smd"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
timeout int
|
||||||
|
threads int
|
||||||
|
ports []int
|
||||||
|
hosts []string
|
||||||
|
withSecureTLS bool
|
||||||
|
certPoolFile string
|
||||||
|
user string
|
||||||
|
pass string
|
||||||
|
dbpath string
|
||||||
|
drivers []string
|
||||||
|
)
|
||||||
|
|
||||||
|
// TODO: discover bmc's on network (dora)
|
||||||
|
// TODO: query bmc component information and store in db (?)
|
||||||
|
// TODO: send bmc component information to smd
|
||||||
|
|
||||||
|
var rootCmd = &cobra.Command{
|
||||||
|
Use: "magellan",
|
||||||
|
Short: "Tool for BMC discovery",
|
||||||
|
Long: "",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
if len(args) == 0 {
|
||||||
|
cmd.Help()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func Execute(){
|
||||||
|
if err := rootCmd.Execute(); err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func init(){
|
||||||
|
rootCmd.PersistentFlags().StringVar(&user, "user", "", "set the BMC user")
|
||||||
|
rootCmd.PersistentFlags().StringVar(&pass, "pass", "", "set the BMC pass")
|
||||||
|
rootCmd.PersistentFlags().StringSliceVar(&hosts, "host", []string{}, "set additional hosts")
|
||||||
|
rootCmd.PersistentFlags().StringVar(&smd.Host, "smd-host", "localhost", "set the host to the hms-smd API")
|
||||||
|
rootCmd.PersistentFlags().IntVar(&threads, "threads", -1, "set the number of threads")
|
||||||
|
rootCmd.PersistentFlags().IntVar(&timeout, "timeout", 10, "set the timeout")
|
||||||
|
rootCmd.PersistentFlags().IntSliceVar(&ports, "port", []int{}, "set the ports to scan")
|
||||||
|
rootCmd.PersistentFlags().StringSliceVar(&drivers, "driver", []string{"redfish"}, "set the BMC driver to use")
|
||||||
|
rootCmd.PersistentFlags().StringVar(&dbpath, "dbpath", ":memory:", "set the probe storage path")
|
||||||
|
rootCmd.PersistentFlags().BoolVar(&withSecureTLS, "secure-tls", false, "enable secure TLS")
|
||||||
|
rootCmd.PersistentFlags().StringVar(&certPoolFile, "cert-pool", "", "path to an file containing x509 CAs. An empty string uses the system CAs. Only takes effect when --secure-tls=true")
|
||||||
|
}
|
||||||
55
cmd/scan.go
Normal file
55
cmd/scan.go
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
magellan "davidallendj/magellan/internal"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/cznic/mathutil"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
begin uint8
|
||||||
|
end uint8
|
||||||
|
subnets []string
|
||||||
|
)
|
||||||
|
|
||||||
|
var scanCmd = &cobra.Command{
|
||||||
|
Use: "scan",
|
||||||
|
Short: "Scan for BMCs",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
// set hosts to use for scanning
|
||||||
|
hostsToScan := []string{}
|
||||||
|
if len(hosts) > 0 {
|
||||||
|
hostsToScan = hosts
|
||||||
|
} else {
|
||||||
|
for _, subnet := range subnets {
|
||||||
|
hostsToScan = append(hostsToScan, magellan.GenerateHosts(subnet, begin, end)...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set ports to use for scanning
|
||||||
|
portsToScan := []int{}
|
||||||
|
if len(ports) > 0 {
|
||||||
|
portsToScan = ports
|
||||||
|
} else {
|
||||||
|
portsToScan = append(magellan.GetDefaultPorts(), ports...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// scan and store probe data in dbPath
|
||||||
|
if threads <= 0 {
|
||||||
|
threads = mathutil.Clamp(len(hostsToScan), 1, 255)
|
||||||
|
}
|
||||||
|
probeStates := magellan.ScanForAssets(hostsToScan, portsToScan, threads, timeout)
|
||||||
|
fmt.Printf("probe states: %v\n", probeStates)
|
||||||
|
magellan.StoreStates(dbpath, &probeStates)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
scanCmd.Flags().Uint8Var(&begin, "begin", 0, "set the starting point for range of IP addresses")
|
||||||
|
scanCmd.Flags().Uint8Var(&end, "end", 255, "set the ending point for range of IP addresses")
|
||||||
|
scanCmd.Flags().StringSliceVar(&subnets, "subnet", []string{"127.0.0.0"}, "set additional subnets")
|
||||||
|
|
||||||
|
rootCmd.AddCommand(scanCmd)
|
||||||
|
}
|
||||||
7
go.mod
7
go.mod
|
|
@ -11,8 +11,10 @@ require (
|
||||||
github.com/go-logr/logr v1.2.4
|
github.com/go-logr/logr v1.2.4
|
||||||
github.com/jacobweinstock/registrar v0.4.7
|
github.com/jacobweinstock/registrar v0.4.7
|
||||||
github.com/jmoiron/sqlx v1.3.5
|
github.com/jmoiron/sqlx v1.3.5
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.6
|
||||||
github.com/sirupsen/logrus v1.9.3
|
github.com/sirupsen/logrus v1.9.3
|
||||||
github.com/spf13/pflag v1.0.5
|
github.com/spf13/cobra v1.7.0
|
||||||
|
github.com/stmcginnis/gofish v0.14.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|
@ -21,11 +23,12 @@ require (
|
||||||
github.com/bmc-toolbox/common v0.0.0-20230717121556-5eb9915a8a5a // indirect
|
github.com/bmc-toolbox/common v0.0.0-20230717121556-5eb9915a8a5a // indirect
|
||||||
github.com/hashicorp/errwrap v1.1.0 // indirect
|
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/jacobweinstock/iamt v0.0.0-20230502042727-d7cdbe67d9ef // indirect
|
github.com/jacobweinstock/iamt v0.0.0-20230502042727-d7cdbe67d9ef // indirect
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||||
github.com/satori/go.uuid v1.2.0 // indirect
|
github.com/satori/go.uuid v1.2.0 // indirect
|
||||||
github.com/stmcginnis/gofish v0.14.0 // indirect
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
github.com/stretchr/testify v1.8.3 // indirect
|
github.com/stretchr/testify v1.8.3 // indirect
|
||||||
golang.org/x/exp v0.0.0-20230127130021-4ca2cb1a16b7 // indirect
|
golang.org/x/exp v0.0.0-20230127130021-4ca2cb1a16b7 // indirect
|
||||||
golang.org/x/net v0.14.0 // indirect
|
golang.org/x/net v0.14.0 // indirect
|
||||||
|
|
|
||||||
13
go.sum
13
go.sum
|
|
@ -2,14 +2,13 @@ github.com/VictorLowther/simplexml v0.0.0-20180716164440-0bff93621230 h1:t95Grn2
|
||||||
github.com/VictorLowther/simplexml v0.0.0-20180716164440-0bff93621230/go.mod h1:t2EzW1qybnPDQ3LR/GgeF0GOzHUXT5IVMLP2gkW1cmc=
|
github.com/VictorLowther/simplexml v0.0.0-20180716164440-0bff93621230/go.mod h1:t2EzW1qybnPDQ3LR/GgeF0GOzHUXT5IVMLP2gkW1cmc=
|
||||||
github.com/VictorLowther/soap v0.0.0-20150314151524-8e36fca84b22 h1:a0MBqYm44o0NcthLKCljZHe1mxlN6oahCQHHThnSwB4=
|
github.com/VictorLowther/soap v0.0.0-20150314151524-8e36fca84b22 h1:a0MBqYm44o0NcthLKCljZHe1mxlN6oahCQHHThnSwB4=
|
||||||
github.com/VictorLowther/soap v0.0.0-20150314151524-8e36fca84b22/go.mod h1:/B7V22rcz4860iDqstGvia/2+IYWXf3/JdQCVd/1D2A=
|
github.com/VictorLowther/soap v0.0.0-20150314151524-8e36fca84b22/go.mod h1:/B7V22rcz4860iDqstGvia/2+IYWXf3/JdQCVd/1D2A=
|
||||||
github.com/bmc-toolbox/bmclib/v2 v2.0.0 h1:4iYc/TMErQ2a7geubO1W/pJZk/MhORLieQvk9m3AWLg=
|
|
||||||
github.com/bmc-toolbox/bmclib/v2 v2.0.0/go.mod h1:J8VqwJ83ciXbN4IimBYS/voa6+8GjJhzKFOXK4mLJdw=
|
|
||||||
github.com/bmc-toolbox/bmclib/v2 v2.0.1-0.20230714152943-a1b87e2ff47f h1:yyUBtuFdqEHyueFLrJIc/HtX/uhlvIWI9YipiQYqoMY=
|
github.com/bmc-toolbox/bmclib/v2 v2.0.1-0.20230714152943-a1b87e2ff47f h1:yyUBtuFdqEHyueFLrJIc/HtX/uhlvIWI9YipiQYqoMY=
|
||||||
github.com/bmc-toolbox/bmclib/v2 v2.0.1-0.20230714152943-a1b87e2ff47f/go.mod h1:a3Ra0ce/LV3wAj7AHuphlHNTx5Sg67iQqtLGr1zoqio=
|
github.com/bmc-toolbox/bmclib/v2 v2.0.1-0.20230714152943-a1b87e2ff47f/go.mod h1:a3Ra0ce/LV3wAj7AHuphlHNTx5Sg67iQqtLGr1zoqio=
|
||||||
github.com/bmc-toolbox/common v0.0.0-20230717121556-5eb9915a8a5a h1:SjtoU9dE3bYfYnPXODCunMztjoDgnE3DVJCPLBqwz6Q=
|
github.com/bmc-toolbox/common v0.0.0-20230717121556-5eb9915a8a5a h1:SjtoU9dE3bYfYnPXODCunMztjoDgnE3DVJCPLBqwz6Q=
|
||||||
github.com/bmc-toolbox/common v0.0.0-20230717121556-5eb9915a8a5a/go.mod h1:SY//n1PJjZfbFbmAsB6GvEKbc7UXz3d30s3kWxfJQ/c=
|
github.com/bmc-toolbox/common v0.0.0-20230717121556-5eb9915a8a5a/go.mod h1:SY//n1PJjZfbFbmAsB6GvEKbc7UXz3d30s3kWxfJQ/c=
|
||||||
github.com/bombsimon/logrusr/v2 v2.0.1 h1:1VgxVNQMCvjirZIYaT9JYn6sAVGVEcNtRE0y4mvaOAM=
|
github.com/bombsimon/logrusr/v2 v2.0.1 h1:1VgxVNQMCvjirZIYaT9JYn6sAVGVEcNtRE0y4mvaOAM=
|
||||||
github.com/bombsimon/logrusr/v2 v2.0.1/go.mod h1:ByVAX+vHdLGAfdroiMg6q0zgq2FODY2lc5YJvzmOJio=
|
github.com/bombsimon/logrusr/v2 v2.0.1/go.mod h1:ByVAX+vHdLGAfdroiMg6q0zgq2FODY2lc5YJvzmOJio=
|
||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 h1:iwZdTE0PVqJCos1vaoKsclOGD3ADKpshg3SRtYBbwso=
|
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 h1:iwZdTE0PVqJCos1vaoKsclOGD3ADKpshg3SRtYBbwso=
|
||||||
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548/go.mod h1:e6NPNENfs9mPDVNRekM7lKScauxd5kXTr1Mfyig6TDM=
|
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548/go.mod h1:e6NPNENfs9mPDVNRekM7lKScauxd5kXTr1Mfyig6TDM=
|
||||||
|
|
@ -27,6 +26,8 @@ github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY
|
||||||
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||||
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
||||||
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
|
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||||
github.com/jacobweinstock/iamt v0.0.0-20230502042727-d7cdbe67d9ef h1:G4k02HGmBUfJFSNu3gfKJ+ki+B3qutKsYzYndkqqKc4=
|
github.com/jacobweinstock/iamt v0.0.0-20230502042727-d7cdbe67d9ef h1:G4k02HGmBUfJFSNu3gfKJ+ki+B3qutKsYzYndkqqKc4=
|
||||||
github.com/jacobweinstock/iamt v0.0.0-20230502042727-d7cdbe67d9ef/go.mod h1:FgmiLTU6cJewV4Xgrq6m5o8CUlTQOJtqzaFLGA0mG+E=
|
github.com/jacobweinstock/iamt v0.0.0-20230502042727-d7cdbe67d9ef/go.mod h1:FgmiLTU6cJewV4Xgrq6m5o8CUlTQOJtqzaFLGA0mG+E=
|
||||||
github.com/jacobweinstock/registrar v0.4.7 h1:s4dOExccgD+Pc7rJC+f3Mc3D+NXHcXUaOibtcEsPxOc=
|
github.com/jacobweinstock/registrar v0.4.7 h1:s4dOExccgD+Pc7rJC+f3Mc3D+NXHcXUaOibtcEsPxOc=
|
||||||
|
|
@ -34,9 +35,11 @@ github.com/jacobweinstock/registrar v0.4.7/go.mod h1:PWmkdGFG5/ZdCqgMo7pvB3pXABO
|
||||||
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
|
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
|
||||||
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
|
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||||
|
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
|
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
|
||||||
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||||
|
|
@ -48,11 +51,14 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
|
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
|
||||||
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
|
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
|
||||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||||
|
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
|
||||||
|
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
|
||||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
github.com/stmcginnis/gofish v0.14.0 h1:geECNAiG33JDB2x2xDkerpOOuXFqxp5YP3EFE3vd5iM=
|
github.com/stmcginnis/gofish v0.14.0 h1:geECNAiG33JDB2x2xDkerpOOuXFqxp5YP3EFE3vd5iM=
|
||||||
|
|
@ -63,6 +69,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
|
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
|
||||||
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
|
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
|
||||||
golang.org/x/exp v0.0.0-20230127130021-4ca2cb1a16b7 h1:o7Ps2IYdzLRolS9/nadqeMSHpa9k8pu8u+VKBFUG7cQ=
|
golang.org/x/exp v0.0.0-20230127130021-4ca2cb1a16b7 h1:o7Ps2IYdzLRolS9/nadqeMSHpa9k8pu8u+VKBFUG7cQ=
|
||||||
golang.org/x/exp v0.0.0-20230127130021-4ca2cb1a16b7/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
|
golang.org/x/exp v0.0.0-20230127130021-4ca2cb1a16b7/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
|
||||||
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
|
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
|
||||||
|
|
@ -73,9 +80,11 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
|
||||||
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
|
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
|
||||||
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
|
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
|
||||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,10 @@ package magellan
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -16,6 +14,8 @@ import (
|
||||||
"github.com/go-logr/logr"
|
"github.com/go-logr/logr"
|
||||||
"github.com/jacobweinstock/registrar"
|
"github.com/jacobweinstock/registrar"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
_ "github.com/stmcginnis/gofish"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -26,22 +26,23 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type bmcProbeResult struct {
|
type bmcProbeResult struct {
|
||||||
Host string
|
Host string `json:"host"`
|
||||||
Port int
|
Port int `json:"port"`
|
||||||
Protocol string
|
Protocol string `json:"protocol"`
|
||||||
State bool
|
State bool `json:"state"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: ...params were getting too long...
|
// NOTE: ...params were getting too long...
|
||||||
type QueryParams struct {
|
type QueryParams struct {
|
||||||
Host string
|
Host string
|
||||||
Port int
|
Port int
|
||||||
User string
|
User string
|
||||||
Pass string
|
Pass string
|
||||||
Drivers []string
|
Drivers []string
|
||||||
Timeout int
|
Timeout int
|
||||||
WithSecureTLS bool
|
WithSecureTLS bool
|
||||||
CertPoolFile string
|
CertPoolFile string
|
||||||
|
Verbose bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func rawConnect(host string, ports []int, timeout int, keepOpenOnly bool) []bmcProbeResult {
|
func rawConnect(host string, ports []int, timeout int, keepOpenOnly bool) []bmcProbeResult {
|
||||||
|
|
@ -88,7 +89,7 @@ func GenerateHosts(subnet string, begin uint8, end uint8) []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ScanForAssets(hosts []string, ports []int, threads int, timeout int) []bmcProbeResult {
|
func ScanForAssets(hosts []string, ports []int, threads int, timeout int) []bmcProbeResult {
|
||||||
states := []bmcProbeResult{}
|
states := make([]bmcProbeResult, 0, len(hosts))
|
||||||
done := make(chan struct{}, threads+1)
|
done := make(chan struct{}, threads+1)
|
||||||
chanHost := make(chan string, threads+1)
|
chanHost := make(chan string, threads+1)
|
||||||
// chanPort := make(chan int, threads+1)
|
// chanPort := make(chan int, threads+1)
|
||||||
|
|
@ -133,12 +134,12 @@ func StoreStates(path string, states *[]bmcProbeResult) error {
|
||||||
|
|
||||||
// create database if it doesn't already exist
|
// create database if it doesn't already exist
|
||||||
schema := `
|
schema := `
|
||||||
CREATE IF NOT EXISTS TABLE scanned_ports (
|
CREATE TABLE IF NOT EXISTS magellan_scanned_ports (
|
||||||
host text,
|
host TEXT PRIMARY KEY NOT NULL,
|
||||||
port integer,
|
port INTEGER,
|
||||||
protocol text,
|
protocol TEXT,
|
||||||
state integer
|
state INTEGER
|
||||||
)
|
);
|
||||||
`
|
`
|
||||||
db, err := sqlx.Open("sqlite3", path)
|
db, err := sqlx.Open("sqlite3", path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -149,8 +150,12 @@ func StoreStates(path string, states *[]bmcProbeResult) error {
|
||||||
// insert all probe states into db
|
// insert all probe states into db
|
||||||
tx := db.MustBegin()
|
tx := db.MustBegin()
|
||||||
for _, state := range *states {
|
for _, state := range *states {
|
||||||
tx.NamedExec(`INSERT INTO scanned_ports (host, port, protocol, state)
|
sql := `INSERT OR REPLACE INTO magellan_scanned_ports (host, port, protocol, state)
|
||||||
VALUES (:Host, :Port, :Protocol, :State)`, &state)
|
VALUES (:host, :port, :protocol, :state);`
|
||||||
|
_, err := tx.NamedExec(sql, &state)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("could not execute transaction: %v\n", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
err = tx.Commit()
|
err = tx.Commit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -166,7 +171,7 @@ func GetStates(path string) ([]bmcProbeResult, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
results := []bmcProbeResult{}
|
results := []bmcProbeResult{}
|
||||||
err = db.Select(&results, "SELECT * FROM scanned_ports ORDER BY host ASC")
|
err = db.Select(&results, "SELECT * FROM magellan_scanned_ports ORDER BY host ASC")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not retrieve probes: %v", err)
|
return nil, fmt.Errorf("could not retrieve probes: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -177,17 +182,77 @@ func GetDefaultPorts() []int {
|
||||||
return []int{SSH_PORT, TLS_PORT, IPMI_PORT, REDFISH_PORT}
|
return []int{SSH_PORT, TLS_PORT, IPMI_PORT, REDFISH_PORT}
|
||||||
}
|
}
|
||||||
|
|
||||||
func QueryInventory(l *logr.Logger, q *QueryParams) ([]byte, error) {
|
func NewClient(l *logr.Logger, q *QueryParams) (*bmclib.Client, error) {
|
||||||
// discover.ScanAndConnect(url, user, pass, clientOpts)
|
// NOTE: bmclib.NewClient(host, port, user, pass)
|
||||||
client, err := makeClient(l, q)
|
// ...seems like the `port` params doesn't work like expected depending on interface
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("could not make query: %v", err)
|
// tr := &http.Transport{
|
||||||
|
// TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
|
// }
|
||||||
|
// httpClient := http.Client{
|
||||||
|
// Transport: tr,
|
||||||
|
// }
|
||||||
|
|
||||||
|
// init client
|
||||||
|
clientOpts := []bmclib.Option{
|
||||||
|
// bmclib.WithSecureTLS(),
|
||||||
|
// bmclib.WithHTTPClient(&httpClient),
|
||||||
|
bmclib.WithLogger(*l),
|
||||||
|
// bmclib.WithRedfishHTTPClient(&httpClient),
|
||||||
|
bmclib.WithRedfishPort(fmt.Sprint(q.Port)),
|
||||||
|
bmclib.WithRedfishUseBasicAuth(true),
|
||||||
|
bmclib.WithIpmitoolPort(fmt.Sprint(q.Port)),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// only work if valid cert is provided
|
||||||
|
if q.WithSecureTLS {
|
||||||
|
var pool *x509.CertPool
|
||||||
|
if q.CertPoolFile != "" {
|
||||||
|
pool = x509.NewCertPool()
|
||||||
|
data, err := os.ReadFile(q.CertPoolFile)
|
||||||
|
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.Sprintf("https://%s:%s@%s", q.User, q.Pass, q.Host)
|
||||||
|
url := ""
|
||||||
|
if q.WithSecureTLS {
|
||||||
|
url = "https://"
|
||||||
|
} else {
|
||||||
|
url = "http://"
|
||||||
|
}
|
||||||
|
|
||||||
|
if q.User != "" && q.Pass != "" {
|
||||||
|
url += fmt.Sprintf("%s:%s@%s", q.User, q.Pass, q.Host)
|
||||||
|
} else {
|
||||||
|
url += fmt.Sprintf("%s", 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 QueryMetadata(client *bmclib.Client, l *logr.Logger, q *QueryParams) ([]byte, error) {
|
||||||
|
// client, err := NewClient(l, q)
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, fmt.Errorf("could not make query: %v", err)
|
||||||
|
// }
|
||||||
|
|
||||||
// open BMC session and update driver registry
|
// open BMC session and update driver registry
|
||||||
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(q.Timeout))
|
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(q.Timeout))
|
||||||
|
|
||||||
client.Registry.FilterForCompatible(ctx)
|
client.Registry.FilterForCompatible(ctx)
|
||||||
err = client.Open(ctx)
|
err := client.Open(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctxCancel()
|
ctxCancel()
|
||||||
return nil, fmt.Errorf("could not open BMC client: %v", err)
|
return nil, fmt.Errorf("could not open BMC client: %v", err)
|
||||||
|
|
@ -195,6 +260,44 @@ func QueryInventory(l *logr.Logger, q *QueryParams) ([]byte, error) {
|
||||||
|
|
||||||
defer client.Close(ctx)
|
defer client.Close(ctx)
|
||||||
|
|
||||||
|
metadata := client.GetMetadata()
|
||||||
|
if err != nil {
|
||||||
|
ctxCancel()
|
||||||
|
return nil, fmt.Errorf("could not 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
if q.Verbose {
|
||||||
|
fmt.Printf("metadata: %v\n", string(b))
|
||||||
|
}
|
||||||
|
ctxCancel()
|
||||||
|
return []byte(b), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func QueryInventory(client *bmclib.Client, l *logr.Logger, q *QueryParams) ([]byte, error) {
|
||||||
|
// discover.ScanAndConnect(url, user, pass, clientOpts)
|
||||||
|
// client, err := NewClient(l, q)
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, fmt.Errorf("could not make query: %v", err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// 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 open client: %v", err)
|
||||||
|
}
|
||||||
|
defer client.Close(ctx)
|
||||||
|
|
||||||
inventory, err := client.Inventory(ctx)
|
inventory, err := client.Inventory(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctxCancel()
|
ctxCancel()
|
||||||
|
|
@ -208,22 +311,37 @@ func QueryInventory(l *logr.Logger, q *QueryParams) ([]byte, error) {
|
||||||
return nil, fmt.Errorf("could not marshal JSON: %v", err)
|
return nil, fmt.Errorf("could not marshal JSON: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("inventory: %v\n", string(b))
|
if q.Verbose {
|
||||||
|
fmt.Printf("inventory: %v\n", string(b))
|
||||||
|
}
|
||||||
ctxCancel()
|
ctxCancel()
|
||||||
return []byte(b), nil
|
return []byte(b), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func QueryUsers(l *logr.Logger, q *QueryParams) ([]byte, error) {
|
|
||||||
|
// func QueryInventoryV2(host string, port int, user string, pass string) ([]byte, error) {
|
||||||
|
// url := fmt.Sprintf("http://%s:%s@%s:%s/redfish/v1/", user, pass, host, fmt.Sprint(port))
|
||||||
|
// res, body, err := api.MakeRequest(url, "GET", nil)
|
||||||
|
// if err != nil {
|
||||||
|
// return nil , fmt.Errorf("could not get endpoint: %v", err)
|
||||||
|
// }
|
||||||
|
// fmt.Println(res)
|
||||||
|
// fmt.Println(string(body))
|
||||||
|
|
||||||
|
// return body, err
|
||||||
|
// }
|
||||||
|
|
||||||
|
func QueryUsers(client *bmclib.Client, l *logr.Logger, q *QueryParams) ([]byte, error) {
|
||||||
// discover.ScanAndConnect(url, user, pass, clientOpts)
|
// discover.ScanAndConnect(url, user, pass, clientOpts)
|
||||||
client, err := makeClient(l, q)
|
// client, err := NewClient(l, q)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return nil, fmt.Errorf("could not make query: %v", err)
|
// return nil, fmt.Errorf("could not make query: %v", err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
// open BMC session and update driver registry
|
// open BMC session and update driver registry
|
||||||
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(q.Timeout))
|
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(q.Timeout))
|
||||||
client.Registry.FilterForCompatible(ctx)
|
client.Registry.FilterForCompatible(ctx)
|
||||||
err = client.Open(ctx)
|
err := client.Open(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctxCancel()
|
ctxCancel()
|
||||||
return nil, fmt.Errorf("could not open BMC client: %v", err)
|
return nil, fmt.Errorf("could not open BMC client: %v", err)
|
||||||
|
|
@ -246,75 +364,31 @@ func QueryUsers(l *logr.Logger, q *QueryParams) ([]byte, error) {
|
||||||
|
|
||||||
// return b, nil
|
// return b, nil
|
||||||
ctxCancel()
|
ctxCancel()
|
||||||
fmt.Printf("users: %v\n", string(b))
|
if q.Verbose {
|
||||||
|
fmt.Printf("users: %v\n", string(b))
|
||||||
|
}
|
||||||
return []byte(b), nil
|
return []byte(b), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// func QueryBios(l *logr.Logger, q *QueryParams) ([]byte, error){
|
func QueryBios(client *bmclib.Client, l *logr.Logger, q *QueryParams) ([]byte, error) {
|
||||||
// client, err := makeClient(l, q)
|
// client, err := NewClient(l, q)
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
// return nil, fmt.Errorf("could not make query: %v", err)
|
// return nil, fmt.Errorf("could not make query: %v", err)
|
||||||
// }
|
// }
|
||||||
// return makeRequest(client, client.GetBiosConfiguration, q.Timeout)
|
b, err := makeRequest(client, client.GetBiosConfiguration, q.Timeout)
|
||||||
// }
|
if q.Verbose {
|
||||||
|
fmt.Printf("bios: %v\n", string(b))
|
||||||
func makeClient(l *logr.Logger, q *QueryParams) (*bmclib.Client, error) {
|
|
||||||
// NOTE: bmclib.NewClient(host, port, user, pass)
|
|
||||||
// ...seems like the `port` params doesn't work like expected depending on interface
|
|
||||||
|
|
||||||
tr := &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
||||||
}
|
}
|
||||||
httpClient := http.Client{
|
return b, err
|
||||||
Transport: tr,
|
|
||||||
}
|
|
||||||
|
|
||||||
// init client
|
|
||||||
clientOpts := []bmclib.Option{
|
|
||||||
// bmclib.WithSecureTLS(),
|
|
||||||
bmclib.WithHTTPClient(&httpClient),
|
|
||||||
bmclib.WithLogger(*l),
|
|
||||||
// bmclib.WithRedfishHTTPClient(&httpClient),
|
|
||||||
bmclib.WithRedfishPort(fmt.Sprint(q.Port)),
|
|
||||||
// bmclib.WithRedfishUseBasicAuth(true),
|
|
||||||
// bmclib.WithDellRedfishUseBasicAuth(true),
|
|
||||||
bmclib.WithIpmitoolPort(fmt.Sprint(q.Port)),
|
|
||||||
}
|
|
||||||
|
|
||||||
// only work if valid cert is provided
|
|
||||||
if q.WithSecureTLS {
|
|
||||||
var pool *x509.CertPool
|
|
||||||
if q.CertPoolFile != "" {
|
|
||||||
pool = x509.NewCertPool()
|
|
||||||
data, err := os.ReadFile(q.CertPoolFile)
|
|
||||||
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.Sprintf("https://%s:%s@%s", q.User, q.Pass, q.Host)
|
|
||||||
url := fmt.Sprintf("https://%s:%s@%s", q.User, q.Pass, q.Host)
|
|
||||||
fmt.Println("url: ", url)
|
|
||||||
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 makeRequest[T interface{}](client *bmclib.Client, fn func(context.Context) (T, error), timeout int) ([]byte, error){
|
func makeRequest[T interface{}](client *bmclib.Client, fn func(context.Context) (T, error), timeout int) ([]byte, error) {
|
||||||
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(timeout))
|
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(timeout))
|
||||||
client.Registry.FilterForCompatible(ctx)
|
client.Registry.FilterForCompatible(ctx)
|
||||||
err := client.Open(ctx)
|
err := client.Open(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctxCancel()
|
ctxCancel()
|
||||||
return nil, fmt.Errorf("could not open BMC client: %v", err)
|
return nil, fmt.Errorf("could not open client: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer client.Close(ctx)
|
defer client.Close(ctx)
|
||||||
|
|
|
||||||
107
main.go
107
main.go
|
|
@ -1,113 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
smd "davidallendj/magellan/api/smd"
|
"davidallendj/magellan/cmd"
|
||||||
magellan "davidallendj/magellan/internal"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
// smd "github.com/alexlovelltroy/hms-smd/pkg/redfish"
|
// smd "github.com/alexlovelltroy/hms-smd/pkg/redfish"
|
||||||
|
|
||||||
logrusr "github.com/bombsimon/logrusr/v2"
|
|
||||||
"github.com/cznic/mathutil"
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"github.com/spf13/pflag"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
timeout int
|
|
||||||
threads int
|
|
||||||
ports []int
|
|
||||||
subnets []string
|
|
||||||
hosts []string
|
|
||||||
withSecureTLS bool
|
|
||||||
certPoolFile string
|
|
||||||
user string
|
|
||||||
pass string
|
|
||||||
dbpath string
|
|
||||||
drivers []string
|
|
||||||
)
|
|
||||||
|
|
||||||
// TODO: discover bmc's on network (dora)
|
|
||||||
// TODO: query bmc component information and store in db (?)
|
|
||||||
// TODO: send bmc component information to smd
|
|
||||||
func main() {
|
func main() {
|
||||||
pflag.StringVar(&user, "user", "root", "set the BMC user")
|
cmd.Execute()
|
||||||
pflag.StringVar(&pass, "pass", "root_password", "set the BMC pass")
|
|
||||||
pflag.StringSliceVar(&subnets, "subnet", []string{"127.0.0.0"}, "set additional subnets")
|
|
||||||
pflag.StringSliceVar(&hosts, "host", []string{}, "set additional hosts")
|
|
||||||
pflag.IntVar(&threads, "threads", -1, "set the number of threads")
|
|
||||||
pflag.IntVar(&timeout, "timeout", 1, "set the timeout")
|
|
||||||
pflag.IntSliceVar(&ports, "port", []int{}, "set the ports to scan")
|
|
||||||
pflag.StringSliceVar(&drivers, "driver", []string{"redfish"}, "set the BMC driver to use")
|
|
||||||
pflag.StringVar(&dbpath, "dbpath", ":memory:", "set the probe storage path")
|
|
||||||
pflag.BoolVar(&withSecureTLS, "secure-tls", false, "enable secure TLS")
|
|
||||||
pflag.StringVar(&certPoolFile, "cert-pool", "", "path to an file containing x509 CAs. An empty string uses the system CAs. Only takes effect when --secure-tls=true")
|
|
||||||
pflag.Parse()
|
|
||||||
|
|
||||||
// make application logger
|
|
||||||
l := logrus.New()
|
|
||||||
l.Level = logrus.DebugLevel
|
|
||||||
logger := logrusr.New(l)
|
|
||||||
|
|
||||||
// set hosts to use for scanning
|
|
||||||
hostsToScan := []string{}
|
|
||||||
if len(hosts) > 0 {
|
|
||||||
hostsToScan = hosts
|
|
||||||
} else {
|
|
||||||
for _, subnet := range subnets {
|
|
||||||
hostsToScan = append(hostsToScan, magellan.GenerateHosts(subnet, 1, 5)...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// set ports to use for scanning
|
|
||||||
portsToScan := []int{}
|
|
||||||
if len(ports) > 0 {
|
|
||||||
portsToScan = ports
|
|
||||||
} else {
|
|
||||||
portsToScan = append(magellan.GetDefaultPorts(), ports...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// scan and store probe data in dbPath
|
|
||||||
if threads <= 0 {
|
|
||||||
threads = mathutil.Clamp(len(hostsToScan), 1, 255)
|
|
||||||
}
|
|
||||||
probeStates := magellan.ScanForAssets(hostsToScan, portsToScan, threads, timeout)
|
|
||||||
fmt.Printf("probe states: %v\n", probeStates)
|
|
||||||
magellan.StoreStates(dbpath, &probeStates)
|
|
||||||
|
|
||||||
// use the found results to query bmc information
|
|
||||||
inventories := [][]byte{}
|
|
||||||
for _, ps := range probeStates {
|
|
||||||
if !ps.State {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
logrus.Infof("querying bmc %v\n", ps)
|
|
||||||
q := magellan.QueryParams{
|
|
||||||
Host: ps.Host,
|
|
||||||
Port: ps.Port,
|
|
||||||
User: user,
|
|
||||||
Pass: pass,
|
|
||||||
Drivers: drivers,
|
|
||||||
Timeout: timeout,
|
|
||||||
}
|
|
||||||
inventory, err := magellan.QueryInventory(&logger, &q)
|
|
||||||
if err != nil {
|
|
||||||
logrus.Errorf("could not query BMC information: %v\n", err)
|
|
||||||
}
|
|
||||||
inventories = append(inventories, inventory)
|
|
||||||
}
|
|
||||||
|
|
||||||
// add all endpoints to smd
|
|
||||||
for _, inventory := range inventories {
|
|
||||||
err := smd.AddRedfishEndpoint(inventory)
|
|
||||||
if err != nil {
|
|
||||||
logrus.Errorf("could not add redfish endpoint: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// confirm the inventories were added
|
|
||||||
err := smd.GetRedfishEndpoints()
|
|
||||||
if err != nil {
|
|
||||||
logrus.Errorf("could not get redfish endpoints: %v\n", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue