Skip to content

Commit 55aa189

Browse files
authored
explicit message for malformed data URL in local items (#3537)
1 parent 0d2c2da commit 55aa189

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

debian/migrate-hub.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/usr/bin/env sh
22

33
# This script is provided (only in the source distribution) as an ad-hoc solution
4-
# to migrate an installation from the crowdsec package maintained in the debian repositories
4+
# to migrate an installation from the crowdsec package maintained in the debian or ubuntu repositories
55
# to the official crowdsec repository.
66

77
set -eu
88

99
if [ ! -d /var/lib/crowdsec/hub/ ]; then
1010
echo "You don't have a hub directory to migrate."
1111
echo
12-
echo "Use this script only if you upgrade from the crowdsec package included in the debian repositories."
12+
echo "Use this script only if you upgrade from the crowdsec package included in the debian or ubuntu repositories."
1313
exit 1
1414
fi
1515

pkg/hubops/colorize.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ func colorizeItemName(fullname string) string {
1515
bold := color.New(color.Bold)
1616
author := parts[0]
1717
name := parts[1]
18+
1819
return author + "/" + bold.Sprint(name)
1920
}
21+
2022
return fullname
2123
}
2224

pkg/hubops/download.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"net/http"
9+
"net/url"
910
"os"
1011
"time"
1112

@@ -118,6 +119,13 @@ func downloadDataSet(ctx context.Context, dataFolder string, force bool, reader
118119
continue
119120
}
120121

122+
// twopenny validation
123+
if u, err := url.Parse(dataS.SourceURL); err != nil {
124+
return false, err
125+
} else if u.Scheme == "" {
126+
return false, fmt.Errorf("a valid URL was expected (note: local items can download data too): %s", dataS.SourceURL)
127+
}
128+
121129
// XXX: check context cancellation
122130
destPath, err := cwhub.SafePath(dataFolder, dataS.DestPath)
123131
if err != nil {

test/bats/20_hub_items.bats

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,29 @@ teardown() {
151151
assert_output --partial "Nothing to do."
152152
}
153153

154+
@test "when upgrading the hub, a local item's data will be downloaded" {
155+
rune -0 mkdir -p "$CONFIG_DIR/collections"
156+
cat >"$CONFIG_DIR"/collections/foobar.yaml <<-EOT
157+
data:
158+
- source_url: https://localhost:1234/database.mmdb
159+
dest_file: database.mmdb
160+
EOT
161+
rune -1 cscli hub upgrade
162+
assert_line "downloading https://localhost:1234/database.mmdb"
163+
assert_stderr --partial 'Get "https://localhost:1234/database.mmdb":'
164+
assert_stderr --partial 'connect: connection refused'
165+
166+
# bad link, or local path
167+
cat >"$CONFIG_DIR"/collections/foobar.yaml <<-EOT
168+
data:
169+
- source_url: /tmp/meh
170+
dest_file: database.mmdb
171+
EOT
172+
rune -1 cscli hub upgrade
173+
refute_line "downloading /tmp/meh"
174+
assert_stderr --partial 'a valid URL was expected (note: local items can download data too): /tmp/meh'
175+
}
176+
154177
@test "a local item cannot be removed by cscli" {
155178
rune -0 mkdir -p "$CONFIG_DIR/scenarios"
156179
rune -0 touch "$CONFIG_DIR/scenarios/foobar.yaml"

0 commit comments

Comments
 (0)