2020-07-16 13:59:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func addToExclusion(name string) error {
|
2021-03-24 17:16:17 +00:00
|
|
|
csConfig.Cscli.SimulationConfig.Exclusions = append(csConfig.Cscli.SimulationConfig.Exclusions, name)
|
2020-07-16 13:59:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeFromExclusion(name string) error {
|
2021-03-24 17:16:17 +00:00
|
|
|
index := indexOf(name, csConfig.Cscli.SimulationConfig.Exclusions)
|
2020-07-16 13:59:09 +00:00
|
|
|
|
|
|
|
// Remove element from the slice
|
2021-03-24 17:16:17 +00:00
|
|
|
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]
|
2020-07-16 13:59:09 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func enableGlobalSimulation() error {
|
2021-03-24 17:16:17 +00:00
|
|
|
csConfig.Cscli.SimulationConfig.Simulation = new(bool)
|
|
|
|
*csConfig.Cscli.SimulationConfig.Simulation = true
|
|
|
|
csConfig.Cscli.SimulationConfig.Exclusions = []string{}
|
2020-07-16 13:59:09 +00:00
|
|
|
|
|
|
|
if err := dumpSimulationFile(); err != nil {
|
|
|
|
log.Fatalf("unable to dump simulation file: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("global simulation: enabled")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dumpSimulationFile() error {
|
2021-03-24 17:16:17 +00:00
|
|
|
newConfigSim, err := yaml.Marshal(csConfig.Cscli.SimulationConfig)
|
2020-07-16 13:59:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to marshal simulation configuration: %s", err)
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
err = ioutil.WriteFile(csConfig.ConfigPaths.SimulationFilePath, newConfigSim, 0644)
|
2020-07-16 13:59:09 +00:00
|
|
|
if err != nil {
|
2020-11-30 09:37:17 +00:00
|
|
|
return fmt.Errorf("write simulation config in '%s' failed: %s", csConfig.ConfigPaths.SimulationFilePath, err)
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
log.Debugf("updated simulation file %s", csConfig.ConfigPaths.SimulationFilePath)
|
2020-07-16 13:59:09 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func disableGlobalSimulation() error {
|
2021-03-24 17:16:17 +00:00
|
|
|
csConfig.Cscli.SimulationConfig.Simulation = new(bool)
|
|
|
|
*csConfig.Cscli.SimulationConfig.Simulation = false
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
csConfig.Cscli.SimulationConfig.Exclusions = []string{}
|
|
|
|
newConfigSim, err := yaml.Marshal(csConfig.Cscli.SimulationConfig)
|
2020-07-16 13:59:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to marshal new simulation configuration: %s", err)
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
err = ioutil.WriteFile(csConfig.ConfigPaths.SimulationFilePath, newConfigSim, 0644)
|
2020-07-16 13:59:09 +00:00
|
|
|
if err != nil {
|
2020-11-30 09:37:17 +00:00
|
|
|
return fmt.Errorf("unable to write new simulation config in '%s' : %s", csConfig.ConfigPaths.SimulationFilePath, err)
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("global simulation: disabled")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func simulationStatus() error {
|
2021-03-24 17:16:17 +00:00
|
|
|
if csConfig.Cscli.SimulationConfig == nil {
|
2020-07-16 13:59:09 +00:00
|
|
|
log.Printf("global simulation: disabled (configuration file is missing)")
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-24 17:16:17 +00:00
|
|
|
if *csConfig.Cscli.SimulationConfig.Simulation {
|
2020-07-16 13:59:09 +00:00
|
|
|
log.Println("global simulation: enabled")
|
2021-03-24 17:16:17 +00:00
|
|
|
if len(csConfig.Cscli.SimulationConfig.Exclusions) > 0 {
|
2020-07-16 13:59:09 +00:00
|
|
|
log.Println("Scenarios not in simulation mode :")
|
2021-03-24 17:16:17 +00:00
|
|
|
for _, scenario := range csConfig.Cscli.SimulationConfig.Exclusions {
|
2020-07-16 13:59:09 +00:00
|
|
|
log.Printf(" - %s", scenario)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Println("global simulation: disabled")
|
2021-03-24 17:16:17 +00:00
|
|
|
if len(csConfig.Cscli.SimulationConfig.Exclusions) > 0 {
|
2020-07-16 13:59:09 +00:00
|
|
|
log.Println("Scenarios in simulation mode :")
|
2021-03-24 17:16:17 +00:00
|
|
|
for _, scenario := range csConfig.Cscli.SimulationConfig.Exclusions {
|
2020-07-16 13:59:09 +00:00
|
|
|
log.Printf(" - %s", scenario)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSimulationCmds() *cobra.Command {
|
|
|
|
var cmdSimulation = &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 {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
if csConfig.Cscli == nil {
|
2020-07-16 13:59:09 +00:00
|
|
|
return fmt.Errorf("you must configure cli before using simulation")
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
cmdSimulation.Flags().SortFlags = false
|
|
|
|
cmdSimulation.PersistentFlags().SortFlags = false
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
var forceGlobalSimulation bool
|
2020-07-16 13:59:09 +00:00
|
|
|
var cmdSimulationEnable = &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) {
|
2021-03-24 17:16:17 +00:00
|
|
|
if err := csConfig.LoadHub(); err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
if err := cwhub.GetHubIdx(csConfig.Hub); err != nil {
|
2020-12-01 11:33:14 +00:00
|
|
|
log.Fatalf("Failed to get Hub index : %v", err)
|
|
|
|
log.Infoln("Run 'sudo cscli hub update' to get the hub index")
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
for _, scenario := range args {
|
2022-02-01 21:08:06 +00:00
|
|
|
var item = cwhub.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
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
if !item.Installed {
|
|
|
|
log.Warningf("'%s' isn't enabled", scenario)
|
|
|
|
}
|
2021-03-24 17:16:17 +00:00
|
|
|
isExcluded := inSlice(scenario, csConfig.Cscli.SimulationConfig.Exclusions)
|
|
|
|
if *csConfig.Cscli.SimulationConfig.Simulation && !isExcluded {
|
2020-07-16 13:59:09 +00:00
|
|
|
log.Warningf("global simulation is already enabled")
|
|
|
|
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 {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
log.Printf("simulation enabled for '%s'", scenario)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := addToExclusion(scenario); err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
log.Printf("simulation mode for '%s' enabled", scenario)
|
|
|
|
}
|
|
|
|
if err := dumpSimulationFile(); err != nil {
|
|
|
|
log.Fatalf("simulation enable: %s", err.Error())
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
} else if forceGlobalSimulation {
|
2020-07-16 13:59:09 +00:00
|
|
|
if err := enableGlobalSimulation(); err != nil {
|
|
|
|
log.Fatalf("unable to enable global simulation mode : %s", err.Error())
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
} else {
|
|
|
|
cmd.Help()
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
cmdSimulationEnable.Flags().BoolVarP(&forceGlobalSimulation, "global", "g", false, "Enable global simulation (reverse mode)")
|
2020-07-16 13:59:09 +00:00
|
|
|
cmdSimulation.AddCommand(cmdSimulationEnable)
|
|
|
|
|
|
|
|
var cmdSimulationDisable = &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 {
|
2021-03-24 17:16:17 +00:00
|
|
|
isExcluded := inSlice(scenario, csConfig.Cscli.SimulationConfig.Exclusions)
|
|
|
|
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 {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
log.Printf("simulation mode for '%s' disabled", scenario)
|
|
|
|
}
|
|
|
|
if err := dumpSimulationFile(); err != nil {
|
|
|
|
log.Fatalf("simulation disable: %s", err.Error())
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
} else if forceGlobalSimulation {
|
2020-07-16 13:59:09 +00:00
|
|
|
if err := disableGlobalSimulation(); err != nil {
|
|
|
|
log.Fatalf("unable to disable global simulation mode : %s", err.Error())
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
} else {
|
|
|
|
cmd.Help()
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
cmdSimulationDisable.Flags().BoolVarP(&forceGlobalSimulation, "global", "g", false, "Disable global simulation (reverse mode)")
|
2020-07-16 13:59:09 +00:00
|
|
|
cmdSimulation.AddCommand(cmdSimulationDisable)
|
|
|
|
|
|
|
|
var cmdSimulationStatus = &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 {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
},
|
2020-07-30 10:15:15 +00:00
|
|
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
},
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
|
|
|
cmdSimulation.AddCommand(cmdSimulationStatus)
|
|
|
|
|
|
|
|
return cmdSimulation
|
|
|
|
}
|