diff --git a/cmd/crowdsec-cli/capi.go b/cmd/crowdsec-cli/capi.go index 1888aa3545a..1155c12c61d 100644 --- a/cmd/crowdsec-cli/capi.go +++ b/cmd/crowdsec-cli/capi.go @@ -156,13 +156,10 @@ func QueryCAPIStatus(hub *cwhub.Hub, credURL string, login string, password stri return false, false, fmt.Errorf("parsing api url: %w", err) } - scenarios, err := hub.GetInstalledNamesByType(cwhub.SCENARIOS) - if err != nil { - return false, false, fmt.Errorf("failed to get scenarios: %w", err) - } + itemsForAPI := hub.GetInstalledListForAPI() - if len(scenarios) == 0 { - return false, false, errors.New("no scenarios installed, abort") + if len(itemsForAPI) == 0 { + return false, false, errors.New("no scenarios or appsec-rules installed, abort") } passwd := strfmt.Password(password) @@ -170,26 +167,14 @@ func QueryCAPIStatus(hub *cwhub.Hub, credURL string, login string, password stri client, err := apiclient.NewClient(&apiclient.Config{ MachineID: login, Password: passwd, - Scenarios: scenarios, + Scenarios: itemsForAPI, UserAgent: cwversion.UserAgent(), URL: apiURL, //I don't believe papi is neede to check enrollement //PapiURL: papiURL, VersionPrefix: "v3", UpdateScenario: func() ([]string, error) { - l_scenarios, err := hub.GetInstalledNamesByType(cwhub.SCENARIOS) - if err != nil { - return nil, err - } - appsecRules, err := hub.GetInstalledNamesByType(cwhub.APPSEC_RULES) - if err != nil { - return nil, err - } - ret := make([]string, 0, len(l_scenarios)+len(appsecRules)) - ret = append(ret, l_scenarios...) - ret = append(ret, appsecRules...) - - return ret, nil + return itemsForAPI, nil }, }) @@ -202,7 +187,7 @@ func QueryCAPIStatus(hub *cwhub.Hub, credURL string, login string, password stri t := models.WatcherAuthRequest{ MachineID: &login, Password: &pw, - Scenarios: scenarios, + Scenarios: itemsForAPI, } authResp, _, err := client.Auth.AuthenticateWatcher(context.Background(), t) diff --git a/cmd/crowdsec-cli/console.go b/cmd/crowdsec-cli/console.go index 979c9f0ea60..72c0f6c2cfe 100644 --- a/cmd/crowdsec-cli/console.go +++ b/cmd/crowdsec-cli/console.go @@ -23,7 +23,6 @@ import ( "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require" "github.com/crowdsecurity/crowdsec/pkg/apiclient" "github.com/crowdsecurity/crowdsec/pkg/csconfig" - "github.com/crowdsecurity/crowdsec/pkg/cwhub" "github.com/crowdsecurity/crowdsec/pkg/cwversion" "github.com/crowdsecurity/crowdsec/pkg/types" ) @@ -74,20 +73,6 @@ func (cli *cliConsole) enroll(key string, name string, overwrite bool, tags []st return fmt.Errorf("could not parse CAPI URL: %w", err) } - hub, err := require.Hub(cfg, nil, nil) - if err != nil { - return err - } - - scenarios, err := hub.GetInstalledNamesByType(cwhub.SCENARIOS) - if err != nil { - return fmt.Errorf("failed to get installed scenarios: %w", err) - } - - if len(scenarios) == 0 { - scenarios = make([]string, 0) - } - enableOpts := []string{csconfig.SEND_MANUAL_SCENARIOS, csconfig.SEND_TAINTED_SCENARIOS} if len(opts) != 0 { @@ -125,10 +110,15 @@ func (cli *cliConsole) enroll(key string, name string, overwrite bool, tags []st } } + hub, err := require.Hub(cfg, nil, nil) + if err != nil { + return err + } + c, _ := apiclient.NewClient(&apiclient.Config{ MachineID: cli.cfg().API.Server.OnlineClient.Credentials.Login, Password: password, - Scenarios: scenarios, + Scenarios: hub.GetInstalledListForAPI(), UserAgent: cwversion.UserAgent(), URL: apiURL, VersionPrefix: "v3", diff --git a/cmd/crowdsec-cli/hub.go b/cmd/crowdsec-cli/hub.go index 7e00eb64b33..e6cba08940a 100644 --- a/cmd/crowdsec-cli/hub.go +++ b/cmd/crowdsec-cli/hub.go @@ -148,16 +148,11 @@ func (cli *cliHub) upgrade(ctx context.Context, force bool) error { } for _, itemType := range cwhub.ItemTypes { - items, err := hub.GetInstalledItemsByType(itemType) - if err != nil { - return err - } - updated := 0 log.Infof("Upgrading %s", itemType) - for _, item := range items { + for _, item := range hub.GetInstalledByType(itemType, true) { didUpdate, err := item.Upgrade(ctx, force) if err != nil { return err diff --git a/cmd/crowdsec-cli/item_suggest.go b/cmd/crowdsec-cli/item_suggest.go index 0ea656549ba..7d3e1e728ae 100644 --- a/cmd/crowdsec-cli/item_suggest.go +++ b/cmd/crowdsec-cli/item_suggest.go @@ -19,7 +19,7 @@ func suggestNearestMessage(hub *cwhub.Hub, itemType string, itemName string) str score := 100 nearest := "" - for _, item := range hub.GetItemMap(itemType) { + for _, item := range hub.GetItemsByType(itemType, false) { d := levenshtein.Distance(itemName, item.Name, nil) if d < score { score = d @@ -44,7 +44,7 @@ func compAllItems(itemType string, args []string, toComplete string, cfg configG comp := make([]string, 0) - for _, item := range hub.GetItemMap(itemType) { + for _, item := range hub.GetItemsByType(itemType, false) { if !slices.Contains(args, item.Name) && strings.Contains(item.Name, toComplete) { comp = append(comp, item.Name) } @@ -61,22 +61,14 @@ func compInstalledItems(itemType string, args []string, toComplete string, cfg c return nil, cobra.ShellCompDirectiveDefault } - items, err := hub.GetInstalledNamesByType(itemType) - if err != nil { - cobra.CompDebugln(fmt.Sprintf("list installed %s err: %s", itemType, err), true) - return nil, cobra.ShellCompDirectiveDefault - } + items := hub.GetInstalledByType(itemType, true) comp := make([]string, 0) - if toComplete != "" { - for _, item := range items { - if strings.Contains(item, toComplete) { - comp = append(comp, item) - } + for _, item := range items { + if strings.Contains(item.Name, toComplete) { + comp = append(comp, item.Name) } - } else { - comp = items } cobra.CompDebugln(fmt.Sprintf("%s: %+v", itemType, comp), true) diff --git a/cmd/crowdsec-cli/itemcli.go b/cmd/crowdsec-cli/itemcli.go index 64c18ae89b1..0a2c1b095e3 100644 --- a/cmd/crowdsec-cli/itemcli.go +++ b/cmd/crowdsec-cli/itemcli.go @@ -147,19 +147,14 @@ func (cli cliItem) remove(args []string, purge bool, force bool, all bool) error } if all { - getter := hub.GetInstalledItemsByType + itemGetter := hub.GetInstalledByType if purge { - getter = hub.GetItemsByType - } - - items, err := getter(cli.name) - if err != nil { - return err + itemGetter = hub.GetItemsByType } removed := 0 - for _, item := range items { + for _, item := range itemGetter(cli.name, true) { didRemove, err := item.Remove(purge, force) if err != nil { return err @@ -262,14 +257,9 @@ func (cli cliItem) upgrade(ctx context.Context, args []string, force bool, all b } if all { - items, err := hub.GetInstalledItemsByType(cli.name) - if err != nil { - return err - } - updated := 0 - for _, item := range items { + for _, item := range hub.GetInstalledByType(cli.name, true) { didUpdate, err := item.Upgrade(ctx, force) if err != nil { return err diff --git a/cmd/crowdsec-cli/items.go b/cmd/crowdsec-cli/items.go index b0c03922166..5a4fee4d582 100644 --- a/cmd/crowdsec-cli/items.go +++ b/cmd/crowdsec-cli/items.go @@ -17,7 +17,12 @@ import ( // selectItems returns a slice of items of a given type, selected by name and sorted by case-insensitive name func selectItems(hub *cwhub.Hub, itemType string, args []string, installedOnly bool) ([]*cwhub.Item, error) { - itemNames := hub.GetNamesByType(itemType) + allItems := hub.GetItemsByType(itemType, true) + + itemNames := make([]string, len(allItems)) + for idx, item := range allItems { + itemNames[idx] = item.Name + } notExist := []string{} @@ -38,7 +43,7 @@ func selectItems(hub *cwhub.Hub, itemType string, args []string, installedOnly b installedOnly = false } - items := make([]*cwhub.Item, 0, len(itemNames)) + wantedItems := make([]*cwhub.Item, 0, len(itemNames)) for _, itemName := range itemNames { item := hub.GetItem(itemType, itemName) @@ -46,12 +51,10 @@ func selectItems(hub *cwhub.Hub, itemType string, args []string, installedOnly b continue } - items = append(items, item) + wantedItems = append(wantedItems, item) } - cwhub.SortItemSlice(items) - - return items, nil + return wantedItems, nil } func listItems(out io.Writer, wantColor string, itemTypes []string, items map[string][]*cwhub.Item, omitIfEmpty bool, output string) error { diff --git a/cmd/crowdsec-cli/lapi.go b/cmd/crowdsec-cli/lapi.go index 0b8bc59dad5..4e104109e84 100644 --- a/cmd/crowdsec-cli/lapi.go +++ b/cmd/crowdsec-cli/lapi.go @@ -45,11 +45,6 @@ func QueryLAPIStatus(hub *cwhub.Hub, credURL string, login string, password stri return fmt.Errorf("parsing api url: %w", err) } - scenarios, err := hub.GetInstalledNamesByType(cwhub.SCENARIOS) - if err != nil { - return fmt.Errorf("failed to get scenarios: %w", err) - } - client, err := apiclient.NewDefaultClient(apiURL, LAPIURLPrefix, cwversion.UserAgent(), @@ -60,10 +55,12 @@ func QueryLAPIStatus(hub *cwhub.Hub, credURL string, login string, password stri pw := strfmt.Password(password) + itemsForAPI := hub.GetInstalledListForAPI() + t := models.WatcherAuthRequest{ MachineID: &login, Password: &pw, - Scenarios: scenarios, + Scenarios: itemsForAPI, } _, _, err = client.Auth.AuthenticateWatcher(context.Background(), t) diff --git a/cmd/crowdsec/lapiclient.go b/cmd/crowdsec/lapiclient.go index 6cc0fba9515..cbafb460042 100644 --- a/cmd/crowdsec/lapiclient.go +++ b/cmd/crowdsec/lapiclient.go @@ -16,20 +16,6 @@ import ( ) func AuthenticatedLAPIClient(credentials csconfig.ApiCredentialsCfg, hub *cwhub.Hub) (*apiclient.ApiClient, error) { - scenarios, err := hub.GetInstalledNamesByType(cwhub.SCENARIOS) - if err != nil { - return nil, fmt.Errorf("loading list of installed hub scenarios: %w", err) - } - - appsecRules, err := hub.GetInstalledNamesByType(cwhub.APPSEC_RULES) - if err != nil { - return nil, fmt.Errorf("loading list of installed hub appsec rules: %w", err) - } - - installedScenariosAndAppsecRules := make([]string, 0, len(scenarios)+len(appsecRules)) - installedScenariosAndAppsecRules = append(installedScenariosAndAppsecRules, scenarios...) - installedScenariosAndAppsecRules = append(installedScenariosAndAppsecRules, appsecRules...) - apiURL, err := url.Parse(credentials.URL) if err != nil { return nil, fmt.Errorf("parsing api url ('%s'): %w", credentials.URL, err) @@ -42,28 +28,18 @@ func AuthenticatedLAPIClient(credentials csconfig.ApiCredentialsCfg, hub *cwhub. password := strfmt.Password(credentials.Password) + itemsForAPI := hub.GetInstalledListForAPI() + client, err := apiclient.NewClient(&apiclient.Config{ MachineID: credentials.Login, Password: password, - Scenarios: installedScenariosAndAppsecRules, + Scenarios: itemsForAPI, UserAgent: cwversion.UserAgent(), URL: apiURL, PapiURL: papiURL, VersionPrefix: "v1", UpdateScenario: func() ([]string, error) { - scenarios, err := hub.GetInstalledNamesByType(cwhub.SCENARIOS) - if err != nil { - return nil, err - } - appsecRules, err := hub.GetInstalledNamesByType(cwhub.APPSEC_RULES) - if err != nil { - return nil, err - } - ret := make([]string, 0, len(scenarios)+len(appsecRules)) - ret = append(ret, scenarios...) - ret = append(ret, appsecRules...) - - return ret, nil + return itemsForAPI, nil }, }) if err != nil { @@ -73,7 +49,7 @@ func AuthenticatedLAPIClient(credentials csconfig.ApiCredentialsCfg, hub *cwhub. authResp, _, err := client.Auth.AuthenticateWatcher(context.Background(), models.WatcherAuthRequest{ MachineID: &credentials.Login, Password: &password, - Scenarios: installedScenariosAndAppsecRules, + Scenarios: itemsForAPI, }) if err != nil { return nil, fmt.Errorf("authenticate watcher (%s): %w", credentials.Login, err) diff --git a/cmd/crowdsec/lpmetrics.go b/cmd/crowdsec/lpmetrics.go index 0fd27054071..f074910f55c 100644 --- a/cmd/crowdsec/lpmetrics.go +++ b/cmd/crowdsec/lpmetrics.go @@ -46,10 +46,8 @@ func getHubState(hub *cwhub.Hub) models.HubItems { for _, itemType := range cwhub.ItemTypes { ret[itemType] = []models.HubItem{} - items, _ := hub.GetInstalledItemsByType(itemType) - cwhub.SortItemSlice(items) - for _, item := range items { + for _, item := range hub.GetInstalledByType(itemType, true) { status := "official" if item.State.IsLocal() { status = "custom" diff --git a/cmd/crowdsec/main.go b/cmd/crowdsec/main.go index 18416e044e7..6d8ca24c335 100644 --- a/cmd/crowdsec/main.go +++ b/cmd/crowdsec/main.go @@ -91,10 +91,8 @@ func LoadBuckets(cConfig *csconfig.Config, hub *cwhub.Hub) error { files []string ) - for _, hubScenarioItem := range hub.GetItemMap(cwhub.SCENARIOS) { - if hubScenarioItem.State.Installed { - files = append(files, hubScenarioItem.State.LocalPath) - } + for _, hubScenarioItem := range hub.GetInstalledByType(cwhub.SCENARIOS, false) { + files = append(files, hubScenarioItem.State.LocalPath) } buckets = leakybucket.NewBuckets() diff --git a/pkg/alertcontext/config.go b/pkg/alertcontext/config.go index 21d16db3972..7348c326379 100644 --- a/pkg/alertcontext/config.go +++ b/pkg/alertcontext/config.go @@ -104,14 +104,9 @@ func LoadConsoleContext(c *csconfig.Config, hub *cwhub.Hub) error { c.Crowdsec.ContextToSend = make(map[string][]string, 0) if hub != nil { - items, err := hub.GetInstalledItemsByType(cwhub.CONTEXTS) - if err != nil { - return err - } - - for _, item := range items { + for _, item := range hub.GetInstalledByType(cwhub.CONTEXTS, true) { // context in item files goes under the key 'context' - if err = addContextFromItem(c.Crowdsec.ContextToSend, item); err != nil { + if err := addContextFromItem(c.Crowdsec.ContextToSend, item); err != nil { return err } } diff --git a/pkg/appsec/appsec.go b/pkg/appsec/appsec.go index 96f977b4738..54ec748744d 100644 --- a/pkg/appsec/appsec.go +++ b/pkg/appsec/appsec.go @@ -177,19 +177,13 @@ func (wc *AppsecConfig) LoadByPath(file string) error { } func (wc *AppsecConfig) Load(configName string) error { - appsecConfigs := hub.GetItemMap(cwhub.APPSEC_CONFIGS) + item := hub.GetItem(cwhub.APPSEC_CONFIGS, configName) - for _, hubAppsecConfigItem := range appsecConfigs { - if !hubAppsecConfigItem.State.Installed { - continue - } - if hubAppsecConfigItem.Name != configName { - continue - } - wc.Logger.Infof("loading %s", hubAppsecConfigItem.State.LocalPath) - err := wc.LoadByPath(hubAppsecConfigItem.State.LocalPath) + if item != nil && item.State.Installed { + wc.Logger.Infof("loading %s", item.State.LocalPath) + err := wc.LoadByPath(item.State.LocalPath) if err != nil { - return fmt.Errorf("unable to load appsec-config %s : %s", hubAppsecConfigItem.State.LocalPath, err) + return fmt.Errorf("unable to load appsec-config %s : %s", item.State.LocalPath, err) } return nil } diff --git a/pkg/appsec/loader.go b/pkg/appsec/loader.go index 56ec23e3671..1c897f9dd20 100644 --- a/pkg/appsec/loader.go +++ b/pkg/appsec/loader.go @@ -17,11 +17,7 @@ func LoadAppsecRules(hubInstance *cwhub.Hub) error { hub = hubInstance appsecRules = make(map[string]AppsecCollectionConfig) - for _, hubAppsecRuleItem := range hub.GetItemMap(cwhub.APPSEC_RULES) { - if !hubAppsecRuleItem.State.Installed { - continue - } - + for _, hubAppsecRuleItem := range hub.GetInstalledByType(cwhub.APPSEC_RULES, false) { content, err := os.ReadFile(hubAppsecRuleItem.State.LocalPath) if err != nil { log.Warnf("unable to read file %s : %s", hubAppsecRuleItem.State.LocalPath, err) diff --git a/pkg/cwhub/cwhub.go b/pkg/cwhub/cwhub.go index 0a9cc443ce0..d8607e7e562 100644 --- a/pkg/cwhub/cwhub.go +++ b/pkg/cwhub/cwhub.go @@ -4,7 +4,6 @@ import ( "fmt" "net/http" "path/filepath" - "sort" "strings" "time" @@ -45,10 +44,3 @@ func safePath(dir, filePath string) (string, error) { return absFilePath, nil } - -// SortItemSlice sorts a slice of items by name, case insensitive. -func SortItemSlice(items []*Item) { - sort.Slice(items, func(i, j int) bool { - return strings.ToLower(items[i].Name) < strings.ToLower(items[j].Name) - }) -} diff --git a/pkg/cwhub/doc.go b/pkg/cwhub/doc.go index 89d8de0fa8b..f86b95c6454 100644 --- a/pkg/cwhub/doc.go +++ b/pkg/cwhub/doc.go @@ -74,7 +74,7 @@ // Now you can use the hub object to access the existing items: // // // list all the parsers -// for _, parser := range hub.GetItemMap(cwhub.PARSERS) { +// for _, parser := range hub.GetItemsByType(cwhub.PARSERS, false) { // fmt.Printf("parser: %s\n", parser.Name) // } // diff --git a/pkg/cwhub/hub.go b/pkg/cwhub/hub.go index 1293d6fa235..d52b6c12006 100644 --- a/pkg/cwhub/hub.go +++ b/pkg/cwhub/hub.go @@ -8,11 +8,11 @@ import ( "io" "os" "path" - "slices" "strings" "github.com/sirupsen/logrus" + "github.com/crowdsecurity/go-cs-lib/maptools" "github.com/crowdsecurity/crowdsec/pkg/csconfig" ) @@ -117,13 +117,14 @@ func (h *Hub) ItemStats() []string { tainted := 0 for _, itemType := range ItemTypes { - if len(h.GetItemMap(itemType)) == 0 { + items := h.GetItemsByType(itemType, false) + if len(items) == 0 { continue } - loaded += fmt.Sprintf("%d %s, ", len(h.GetItemMap(itemType)), itemType) + loaded += fmt.Sprintf("%d %s, ", len(items), itemType) - for _, item := range h.GetItemMap(itemType) { + for _, item := range items { if item.State.IsLocal() { local++ } @@ -218,73 +219,62 @@ func (h *Hub) GetItemFQ(itemFQName string) (*Item, error) { return i, nil } -// GetNamesByType returns a slice of (full) item names for a given type -// (eg. for collections: crowdsecurity/apache2 crowdsecurity/nginx). -func (h *Hub) GetNamesByType(itemType string) []string { - m := h.GetItemMap(itemType) - if m == nil { - return nil - } +// GetItemsByType returns a slice of all the items of a given type, installed or not, optionally sorted by case-insensitive name. +// A non-existent type will silently return an empty slice. +func (h *Hub) GetItemsByType(itemType string, sorted bool) []*Item { + items := h.items[itemType] - names := make([]string, 0, len(m)) - for k := range m { - names = append(names, k) - } + ret := make([]*Item, len(items)) - return names -} + if sorted { + for idx, name := range maptools.SortedKeysNoCase(items) { + ret[idx] = items[name] + } -// GetItemsByType returns a slice of all the items of a given type, installed or not. -func (h *Hub) GetItemsByType(itemType string) ([]*Item, error) { - if !slices.Contains(ItemTypes, itemType) { - return nil, fmt.Errorf("invalid item type %s", itemType) + return ret } - items := h.items[itemType] - - ret := make([]*Item, len(items)) - idx := 0 - for _, item := range items { ret[idx] = item - idx++ + idx += 1 } - return ret, nil + return ret } -// GetInstalledItemsByType returns a slice of the installed items of a given type. -func (h *Hub) GetInstalledItemsByType(itemType string) ([]*Item, error) { - if !slices.Contains(ItemTypes, itemType) { - return nil, fmt.Errorf("invalid item type %s", itemType) - } - - items := h.items[itemType] +// GetInstalledByType returns a slice of all the installed items of a given type, optionally sorted by case-insensitive name. +// A non-existent type will silently return an empty slice. +func (h *Hub) GetInstalledByType(itemType string, sorted bool) []*Item { + ret := make([]*Item, 0) - retItems := make([]*Item, 0) - - for _, item := range items { + for _, item := range h.GetItemsByType(itemType, sorted) { if item.State.Installed { - retItems = append(retItems, item) + ret = append(ret, item) } } - return retItems, nil + return ret } -// GetInstalledNamesByType returns the names of the installed items of a given type. -func (h *Hub) GetInstalledNamesByType(itemType string) ([]string, error) { - items, err := h.GetInstalledItemsByType(itemType) - if err != nil { - return nil, err - } +// GetInstalledListForAPI returns a slice of names of all the installed scenarios and appsec-rules. +// The returned list is sorted by type (scenarios first) and case-insensitive name. +func (h *Hub) GetInstalledListForAPI() []string { + scenarios := h.GetInstalledByType(SCENARIOS, true) + appsecRules := h.GetInstalledByType(APPSEC_RULES, true) + + ret := make([]string, len(scenarios)+len(appsecRules)) - retStr := make([]string, len(items)) + idx := 0 + for _, item := range scenarios { + ret[idx] = item.Name + idx += 1 + } - for idx, it := range items { - retStr[idx] = it.Name + for _, item := range appsecRules { + ret[idx] = item.Name + idx += 1 } - return retStr, nil + return ret } diff --git a/pkg/hubtest/hubtest_item.go b/pkg/hubtest/hubtest_item.go index da4969ee8dd..42792413b5d 100644 --- a/pkg/hubtest/hubtest_item.go +++ b/pkg/hubtest/hubtest_item.go @@ -223,39 +223,30 @@ func (t *HubTestItem) InstallHub() error { ctx := context.Background() // install data for parsers if needed - ret := hub.GetItemMap(cwhub.PARSERS) - for parserName, item := range ret { - if item.State.Installed { - if err := item.DownloadDataIfNeeded(ctx, true); err != nil { - return fmt.Errorf("unable to download data for parser '%s': %+v", parserName, err) - } - - log.Debugf("parser '%s' installed successfully in runtime environment", parserName) + for _, item := range hub.GetInstalledByType(cwhub.PARSERS, true) { + if err := item.DownloadDataIfNeeded(ctx, true); err != nil { + return fmt.Errorf("unable to download data for parser '%s': %+v", item.Name, err) } + + log.Debugf("parser '%s' installed successfully in runtime environment", item.Name) } // install data for scenarios if needed - ret = hub.GetItemMap(cwhub.SCENARIOS) - for scenarioName, item := range ret { - if item.State.Installed { - if err := item.DownloadDataIfNeeded(ctx, true); err != nil { - return fmt.Errorf("unable to download data for parser '%s': %+v", scenarioName, err) - } - - log.Debugf("scenario '%s' installed successfully in runtime environment", scenarioName) + for _, item := range hub.GetInstalledByType(cwhub.SCENARIOS, true) { + if err := item.DownloadDataIfNeeded(ctx, true); err != nil { + return fmt.Errorf("unable to download data for parser '%s': %+v", item.Name, err) } + + log.Debugf("scenario '%s' installed successfully in runtime environment", item.Name) } // install data for postoverflows if needed - ret = hub.GetItemMap(cwhub.POSTOVERFLOWS) - for postoverflowName, item := range ret { - if item.State.Installed { - if err := item.DownloadDataIfNeeded(ctx, true); err != nil { - return fmt.Errorf("unable to download data for parser '%s': %+v", postoverflowName, err) - } - - log.Debugf("postoverflow '%s' installed successfully in runtime environment", postoverflowName) + for _, item := range hub.GetInstalledByType(cwhub.POSTOVERFLOWS, true) { + if err := item.DownloadDataIfNeeded(ctx, true); err != nil { + return fmt.Errorf("unable to download data for parser '%s': %+v", item.Name, err) } + + log.Debugf("postoverflow '%s' installed successfully in runtime environment", item.Name) } return nil diff --git a/pkg/parser/unix_parser.go b/pkg/parser/unix_parser.go index 280d122ecc1..9d98fbcf29a 100644 --- a/pkg/parser/unix_parser.go +++ b/pkg/parser/unix_parser.go @@ -66,21 +66,20 @@ func NewParsers(hub *cwhub.Hub) *Parsers { } for _, itemType := range []string{cwhub.PARSERS, cwhub.POSTOVERFLOWS} { - for _, hubParserItem := range hub.GetItemMap(itemType) { - if hubParserItem.State.Installed { - stagefile := Stagefile{ - Filename: hubParserItem.State.LocalPath, - Stage: hubParserItem.Stage, - } - if itemType == cwhub.PARSERS { - parsers.StageFiles = append(parsers.StageFiles, stagefile) - } - if itemType == cwhub.POSTOVERFLOWS { - parsers.PovfwStageFiles = append(parsers.PovfwStageFiles, stagefile) - } + for _, hubParserItem := range hub.GetInstalledByType(itemType, false) { + stagefile := Stagefile{ + Filename: hubParserItem.State.LocalPath, + Stage: hubParserItem.Stage, + } + if itemType == cwhub.PARSERS { + parsers.StageFiles = append(parsers.StageFiles, stagefile) + } + if itemType == cwhub.POSTOVERFLOWS { + parsers.PovfwStageFiles = append(parsers.PovfwStageFiles, stagefile) } } } + if parsers.StageFiles != nil { sort.Slice(parsers.StageFiles, func(i, j int) bool { return parsers.StageFiles[i].Filename < parsers.StageFiles[j].Filename diff --git a/test/bats/04_capi.bats b/test/bats/04_capi.bats index d5154c1a0d7..830d0668cbb 100644 --- a/test/bats/04_capi.bats +++ b/test/bats/04_capi.bats @@ -51,7 +51,7 @@ setup() { config_enable_capi rune -0 cscli capi register --schmilblick githubciXXXXXXXXXXXXXXXXXXXXXXXX rune -1 cscli capi status - assert_stderr --partial "no scenarios installed, abort" + assert_stderr --partial "no scenarios or appsec-rules installed, abort" rune -0 cscli scenarios install crowdsecurity/ssh-bf rune -0 cscli capi status