2020-07-16 13:59:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-09-06 11:55:03 +00:00
|
|
|
"os"
|
2024-02-06 09:50:28 +00:00
|
|
|
"slices"
|
2020-07-16 13:59:09 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"gopkg.in/yaml.v2"
|
2022-10-13 10:28:24 +00:00
|
|
|
|
2023-10-04 08:42:47 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
2022-10-13 10:28:24 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
2020-07-16 13:59:09 +00:00
|
|
|
)
|
|
|
|
|
2024-02-06 09:50:28 +00:00
|
|
|
type cliSimulation struct {
|
2024-02-01 16:26:46 +00:00
|
|
|
cfg configGetter
|
|
|
|
}
|
2020-07-16 13:59:09 +00:00
|
|
|
|
2024-02-06 09:50:28 +00:00
|
|
|
func NewCLISimulation(cfg configGetter) *cliSimulation {
|
2024-02-01 16:26:46 +00:00
|
|
|
return &cliSimulation{
|
2024-02-06 09:50:28 +00:00
|
|
|
cfg: cfg,
|
2024-02-01 16:26:46 +00:00
|
|
|
}
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
func (cli *cliSimulation) NewCommand() *cobra.Command {
|
2023-12-11 09:32:54 +00:00
|
|
|
cmd := &cobra.Command{
|
2020-11-30 09:37:17 +00:00
|
|
|
Use: "simulation [command]",
|
|
|
|
Short: "Manage simulation status of scenarios",
|
|
|
|
Example: `cscli simulation status
|
|
|
|
cscli simulation enable crowdsecurity/ssh-bf
|
|
|
|
cscli simulation disable crowdsecurity/ssh-bf`,
|
2021-08-31 13:03:47 +00:00
|
|
|
DisableAutoGenTag: true,
|
2024-02-01 16:26:46 +00:00
|
|
|
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
|
|
|
|
if err := cli.cfg().LoadSimulation(); err != nil {
|
|
|
|
return err
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
2024-02-01 16:26:46 +00:00
|
|
|
if cli.cfg().Cscli.SimulationConfig == nil {
|
2021-03-24 17:16:17 +00:00
|
|
|
return fmt.Errorf("no simulation configured")
|
|
|
|
}
|
2024-02-06 09:50:28 +00:00
|
|
|
|
2020-07-16 13:59:09 +00:00
|
|
|
return nil
|
|
|
|
},
|
2024-02-01 16:26:46 +00:00
|
|
|
PersistentPostRun: func(cmd *cobra.Command, _ []string) {
|
2020-11-30 09:37:17 +00:00
|
|
|
if cmd.Name() != "status" {
|
2021-03-26 16:42:56 +00:00
|
|
|
log.Infof(ReloadMessage())
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2020-07-30 10:15:15 +00:00
|
|
|
},
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
2023-12-11 09:32:54 +00:00
|
|
|
cmd.Flags().SortFlags = false
|
|
|
|
cmd.PersistentFlags().SortFlags = false
|
2020-07-16 13:59:09 +00:00
|
|
|
|
2023-12-11 09:32:54 +00:00
|
|
|
cmd.AddCommand(cli.NewEnableCmd())
|
|
|
|
cmd.AddCommand(cli.NewDisableCmd())
|
|
|
|
cmd.AddCommand(cli.NewStatusCmd())
|
2023-01-19 12:29:36 +00:00
|
|
|
|
2023-12-11 09:32:54 +00:00
|
|
|
return cmd
|
2023-01-19 12:29:36 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
func (cli *cliSimulation) NewEnableCmd() *cobra.Command {
|
2020-11-30 09:37:17 +00:00
|
|
|
var forceGlobalSimulation bool
|
2023-01-19 12:29:36 +00:00
|
|
|
|
2023-12-11 09:32:54 +00:00
|
|
|
cmd := &cobra.Command{
|
2021-08-31 13:03:47 +00:00
|
|
|
Use: "enable [scenario] [-global]",
|
|
|
|
Short: "Enable the simulation, globally or on specified scenarios",
|
|
|
|
Example: `cscli simulation enable`,
|
|
|
|
DisableAutoGenTag: true,
|
2024-02-01 16:26:46 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
hub, err := require.Hub(cli.cfg(), nil, nil)
|
2023-11-24 14:57:32 +00:00
|
|
|
if err != nil {
|
2024-02-01 16:26:46 +00:00
|
|
|
return err
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
2020-07-16 13:59:09 +00:00
|
|
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
for _, scenario := range args {
|
2023-11-24 14:57:32 +00:00
|
|
|
var item = hub.GetItem(cwhub.SCENARIOS, scenario)
|
2020-11-30 09:37:17 +00:00
|
|
|
if item == nil {
|
|
|
|
log.Errorf("'%s' doesn't exist or is not a scenario", scenario)
|
|
|
|
continue
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
if !item.State.Installed {
|
2020-11-30 09:37:17 +00:00
|
|
|
log.Warningf("'%s' isn't enabled", scenario)
|
|
|
|
}
|
2024-02-01 16:26:46 +00:00
|
|
|
isExcluded := slices.Contains(cli.cfg().Cscli.SimulationConfig.Exclusions, scenario)
|
|
|
|
if *cli.cfg().Cscli.SimulationConfig.Simulation && !isExcluded {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Warning("global simulation is already enabled")
|
2020-07-16 13:59:09 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-02-01 16:26:46 +00:00
|
|
|
if !*cli.cfg().Cscli.SimulationConfig.Simulation && isExcluded {
|
2020-07-16 13:59:09 +00:00
|
|
|
log.Warningf("simulation for '%s' already enabled", scenario)
|
|
|
|
continue
|
|
|
|
}
|
2024-02-01 16:26:46 +00:00
|
|
|
if *cli.cfg().Cscli.SimulationConfig.Simulation && isExcluded {
|
|
|
|
cli.removeFromExclusion(scenario)
|
2020-07-16 13:59:09 +00:00
|
|
|
log.Printf("simulation enabled for '%s'", scenario)
|
|
|
|
continue
|
|
|
|
}
|
2024-02-01 16:26:46 +00:00
|
|
|
cli.addToExclusion(scenario)
|
2020-07-16 13:59:09 +00:00
|
|
|
log.Printf("simulation mode for '%s' enabled", scenario)
|
|
|
|
}
|
2024-02-01 16:26:46 +00:00
|
|
|
if err := cli.dumpSimulationFile(); err != nil {
|
|
|
|
return fmt.Errorf("simulation enable: %s", err)
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
} else if forceGlobalSimulation {
|
2024-02-01 16:26:46 +00:00
|
|
|
if err := cli.enableGlobalSimulation(); err != nil {
|
|
|
|
return fmt.Errorf("unable to enable global simulation mode: %s", err)
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
} else {
|
2022-03-10 12:55:25 +00:00
|
|
|
printHelp(cmd)
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
2024-02-01 16:26:46 +00:00
|
|
|
|
|
|
|
return nil
|
2020-07-16 13:59:09 +00:00
|
|
|
},
|
|
|
|
}
|
2023-12-11 09:32:54 +00:00
|
|
|
cmd.Flags().BoolVarP(&forceGlobalSimulation, "global", "g", false, "Enable global simulation (reverse mode)")
|
2023-01-19 12:29:36 +00:00
|
|
|
|
2023-12-11 09:32:54 +00:00
|
|
|
return cmd
|
2023-01-19 12:29:36 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
func (cli *cliSimulation) NewDisableCmd() *cobra.Command {
|
2023-01-19 12:29:36 +00:00
|
|
|
var forceGlobalSimulation bool
|
2020-07-16 13:59:09 +00:00
|
|
|
|
2023-12-11 09:32:54 +00:00
|
|
|
cmd := &cobra.Command{
|
2021-08-31 13:03:47 +00:00
|
|
|
Use: "disable [scenario]",
|
|
|
|
Short: "Disable the simulation mode. Disable only specified scenarios",
|
|
|
|
Example: `cscli simulation disable`,
|
|
|
|
DisableAutoGenTag: true,
|
2024-02-01 16:26:46 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-07-16 13:59:09 +00:00
|
|
|
if len(args) > 0 {
|
|
|
|
for _, scenario := range args {
|
2024-02-01 16:26:46 +00:00
|
|
|
isExcluded := slices.Contains(cli.cfg().Cscli.SimulationConfig.Exclusions, scenario)
|
|
|
|
if !*cli.cfg().Cscli.SimulationConfig.Simulation && !isExcluded {
|
2020-07-16 13:59:09 +00:00
|
|
|
log.Warningf("%s isn't in simulation mode", scenario)
|
|
|
|
continue
|
|
|
|
}
|
2024-02-01 16:26:46 +00:00
|
|
|
if !*cli.cfg().Cscli.SimulationConfig.Simulation && isExcluded {
|
|
|
|
cli.removeFromExclusion(scenario)
|
2020-07-16 13:59:09 +00:00
|
|
|
log.Printf("simulation mode for '%s' disabled", scenario)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if isExcluded {
|
|
|
|
log.Warningf("simulation mode is enabled but is already disable for '%s'", scenario)
|
|
|
|
continue
|
|
|
|
}
|
2024-02-01 16:26:46 +00:00
|
|
|
cli.addToExclusion(scenario)
|
2020-07-16 13:59:09 +00:00
|
|
|
log.Printf("simulation mode for '%s' disabled", scenario)
|
|
|
|
}
|
2024-02-01 16:26:46 +00:00
|
|
|
if err := cli.dumpSimulationFile(); err != nil {
|
|
|
|
return fmt.Errorf("simulation disable: %s", err)
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
} else if forceGlobalSimulation {
|
2024-02-01 16:26:46 +00:00
|
|
|
if err := cli.disableGlobalSimulation(); err != nil {
|
|
|
|
return fmt.Errorf("unable to disable global simulation mode: %s", err)
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
} else {
|
2022-03-10 12:55:25 +00:00
|
|
|
printHelp(cmd)
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
2024-02-01 16:26:46 +00:00
|
|
|
|
|
|
|
return nil
|
2020-07-16 13:59:09 +00:00
|
|
|
},
|
|
|
|
}
|
2023-12-11 09:32:54 +00:00
|
|
|
cmd.Flags().BoolVarP(&forceGlobalSimulation, "global", "g", false, "Disable global simulation (reverse mode)")
|
2020-07-16 13:59:09 +00:00
|
|
|
|
2023-12-11 09:32:54 +00:00
|
|
|
return cmd
|
2023-01-19 12:29:36 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
func (cli *cliSimulation) NewStatusCmd() *cobra.Command {
|
2023-12-11 09:32:54 +00:00
|
|
|
cmd := &cobra.Command{
|
2021-08-31 13:03:47 +00:00
|
|
|
Use: "status",
|
|
|
|
Short: "Show simulation mode status",
|
|
|
|
Example: `cscli simulation status`,
|
|
|
|
DisableAutoGenTag: true,
|
2024-02-01 16:26:46 +00:00
|
|
|
Run: func(_ *cobra.Command, _ []string) {
|
|
|
|
cli.status()
|
2020-07-16 13:59:09 +00:00
|
|
|
},
|
2020-07-30 10:15:15 +00:00
|
|
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
},
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 09:32:54 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
func (cli *cliSimulation) addToExclusion(name string) {
|
|
|
|
cfg := cli.cfg()
|
|
|
|
cfg.Cscli.SimulationConfig.Exclusions = append(cfg.Cscli.SimulationConfig.Exclusions, name)
|
2023-12-11 09:32:54 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
func (cli *cliSimulation) removeFromExclusion(name string) {
|
|
|
|
cfg := cli.cfg()
|
|
|
|
index := slices.Index(cfg.Cscli.SimulationConfig.Exclusions, name)
|
2023-12-11 09:32:54 +00:00
|
|
|
|
|
|
|
// Remove element from the slice
|
2024-02-01 16:26:46 +00:00
|
|
|
cfg.Cscli.SimulationConfig.Exclusions[index] = cfg.Cscli.SimulationConfig.Exclusions[len(cfg.Cscli.SimulationConfig.Exclusions)-1]
|
|
|
|
cfg.Cscli.SimulationConfig.Exclusions[len(cfg.Cscli.SimulationConfig.Exclusions)-1] = ""
|
|
|
|
cfg.Cscli.SimulationConfig.Exclusions = cfg.Cscli.SimulationConfig.Exclusions[:len(cfg.Cscli.SimulationConfig.Exclusions)-1]
|
2023-12-11 09:32:54 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
func (cli *cliSimulation) enableGlobalSimulation() error {
|
|
|
|
cfg := cli.cfg()
|
|
|
|
cfg.Cscli.SimulationConfig.Simulation = new(bool)
|
|
|
|
*cfg.Cscli.SimulationConfig.Simulation = true
|
|
|
|
cfg.Cscli.SimulationConfig.Exclusions = []string{}
|
2023-12-11 09:32:54 +00:00
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
if err := cli.dumpSimulationFile(); err != nil {
|
|
|
|
return fmt.Errorf("unable to dump simulation file: %s", err)
|
2023-12-11 09:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("global simulation: enabled")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
func (cli *cliSimulation) dumpSimulationFile() error {
|
|
|
|
cfg := cli.cfg()
|
2024-02-01 21:36:21 +00:00
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
newConfigSim, err := yaml.Marshal(cfg.Cscli.SimulationConfig)
|
2023-12-11 09:32:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to marshal simulation configuration: %s", err)
|
|
|
|
}
|
2024-02-01 21:36:21 +00:00
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
err = os.WriteFile(cfg.ConfigPaths.SimulationFilePath, newConfigSim, 0o644)
|
2023-12-11 09:32:54 +00:00
|
|
|
if err != nil {
|
2024-02-01 16:26:46 +00:00
|
|
|
return fmt.Errorf("write simulation config in '%s' failed: %s", cfg.ConfigPaths.SimulationFilePath, err)
|
2023-12-11 09:32:54 +00:00
|
|
|
}
|
2024-02-01 21:36:21 +00:00
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
log.Debugf("updated simulation file %s", cfg.ConfigPaths.SimulationFilePath)
|
2023-12-11 09:32:54 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
func (cli *cliSimulation) disableGlobalSimulation() error {
|
|
|
|
cfg := cli.cfg()
|
|
|
|
cfg.Cscli.SimulationConfig.Simulation = new(bool)
|
|
|
|
*cfg.Cscli.SimulationConfig.Simulation = false
|
2023-12-11 09:32:54 +00:00
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
cfg.Cscli.SimulationConfig.Exclusions = []string{}
|
2024-02-01 21:36:21 +00:00
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
newConfigSim, err := yaml.Marshal(cfg.Cscli.SimulationConfig)
|
2023-12-11 09:32:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to marshal new simulation configuration: %s", err)
|
|
|
|
}
|
2024-02-01 21:36:21 +00:00
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
err = os.WriteFile(cfg.ConfigPaths.SimulationFilePath, newConfigSim, 0o644)
|
2023-12-11 09:32:54 +00:00
|
|
|
if err != nil {
|
2024-02-01 21:36:21 +00:00
|
|
|
return fmt.Errorf("unable to write new simulation config in '%s': %s", cfg.ConfigPaths.SimulationFilePath, err)
|
2023-12-11 09:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("global simulation: disabled")
|
2024-02-01 21:36:21 +00:00
|
|
|
|
2023-12-11 09:32:54 +00:00
|
|
|
return nil
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
2023-12-11 09:32:54 +00:00
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
func (cli *cliSimulation) status() {
|
|
|
|
cfg := cli.cfg()
|
|
|
|
if cfg.Cscli.SimulationConfig == nil {
|
2023-12-11 09:32:54 +00:00
|
|
|
log.Printf("global simulation: disabled (configuration file is missing)")
|
2024-02-01 16:26:46 +00:00
|
|
|
return
|
2023-12-11 09:32:54 +00:00
|
|
|
}
|
2024-02-01 21:36:21 +00:00
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
if *cfg.Cscli.SimulationConfig.Simulation {
|
2023-12-11 09:32:54 +00:00
|
|
|
log.Println("global simulation: enabled")
|
2024-02-01 21:36:21 +00:00
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
if len(cfg.Cscli.SimulationConfig.Exclusions) > 0 {
|
2023-12-11 09:32:54 +00:00
|
|
|
log.Println("Scenarios not in simulation mode :")
|
2024-02-01 21:36:21 +00:00
|
|
|
|
2024-02-01 16:26:46 +00:00
|
|
|
for _, scenario := range cfg.Cscli.SimulationConfig.Exclusions {
|
2023-12-11 09:32:54 +00:00
|
|
|
log.Printf(" - %s", scenario)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Println("global simulation: disabled")
|
2024-02-01 16:26:46 +00:00
|
|
|
if len(cfg.Cscli.SimulationConfig.Exclusions) > 0 {
|
2023-12-11 09:32:54 +00:00
|
|
|
log.Println("Scenarios in simulation mode :")
|
2024-02-01 16:26:46 +00:00
|
|
|
for _, scenario := range cfg.Cscli.SimulationConfig.Exclusions {
|
2023-12-11 09:32:54 +00:00
|
|
|
log.Printf(" - %s", scenario)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|