2022-03-29 15:06:49 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -eu
|
|
|
|
script_name=$0
|
|
|
|
|
|
|
|
die() {
|
|
|
|
echo >&2 "$@"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
about() {
|
2022-06-13 19:54:47 +00:00
|
|
|
die "usage: ${script_name} [make | load | clean]"
|
2022-03-29 15:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#shellcheck disable=SC1007
|
|
|
|
THIS_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
|
|
|
cd "${THIS_DIR}"/../../
|
2022-04-05 09:00:11 +00:00
|
|
|
#shellcheck disable=SC1091
|
2022-03-29 15:06:49 +00:00
|
|
|
. ./.environment.sh
|
|
|
|
|
|
|
|
# you have not removed set -u above, have you?
|
|
|
|
|
2022-06-13 19:54:47 +00:00
|
|
|
[[ -z "${TEST_DIR-}" ]] && die "\$TEST_DIR must be defined."
|
|
|
|
[[ -z "${LOCAL_DIR-}" ]] && die "\$LOCAL_DIR must be defined."
|
|
|
|
[[ -z "${CSCLI-}" ]] && die "\$CSCLI must be defined."
|
|
|
|
[[ -z "${LOCAL_INIT_DIR-}" ]] && die "\$LOCAL_INIT_DIR must be defined."
|
|
|
|
[[ -z "${PLUGIN_DIR-}" ]] && die "\$PLUGIN_DIR must be defined."
|
|
|
|
[[ -z "${DB_BACKEND-}" ]] && die "\$DB_BACKEND must be defined."
|
2022-03-29 15:06:49 +00:00
|
|
|
|
2022-06-13 19:54:47 +00:00
|
|
|
if [[ ! -f "${CSCLI}" ]]; then
|
2022-03-29 15:06:49 +00:00
|
|
|
die "${CSCLI} is missing. Please build (with 'make bats-build') or install it."
|
|
|
|
fi
|
|
|
|
|
|
|
|
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-10-17 15:32:08 +00:00
|
|
|
HUB_DIR="${CONFIG_DIR}/hub"
|
|
|
|
export HUB_DIR
|
2022-03-29 15:06:49 +00:00
|
|
|
|
2022-08-04 09:25:34 +00:00
|
|
|
if [[ $(uname) == "OpenBSD" ]]; then
|
|
|
|
TAR=gtar
|
|
|
|
else
|
|
|
|
TAR=tar
|
|
|
|
fi
|
|
|
|
|
2022-03-29 15:06:49 +00:00
|
|
|
remove_init_data() {
|
2022-10-07 14:17:36 +00:00
|
|
|
./bin/assert-crowdsec-not-running || die "Cannot remove fixture data."
|
2022-03-29 15:06:49 +00:00
|
|
|
rm -rf -- "${LOCAL_DIR:?}/${REL_CONFIG_DIR}"/* "${LOCAL_DIR:?}/${REL_DATA_DIR:?}"/*
|
|
|
|
}
|
|
|
|
|
|
|
|
config_generate() {
|
|
|
|
mkdir -p "${CONFIG_DIR}"
|
|
|
|
|
2022-06-22 08:00:51 +00:00
|
|
|
cp ../config/profiles.yaml \
|
2022-03-29 15:06:49 +00:00
|
|
|
../config/simulation.yaml \
|
|
|
|
../config/online_api_credentials.yaml \
|
|
|
|
"${CONFIG_DIR}/"
|
|
|
|
|
2023-02-06 06:33:04 +00:00
|
|
|
cp ../config/detect.yaml \
|
|
|
|
"${HUB_DIR}"
|
|
|
|
|
2022-06-22 08:00:51 +00:00
|
|
|
# the default acquis file contains files that are not readable by everyone
|
2022-10-14 14:12:21 +00:00
|
|
|
touch "$LOG_DIR/empty.log"
|
|
|
|
cat <<-EOT >"$CONFIG_DIR/acquis.yaml"
|
|
|
|
source: file
|
|
|
|
filenames:
|
|
|
|
- $LOG_DIR/empty.log
|
|
|
|
labels:
|
|
|
|
type: syslog
|
|
|
|
EOT
|
2022-06-22 08:00:51 +00:00
|
|
|
|
2023-08-24 07:46:25 +00:00
|
|
|
cp ../cmd/notification-*/*.yaml \
|
2022-03-29 15:06:49 +00:00
|
|
|
"${CONFIG_DIR}/notifications/"
|
|
|
|
|
2022-04-20 13:05:34 +00:00
|
|
|
yq e '
|
2022-04-05 09:00:11 +00:00
|
|
|
.common.daemonize=true |
|
2022-03-29 15:06:49 +00:00
|
|
|
del(.common.pid_dir) |
|
2022-04-05 09:00:11 +00:00
|
|
|
.common.log_level="info" |
|
2022-06-17 14:12:49 +00:00
|
|
|
.common.force_color_logs=true |
|
2022-03-29 15:06:49 +00:00
|
|
|
.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" |
|
2022-10-17 15:32:08 +00:00
|
|
|
.config_paths.hub_dir=strenv(HUB_DIR) |
|
|
|
|
.config_paths.index_path=strenv(HUB_DIR)+"/.index.json" |
|
|
|
|
.config_paths.notification_dir=strenv(CONFIG_DIR)+"/notifications" |
|
2022-03-29 15:06:49 +00:00
|
|
|
.config_paths.plugin_dir=strenv(PLUGIN_DIR) |
|
|
|
|
.crowdsec_service.acquisition_path=strenv(CONFIG_DIR)+"/acquis.yaml" |
|
2022-10-17 15:32:08 +00:00
|
|
|
.crowdsec_service.acquisition_dir=strenv(CONFIG_DIR)+"/acquis.d" |
|
2022-03-29 15:06:49 +00:00
|
|
|
.db_config.db_path=strenv(DATA_DIR)+"/crowdsec.db" |
|
2022-09-14 13:09:54 +00:00
|
|
|
.db_config.use_wal=true |
|
2022-03-29 15:06:49 +00:00
|
|
|
.api.client.credentials_path=strenv(CONFIG_DIR)+"/local_api_credentials.yaml" |
|
2024-03-14 09:43:02 +00:00
|
|
|
.api.server.listen_socket=strenv(DATA_DIR)+"/crowdsec.sock" |
|
2022-03-29 15:06:49 +00:00
|
|
|
.api.server.profiles_path=strenv(CONFIG_DIR)+"/profiles.yaml" |
|
|
|
|
.api.server.console_path=strenv(CONFIG_DIR)+"/console.yaml" |
|
2023-07-05 15:45:31 +00:00
|
|
|
del(.api.server.online_client)
|
2022-06-22 08:00:51 +00:00
|
|
|
' ../config/config.yaml >"${CONFIG_DIR}/config.yaml"
|
2022-03-29 15:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
make_init_data() {
|
2022-10-07 14:17:36 +00:00
|
|
|
./bin/assert-crowdsec-not-running || die "Cannot create fixture data."
|
2022-03-29 15:06:49 +00:00
|
|
|
|
2022-08-04 09:25:34 +00:00
|
|
|
remove_init_data
|
2022-03-29 15:06:49 +00:00
|
|
|
mkdir -p "${DATA_DIR}"
|
|
|
|
mkdir -p "${CONFIG_DIR}/notifications"
|
|
|
|
mkdir -p "${CONFIG_DIR}/hub"
|
|
|
|
mkdir -p "${CONFIG_DIR}/patterns"
|
2022-08-04 09:25:34 +00:00
|
|
|
cp -a "../config/patterns" "${CONFIG_DIR}/"
|
2022-03-29 15:06:49 +00:00
|
|
|
config_generate
|
|
|
|
# XXX errors from instance-db should be reported...
|
|
|
|
./instance-db config-yaml
|
|
|
|
./instance-db setup
|
|
|
|
|
2023-07-05 15:45:31 +00:00
|
|
|
"$CSCLI" --warning hub update
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2023-12-01 08:36:38 +00:00
|
|
|
./bin/preload-hub-items
|
2022-03-29 15:06:49 +00:00
|
|
|
|
2024-03-14 09:43:02 +00:00
|
|
|
# force TCP, the default would be unix socket
|
|
|
|
"$CSCLI" --warning machines add githubciXXXXXXXXXXXXXXXXXXXXXXXX --url http://127.0.0.1:8080 --auto --force
|
2023-12-04 21:59:52 +00:00
|
|
|
|
2023-03-30 13:05:09 +00:00
|
|
|
mkdir -p "$LOCAL_INIT_DIR"
|
2022-03-29 15:06:49 +00:00
|
|
|
|
|
|
|
./instance-db dump "${LOCAL_INIT_DIR}/database"
|
2022-08-04 09:25:34 +00:00
|
|
|
|
2022-03-29 15:06:49 +00:00
|
|
|
echo "${DB_BACKEND}" > "${LOCAL_INIT_DIR}/.backend"
|
|
|
|
|
2022-08-04 09:25:34 +00:00
|
|
|
"${TAR}" -C "${LOCAL_DIR}" --create \
|
2022-06-13 19:54:47 +00:00
|
|
|
--exclude "${REL_DATA_DIR}"/crowdsec.db \
|
|
|
|
--file "${LOCAL_INIT_DIR}/init-config-data.tar" "${REL_CONFIG_DIR}" "${REL_DATA_DIR}"
|
2022-03-29 15:06:49 +00:00
|
|
|
|
|
|
|
remove_init_data
|
|
|
|
}
|
|
|
|
|
|
|
|
load_init_data() {
|
2022-10-07 14:17:36 +00:00
|
|
|
./bin/assert-crowdsec-not-running || die "Cannot load fixture data."
|
2022-08-04 09:25:34 +00:00
|
|
|
|
2022-06-13 19:54:47 +00:00
|
|
|
if [[ ! -f "${LOCAL_INIT_DIR}/init-config-data.tar" ]]; then
|
2022-12-03 07:56:11 +00:00
|
|
|
die "Initial data not found; did you run 'make bats-fixture' ?"
|
2022-03-29 15:06:49 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
dump_backend="$(cat "${LOCAL_INIT_DIR}/.backend")"
|
2022-06-13 19:54:47 +00:00
|
|
|
if [[ "${DB_BACKEND}" != "${dump_backend}" ]]; then
|
2022-06-17 14:12:49 +00:00
|
|
|
die "Can't run with backend '${DB_BACKEND}' because the test data was built with '${dump_backend}'"
|
2022-03-29 15:06:49 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
remove_init_data
|
|
|
|
|
2022-08-04 09:25:34 +00:00
|
|
|
"${TAR}" -C "${LOCAL_DIR}" --extract --file "${LOCAL_INIT_DIR}/init-config-data.tar"
|
|
|
|
|
2022-03-29 15:06:49 +00:00
|
|
|
./instance-db restore "${LOCAL_INIT_DIR}/database"
|
|
|
|
}
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
|
2022-06-13 19:54:47 +00:00
|
|
|
[[ $# -lt 1 ]] && about
|
2022-03-29 15:06:49 +00:00
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
make)
|
|
|
|
make_init_data
|
|
|
|
;;
|
|
|
|
load)
|
|
|
|
load_init_data
|
|
|
|
;;
|
|
|
|
clean)
|
|
|
|
remove_init_data
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
about
|
|
|
|
;;
|
|
|
|
esac;
|
|
|
|
|