cscli refact: extracted New.*Cmd from alerts, capi, dashboard; removed (some) globals

This commit is contained in:
Marco Mariani 2023-01-06 23:14:02 +01:00 committed by mmetc
parent 3d8c891699
commit b603bdfccc
5 changed files with 74 additions and 25 deletions

View file

@ -24,8 +24,6 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/models" "github.com/crowdsecurity/crowdsec/pkg/models"
) )
var printMachine bool
var limit *int
func DecisionsFromAlert(alert *models.Alert) string { func DecisionsFromAlert(alert *models.Alert) string {
ret := "" ret := ""
@ -146,8 +144,8 @@ func DisplayOneAlert(alert *models.Alert, withDetail bool) error {
return nil return nil
} }
func NewAlertsCmd() *cobra.Command { func NewAlertsCmd() *cobra.Command {
/* ---- ALERTS COMMAND */
var cmdAlerts = &cobra.Command{ var cmdAlerts = &cobra.Command{
Use: "alerts [action]", Use: "alerts [action]",
Short: "Manage alerts", Short: "Manage alerts",
@ -177,6 +175,16 @@ func NewAlertsCmd() *cobra.Command {
}, },
} }
cmdAlerts.AddCommand(NewAlertsListCmd())
cmdAlerts.AddCommand(NewAlertsInspectCmd())
cmdAlerts.AddCommand(NewAlertsFlushCmd())
cmdAlerts.AddCommand(NewAlertsDeleteCmd())
return cmdAlerts
}
func NewAlertsListCmd() *cobra.Command {
var alertListFilter = apiclient.AlertsListOpts{ var alertListFilter = apiclient.AlertsListOpts{
ScopeEquals: new(string), ScopeEquals: new(string),
ValueEquals: new(string), ValueEquals: new(string),
@ -188,8 +196,9 @@ func NewAlertsCmd() *cobra.Command {
TypeEquals: new(string), TypeEquals: new(string),
IncludeCAPI: new(bool), IncludeCAPI: new(bool),
} }
limit = new(int) var limit = new(int)
contained := new(bool) contained := new(bool)
var printMachine bool
var cmdAlertsList = &cobra.Command{ var cmdAlertsList = &cobra.Command{
Use: "list [filters]", Use: "list [filters]",
Short: "List alerts", Short: "List alerts",
@ -285,11 +294,15 @@ cscli alerts list --type ban`,
cmdAlertsList.Flags().BoolVar(contained, "contained", false, "query decisions contained by range") cmdAlertsList.Flags().BoolVar(contained, "contained", false, "query decisions contained by range")
cmdAlertsList.Flags().BoolVarP(&printMachine, "machine", "m", false, "print machines that sent alerts") cmdAlertsList.Flags().BoolVarP(&printMachine, "machine", "m", false, "print machines that sent alerts")
cmdAlertsList.Flags().IntVarP(limit, "limit", "l", 50, "limit size of alerts list table (0 to view all alerts)") cmdAlertsList.Flags().IntVarP(limit, "limit", "l", 50, "limit size of alerts list table (0 to view all alerts)")
cmdAlerts.AddCommand(cmdAlertsList)
return cmdAlertsList
}
func NewAlertsDeleteCmd() *cobra.Command {
var ActiveDecision *bool var ActiveDecision *bool
var AlertDeleteAll bool var AlertDeleteAll bool
var delAlertByID string var delAlertByID string
contained := new(bool)
var alertDeleteFilter = apiclient.AlertsDeleteOpts{ var alertDeleteFilter = apiclient.AlertsDeleteOpts{
ScopeEquals: new(string), ScopeEquals: new(string),
ValueEquals: new(string), ValueEquals: new(string),
@ -380,9 +393,11 @@ cscli alerts delete -s crowdsecurity/ssh-bf"`,
cmdAlertsDelete.Flags().StringVar(&delAlertByID, "id", "", "alert ID") cmdAlertsDelete.Flags().StringVar(&delAlertByID, "id", "", "alert ID")
cmdAlertsDelete.Flags().BoolVarP(&AlertDeleteAll, "all", "a", false, "delete all alerts") cmdAlertsDelete.Flags().BoolVarP(&AlertDeleteAll, "all", "a", false, "delete all alerts")
cmdAlertsDelete.Flags().BoolVar(contained, "contained", false, "query decisions contained by range") cmdAlertsDelete.Flags().BoolVar(contained, "contained", false, "query decisions contained by range")
return cmdAlertsDelete
}
cmdAlerts.AddCommand(cmdAlertsDelete)
func NewAlertsInspectCmd() *cobra.Command {
var details bool var details bool
var cmdAlertsInspect = &cobra.Command{ var cmdAlertsInspect = &cobra.Command{
Use: `inspect "alert_id"`, Use: `inspect "alert_id"`,
@ -428,8 +443,10 @@ cscli alerts delete -s crowdsecurity/ssh-bf"`,
cmdAlertsInspect.Flags().SortFlags = false cmdAlertsInspect.Flags().SortFlags = false
cmdAlertsInspect.Flags().BoolVarP(&details, "details", "d", false, "show alerts with events") cmdAlertsInspect.Flags().BoolVarP(&details, "details", "d", false, "show alerts with events")
cmdAlerts.AddCommand(cmdAlertsInspect) return cmdAlertsInspect
}
func NewAlertsFlushCmd() *cobra.Command {
var maxItems int var maxItems int
var maxAge string var maxAge string
var cmdAlertsFlush = &cobra.Command{ var cmdAlertsFlush = &cobra.Command{
@ -460,7 +477,5 @@ cscli alerts delete -s crowdsecurity/ssh-bf"`,
cmdAlertsFlush.Flags().IntVar(&maxItems, "max-items", 5000, "Maximum number of alert items to keep in the database") cmdAlertsFlush.Flags().IntVar(&maxItems, "max-items", 5000, "Maximum number of alert items to keep in the database")
cmdAlertsFlush.Flags().StringVar(&maxAge, "max-age", "7d", "Maximum age of alert items to keep in the database") cmdAlertsFlush.Flags().StringVar(&maxAge, "max-age", "7d", "Maximum age of alert items to keep in the database")
cmdAlerts.AddCommand(cmdAlertsFlush) return cmdAlertsFlush
return cmdAlerts
} }

View file

@ -191,7 +191,6 @@ func NewBouncersDeleteCmd() *cobra.Command {
} }
func NewBouncersCmd() *cobra.Command { func NewBouncersCmd() *cobra.Command {
/* ---- DECISIONS COMMAND */
var cmdBouncers = &cobra.Command{ var cmdBouncers = &cobra.Command{
Use: "bouncers [action]", Use: "bouncers [action]",
Short: "Manage bouncers [requires local API]", Short: "Manage bouncers [requires local API]",

View file

@ -19,10 +19,9 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/models" "github.com/crowdsecurity/crowdsec/pkg/models"
) )
var CAPIURLPrefix string = "v2" const CAPIBaseURL string = "https://api.crowdsec.net/"
var CAPIBaseURL string = "https://api.crowdsec.net/" const CAPIURLPrefix = "v2"
var capiUserPrefix string
var outputFile string
func NewCapiCmd() *cobra.Command { func NewCapiCmd() *cobra.Command {
var cmdCapi = &cobra.Command{ var cmdCapi = &cobra.Command{
@ -42,6 +41,17 @@ func NewCapiCmd() *cobra.Command {
}, },
} }
cmdCapi.AddCommand(NewCapiRegisterCmd())
cmdCapi.AddCommand(NewCapiStatusCmd())
return cmdCapi
}
func NewCapiRegisterCmd() *cobra.Command {
var capiUserPrefix string
var outputFile string
var cmdCapiRegister = &cobra.Command{ var cmdCapiRegister = &cobra.Command{
Use: "register", Use: "register",
Short: "Register to Central API (CAPI)", Short: "Register to Central API (CAPI)",
@ -107,8 +117,12 @@ func NewCapiCmd() *cobra.Command {
if err := cmdCapiRegister.Flags().MarkHidden("schmilblick"); err != nil { if err := cmdCapiRegister.Flags().MarkHidden("schmilblick"); err != nil {
log.Fatalf("failed to hide flag: %s", err) log.Fatalf("failed to hide flag: %s", err)
} }
cmdCapi.AddCommand(cmdCapiRegister)
return cmdCapiRegister
}
func NewCapiStatusCmd() *cobra.Command {
var cmdCapiStatus = &cobra.Command{ var cmdCapiStatus = &cobra.Command{
Use: "status", Use: "status",
Short: "Check status with the Central API (CAPI)", Short: "Check status with the Central API (CAPI)",
@ -167,7 +181,6 @@ func NewCapiCmd() *cobra.Command {
log.Infof("You can successfully interact with Central API (CAPI)") log.Infof("You can successfully interact with Central API (CAPI)")
}, },
} }
cmdCapi.AddCommand(cmdCapiStatus)
return cmdCapi return cmdCapiStatus
} }

View file

@ -87,7 +87,19 @@ cscli dashboard remove
}, },
} }
cmdDashboard.AddCommand(NewDashboardSetupCmd())
cmdDashboard.AddCommand(NewDashboardStartCmd())
cmdDashboard.AddCommand(NewDashboardStopCmd())
cmdDashboard.AddCommand(NewDashboardShowPasswordCmd())
cmdDashboard.AddCommand(NewDashboardRemoveCmd())
return cmdDashboard
}
func NewDashboardSetupCmd() *cobra.Command {
var force bool var force bool
var cmdDashSetup = &cobra.Command{ var cmdDashSetup = &cobra.Command{
Use: "setup", Use: "setup",
Short: "Setup a metabase container.", Short: "Setup a metabase container.",
@ -192,8 +204,10 @@ cscli dashboard setup -l 0.0.0.0 -p 443 --password <password>
//cmdDashSetup.Flags().StringVarP(&metabaseUser, "user", "u", "crowdsec@crowdsec.net", "metabase user") //cmdDashSetup.Flags().StringVarP(&metabaseUser, "user", "u", "crowdsec@crowdsec.net", "metabase user")
cmdDashSetup.Flags().StringVar(&metabasePassword, "password", "", "metabase password") cmdDashSetup.Flags().StringVar(&metabasePassword, "password", "", "metabase password")
cmdDashboard.AddCommand(cmdDashSetup) return cmdDashSetup
}
func NewDashboardStartCmd() *cobra.Command {
var cmdDashStart = &cobra.Command{ var cmdDashStart = &cobra.Command{
Use: "start", Use: "start",
Short: "Start the metabase container.", Short: "Start the metabase container.",
@ -212,8 +226,10 @@ cscli dashboard setup -l 0.0.0.0 -p 443 --password <password>
log.Infof("url : http://%s:%s", metabaseListenAddress, metabaseListenPort) log.Infof("url : http://%s:%s", metabaseListenAddress, metabaseListenPort)
}, },
} }
cmdDashboard.AddCommand(cmdDashStart) return cmdDashStart
}
func NewDashboardStopCmd() *cobra.Command {
var cmdDashStop = &cobra.Command{ var cmdDashStop = &cobra.Command{
Use: "stop", Use: "stop",
Short: "Stops the metabase container.", Short: "Stops the metabase container.",
@ -226,8 +242,11 @@ cscli dashboard setup -l 0.0.0.0 -p 443 --password <password>
} }
}, },
} }
cmdDashboard.AddCommand(cmdDashStop) return cmdDashStop
}
func NewDashboardShowPasswordCmd() *cobra.Command {
var cmdDashShowPassword = &cobra.Command{Use: "show-password", var cmdDashShowPassword = &cobra.Command{Use: "show-password",
Short: "displays password of metabase.", Short: "displays password of metabase.",
Args: cobra.ExactArgs(0), Args: cobra.ExactArgs(0),
@ -240,7 +259,12 @@ cscli dashboard setup -l 0.0.0.0 -p 443 --password <password>
log.Printf("'%s'", m.Config.Password) log.Printf("'%s'", m.Config.Password)
}, },
} }
cmdDashboard.AddCommand(cmdDashShowPassword) return cmdDashShowPassword
}
func NewDashboardRemoveCmd() *cobra.Command {
var force bool
var cmdDashRemove = &cobra.Command{ var cmdDashRemove = &cobra.Command{
Use: "remove", Use: "remove",
@ -303,9 +327,8 @@ cscli dashboard remove --force
} }
cmdDashRemove.Flags().BoolVarP(&force, "force", "f", false, "Remove also the metabase image") cmdDashRemove.Flags().BoolVarP(&force, "force", "f", false, "Remove also the metabase image")
cmdDashRemove.Flags().BoolVarP(&forceYes, "yes", "y", false, "force yes") cmdDashRemove.Flags().BoolVarP(&forceYes, "yes", "y", false, "force yes")
cmdDashboard.AddCommand(cmdDashRemove)
return cmdDashboard return cmdDashRemove
} }
func passwordIsValid(password string) bool { func passwordIsValid(password string) bool {

View file

@ -263,7 +263,6 @@ func FormatPrometheusMetrics(out io.Writer, url string, formatType string) error
var noUnit bool var noUnit bool
func NewMetricsCmd() *cobra.Command { func NewMetricsCmd() *cobra.Command {
/* ---- UPDATE COMMAND */
var cmdMetrics = &cobra.Command{ var cmdMetrics = &cobra.Command{
Use: "metrics", Use: "metrics",
Short: "Display crowdsec prometheus metrics.", Short: "Display crowdsec prometheus metrics.",