github.com/crowdsecurity/crowdsec@v1.6.1/test/lib/config/config-local (about) 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/online_api_credentials.yaml \ 61 "${CONFIG_DIR}/" 62 63 cp ../config/detect.yaml \ 64 "${HUB_DIR}" 65 66 # the default acquis file contains files that are not readable by everyone 67 touch "$LOG_DIR/empty.log" 68 cat <<-EOT >"$CONFIG_DIR/acquis.yaml" 69 source: file 70 filenames: 71 - $LOG_DIR/empty.log 72 labels: 73 type: syslog 74 EOT 75 76 cp ../cmd/notification-*/*.yaml \ 77 "${CONFIG_DIR}/notifications/" 78 79 yq e ' 80 .common.daemonize=true | 81 del(.common.pid_dir) | 82 .common.log_level="info" | 83 .common.force_color_logs=true | 84 .common.log_dir=strenv(LOG_DIR) | 85 .config_paths.config_dir=strenv(CONFIG_DIR) | 86 .config_paths.data_dir=strenv(DATA_DIR) | 87 .config_paths.simulation_path=strenv(CONFIG_DIR)+"/simulation.yaml" | 88 .config_paths.hub_dir=strenv(HUB_DIR) | 89 .config_paths.index_path=strenv(HUB_DIR)+"/.index.json" | 90 .config_paths.notification_dir=strenv(CONFIG_DIR)+"/notifications" | 91 .config_paths.plugin_dir=strenv(PLUGIN_DIR) | 92 .crowdsec_service.acquisition_path=strenv(CONFIG_DIR)+"/acquis.yaml" | 93 .crowdsec_service.acquisition_dir=strenv(CONFIG_DIR)+"/acquis.d" | 94 .db_config.db_path=strenv(DATA_DIR)+"/crowdsec.db" | 95 .db_config.use_wal=true | 96 .api.client.credentials_path=strenv(CONFIG_DIR)+"/local_api_credentials.yaml" | 97 .api.server.listen_socket=strenv(DATA_DIR)+"/crowdsec.sock" | 98 .api.server.profiles_path=strenv(CONFIG_DIR)+"/profiles.yaml" | 99 .api.server.console_path=strenv(CONFIG_DIR)+"/console.yaml" | 100 del(.api.server.online_client) 101 ' ../config/config.yaml >"${CONFIG_DIR}/config.yaml" 102 } 103 104 make_init_data() { 105 ./bin/assert-crowdsec-not-running || die "Cannot create fixture data." 106 107 remove_init_data 108 mkdir -p "${DATA_DIR}" 109 mkdir -p "${CONFIG_DIR}/notifications" 110 mkdir -p "${CONFIG_DIR}/hub" 111 mkdir -p "${CONFIG_DIR}/patterns" 112 cp -a "../config/patterns" "${CONFIG_DIR}/" 113 config_generate 114 # XXX errors from instance-db should be reported... 115 ./instance-db config-yaml 116 ./instance-db setup 117 118 "$CSCLI" --warning hub update 119 120 ./bin/preload-hub-items 121 122 # force TCP, the default would be unix socket 123 "$CSCLI" --warning machines add githubciXXXXXXXXXXXXXXXXXXXXXXXX --url http://127.0.0.1:8080 --auto --force 124 125 mkdir -p "$LOCAL_INIT_DIR" 126 127 ./instance-db dump "${LOCAL_INIT_DIR}/database" 128 129 echo "${DB_BACKEND}" > "${LOCAL_INIT_DIR}/.backend" 130 131 "${TAR}" -C "${LOCAL_DIR}" --create \ 132 --exclude "${REL_DATA_DIR}"/crowdsec.db \ 133 --file "${LOCAL_INIT_DIR}/init-config-data.tar" "${REL_CONFIG_DIR}" "${REL_DATA_DIR}" 134 135 remove_init_data 136 } 137 138 load_init_data() { 139 ./bin/assert-crowdsec-not-running || die "Cannot load fixture data." 140 141 if [[ ! -f "${LOCAL_INIT_DIR}/init-config-data.tar" ]]; then 142 die "Initial data not found; did you run 'make bats-fixture' ?" 143 fi 144 145 dump_backend="$(cat "${LOCAL_INIT_DIR}/.backend")" 146 if [[ "${DB_BACKEND}" != "${dump_backend}" ]]; then 147 die "Can't run with backend '${DB_BACKEND}' because the test data was built with '${dump_backend}'" 148 fi 149 150 remove_init_data 151 152 "${TAR}" -C "${LOCAL_DIR}" --extract --file "${LOCAL_INIT_DIR}/init-config-data.tar" 153 154 ./instance-db restore "${LOCAL_INIT_DIR}/database" 155 } 156 157 # --------------------------- 158 159 [[ $# -lt 1 ]] && about 160 161 case "$1" in 162 make) 163 make_init_data 164 ;; 165 load) 166 load_init_data 167 ;; 168 clean) 169 remove_init_data 170 ;; 171 *) 172 about 173 ;; 174 esac; 175