Skip to content

Commit d1be57b

Browse files
committed
Merge branch 'master' into releases/1.6.x
2 parents a927c61 + 55aa189 commit d1be57b

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

debian/migrate-hub.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env sh
2+
3+
# 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 or ubuntu repositories
5+
# to the official crowdsec repository.
6+
7+
set -eu
8+
9+
if [ ! -d /var/lib/crowdsec/hub/ ]; then
10+
echo "You don't have a hub directory to migrate."
11+
echo
12+
echo "Use this script only if you upgrade from the crowdsec package included in the debian or ubuntu repositories."
13+
exit 1
14+
fi
15+
16+
# Download everything on the new hub but don't install anything yet
17+
18+
echo "Downloading Hub content..."
19+
20+
for itemtype in $(cscli hub types -o raw); do
21+
ALL_ITEMS=$(cscli "$itemtype" list -a -o raw | tail +2 | cut -d, -f1)
22+
if [ -n "${ALL_ITEMS}" ]; then
23+
# shellcheck disable=SC2086
24+
cscli "$itemtype" install \
25+
$ALL_ITEMS \
26+
--download-only -y
27+
fi
28+
done
29+
30+
# Fix links
31+
32+
BASEDIR=/etc/crowdsec/
33+
OLD_PATH=/var/lib/crowdsec/hub/
34+
NEW_PATH=/etc/crowdsec/hub/
35+
36+
find "$BASEDIR" -type l 2>/dev/null | while IFS= read -r link
37+
do
38+
target="$(readlink "$link")" || continue
39+
40+
case "$target" in
41+
"$OLD_PATH"*)
42+
suffix="${target#"$OLD_PATH"}"
43+
new_target="${NEW_PATH}${suffix}"
44+
45+
if [ -e "$target" ]; then
46+
continue
47+
fi
48+
49+
if [ ! -e "$new_target" ]; then
50+
continue
51+
fi
52+
53+
echo "Update symlink: $link"
54+
ln -sf "$new_target" "$link"
55+
;;
56+
*)
57+
;;
58+
esac
59+
done
60+
61+
# upgrade tainted collections
62+
63+
cscli hub upgrade --force

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)