mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-11 12:25:53 +02:00
cscli refact: package 'clihub' (#3198)
* cscli refact: package 'clihub' * check for errors
This commit is contained in:
parent
eec32ad64b
commit
b880df9a68
8 changed files with 50 additions and 44 deletions
|
@ -482,7 +482,7 @@ issues:
|
||||||
|
|
||||||
- linters:
|
- linters:
|
||||||
- revive
|
- revive
|
||||||
path: "cmd/crowdsec-cli/item_metrics.go"
|
path: "cmd/crowdsec-cli/clihub/item_metrics.go"
|
||||||
text: "deep-exit: .*"
|
text: "deep-exit: .*"
|
||||||
|
|
||||||
- linters:
|
- linters:
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
package main
|
package clihub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
@ -11,14 +12,17 @@ import (
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type configGetter = func() *csconfig.Config
|
||||||
|
|
||||||
type cliHub struct {
|
type cliHub struct {
|
||||||
cfg configGetter
|
cfg configGetter
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCLIHub(cfg configGetter) *cliHub {
|
func New(cfg configGetter) *cliHub {
|
||||||
return &cliHub{
|
return &cliHub{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
}
|
}
|
||||||
|
@ -47,14 +51,9 @@ cscli hub upgrade`,
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliHub) list(all bool) error {
|
func (cli *cliHub) List(out io.Writer, hub *cwhub.Hub, all bool) error {
|
||||||
cfg := cli.cfg()
|
cfg := cli.cfg()
|
||||||
|
|
||||||
hub, err := require.Hub(cfg, nil, log.StandardLogger())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, v := range hub.Warnings {
|
for _, v := range hub.Warnings {
|
||||||
log.Info(v)
|
log.Info(v)
|
||||||
}
|
}
|
||||||
|
@ -65,14 +64,16 @@ func (cli *cliHub) list(all bool) error {
|
||||||
|
|
||||||
items := make(map[string][]*cwhub.Item)
|
items := make(map[string][]*cwhub.Item)
|
||||||
|
|
||||||
|
var err error
|
||||||
|
|
||||||
for _, itemType := range cwhub.ItemTypes {
|
for _, itemType := range cwhub.ItemTypes {
|
||||||
items[itemType], err = selectItems(hub, itemType, nil, !all)
|
items[itemType], err = SelectItems(hub, itemType, nil, !all)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = listItems(color.Output, cfg.Cscli.Color, cwhub.ItemTypes, items, true, cfg.Cscli.Output)
|
err = ListItems(out, cfg.Cscli.Color, cwhub.ItemTypes, items, true, cfg.Cscli.Output)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -89,7 +90,12 @@ func (cli *cliHub) newListCmd() *cobra.Command {
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(_ *cobra.Command, _ []string) error {
|
||||||
return cli.list(all)
|
hub, err := require.Hub(cli.cfg(), nil, log.StandardLogger())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return cli.List(color.Output, hub, all)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package clihub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package clihub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
|
@ -16,7 +16,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// selectItems returns a slice of items of a given type, selected by name and sorted by case-insensitive name
|
// selectItems returns a slice of items of a given type, selected by name and sorted by case-insensitive name
|
||||||
func selectItems(hub *cwhub.Hub, itemType string, args []string, installedOnly bool) ([]*cwhub.Item, error) {
|
func SelectItems(hub *cwhub.Hub, itemType string, args []string, installedOnly bool) ([]*cwhub.Item, error) {
|
||||||
allItems := hub.GetItemsByType(itemType, true)
|
allItems := hub.GetItemsByType(itemType, true)
|
||||||
|
|
||||||
itemNames := make([]string, len(allItems))
|
itemNames := make([]string, len(allItems))
|
||||||
|
@ -57,7 +57,7 @@ func selectItems(hub *cwhub.Hub, itemType string, args []string, installedOnly b
|
||||||
return wantedItems, nil
|
return wantedItems, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func listItems(out io.Writer, wantColor string, itemTypes []string, items map[string][]*cwhub.Item, omitIfEmpty bool, output string) error {
|
func ListItems(out io.Writer, wantColor string, itemTypes []string, items map[string][]*cwhub.Item, omitIfEmpty bool, output string) error {
|
||||||
switch output {
|
switch output {
|
||||||
case "human":
|
case "human":
|
||||||
nothingToDisplay := true
|
nothingToDisplay := true
|
||||||
|
@ -146,7 +146,7 @@ func listItems(out io.Writer, wantColor string, itemTypes []string, items map[st
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func inspectItem(item *cwhub.Item, showMetrics bool, output string, prometheusURL string, wantColor string) error {
|
func InspectItem(item *cwhub.Item, showMetrics bool, output string, prometheusURL string, wantColor string) error {
|
||||||
switch output {
|
switch output {
|
||||||
case "human", "raw":
|
case "human", "raw":
|
||||||
enc := yaml.NewEncoder(os.Stdout)
|
enc := yaml.NewEncoder(os.Stdout)
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package clihub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
|
@ -15,6 +15,7 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clihub"
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/reload"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/reload"
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
|
@ -372,7 +373,7 @@ func (cli cliItem) inspect(ctx context.Context, args []string, url string, diff
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = inspectItem(item, !noMetrics, cfg.Cscli.Output, cfg.Cscli.PrometheusUrl, cfg.Cscli.Color); err != nil {
|
if err = clihub.InspectItem(item, !noMetrics, cfg.Cscli.Output, cfg.Cscli.PrometheusUrl, cfg.Cscli.Color); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -428,12 +429,12 @@ func (cli cliItem) list(args []string, all bool) error {
|
||||||
|
|
||||||
items := make(map[string][]*cwhub.Item)
|
items := make(map[string][]*cwhub.Item)
|
||||||
|
|
||||||
items[cli.name], err = selectItems(hub, cli.name, args, !all)
|
items[cli.name], err = clihub.SelectItems(hub, cli.name, args, !all)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return listItems(color.Output, cfg.Cscli.Color, []string{cli.name}, items, false, cfg.Cscli.Output)
|
return clihub.ListItems(color.Output, cfg.Cscli.Color, []string{cli.name}, items, false, cfg.Cscli.Output)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli cliItem) newListCmd() *cobra.Command {
|
func (cli cliItem) newListCmd() *cobra.Command {
|
||||||
|
|
|
@ -17,6 +17,7 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clicapi"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clicapi"
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cliconsole"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cliconsole"
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cliexplain"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cliexplain"
|
||||||
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clihub"
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clihubtest"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clihubtest"
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clilapi"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clilapi"
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/climetrics"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/climetrics"
|
||||||
|
@ -249,7 +250,7 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
|
||||||
cmd.AddCommand(NewCLIDoc().NewCommand(cmd))
|
cmd.AddCommand(NewCLIDoc().NewCommand(cmd))
|
||||||
cmd.AddCommand(NewCLIVersion().NewCommand())
|
cmd.AddCommand(NewCLIVersion().NewCommand())
|
||||||
cmd.AddCommand(NewCLIConfig(cli.cfg).NewCommand())
|
cmd.AddCommand(NewCLIConfig(cli.cfg).NewCommand())
|
||||||
cmd.AddCommand(NewCLIHub(cli.cfg).NewCommand())
|
cmd.AddCommand(clihub.New(cli.cfg).NewCommand())
|
||||||
cmd.AddCommand(climetrics.New(cli.cfg).NewCommand())
|
cmd.AddCommand(climetrics.New(cli.cfg).NewCommand())
|
||||||
cmd.AddCommand(NewCLIDashboard(cli.cfg).NewCommand())
|
cmd.AddCommand(NewCLIDashboard(cli.cfg).NewCommand())
|
||||||
cmd.AddCommand(NewCLIDecisions(cli.cfg).NewCommand())
|
cmd.AddCommand(NewCLIDecisions(cli.cfg).NewCommand())
|
||||||
|
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"github.com/crowdsecurity/go-cs-lib/trace"
|
"github.com/crowdsecurity/go-cs-lib/trace"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clicapi"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clicapi"
|
||||||
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clihub"
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clilapi"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clilapi"
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/climetrics"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/climetrics"
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
|
@ -38,7 +39,7 @@ const (
|
||||||
SUPPORT_VERSION_PATH = "version.txt"
|
SUPPORT_VERSION_PATH = "version.txt"
|
||||||
SUPPORT_FEATURES_PATH = "features.txt"
|
SUPPORT_FEATURES_PATH = "features.txt"
|
||||||
SUPPORT_OS_INFO_PATH = "osinfo.txt"
|
SUPPORT_OS_INFO_PATH = "osinfo.txt"
|
||||||
SUPPORT_HUB_DIR = "hub/"
|
SUPPORT_HUB = "hub.txt"
|
||||||
SUPPORT_BOUNCERS_PATH = "lapi/bouncers.txt"
|
SUPPORT_BOUNCERS_PATH = "lapi/bouncers.txt"
|
||||||
SUPPORT_AGENTS_PATH = "lapi/agents.txt"
|
SUPPORT_AGENTS_PATH = "lapi/agents.txt"
|
||||||
SUPPORT_CROWDSEC_CONFIG_PATH = "config/crowdsec.yaml"
|
SUPPORT_CROWDSEC_CONFIG_PATH = "config/crowdsec.yaml"
|
||||||
|
@ -163,26 +164,23 @@ func (cli *cliSupport) dumpOSInfo(zw *zip.Writer) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliSupport) dumpHubItems(zw *zip.Writer, hub *cwhub.Hub, itemType string) error {
|
func (cli *cliSupport) dumpHubItems(zw *zip.Writer, hub *cwhub.Hub) error {
|
||||||
var err error
|
if hub == nil {
|
||||||
|
return errors.New("no hub connection")
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infof("Collecting hub")
|
||||||
|
|
||||||
out := new(bytes.Buffer)
|
out := new(bytes.Buffer)
|
||||||
|
|
||||||
log.Infof("Collecting hub: %s", itemType)
|
ch := clihub.New(cli.cfg)
|
||||||
|
if err := ch.List(out, hub, false); err != nil {
|
||||||
items := make(map[string][]*cwhub.Item)
|
return err
|
||||||
|
|
||||||
if items[itemType], err = selectItems(hub, itemType, nil, true); err != nil {
|
|
||||||
return fmt.Errorf("could not collect %s list: %w", itemType, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := listItems(out, cli.cfg().Cscli.Color, []string{itemType}, items, false, "human"); err != nil {
|
|
||||||
return fmt.Errorf("could not list %s: %w", itemType, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stripped := stripAnsiString(out.String())
|
stripped := stripAnsiString(out.String())
|
||||||
|
|
||||||
cli.writeToZip(zw, SUPPORT_HUB_DIR+itemType+".txt", time.Now(), strings.NewReader(stripped))
|
cli.writeToZip(zw, SUPPORT_HUB, time.Now(), strings.NewReader(stripped))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -198,7 +196,9 @@ func (cli *cliSupport) dumpBouncers(zw *zip.Writer, db *database.Client) error {
|
||||||
|
|
||||||
// call the "cscli bouncers list" command directly, skip any preRun
|
// call the "cscli bouncers list" command directly, skip any preRun
|
||||||
cm := cliBouncers{db: db, cfg: cli.cfg}
|
cm := cliBouncers{db: db, cfg: cli.cfg}
|
||||||
cm.list(out)
|
if err := cm.list(out); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
stripped := stripAnsiString(out.String())
|
stripped := stripAnsiString(out.String())
|
||||||
|
|
||||||
|
@ -218,7 +218,9 @@ func (cli *cliSupport) dumpAgents(zw *zip.Writer, db *database.Client) error {
|
||||||
|
|
||||||
// call the "cscli machines list" command directly, skip any preRun
|
// call the "cscli machines list" command directly, skip any preRun
|
||||||
cm := cliMachines{db: db, cfg: cli.cfg}
|
cm := cliMachines{db: db, cfg: cli.cfg}
|
||||||
cm.list(out)
|
if err := cm.list(out); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
stripped := stripAnsiString(out.String())
|
stripped := stripAnsiString(out.String())
|
||||||
|
|
||||||
|
@ -513,12 +515,8 @@ func (cli *cliSupport) dump(ctx context.Context, outFile string) error {
|
||||||
log.Warnf("could not collect main config file: %s", err)
|
log.Warnf("could not collect main config file: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if hub != nil {
|
if err = cli.dumpHubItems(zipWriter, hub); err != nil {
|
||||||
for _, itemType := range cwhub.ItemTypes {
|
log.Warnf("could not collect hub information: %s", err)
|
||||||
if err = cli.dumpHubItems(zipWriter, hub, itemType); err != nil {
|
|
||||||
log.Warnf("could not collect %s information: %s", itemType, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = cli.dumpBouncers(zipWriter, db); err != nil {
|
if err = cli.dumpBouncers(zipWriter, db); err != nil {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue