2020-07-16 13:59:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-09-06 11:55:03 +00:00
|
|
|
"os"
|
2020-07-16 13:59:09 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"gopkg.in/yaml.v2"
|
2023-11-24 14:57:32 +00:00
|
|
|
"slices"
|
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
|
|
|
)
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
type cliSimulation struct{}
|
2020-07-16 13:59:09 +00:00
|
|
|
|
2023-12-11 09:32:54 +00:00
|
|
|
func NewCLISimulation() *cliSimulation {
|
|
|
|
return &cliSimulation{}
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 09:32:54 +00:00
|
|
|
func (cli cliSimulation) NewCommand() *cobra.Command {
|
|
|
|
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,
|
2020-07-16 13:59:09 +00:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
2021-03-24 17:16:17 +00:00
|
|
|
if err := csConfig.LoadSimulation(); err != nil {
|
2022-11-29 08:16:07 +00:00
|
|
|
log.Fatal(err)
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
if csConfig.Cscli.SimulationConfig == nil {
|
|
|
|
return fmt.Errorf("no simulation configured")
|
|
|
|
}
|
2020-07-16 13:59:09 +00:00
|
|
|
return nil
|
|
|
|
},
|
2020-07-30 10:15:15 +00:00
|
|
|
PersistentPostRun: func(cmd *cobra.Command, args []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
|
|
|
}
|
|
|
|
|
2023-12-11 09:32:54 +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,
|
2020-07-16 13:59:09 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2023-12-19 16:20:09 +00:00
|
|
|
hub, err := require.Hub(csConfig, nil, nil)
|
2023-11-24 14:57:32 +00:00
|
|
|
if err != nil {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Fatal(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)
|
|
|
|
}
|
2023-05-31 10:39:22 +00:00
|
|
|
isExcluded := slices.Contains(csConfig.Cscli.SimulationConfig.Exclusions, scenario)
|
2021-03-24 17:16:17 +00:00
|
|
|
if *csConfig.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
|
|
|
|
}
|
2021-03-24 17:16:17 +00:00
|
|
|
if !*csConfig.Cscli.SimulationConfig.Simulation && isExcluded {
|
2020-07-16 13:59:09 +00:00
|
|
|
log.Warningf("simulation for '%s' already enabled", scenario)
|
|
|
|
continue
|
|
|
|
}
|
2021-03-24 17:16:17 +00:00
|
|
|
if *csConfig.Cscli.SimulationConfig.Simulation && isExcluded {
|
2020-07-16 13:59:09 +00:00
|
|
|
if err := removeFromExclusion(scenario); err != nil {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Fatal(err)
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
|
|
|
log.Printf("simulation enabled for '%s'", scenario)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := addToExclusion(scenario); err != nil {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Fatal(err)
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
|
|
|
log.Printf("simulation mode for '%s' enabled", scenario)
|
|
|
|
}
|
|
|
|
if err := dumpSimulationFile(); err != nil {
|
2022-06-22 13:53:53 +00:00
|
|
|
log.Fatalf("simulation enable: %s", err)
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
} else if forceGlobalSimulation {
|
2020-07-16 13:59:09 +00:00
|
|
|
if err := enableGlobalSimulation(); err != nil {
|
2022-06-22 13:53:53 +00:00
|
|
|
log.Fatalf("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
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-12-11 09:32:54 +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,
|
2020-07-16 13:59:09 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if len(args) > 0 {
|
|
|
|
for _, scenario := range args {
|
2023-05-31 10:39:22 +00:00
|
|
|
isExcluded := slices.Contains(csConfig.Cscli.SimulationConfig.Exclusions, scenario)
|
2021-03-24 17:16:17 +00:00
|
|
|
if !*csConfig.Cscli.SimulationConfig.Simulation && !isExcluded {
|
2020-07-16 13:59:09 +00:00
|
|
|
log.Warningf("%s isn't in simulation mode", scenario)
|
|
|
|
continue
|
|
|
|
}
|
2021-03-24 17:16:17 +00:00
|
|
|
if !*csConfig.Cscli.SimulationConfig.Simulation && isExcluded {
|
2020-07-16 13:59:09 +00:00
|
|
|
if err := removeFromExclusion(scenario); err != nil {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Fatal(err)
|
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
|
|
|
|
}
|
|
|
|
if err := addToExclusion(scenario); err != nil {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Fatal(err)
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
|
|
|
log.Printf("simulation mode for '%s' disabled", scenario)
|
|
|
|
}
|
|
|
|
if err := dumpSimulationFile(); err != nil {
|
2022-06-22 13:53:53 +00:00
|
|
|
log.Fatalf("simulation disable: %s", err)
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
} else if forceGlobalSimulation {
|
2020-07-16 13:59:09 +00:00
|
|
|
if err := disableGlobalSimulation(); err != nil {
|
2022-06-22 13:53:53 +00:00
|
|
|
log.Fatalf("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
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-12-11 09:32:54 +00:00
|
|
|
func (cli cliSimulation) NewStatusCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2021-08-31 13:03:47 +00:00
|
|
|
Use: "status",
|
|
|
|
Short: "Show simulation mode status",
|
|
|
|
Example: `cscli simulation status`,
|
|
|
|
DisableAutoGenTag: true,
|
2020-07-16 13:59:09 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if err := simulationStatus(); err != nil {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Fatal(err)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func addToExclusion(name string) error {
|
|
|
|
csConfig.Cscli.SimulationConfig.Exclusions = append(csConfig.Cscli.SimulationConfig.Exclusions, name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeFromExclusion(name string) error {
|
|
|
|
index := slices.Index(csConfig.Cscli.SimulationConfig.Exclusions, name)
|
|
|
|
|
|
|
|
// Remove element from the slice
|
|
|
|
csConfig.Cscli.SimulationConfig.Exclusions[index] = csConfig.Cscli.SimulationConfig.Exclusions[len(csConfig.Cscli.SimulationConfig.Exclusions)-1]
|
|
|
|
csConfig.Cscli.SimulationConfig.Exclusions[len(csConfig.Cscli.SimulationConfig.Exclusions)-1] = ""
|
|
|
|
csConfig.Cscli.SimulationConfig.Exclusions = csConfig.Cscli.SimulationConfig.Exclusions[:len(csConfig.Cscli.SimulationConfig.Exclusions)-1]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func enableGlobalSimulation() error {
|
|
|
|
csConfig.Cscli.SimulationConfig.Simulation = new(bool)
|
|
|
|
*csConfig.Cscli.SimulationConfig.Simulation = true
|
|
|
|
csConfig.Cscli.SimulationConfig.Exclusions = []string{}
|
|
|
|
|
|
|
|
if err := dumpSimulationFile(); err != nil {
|
|
|
|
log.Fatalf("unable to dump simulation file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("global simulation: enabled")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dumpSimulationFile() error {
|
|
|
|
newConfigSim, err := yaml.Marshal(csConfig.Cscli.SimulationConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to marshal simulation configuration: %s", err)
|
|
|
|
}
|
|
|
|
err = os.WriteFile(csConfig.ConfigPaths.SimulationFilePath, newConfigSim, 0o644)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("write simulation config in '%s' failed: %s", csConfig.ConfigPaths.SimulationFilePath, err)
|
|
|
|
}
|
|
|
|
log.Debugf("updated simulation file %s", csConfig.ConfigPaths.SimulationFilePath)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func disableGlobalSimulation() error {
|
|
|
|
csConfig.Cscli.SimulationConfig.Simulation = new(bool)
|
|
|
|
*csConfig.Cscli.SimulationConfig.Simulation = false
|
|
|
|
|
|
|
|
csConfig.Cscli.SimulationConfig.Exclusions = []string{}
|
|
|
|
newConfigSim, err := yaml.Marshal(csConfig.Cscli.SimulationConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to marshal new simulation configuration: %s", err)
|
|
|
|
}
|
|
|
|
err = os.WriteFile(csConfig.ConfigPaths.SimulationFilePath, newConfigSim, 0o644)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to write new simulation config in '%s' : %s", csConfig.ConfigPaths.SimulationFilePath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("global simulation: disabled")
|
|
|
|
return nil
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
2023-12-11 09:32:54 +00:00
|
|
|
|
|
|
|
func simulationStatus() error {
|
|
|
|
if csConfig.Cscli.SimulationConfig == nil {
|
|
|
|
log.Printf("global simulation: disabled (configuration file is missing)")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if *csConfig.Cscli.SimulationConfig.Simulation {
|
|
|
|
log.Println("global simulation: enabled")
|
|
|
|
if len(csConfig.Cscli.SimulationConfig.Exclusions) > 0 {
|
|
|
|
log.Println("Scenarios not in simulation mode :")
|
|
|
|
for _, scenario := range csConfig.Cscli.SimulationConfig.Exclusions {
|
|
|
|
log.Printf(" - %s", scenario)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Println("global simulation: disabled")
|
|
|
|
if len(csConfig.Cscli.SimulationConfig.Exclusions) > 0 {
|
|
|
|
log.Println("Scenarios in simulation mode :")
|
|
|
|
for _, scenario := range csConfig.Cscli.SimulationConfig.Exclusions {
|
|
|
|
log.Printf(" - %s", scenario)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|