Skip to content

Commit

Permalink
new tests + item removal
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Dec 11, 2024
1 parent f30ce55 commit 48230fa
Show file tree
Hide file tree
Showing 17 changed files with 403 additions and 39 deletions.
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/require/require.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func Hub(c *csconfig.Config, remote *cwhub.RemoteHubCfg, logger *logrus.Logger)
}

if err := hub.Load(); err != nil {
return nil, fmt.Errorf("failed to read Hub index: %w. Run 'sudo cscli hub update' to download the index again", err)
return nil, fmt.Errorf("failed to read hub index: %w. Run 'sudo cscli hub update' to download the index again", err)
}

return hub, nil
Expand Down
15 changes: 14 additions & 1 deletion pkg/cwhub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (h *Hub) Load() error {
h.logger.Debugf("loading hub idx %s", h.local.HubIndexFile)

if err := h.parseIndex(); err != nil {
return fmt.Errorf("failed to load hub index: %w", err)
return err
}

if err := h.localSync(); err != nil {
Expand All @@ -85,6 +85,19 @@ func (h *Hub) parseIndex() error {
// Iterate over the different types to complete the struct
for _, itemType := range ItemTypes {
for name, item := range h.GetItemMap(itemType) {
if item == nil {
// likely defined as empty object or null in the index file
return fmt.Errorf("%s:%s has no index metadata", itemType, name)
}

if item.RemotePath == "" {
return fmt.Errorf("%s:%s has no download path", itemType, name)
}

if (itemType == PARSERS || itemType == POSTOVERFLOWS) && item.Stage == "" {
return fmt.Errorf("%s:%s has no stage", itemType, name)
}

item.hub = h
item.Name = name

Expand Down
3 changes: 2 additions & 1 deletion pkg/cwhub/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ type Item struct {
Content string `json:"content,omitempty" yaml:"-"`
References []string `json:"references,omitempty" yaml:"references,omitempty"`

// NOTE: RemotePath could be derived from the other fields
RemotePath string `json:"path,omitempty" yaml:"path,omitempty"` // path relative to the base URL eg. /parsers/stage/author/file.yaml
Version string `json:"version,omitempty" yaml:"version,omitempty"` // the last available version
Versions map[string]ItemVersion `json:"versions,omitempty" yaml:"-"` // all the known versions
Expand All @@ -181,7 +182,7 @@ func (i *Item) InstallPath() (string, error) {
return SafePath(i.hub.local.InstallDir, filepath.Join(p, i.FileName))
}

// downloadPath returns the location of the actual config file in the hub
// DownloadPath returns the location of the actual config file in the hub
// (eg. /etc/crowdsec/hub/collections/author/xyz.yaml).
// Raises an error if the path goes outside of the hub dir.
func (i *Item) DownloadPath() (string, error) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/cwhub/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ func (h *Hub) getItemFileInfo(path string, logger *logrus.Logger) (*itemFileInfo
fname := subsHub[2]

if ftype == PARSERS || ftype == POSTOVERFLOWS {
if len(subsHub) < 4 {
return nil, fmt.Errorf("path is too short: %s (%d)", path, len(subsHub))
}
stage = subsHub[1]
fauthor = subsHub[2]
fname = subsHub[3]
Expand Down
7 changes: 6 additions & 1 deletion pkg/hubops/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func (c *DownloadCommand) Prepare(plan *ActionPlan) (bool, error) {
}

Check warning on line 65 in pkg/hubops/download.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubops/download.go#L64-L65

Added lines #L64 - L65 were not covered by tests

if i.State.Installed {
// ensure the _new_ dependencies are installed too
if err := plan.AddCommand(NewEnableCommand(sub, c.Force)); err != nil {
return false, err
}

Check warning on line 71 in pkg/hubops/download.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubops/download.go#L70-L71

Added lines #L70 - L71 were not covered by tests

for _, sub2 := range disableKeys {
if sub2 == sub {
delete(toDisable, sub)
Expand Down Expand Up @@ -155,7 +160,7 @@ func (c *DownloadCommand) Run(ctx context.Context, plan *ActionPlan) error {

downloaded, _, err := i.FetchContentTo(ctx, finalPath)
if err != nil {
return err
return fmt.Errorf("%s: %w", i.FQName(), err)
}

if downloaded {
Expand Down
5 changes: 3 additions & 2 deletions pkg/hubops/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func (p *ActionPlan) Description(verbose bool) string {
func (p *ActionPlan) verboseDescription() string {
sb := strings.Builder{}

// Here we display the commands in the order they will be executed.
for _, cmd := range p.commands {
sb.WriteString(colorizeOpType(cmd.OperationType()) + " " + cmd.ItemType() + ":" + cmd.Detail() + "\n")
}
Expand All @@ -139,7 +140,7 @@ func describe(opType string, desc map[string]map[string][]string, sb *strings.Bu
}

details := desc[opType][itemType]
// NOTE: sorting for user convenience, but it's not the order the commands will be carried out
// Sorting for user convenience, but it's not the same order the commands will be carried out.
slices.Sort(details)

if itemType != "" {
Expand Down Expand Up @@ -170,7 +171,7 @@ func (p *ActionPlan) compactDescription() string {

sb := strings.Builder{}

// crude hack to guarantee presentation order
// Enforce presentation order.

describe("download", desc, &sb)
delete(desc, "download")
Expand Down
1 change: 0 additions & 1 deletion test/bats/20_hub.bats
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ setup() {
load "../lib/setup.sh"
load "../lib/bats-file/load.bash"
./instance-data load
hub_strip_index
}

teardown() {
Expand Down
1 change: 0 additions & 1 deletion test/bats/20_hub_collections_dep.bats
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ setup() {
load "../lib/setup.sh"
load "../lib/bats-file/load.bash"
./instance-data load
hub_strip_index
}

teardown() {
Expand Down
3 changes: 1 addition & 2 deletions test/bats/20_hub_items.bats
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ setup() {
load "../lib/setup.sh"
load "../lib/bats-file/load.bash"
./instance-data load
hub_strip_index
}

teardown() {
Expand Down Expand Up @@ -83,7 +82,7 @@ teardown() {
rune -1 cscli collections inspect crowdsecurity/sshd --no-metrics -o json
# XXX: we are on the verbose side here...
rune -0 jq -r ".msg" <(stderr)
assert_output --regexp "failed to read Hub index: failed to sync hub items: failed to scan .*: while syncing collections sshd.yaml: 1.2.3.4: Invalid Semantic Version. Run 'sudo cscli hub update' to download the index again"
assert_output --regexp "failed to read hub index: failed to sync hub items: failed to scan .*: while syncing collections sshd.yaml: 1.2.3.4: Invalid Semantic Version. Run 'sudo cscli hub update' to download the index again"
}

@test "removing or purging an item already removed by hand" {
Expand Down
5 changes: 0 additions & 5 deletions test/bats/cscli-hubtype-install.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ set -u
setup_file() {
load "../lib/setup_file.sh"
./instance-data load
# XXX: reload message is different on bsd
# shellcheck disable=SC2089
RELOAD_MESSAGE="Run 'sudo systemctl reload crowdsec' for the new configuration to be effective."
# shellcheck disable=SC2090
export RELOAD_MESSAGE
HUB_DIR=$(config_get '.config_paths.hub_dir')
export HUB_DIR
# INDEX_PATH=$(config_get '.config_paths.index_path')
Expand Down
1 change: 0 additions & 1 deletion test/bats/cscli-hubtype-list.bats
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ setup() {
load "../lib/setup.sh"
load "../lib/bats-file/load.bash"
./instance-data load
hub_strip_index
}

teardown() {
Expand Down
5 changes: 0 additions & 5 deletions test/bats/cscli-hubtype-remove.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ set -u
setup_file() {
load "../lib/setup_file.sh"
./instance-data load
# XXX: reload message is different on bsd
# shellcheck disable=SC2089
RELOAD_MESSAGE="Run 'sudo systemctl reload crowdsec' for the new configuration to be effective."
# shellcheck disable=SC2090
export RELOAD_MESSAGE
HUB_DIR=$(config_get '.config_paths.hub_dir')
export HUB_DIR
# INDEX_PATH=$(config_get '.config_paths.index_path')
Expand Down
8 changes: 2 additions & 6 deletions test/bats/cscli-hubtype-upgrade.bats
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ set -u
setup_file() {
load "../lib/setup_file.sh"
./instance-data load
# shellcheck disable=SC2089
RELOAD_MESSAGE="Run 'sudo systemctl reload crowdsec' for the new configuration to be effective."
# shellcheck disable=SC2090
export RELOAD_MESSAGE
HUB_DIR=$(config_get '.config_paths.hub_dir')
export HUB_DIR
INDEX_PATH=$(config_get '.config_paths.index_path')
Expand Down Expand Up @@ -45,7 +41,7 @@ hub_inject_v0() {
# add a version 0.0 to all parsers

# hash of the string "v0.0"
sha256_0_0="dfebecf42784a31aa3d009dbcec0c657154a034b45f49cf22a895373f6dbf63d"
sha256_0_0="daa1832414a685d69269e0ae15024b908f4602db45f9900e9c6e7f204af207c0"

new_hub=$(jq --arg DIGEST "$sha256_0_0" <"$INDEX_PATH" '.parsers |= with_entries(.value.versions["0.0"] = {"digest": $DIGEST, "deprecated": false})')
echo "$new_hub" >"$INDEX_PATH"
Expand All @@ -58,7 +54,7 @@ install_v0() {
shift

cscli "$hubtype" install "$item_name"
echo "v0.0" > "$(jq -r '.local_path' <(cscli "$hubtype" inspect "$item_name" --no-metrics -o json))"
printf "%s" "v0.0" > "$(jq -r '.local_path' <(cscli "$hubtype" inspect "$item_name" --no-metrics -o json))"
}

#----------
Expand Down
1 change: 0 additions & 1 deletion test/bats/cscli-parsers.bats
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ setup() {
load "../lib/setup.sh"
load "../lib/bats-file/load.bash"
./instance-data load
hub_strip_index
}

teardown() {
Expand Down
1 change: 0 additions & 1 deletion test/bats/cscli-postoverflows.bats
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ setup() {
load "../lib/setup.sh"
load "../lib/bats-file/load.bash"
./instance-data load
hub_strip_index
}

teardown() {
Expand Down
Loading

0 comments on commit 48230fa

Please sign in to comment.