Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/cwhub: improve error messages #2712

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/cwhub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func (h *Hub) parseIndex() error {
item.FileName = path.Base(item.RemotePath)

item.logMissingSubItems()

if item.latestHash() == "" {
h.logger.Errorf("invalid hub item %s: latest version missing from index", item.FQName())
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/cwhub/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,15 @@ func (i *Item) addTaint(sub *Item) {
ancestor.addTaint(sub)
}
}

// latestHash() returns the hash of the latest version of the item.
// if it's missing, the index file has been manually modified or corrupted.
func (i *Item) latestHash() string {
for k, v := range i.Versions {
if k == i.Version {
return v.Digest
}
}

return ""
}
2 changes: 1 addition & 1 deletion pkg/cwhub/iteminstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (i *Item) Install(force bool, downloadOnly bool) error {

filePath, err := i.downloadLatest(force, true)
if err != nil {
return fmt.Errorf("while downloading %s: %w", i.Name, err)
return err
}

if downloadOnly {
Expand Down
19 changes: 14 additions & 5 deletions pkg/cwhub/itemupgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"bytes"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -82,14 +83,14 @@
i.hub.logger.Tracef("collection, recurse")

if _, err := sub.downloadLatest(overwrite, updateOnly); err != nil {
return "", fmt.Errorf("while downloading %s: %w", sub.Name, err)
return "", err

Check warning on line 86 in pkg/cwhub/itemupgrade.go

View check run for this annotation

Codecov / codecov/patch

pkg/cwhub/itemupgrade.go#L86

Added line #L86 was not covered by tests
}
}

downloaded := sub.State.Downloaded

if _, err := sub.download(overwrite); err != nil {
return "", fmt.Errorf("while downloading %s: %w", sub.Name, err)
return "", err

Check warning on line 93 in pkg/cwhub/itemupgrade.go

View check run for this annotation

Codecov / codecov/patch

pkg/cwhub/itemupgrade.go#L93

Added line #L93 was not covered by tests
}

// We need to enable an item when it has been added to a collection since latest release of the collection.
Expand All @@ -108,14 +109,18 @@

ret, err := i.download(overwrite)
if err != nil {
return "", fmt.Errorf("failed to download item: %w", err)
return "", err
}

return ret, nil
}

// FetchLatest downloads the latest item from the hub, verifies the hash and returns the content and the used url.
func (i *Item) FetchLatest() ([]byte, string, error) {
if i.latestHash() == "" {
return nil, "", errors.New("latest hash missing from index")
}

url, err := i.hub.remote.urlTo(i.RemotePath)
if err != nil {
return nil, "", fmt.Errorf("failed to build request: %w", err)
Expand Down Expand Up @@ -146,7 +151,7 @@
i.hub.logger.Errorf("Downloaded version doesn't match index, please 'hub update'")
i.hub.logger.Debugf("got %s, expected %s", meow, i.Versions[i.Version].Digest)

return nil, "", fmt.Errorf("invalid download hash for %s", i.Name)
return nil, "", fmt.Errorf("invalid download hash")

Check warning on line 154 in pkg/cwhub/itemupgrade.go

View check run for this annotation

Codecov / codecov/patch

pkg/cwhub/itemupgrade.go#L154

Added line #L154 was not covered by tests
}

return body, url, nil
Expand Down Expand Up @@ -180,7 +185,11 @@

body, url, err := i.FetchLatest()
if err != nil {
return "", fmt.Errorf("while downloading %s: %w", url, err)
what := i.Name
if url != "" {
what += " from " + url
}

Check warning on line 191 in pkg/cwhub/itemupgrade.go

View check run for this annotation

Codecov / codecov/patch

pkg/cwhub/itemupgrade.go#L190-L191

Added lines #L190 - L191 were not covered by tests
return "", fmt.Errorf("while downloading %s: %w", what, err)
}

// all good, install
Expand Down
10 changes: 10 additions & 0 deletions test/bats/20_hub.bats
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ teardown() {
assert_output --partial 'crowdsecurity/iptables'
}

@test "cscli hub list (invalid index)" {
new_hub=$(jq <"$INDEX_PATH" '."appsec-rules"."crowdsecurity/vpatch-laravel-debug-mode".version="999"')
echo "$new_hub" >"$INDEX_PATH"
rune -0 cscli hub list --error
assert_stderr --partial "invalid hub item appsec-rules:crowdsecurity/vpatch-laravel-debug-mode: latest version missing from index"

rune -1 cscli appsec-rules install crowdsecurity/vpatch-laravel-debug-mode --force
assert_stderr --partial "error while installing 'crowdsecurity/vpatch-laravel-debug-mode': while downloading crowdsecurity/vpatch-laravel-debug-mode: latest hash missing from index"
}

@test "missing reference in hub index" {
new_hub=$(jq <"$INDEX_PATH" 'del(.parsers."crowdsecurity/smb-logs") | del (.scenarios."crowdsecurity/mysql-bf")')
echo "$new_hub" >"$INDEX_PATH"
Expand Down