Skip to content

Commit

Permalink
refact "cscli simulation"
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Jan 31, 2024
1 parent 4192af3 commit 94b1d29
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 51 deletions.
4 changes: 3 additions & 1 deletion cmd/crowdsec-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ var mergedConfig string
// flagBranch overrides the value in csConfig.Cscli.HubBranch
var flagBranch = ""

type configGetter func() *csconfig.Config

func initConfig() {
var err error

Expand Down Expand Up @@ -195,7 +197,7 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
cmd.AddCommand(NewCLIDashboard().NewCommand())
cmd.AddCommand(NewCLIDecisions().NewCommand())
cmd.AddCommand(NewCLIAlerts().NewCommand())
cmd.AddCommand(NewCLISimulation().NewCommand())
cmd.AddCommand(NewCLISimulation(getconfig).NewCommand())
cmd.AddCommand(NewCLIBouncers(getconfig).NewCommand())
cmd.AddCommand(NewCLIMachines().NewCommand())
cmd.AddCommand(NewCLICapi().NewCommand())
Expand Down
94 changes: 44 additions & 50 deletions cmd/crowdsec-cli/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,34 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
)

type cliSimulation struct{}
type cliSimulation struct{
cfg configGetter
}

func NewCLISimulation() *cliSimulation {
return &cliSimulation{}
func NewCLISimulation(getconfig configGetter) *cliSimulation {
return &cliSimulation{
cfg: getconfig,
}
}

func (cli cliSimulation) NewCommand() *cobra.Command {
func (cli *cliSimulation) NewCommand() *cobra.Command {
cmd := &cobra.Command{
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`,
DisableAutoGenTag: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
if err := csConfig.LoadSimulation(); err != nil {
log.Fatal(err)
return err

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/simulation.go#L36

Added line #L36 was not covered by tests
}
if csConfig.Cscli.SimulationConfig == nil {
return fmt.Errorf("no simulation configured")
}
return nil
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
PersistentPostRun: func(cmd *cobra.Command, _ []string) {
if cmd.Name() != "status" {
log.Infof(ReloadMessage())
}
Expand All @@ -52,18 +56,18 @@ cscli simulation disable crowdsecurity/ssh-bf`,
return cmd
}

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

cmd := &cobra.Command{
Use: "enable [scenario] [-global]",
Short: "Enable the simulation, globally or on specified scenarios",
Example: `cscli simulation enable`,
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
hub, err := require.Hub(csConfig, nil, nil)
if err != nil {
log.Fatal(err)
return err

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/simulation.go#L70

Added line #L70 was not covered by tests
}

if len(args) > 0 {
Expand All @@ -86,43 +90,41 @@ func (cli cliSimulation) NewEnableCmd() *cobra.Command {
continue
}
if *csConfig.Cscli.SimulationConfig.Simulation && isExcluded {
if err := removeFromExclusion(scenario); err != nil {
log.Fatal(err)
}
cli.removeFromExclusion(scenario)

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/simulation.go#L93

Added line #L93 was not covered by tests
log.Printf("simulation enabled for '%s'", scenario)
continue
}
if err := addToExclusion(scenario); err != nil {
log.Fatal(err)
}
cli.addToExclusion(scenario)
log.Printf("simulation mode for '%s' enabled", scenario)
}
if err := dumpSimulationFile(); err != nil {
log.Fatalf("simulation enable: %s", err)
if err := cli.dumpSimulationFile(); err != nil {
return fmt.Errorf("simulation enable: %s", err)

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/simulation.go#L101

Added line #L101 was not covered by tests
}
} else if forceGlobalSimulation {
if err := enableGlobalSimulation(); err != nil {
log.Fatalf("unable to enable global simulation mode : %s", err)
if err := cli.enableGlobalSimulation(); err != nil {
return fmt.Errorf("unable to enable global simulation mode: %s", err)

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/simulation.go#L105

Added line #L105 was not covered by tests
}
} else {
printHelp(cmd)
}

return nil
},
}
cmd.Flags().BoolVarP(&forceGlobalSimulation, "global", "g", false, "Enable global simulation (reverse mode)")

return cmd
}

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

cmd := &cobra.Command{
Use: "disable [scenario]",
Short: "Disable the simulation mode. Disable only specified scenarios",
Example: `cscli simulation disable`,
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
for _, scenario := range args {
isExcluded := slices.Contains(csConfig.Cscli.SimulationConfig.Exclusions, scenario)
Expand All @@ -131,48 +133,44 @@ func (cli cliSimulation) NewDisableCmd() *cobra.Command {
continue
}
if !*csConfig.Cscli.SimulationConfig.Simulation && isExcluded {
if err := removeFromExclusion(scenario); err != nil {
log.Fatal(err)
}
cli.removeFromExclusion(scenario)
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.Fatal(err)
}
cli.addToExclusion(scenario)

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/simulation.go#L144

Added line #L144 was not covered by tests
log.Printf("simulation mode for '%s' disabled", scenario)
}
if err := dumpSimulationFile(); err != nil {
log.Fatalf("simulation disable: %s", err)
if err := cli.dumpSimulationFile(); err != nil {
return fmt.Errorf("simulation disable: %s", err)

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/simulation.go#L148

Added line #L148 was not covered by tests
}
} else if forceGlobalSimulation {
if err := disableGlobalSimulation(); err != nil {
log.Fatalf("unable to disable global simulation mode : %s", err)
if err := cli.disableGlobalSimulation(); err != nil {
return fmt.Errorf("unable to disable global simulation mode: %s", err)

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/simulation.go#L152

Added line #L152 was not covered by tests
}
} else {
printHelp(cmd)
}

return nil
},
}
cmd.Flags().BoolVarP(&forceGlobalSimulation, "global", "g", false, "Disable global simulation (reverse mode)")

return cmd
}

func (cli cliSimulation) NewStatusCmd() *cobra.Command {
func (cli *cliSimulation) NewStatusCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "status",
Short: "Show simulation mode status",
Example: `cscli simulation status`,
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
if err := simulationStatus(); err != nil {
log.Fatal(err)
}
Run: func(_ *cobra.Command, _ []string) {
cli.status()
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
},
Expand All @@ -181,37 +179,34 @@ func (cli cliSimulation) NewStatusCmd() *cobra.Command {
return cmd
}

func addToExclusion(name string) error {
func (cli *cliSimulation) addToExclusion(name string) {
csConfig.Cscli.SimulationConfig.Exclusions = append(csConfig.Cscli.SimulationConfig.Exclusions, name)
return nil
}

func removeFromExclusion(name string) error {
func (cli *cliSimulation) removeFromExclusion(name string) {
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 {
func (cli *cliSimulation) 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)
if err := cli.dumpSimulationFile(); err != nil {
return fmt.Errorf("unable to dump simulation file: %s", err)

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/simulation.go#L201

Added line #L201 was not covered by tests
}

log.Printf("global simulation: enabled")

return nil
}

func dumpSimulationFile() error {
func (cli *cliSimulation) dumpSimulationFile() error {
newConfigSim, err := yaml.Marshal(csConfig.Cscli.SimulationConfig)
if err != nil {
return fmt.Errorf("unable to marshal simulation configuration: %s", err)
Expand All @@ -225,7 +220,7 @@ func dumpSimulationFile() error {
return nil
}

func disableGlobalSimulation() error {
func (cli *cliSimulation) disableGlobalSimulation() error {
csConfig.Cscli.SimulationConfig.Simulation = new(bool)
*csConfig.Cscli.SimulationConfig.Simulation = false

Expand All @@ -243,10 +238,10 @@ func disableGlobalSimulation() error {
return nil
}

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

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/simulation.go#L244

Added line #L244 was not covered by tests
}
if *csConfig.Cscli.SimulationConfig.Simulation {
log.Println("global simulation: enabled")
Expand All @@ -265,5 +260,4 @@ func simulationStatus() error {
}
}
}
return nil
}

0 comments on commit 94b1d29

Please sign in to comment.