crowdsec/test/lib/config/config-global
mmetc a5ab73d458
cscli machines add: don't overwrite existing credential file (#2625)
* cscli machines add: don't overwrite existing credential file
* keep old behavior with --force
Now --force is used both to override the replacement of and existing machine,
and an existing credentials file. To retain the old behavior, the
existence of the file is only checked for the default configuration, not
if explicitly specified.
2023-12-04 22:59:52 +01:00

124 lines
3.2 KiB
Bash
Executable file

#!/usr/bin/env bash
set -eu
script_name=$0
die() {
echo >&2 "$@"
exit 1
}
about() {
die "usage: ${script_name} [make | load | clean]"
}
#shellcheck disable=SC1007
THIS_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
cd "${THIS_DIR}"/../../
#shellcheck disable=SC1091
. ./.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."
[[ -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."
if [[ ! -f "${CSCLI}" ]]; then
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
HUB_DIR="${CONFIG_DIR}/hub"
export HUB_DIR
if [[ $(uname) == "OpenBSD" ]]; then
TAR=gtar
else
TAR=tar
fi
remove_init_data() {
./bin/assert-crowdsec-not-running || die "Cannot remove fixture data."
rm -rf -- "${LOCAL_DIR:?}/${REL_CONFIG_DIR}"/* "${LOCAL_DIR:?}/${REL_DATA_DIR:?}"/*
}
# we need a separate function for initializing config when testing package
# because we want to test the configuration as well
make_init_data() {
./bin/assert-crowdsec-not-running || die "Cannot create fixture data."
./instance-db config-yaml
./instance-db setup
./bin/preload-hub-items
# when installed packages are always using sqlite, so no need to regenerate
# local credz for sqlite
[[ "${DB_BACKEND}" == "sqlite" ]] || ${CSCLI} machines add githubciXXXXXXXXXXXXXXXXXXXXXXXX --auto --force
mkdir -p "$LOCAL_INIT_DIR"
./instance-db dump "${LOCAL_INIT_DIR}/database"
echo "${DB_BACKEND}" > "${LOCAL_INIT_DIR}/.backend"
# disable CAPI by default
yq e 'del(.api.server.online_client)' -i "${CONFIG_DIR}/config.yaml"
"${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}"
}
load_init_data() {
./bin/assert-crowdsec-not-running || die "Cannot load fixture data."
if [[ ! -f "${LOCAL_INIT_DIR}/init-config-data.tar" ]]; then
die "Initial data not found; did you run 'make bats-fixture' ?"
fi
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 built with '${dump_backend}'"
fi
remove_init_data
"${TAR}" -C "${LOCAL_DIR}" --extract --file "${LOCAL_INIT_DIR}/init-config-data.tar"
./instance-db restore "${LOCAL_INIT_DIR}/database"
}
# ---------------------------
[[ $# -lt 1 ]] && about
case "$1" in
make)
"${TEST_DIR}/instance-crowdsec" stop
make_init_data
;;
load)
load_init_data
;;
clean)
remove_init_data
;;
*)
about
;;
esac;