Skip to content

Commit

Permalink
cscli refact - cscli Simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Dec 8, 2023
1 parent 82c17fe commit 18b72da
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 104 deletions.
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
rootCmd.AddCommand(NewCLIDashboard().NewCommand())
rootCmd.AddCommand(NewCLIDecisions().NewCommand())
rootCmd.AddCommand(NewCLIAlerts().NewCommand())
rootCmd.AddCommand(NewSimulationCmds())
rootCmd.AddCommand(NewCLISimulation().NewCommand())
rootCmd.AddCommand(NewCLIBouncers().NewCommand())
rootCmd.AddCommand(NewCLIMachines().NewCommand())
rootCmd.AddCommand(NewCLICapi().NewCommand())
Expand Down
213 changes: 110 additions & 103 deletions cmd/crowdsec-cli/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,95 +13,14 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
)

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
}
type cliSimulation struct {}

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
func NewCLISimulation() *cliSimulation {
return &cliSimulation{}
}

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
}

func NewSimulationCmds() *cobra.Command {
var cmdSimulation = &cobra.Command{
func (cli cliSimulation) NewCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "simulation [command]",
Short: "Manage simulation status of scenarios",
Example: `cscli simulation status
Expand All @@ -123,20 +42,20 @@ cscli simulation disable crowdsecurity/ssh-bf`,
}
},
}
cmdSimulation.Flags().SortFlags = false
cmdSimulation.PersistentFlags().SortFlags = false
cmd.Flags().SortFlags = false
cmd.PersistentFlags().SortFlags = false

cmdSimulation.AddCommand(NewSimulationEnableCmd())
cmdSimulation.AddCommand(NewSimulationDisableCmd())
cmdSimulation.AddCommand(NewSimulationStatusCmd())
cmd.AddCommand(cli.NewEnableCmd())
cmd.AddCommand(cli.NewDisableCmd())
cmd.AddCommand(cli.NewStatusCmd())

return cmdSimulation
return cmd
}

func NewSimulationEnableCmd() *cobra.Command {
func (cli cliSimulation) NewEnableCmd() *cobra.Command {
var forceGlobalSimulation bool

var cmdSimulationEnable = &cobra.Command{
cmd := &cobra.Command{
Use: "enable [scenario] [-global]",
Short: "Enable the simulation, globally or on specified scenarios",
Example: `cscli simulation enable`,
Expand Down Expand Up @@ -190,15 +109,15 @@ func NewSimulationEnableCmd() *cobra.Command {
}
},
}
cmdSimulationEnable.Flags().BoolVarP(&forceGlobalSimulation, "global", "g", false, "Enable global simulation (reverse mode)")
cmd.Flags().BoolVarP(&forceGlobalSimulation, "global", "g", false, "Enable global simulation (reverse mode)")

return cmdSimulationEnable
return cmd
}

func NewSimulationDisableCmd() *cobra.Command {
func (cli cliSimulation) NewDisableCmd() *cobra.Command {
var forceGlobalSimulation bool

var cmdSimulationDisable = &cobra.Command{
cmd := &cobra.Command{
Use: "disable [scenario]",
Short: "Disable the simulation mode. Disable only specified scenarios",
Example: `cscli simulation disable`,
Expand Down Expand Up @@ -239,13 +158,13 @@ func NewSimulationDisableCmd() *cobra.Command {
}
},
}
cmdSimulationDisable.Flags().BoolVarP(&forceGlobalSimulation, "global", "g", false, "Disable global simulation (reverse mode)")
cmd.Flags().BoolVarP(&forceGlobalSimulation, "global", "g", false, "Disable global simulation (reverse mode)")

return cmdSimulationDisable
return cmd
}

func NewSimulationStatusCmd() *cobra.Command {
var cmdSimulationStatus = &cobra.Command{
func (cli cliSimulation) NewStatusCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "status",
Short: "Show simulation mode status",
Example: `cscli simulation status`,
Expand All @@ -259,5 +178,93 @@ func NewSimulationStatusCmd() *cobra.Command {
},
}

return cmdSimulationStatus
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)
}

Check warning on line 207 in cmd/crowdsec-cli/simulation.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/simulation.go#L206-L207

Added lines #L206 - L207 were not covered by tests

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)
}

Check warning on line 218 in cmd/crowdsec-cli/simulation.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/simulation.go#L217-L218

Added lines #L217 - L218 were not covered by tests
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)
}

Check warning on line 222 in cmd/crowdsec-cli/simulation.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/simulation.go#L221-L222

Added lines #L221 - L222 were not covered by tests
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)
}

Check warning on line 236 in cmd/crowdsec-cli/simulation.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/simulation.go#L235-L236

Added lines #L235 - L236 were not covered by tests
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)
}

Check warning on line 240 in cmd/crowdsec-cli/simulation.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/simulation.go#L239-L240

Added lines #L239 - L240 were not covered by tests

log.Printf("global simulation: disabled")
return nil
}

func simulationStatus() error {
if csConfig.Cscli.SimulationConfig == nil {
log.Printf("global simulation: disabled (configuration file is missing)")
return nil
}

Check warning on line 250 in cmd/crowdsec-cli/simulation.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/simulation.go#L248-L250

Added lines #L248 - L250 were not covered by tests
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)
}

Check warning on line 257 in cmd/crowdsec-cli/simulation.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/simulation.go#L254-L257

Added lines #L254 - L257 were not covered by tests
}
} 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)
}

Check warning on line 265 in cmd/crowdsec-cli/simulation.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/simulation.go#L262-L265

Added lines #L262 - L265 were not covered by tests
}
}
return nil
}

0 comments on commit 18b72da

Please sign in to comment.