2022-03-09 13:45:36 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -eu
|
|
|
|
script_name=$0
|
|
|
|
|
|
|
|
die() {
|
|
|
|
echo >&2 "$@"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
about() {
|
2022-03-18 09:13:12 +00:00
|
|
|
die "usage: $script_name [make | load | clean]"
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#shellcheck disable=SC1007
|
|
|
|
THIS_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
|
|
|
cd "${THIS_DIR}"
|
|
|
|
#shellcheck disable=SC1090
|
|
|
|
. ./.environment.sh
|
|
|
|
|
|
|
|
# you have not removed set -u above, have you?
|
|
|
|
|
|
|
|
[ -z "${TEST_DIR-}" ] && die "\$TEST_DIR must be defined."
|
|
|
|
[ -z "${LOCAL_DIR-}" ] && die "\$LOCAL_DIR must be defined."
|
2022-03-18 09:13:12 +00:00
|
|
|
[ -z "${CSCLI-}" ] && die "\$CSCLI must be defined."
|
2022-03-09 13:45:36 +00:00
|
|
|
[ -z "${LOCAL_INIT_DIR-}" ] && die "\$LOCAL_INIT_DIR must be defined."
|
|
|
|
[ -z "${PLUGIN_DIR-}" ] && die "\$PLUGIN_DIR must be defined."
|
2022-03-18 16:36:51 +00:00
|
|
|
[ -z "${DB_BACKEND-}" ] && die "\$DB_BACKEND must be defined."
|
2022-03-09 13:45:36 +00:00
|
|
|
|
2022-03-18 09:13:12 +00:00
|
|
|
if [ ! -f "${CSCLI}" ]; then
|
|
|
|
die "${CSCLI} is missing. Please build (with 'make bats-build') or install it."
|
2022-03-09 13:45:36 +00:00
|
|
|
fi
|
|
|
|
|
2022-03-18 09:13:12 +00:00
|
|
|
REL_CONFIG_DIR="etc/crowdsec"
|
|
|
|
REL_DATA_DIR="var/lib/crowdsec/data"
|
|
|
|
|
|
|
|
DATA_DIR="${LOCAL_DIR}/${REL_DATA_DIR}"
|
|
|
|
export DATA_DIR
|
|
|
|
CONFIG_DIR="${LOCAL_DIR}/${REL_CONFIG_DIR}"
|
|
|
|
export CONFIG_DIR
|
2022-03-09 13:45:36 +00:00
|
|
|
|
|
|
|
remove_init_data() {
|
2022-03-18 09:13:12 +00:00
|
|
|
rm -rf -- "${LOCAL_DIR:?}/${REL_CONFIG_DIR}"/* "${LOCAL_DIR:?}/${REL_DATA_DIR:?}"/*
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 11:28:45 +00:00
|
|
|
config_generate() {
|
2022-03-18 09:13:12 +00:00
|
|
|
mkdir -p "${CONFIG_DIR}"
|
|
|
|
|
|
|
|
cp ../config/acquis.yaml \
|
|
|
|
../config/profiles.yaml \
|
|
|
|
../config/simulation.yaml \
|
|
|
|
../config/local_api_credentials.yaml \
|
|
|
|
../config/online_api_credentials.yaml \
|
|
|
|
"${CONFIG_DIR}/"
|
|
|
|
|
|
|
|
cp ../plugins/notifications/*/{http,email,slack,splunk,dummy}.yaml \
|
|
|
|
"${CONFIG_DIR}/notifications/"
|
2022-03-17 11:28:45 +00:00
|
|
|
|
|
|
|
yq '
|
|
|
|
.common.daemonize=false |
|
|
|
|
del(.common.pid_dir) |
|
|
|
|
.common.log_dir=strenv(LOG_DIR) |
|
|
|
|
.config_paths.config_dir=strenv(CONFIG_DIR) |
|
|
|
|
.config_paths.data_dir=strenv(DATA_DIR) |
|
|
|
|
.config_paths.simulation_path=strenv(CONFIG_DIR)+"/simulation.yaml" |
|
|
|
|
.config_paths.hub_dir=strenv(CONFIG_DIR)+"/hub/" |
|
|
|
|
.config_paths.index_path=strenv(CONFIG_DIR)+"/hub/.index.json" |
|
|
|
|
.config_paths.notification_dir=strenv(CONFIG_DIR)+"/notifications/" |
|
|
|
|
.config_paths.plugin_dir=strenv(PLUGIN_DIR) |
|
|
|
|
.crowdsec_service.acquisition_path=strenv(CONFIG_DIR)+"/acquis.yaml" |
|
|
|
|
.db_config.db_path=strenv(DATA_DIR)+"/crowdsec.db" |
|
|
|
|
.api.client.credentials_path=strenv(CONFIG_DIR)+"/local_api_credentials.yaml" |
|
|
|
|
.api.server.profiles_path=strenv(CONFIG_DIR)+"/profiles.yaml" |
|
|
|
|
.api.server.console_path=strenv(CONFIG_DIR)+"/console.yaml" |
|
|
|
|
.api.server.online_client.credentials_path=strenv(CONFIG_DIR)+"/online_api_credentials.yaml"
|
|
|
|
' <../config/config.yaml >"${CONFIG_DIR}/config.yaml"
|
|
|
|
}
|
|
|
|
|
2022-03-09 13:45:36 +00:00
|
|
|
make_init_data() {
|
|
|
|
remove_init_data
|
|
|
|
|
2022-03-18 09:13:12 +00:00
|
|
|
mkdir -p "${DATA_DIR}"
|
2022-03-09 13:45:36 +00:00
|
|
|
mkdir -p "${CONFIG_DIR}/notifications"
|
2022-03-21 14:51:05 +00:00
|
|
|
mkdir -p "${CONFIG_DIR}/hub"
|
|
|
|
mkdir -p "${CONFIG_DIR}/patterns"
|
|
|
|
cp -ax "../config/patterns" "${CONFIG_DIR}/"
|
2022-03-17 11:28:45 +00:00
|
|
|
config_generate
|
2022-03-21 14:51:05 +00:00
|
|
|
# XXX errors from instance-db should be reported...
|
|
|
|
./instance-db config-yaml
|
2022-03-18 16:36:51 +00:00
|
|
|
./instance-db setup
|
2022-03-09 13:45:36 +00:00
|
|
|
|
2022-03-18 09:13:12 +00:00
|
|
|
"${CSCLI}" machines add githubciXXXXXXXXXXXXXXXXXXXXXXXX --auto
|
|
|
|
"${CSCLI}" capi register
|
|
|
|
"${CSCLI}" hub update
|
|
|
|
"${CSCLI}" collections install crowdsecurity/linux
|
2022-03-09 13:45:36 +00:00
|
|
|
|
|
|
|
"${TEST_DIR}/instance-crowdsec" start
|
2022-03-18 09:13:12 +00:00
|
|
|
"${CSCLI}" lapi status
|
2022-03-09 13:45:36 +00:00
|
|
|
"${TEST_DIR}/instance-crowdsec" stop
|
|
|
|
|
2022-03-18 09:13:12 +00:00
|
|
|
mkdir -p "${LOCAL_INIT_DIR}"
|
|
|
|
|
2022-03-21 14:51:05 +00:00
|
|
|
./instance-db dump "${LOCAL_INIT_DIR}/database"
|
|
|
|
echo "${DB_BACKEND}" > "${LOCAL_INIT_DIR}/.backend"
|
2022-03-18 09:13:12 +00:00
|
|
|
|
|
|
|
tar -C "${LOCAL_DIR}" --create \
|
|
|
|
--exclude "$REL_DATA_DIR"/crowdsec.db \
|
|
|
|
--file "${LOCAL_INIT_DIR}/init-config-data.tar" "$REL_CONFIG_DIR" "$REL_DATA_DIR"
|
2022-03-09 13:45:36 +00:00
|
|
|
|
|
|
|
remove_init_data
|
|
|
|
}
|
|
|
|
|
|
|
|
load_init_data() {
|
|
|
|
if [ ! -f "${LOCAL_INIT_DIR}/init-config-data.tar" ]; then
|
|
|
|
die "Initial data not found; did you run '$script_name make' ?"
|
|
|
|
fi
|
|
|
|
|
2022-03-21 14:51:05 +00:00
|
|
|
dump_backend="$(cat "${LOCAL_INIT_DIR}/.backend")"
|
|
|
|
if [ "$DB_BACKEND" != "$dump_backend" ]; then
|
|
|
|
die "Can't run with backend '$DB_BACKEND' because the test data was build with '$dump_backend'"
|
|
|
|
fi
|
|
|
|
|
2022-03-09 13:45:36 +00:00
|
|
|
remove_init_data
|
|
|
|
|
|
|
|
tar -C "${LOCAL_DIR}" --extract --file "${LOCAL_INIT_DIR}/init-config-data.tar"
|
2022-03-18 09:13:12 +00:00
|
|
|
./instance-db restore "${LOCAL_INIT_DIR}/database"
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
|
|
|
|
[ $# -lt 1 ] && about
|
|
|
|
|
|
|
|
./assert-crowdsec-not-running
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
make)
|
|
|
|
make_init_data
|
|
|
|
;;
|
|
|
|
load)
|
|
|
|
load_init_data
|
|
|
|
;;
|
|
|
|
clean)
|
|
|
|
remove_init_data
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
about
|
|
|
|
;;
|
|
|
|
esac;
|
|
|
|
|