From f7c5726a0a0792b00fe97bca62d449ca546e4da3 Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Tue, 5 Dec 2023 17:06:25 +0100 Subject: [PATCH] minor reverts and tweaks (#2639) --- cmd/crowdsec-cli/main.go | 1 - pkg/csconfig/config.go | 3 --- pkg/csconfig/config_paths.go | 4 ---- pkg/cwhub/sync.go | 21 +++++++-------------- test/bats/20_hub.bats | 6 +++--- 5 files changed, 10 insertions(+), 25 deletions(-) diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index d5def40c7..b38d56ce9 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -55,7 +55,6 @@ func initConfig() { log.Fatal(err) } } else { - // XXX: check all the defaults csConfig = csconfig.NewDefaultConfig() } diff --git a/pkg/csconfig/config.go b/pkg/csconfig/config.go index 9e90c20cc..09f46e250 100644 --- a/pkg/csconfig/config.go +++ b/pkg/csconfig/config.go @@ -100,9 +100,6 @@ func GetConfig() Config { return globalConfig } -// XXX: We must not have a different behavior with an empty vs a missing configuration file. -// XXX: For this reason, all defaults have to come from NewConfig(). The following function should -// XXX: be replaced func NewDefaultConfig() *Config { logLevel := log.InfoLevel commonCfg := CommonCfg{ diff --git a/pkg/csconfig/config_paths.go b/pkg/csconfig/config_paths.go index 07db4bd71..71e3bacda 100644 --- a/pkg/csconfig/config_paths.go +++ b/pkg/csconfig/config_paths.go @@ -18,22 +18,18 @@ type ConfigurationPaths struct { func (c *Config) loadConfigurationPaths() error { var err error if c.ConfigPaths == nil { - // XXX: test me return fmt.Errorf("no configuration paths provided") } if c.ConfigPaths.DataDir == "" { - // XXX: test me return fmt.Errorf("please provide a data directory with the 'data_dir' directive in the 'config_paths' section") } if c.ConfigPaths.HubDir == "" { - // XXX: test me c.ConfigPaths.HubDir = filepath.Clean(c.ConfigPaths.ConfigDir + "/hub") } if c.ConfigPaths.HubIndexFile == "" { - // XXX: test me c.ConfigPaths.HubIndexFile = filepath.Clean(c.ConfigPaths.HubDir + "/.index.json") } diff --git a/pkg/cwhub/sync.go b/pkg/cwhub/sync.go index 72f97b114..ed3abc2c5 100644 --- a/pkg/cwhub/sync.go +++ b/pkg/cwhub/sync.go @@ -7,6 +7,7 @@ import ( "io" "os" "path/filepath" + "slices" "sort" "strings" @@ -112,21 +113,13 @@ func (h *Hub) getItemFileInfo(path string) (*itemFileInfo, error) { log.Tracef("stage:%s ftype:%s", ret.stage, ret.ftype) - if ret.stage == SCENARIOS { - ret.ftype = SCENARIOS + if ret.ftype != PARSERS && ret.ftype != POSTOVERFLOWS { + if !slices.Contains(ItemTypes, ret.stage) { + return nil, fmt.Errorf("unknown configuration type for file '%s'", path) + } + + ret.ftype = ret.stage ret.stage = "" - } else if ret.stage == COLLECTIONS { - ret.ftype = COLLECTIONS - ret.stage = "" - } else if ret.stage == APPSEC_RULES { - ret.ftype = APPSEC_RULES - ret.stage = "" - } else if ret.stage == APPSEC_CONFIGS { - ret.ftype = APPSEC_CONFIGS - ret.stage = "" - } else if ret.ftype != PARSERS && ret.ftype != POSTOVERFLOWS { - // it's a PARSER / POSTOVERFLOW with a stage - return nil, fmt.Errorf("unknown configuration type for file '%s'", path) } log.Tracef("CORRECTED [%s] by [%s] in stage [%s] of type [%s]", ret.fname, ret.fauthor, ret.stage, ret.ftype) diff --git a/test/bats/20_hub.bats b/test/bats/20_hub.bats index 3558e8604..0b222dde0 100644 --- a/test/bats/20_hub.bats +++ b/test/bats/20_hub.bats @@ -36,7 +36,7 @@ teardown() { rune -0 cscli hub list assert_output "No items to display" rune -0 cscli hub list -o json - assert_json '{appsec-configs:[],appsec-rules:[],parsers:[],scenarios:[],collections:[],postoverflows:[]}' + assert_json '{"appsec-configs":[],"appsec-rules":[],parsers:[],scenarios:[],collections:[],postoverflows:[]}' rune -0 cscli hub list -o raw assert_output 'name,status,version,description,type' @@ -137,7 +137,7 @@ teardown() { assert_line "collections" rune -0 cscli hub types -o human rune -0 yq -o json <(output) - assert_json '["appsec-configs","appsec-rules","parsers","postoverflows","scenarios","collections"]' + assert_json '["parsers","postoverflows","scenarios","appsec-configs","appsec-rules","collections"]' rune -0 cscli hub types -o json - assert_json '["appsec-configs","appsec-rules","parsers","postoverflows","scenarios","collections"]' + assert_json '["parsers","postoverflows","scenarios","appsec-configs","appsec-rules","collections"]' }