Skip to content

Commit 0e8a1c6

Browse files
authored
docker: pre-download all hub items and data, opt-in hub update/upgrade (#2933)
* docker: pre-download all hub items and data, opt-in hub update/upgrade * docker/bars: don't purge anything before pre-downloading hub * Docker: README update
1 parent 990dd5e commit 0e8a1c6

File tree

5 files changed

+36
-22
lines changed

5 files changed

+36
-22
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ RUN make clean release DOCKER_BUILD=1 BUILD_STATIC=1 && \
2525
./wizard.sh --docker-mode && \
2626
cd - >/dev/null && \
2727
cscli hub update && \
28+
./docker/preload-hub-items && \
2829
cscli collections install crowdsecurity/linux && \
2930
cscli parsers install crowdsecurity/whitelists
3031

docker/README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ labels:
134134
type: apache2
135135
```
136136

137-
138137
## Recommended configuration
139138

140139
### Volumes
@@ -146,6 +145,14 @@ to avoid losing credentials and decision data in case of container destruction a
146145
* Acquisition: `/etc/crowdsec/acquis.d` and/or `/etc/crowdsec.acquis.yaml` (yes, they can be nested in `/etc/crowdsec`)
147146
* Database when using SQLite (default): `/var/lib/crowdsec/data`
148147

148+
### Hub updates
149+
150+
To ensure you have the latest version of the collections, scenarios, parsers, etc., you can set the variable `DO_HUB_UPGRADE` to true.
151+
This will perform an update/upgrade of the hub every time the container is started.
152+
153+
Be aware that if your container is misbehaving and caught in a restart loop, the CrowdSec hub may ban your IP for some time and your containers
154+
will run with the version of the hub that is cached in the container's image. If you enable `DO_HUB_UPGRADE`, do it when your infrastructure is running
155+
correctly and make sure you have some monitoring in place.
149156

150157
## Start a Crowdsec instance
151158

@@ -316,7 +323,7 @@ config.yaml) each time the container is run.
316323
| `BOUNCERS_ALLOWED_OU` | bouncer-ou | OU values allowed for bouncers, separated by comma |
317324
| | | |
318325
| __Hub management__ | | |
319-
| `NO_HUB_UPGRADE` | false | Skip hub update / upgrade when the container starts |
326+
| `DO_HUB_UPGRADE` | false | Force hub update / upgrade when the container starts. If for some reason the container restarts too often, it may lead to a temporary ban from hub updates. |
320327
| `COLLECTIONS` | | Collections to install, separated by space: `-e COLLECTIONS="crowdsecurity/linux crowdsecurity/apache2"` |
321328
| `PARSERS` | | Parsers to install, separated by space |
322329
| `SCENARIOS` | | Scenarios to install, separated by space |

docker/docker_start.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,8 @@ conf_set_if "$PLUGIN_DIR" '.config_paths.plugin_dir = strenv(PLUGIN_DIR)'
304304

305305
## Install hub items
306306

307-
cscli hub update || true
308-
309-
if isfalse "$NO_HUB_UPGRADE"; then
307+
if istrue "$DO_HUB_UPGRADE"; then
308+
cscli hub update || true
310309
cscli hub upgrade || true
311310
fi
312311

docker/preload-hub-items

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
3+
set -eu
4+
5+
# pre-download everything but don't install anything
6+
7+
echo "Pre-downloading Hub content..."
8+
9+
types=$(cscli hub types -o raw)
10+
11+
for itemtype in $types; do
12+
ALL_ITEMS=$(cscli "$itemtype" list -a -o json | itemtype="$itemtype" yq '.[env(itemtype)][] | .name')
13+
if [[ -n "${ALL_ITEMS}" ]]; then
14+
#shellcheck disable=SC2086
15+
cscli "$itemtype" install \
16+
$ALL_ITEMS \
17+
--download-only \
18+
--error
19+
fi
20+
done
21+
22+
echo " done."

test/bin/preload-hub-items

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,12 @@ THIS_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
99

1010
# pre-download everything but don't install anything
1111

12-
echo -n "Purging existing hub..."
12+
echo "Pre-downloading Hub content..."
1313

1414
types=$("$CSCLI" hub types -o raw)
1515

1616
for itemtype in $types; do
17-
"$CSCLI" "${itemtype}" delete --all --error --purge --force
18-
done
19-
20-
echo " done."
21-
22-
echo -n "Pre-downloading Hub content..."
23-
24-
for itemtype in $types; do
25-
ALL_ITEMS=$("$CSCLI" "$itemtype" list -a -o json | jq --arg itemtype "$itemtype" -r '.[$itemtype][].name')
17+
ALL_ITEMS=$("$CSCLI" "$itemtype" list -a -o json | itemtype="$itemtype" yq '.[env(itemtype)][] | .name')
2618
if [[ -n "${ALL_ITEMS}" ]]; then
2719
#shellcheck disable=SC2086
2820
"$CSCLI" "$itemtype" install \
@@ -32,11 +24,4 @@ for itemtype in $types; do
3224
fi
3325
done
3426

35-
# XXX: download-only works only for collections, not for parsers, scenarios, postoverflows.
36-
# so we have to delete the links manually, and leave the downloaded files in place
37-
38-
for itemtype in $types; do
39-
"$CSCLI" "$itemtype" delete --all --error
40-
done
41-
4227
echo " done."

0 commit comments

Comments
 (0)