Debian Codegraph @

tests/lib/config/config-local

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 set -eu
4 script_name=$0
5
6 die() {
7 echo >&2 "$@"
8 exit 1
9 }
10
11 about() {
12 die "usage: ${script_name} [make | load | clean]"
13 }
14
15 #shellcheck disable=SC1007
16 THIS_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
17 cd "${THIS_DIR}"/../../
18 #shellcheck disable=SC1091
19 . ./.environment.sh
20
21 # you have not removed set -u above, have you?
22
23 [[ -z "${TEST_DIR-}" ]] && die "\$TEST_DIR must be defined."
24 [[ -z "${LOCAL_DIR-}" ]] && die "\$LOCAL_DIR must be defined."
25 [[ -z "${CSCLI-}" ]] && die "\$CSCLI must be defined."
26 [[ -z "${LOCAL_INIT_DIR-}" ]] && die "\$LOCAL_INIT_DIR must be defined."
27 [[ -z "${PLUGIN_DIR-}" ]] && die "\$PLUGIN_DIR must be defined."
28 [[ -z "${DB_BACKEND-}" ]] && die "\$DB_BACKEND must be defined."
29
30 if [[ ! -f "${CSCLI}" ]]; then
31 die "${CSCLI} is missing. Please build (with 'make bats-build') or install it."
32 fi
33
34 REL_CONFIG_DIR="etc/crowdsec"
35 REL_DATA_DIR="var/lib/crowdsec/data"
36
37 DATA_DIR="${LOCAL_DIR}/${REL_DATA_DIR}"
38 export DATA_DIR
39 CONFIG_DIR="${LOCAL_DIR}/${REL_CONFIG_DIR}"
40 export CONFIG_DIR
41 HUB_DIR="${CONFIG_DIR}/hub"
42 export HUB_DIR
43
44 if [[ $(uname) == "OpenBSD" ]]; then
45 TAR=gtar
46 else
47 TAR=tar
48 fi
49
50 remove_init_data() {
51 ./bin/assert-crowdsec-not-running || die "Cannot remove fixture data."
52 rm -rf -- "${LOCAL_DIR:?}/${REL_CONFIG_DIR}"/* "${LOCAL_DIR:?}/${REL_DATA_DIR:?}"/*
53 }
54
55 config_generate() {
56 mkdir -p "${CONFIG_DIR}"
57
58 cp ../config/profiles.yaml \
59 ../config/simulation.yaml \
60 ../config/local_api_credentials.yaml \
61 ../config/online_api_credentials.yaml \
62 "${CONFIG_DIR}/"
63
64 # the default acquis file contains files that are not readable by everyone
65 touch "$LOG_DIR/empty.log"
66 cat <<-EOT >"$CONFIG_DIR/acquis.yaml"
67 source: file
68 filenames:
69 - $LOG_DIR/empty.log
70 labels:
71 type: syslog
72 EOT
73
74 cp ../plugins/notifications/*/{http,email,slack,splunk,dummy}.yaml \
75 "${CONFIG_DIR}/notifications/"
76
77 yq e '
78 .common.daemonize=true |
79 del(.common.pid_dir) |
80 .common.log_level="info" |
81 .common.force_color_logs=true |
82 .common.log_dir=strenv(LOG_DIR) |
83 .config_paths.config_dir=strenv(CONFIG_DIR) |
84 .config_paths.data_dir=strenv(DATA_DIR) |
85 .config_paths.simulation_path=strenv(CONFIG_DIR)+"/simulation.yaml" |
86 .config_paths.hub_dir=strenv(HUB_DIR) |
87 .config_paths.index_path=strenv(HUB_DIR)+"/.index.json" |
88 .config_paths.notification_dir=strenv(CONFIG_DIR)+"/notifications" |
89 .config_paths.plugin_dir=strenv(PLUGIN_DIR) |
90 .crowdsec_service.acquisition_path=strenv(CONFIG_DIR)+"/acquis.yaml" |
91 .crowdsec_service.acquisition_dir=strenv(CONFIG_DIR)+"/acquis.d" |
92 .db_config.db_path=strenv(DATA_DIR)+"/crowdsec.db" |
93 .db_config.use_wal=true |
94 .api.client.credentials_path=strenv(CONFIG_DIR)+"/local_api_credentials.yaml" |
95 .api.server.profiles_path=strenv(CONFIG_DIR)+"/profiles.yaml" |
96 .api.server.console_path=strenv(CONFIG_DIR)+"/console.yaml" |
97 .api.server.online_client.credentials_path=strenv(CONFIG_DIR)+"/online_api_credentials.yaml"
98 ' ../config/config.yaml >"${CONFIG_DIR}/config.yaml"
99 }
100
101
102 make_init_data() {
103 ./bin/assert-crowdsec-not-running || die "Cannot create fixture data."
104
105 remove_init_data
106 mkdir -p "${DATA_DIR}"
107 mkdir -p "${CONFIG_DIR}/notifications"
108 mkdir -p "${CONFIG_DIR}/hub"
109 mkdir -p "${CONFIG_DIR}/patterns"
110 cp -a "../config/patterns" "${CONFIG_DIR}/"
111 config_generate
112 # XXX errors from instance-db should be reported...
113 ./instance-db config-yaml
114 ./instance-db setup
115
116 "${CSCLI}" machines add githubciXXXXXXXXXXXXXXXXXXXXXXXX --auto
117 "${CSCLI}" hub update
118 "${CSCLI}" collections install crowdsecurity/linux
119
120 mkdir -p "${LOCAL_INIT_DIR}"
121
122 ./instance-db dump "${LOCAL_INIT_DIR}/database"
123
124 echo "${DB_BACKEND}" > "${LOCAL_INIT_DIR}/.backend"
125
126 # disable CAPI by default
127 yq e 'del(.api.server.online_client)' -i "${CONFIG_DIR}/config.yaml"
128
129 "${TAR}" -C "${LOCAL_DIR}" --create \
130 --exclude "${REL_DATA_DIR}"/crowdsec.db \
131 --file "${LOCAL_INIT_DIR}/init-config-data.tar" "${REL_CONFIG_DIR}" "${REL_DATA_DIR}"
132
133 remove_init_data
134 }
135
136 load_init_data() {
137 ./bin/assert-crowdsec-not-running || die "Cannot load fixture data."
138
139 if [[ ! -f "${LOCAL_INIT_DIR}/init-config-data.tar" ]]; then
140 die "Initial data not found; did you run '${script_name} make' ?"
141 fi
142
143 dump_backend="$(cat "${LOCAL_INIT_DIR}/.backend")"
144 if [[ "${DB_BACKEND}" != "${dump_backend}" ]]; then
145 die "Can't run with backend '${DB_BACKEND}' because the test data was built with '${dump_backend}'"
146 fi
147
148 remove_init_data
149
150 "${TAR}" -C "${LOCAL_DIR}" --extract --file "${LOCAL_INIT_DIR}/init-config-data.tar"
151
152 ./instance-db restore "${LOCAL_INIT_DIR}/database"
153 }
154
155
156 # ---------------------------
157
158 [[ $# -lt 1 ]] && about
159
160 case "$1" in
161 make)
162 make_init_data
163 ;;
164 load)
165 load_init_data
166 ;;
167 clean)
168 remove_init_data
169 ;;
170 *)
171 about
172 ;;
173 esac;
174