update test_env.sh (#3829)

* scripts/test_env.sh: set absolute paths in config

* test_env: return error code

* test_env: quoting

* test_env: don't require bash

* test_env: remove redundant code, set -e

* quote dev.yaml, add windows-dev.yaml and update test_env.ps1
This commit is contained in:
mmetc
2025-10-03 10:54:15 +02:00
committed by GitHub
parent 19838930fa
commit f6f7cc3233
5 changed files with 96 additions and 75 deletions
+12 -12
View File
@@ -2,24 +2,24 @@ common:
log_media: stdout
log_level: info
config_paths:
config_dir: ./config
data_dir: ./data/
notification_dir: ./config/notifications/
plugin_dir: ./plugins/
config_dir: "$CONFIG_DIR"
data_dir: "$DATA_DIR"
notification_dir: "$CONFIG_DIR/notifications/"
plugin_dir: "$PLUGINS_DIR"
#simulation_path: /etc/crowdsec/config/simulation.yaml
#hub_dir: /etc/crowdsec/hub/
#index_path: ./config/hub/.index.json
crowdsec_service:
acquisition_path: ./config/acquis.yaml
acquisition_path: "$CONFIG_DIR/acquis.yaml"
parser_routines: 1
plugin_config:
user: $USER # plugin process would be ran on behalf of this user
group: $USER # plugin process would be ran on behalf of this group
user: "$USER" # plugin process would be ran on behalf of this user
group: "$USER" # plugin process would be ran on behalf of this group
cscli:
output: human
db_config:
type: sqlite
db_path: ./data/crowdsec.db
db_path: "$DATA_DIR/crowdsec.db"
user: root
password: crowdsec
db_name: crowdsec
@@ -30,17 +30,17 @@ db_config:
#max_age: 168h
api:
client:
credentials_path: ./config/local_api_credentials.yaml
credentials_path: "$CONFIG_DIR/local_api_credentials.yaml"
server:
console_path: ./config/console.yaml
console_path: "$CONFIG_DIR/console.yaml"
#insecure_skip_verify: true
listen_uri: 127.0.0.1:8081
profiles_path: ./config/profiles.yaml
profiles_path: "$CONFIG_DIR/profiles.yaml"
tls:
#cert_file: ./cert.pem
#key_file: ./key.pem
online_client: # Central API
credentials_path: ./config/online_api_credentials.yaml
credentials_path: "$CONFIG_DIR/online_api_credentials.yaml"
prometheus:
enabled: true
level: full
+46
View File
@@ -0,0 +1,46 @@
common:
log_media: stdout
log_level: info
config_paths:
config_dir: "%CONFIG_DIR%"
data_dir: "%DATA_DIR%"
notification_dir: "%CONFIG_DIR%/notifications/"
plugin_dir: "%PLUGINS_DIR%"
#simulation_path: /etc/crowdsec/config/simulation.yaml
#hub_dir: /etc/crowdsec/hub/
#index_path: ./config/hub/.index.json
crowdsec_service:
acquisition_path: "%CONFIG_DIR%/acquis.yaml"
parser_routines: 1
plugin_config:
user: "%USERNAME%" # plugin process would be ran on behalf of this user
group: "%USERNAME%" # plugin process would be ran on behalf of this group
cscli:
output: human
db_config:
type: sqlite
db_path: "%DATA_DIR%/crowdsec.db"
user: root
password: crowdsec
db_name: crowdsec
host: "172.17.0.2"
port: 3306
flush:
#max_items: 10000
#max_age: 168h
api:
client:
credentials_path: "%CONFIG_DIR%/local_api_credentials.yaml"
server:
console_path: "%CONFIG_DIR%/console.yaml"
#insecure_skip_verify: true
listen_uri: 127.0.0.1:8081
profiles_path: "%CONFIG_DIR%/profiles.yaml"
tls:
#cert_file: ./cert.pem
#key_file: ./key.pem
online_client: # Central API
credentials_path: "%CONFIG_DIR%/online_api_credentials.yaml"
prometheus:
enabled: true
level: full
+14 -25
View File
@@ -13,13 +13,6 @@ function create_tree() {
$null = New-Item -ItemType Directory $data_dir
$null = New-Item -ItemType Directory $log_dir
$null = New-Item -ItemType Directory $config_dir
$null = New-Item -ItemType Directory $parser_dir
$null = New-Item -ItemType Directory $parser_s00
$null = New-Item -ItemType Directory $parser_s01
$null = New-Item -ItemType Directory $parser_s02
$null = New-Item -ItemType Directory $scenarios_dir
$null = New-Item -ItemType Directory $postoverflows_dir
$null = New-Item -ItemType Directory $cscli_dir
$null = New-Item -ItemType Directory $hub_dir
$null = New-Item -ItemType Directory $config_dir\$notif_dir
$null = New-Item -ItemType Directory $base\$plugins_dir
@@ -27,15 +20,18 @@ function create_tree() {
function copy_file() {
$null = Copy-Item ".\config\profiles.yaml" $config_dir
$null = Copy-Item ".\config\simulation.yaml" $config_dir
$null = Copy-Item ".\cmd\crowdsec\crowdsec.exe" $base
$null = Copy-Item ".\cmd\crowdsec-cli\cscli.exe" $base
$null = Copy-Item -Recurse ".\config\patterns" $config_dir
$null = Copy-Item ".\config\acquis.yaml" $config_dir
$null = New-Item -ItemType File $config_dir\local_api_credentials.yaml
$null = New-Item -ItemType File $config_dir\online_api_credentials.yaml
#envsubst < "./config/dev.yaml" > $BASE/dev.yaml
Copy-Item .\config\dev.yaml $base\dev.yaml
$null = Copy-Item ".\config\simulation.yaml" $config_dir
$null = Copy-Item ".\cmd\crowdsec\crowdsec.exe" $base
$null = Copy-Item ".\cmd\crowdsec-cli\cscli.exe" $base
$null = Copy-Item -Recurse ".\config\patterns" $config_dir
$null = Copy-Item ".\config\acquis.yaml" $config_dir
$null = New-Item -ItemType File $config_dir\local_api_credentials.yaml
$null = New-Item -ItemType File $config_dir\online_api_credentials.yaml
#envsubst < "./config/dev.yaml" > $BASE/dev.yaml
$raw = Get-Content .\config\dev.yaml -Raw
[Environment]::ExpandEnvironmentVariables($raw) | Set-Content $base\dev.yaml
$plugins | ForEach-Object {
Copy-Item .\cmd\notification-$_\notification-$_.exe $base\$plugins_dir\notification-$_.exe
Copy-Item .\cmd\notification-$_\$_.yaml $config_dir\$notif_dir\$_.yaml
@@ -62,14 +58,7 @@ $base=(Resolve-Path $base).Path
$data_dir="$base\data"
$log_dir="$base\logs\"
$config_dir="$base\config"
$config_file="$base\dev.yaml"
$cscli_dir="$config_dir\crowdsec-cli"
$parser_dir="$config_dir\parsers"
$parser_s00="$parser_dir\s00-raw"
$parser_s01="$parser_dir\s01-parse"
$parser_s02="$parser_dir\s02-enrich"
$scenarios_dir="$config_dir\scenarios"
$postoverflows_dir="$config_dir\postoverflows"
$config_file="$base\windows-dev.yaml"
$hub_dir="$config_dir\hub"
$plugins=@("http", "slack", "splunk", "email", "sentinel")
$plugins_dir="plugins"
@@ -87,4 +76,4 @@ $cur_path=$pwd
Set-Location $base
setup_api
setup
Set-Location $cur_path
Set-Location $cur_path
+22 -38
View File
@@ -1,4 +1,6 @@
#!/bin/bash
#!/bin/sh
set -eu
BASE="./tests"
@@ -6,11 +8,10 @@ usage() {
echo "Usage:"
echo " $0 -h Display this help message."
echo " $0 -d ./tests Create test environment in './tests' folder"
exit 0
}
while [[ $# -gt 0 ]]
while [ $# -gt 0 ]
do
key="${1}"
case ${key} in
@@ -31,24 +32,15 @@ do
esac
done
BASE=$(realpath $BASE)
BASE=$(realpath "$BASE")
DATA_DIR="$BASE/data"
LOG_DIR="$BASE/logs/"
CONFIG_DIR="$BASE/config"
CONFIG_FILE="$BASE/dev.yaml"
CSCLI_DIR="$CONFIG_DIR/crowdsec-cli"
PARSER_DIR="$CONFIG_DIR/parsers"
PARSER_S00="$PARSER_DIR/s00-raw"
PARSER_S01="$PARSER_DIR/s01-parse"
PARSER_S02="$PARSER_DIR/s02-enrich"
SCENARIOS_DIR="$CONFIG_DIR/scenarios"
POSTOVERFLOWS_DIR="$CONFIG_DIR/postoverflows"
HUB_DIR="$CONFIG_DIR/hub"
PLUGINS="http slack splunk email sentinel"
PLUGINS_DIR="plugins"
PLUGINS="http slack splunk email sentinel file"
PLUGINS_DIR="$BASE/plugins"
NOTIF_DIR="notifications"
log_info() {
@@ -60,18 +52,11 @@ log_info() {
create_tree() {
mkdir -p "$BASE"
mkdir -p "$DATA_DIR"
mkdir -p "$LOG_DIR"
mkdir -p "$BASE/logs/"
mkdir -p "$CONFIG_DIR"
mkdir -p "$PARSER_DIR"
mkdir -p "$PARSER_S00"
mkdir -p "$PARSER_S01"
mkdir -p "$PARSER_S02"
mkdir -p "$SCENARIOS_DIR"
mkdir -p "$POSTOVERFLOWS_DIR"
mkdir -p "$CSCLI_DIR"
mkdir -p "$HUB_DIR"
mkdir -p "$CONFIG_DIR/$NOTIF_DIR/$plugin"
mkdir -p "$BASE/$PLUGINS_DIR"
mkdir -p "$CONFIG_DIR/$NOTIF_DIR"
mkdir -p "$PLUGINS_DIR"
}
copy_files() {
@@ -83,25 +68,25 @@ copy_files() {
cp "./config/acquis.yaml" "$CONFIG_DIR"
touch "$CONFIG_DIR"/local_api_credentials.yaml
touch "$CONFIG_DIR"/online_api_credentials.yaml
envsubst < "./config/dev.yaml" > $BASE/dev.yaml
for plugin in $PLUGINS
do
cp cmd/notification-$plugin/notification-$plugin $BASE/$PLUGINS_DIR/notification-$plugin
cp cmd/notification-$plugin/$plugin.yaml $CONFIG_DIR/$NOTIF_DIR/$plugin.yaml
# shellcheck disable=SC2016
CONFIG_DIR="$CONFIG_DIR" DATA_DIR="$DATA_DIR" PLUGINS_DIR="$PLUGINS_DIR" envsubst '$CONFIG_DIR $DATA_DIR $PLUGINS_DIR' < "./config/dev.yaml" > "$BASE/dev.yaml"
for plugin in $PLUGINS; do
cp "cmd/notification-$plugin/notification-$plugin" "$PLUGINS_DIR/notification-$plugin"
cp "cmd/notification-$plugin/$plugin.yaml" "$CONFIG_DIR/$NOTIF_DIR/$plugin.yaml"
done
}
setup() {
$BASE/cscli -c "$CONFIG_FILE" hub update
$BASE/cscli -c "$CONFIG_FILE" collections install crowdsecurity/linux
"$BASE/cscli" -c "$CONFIG_FILE" hub update
"$BASE/cscli" -c "$CONFIG_FILE" collections install crowdsecurity/linux
}
setup_api() {
$BASE/cscli -c "$CONFIG_FILE" machines add test -p testpassword -f $CONFIG_DIR/local_api_credentials.yaml --force
"$BASE/cscli" -c "$CONFIG_FILE" machines add test -p testpassword -f "$CONFIG_DIR/local_api_credentials.yaml" --force
}
main() {
log_info "Creating test tree in $BASE"
create_tree
@@ -111,12 +96,11 @@ main() {
log_info "Files copied"
log_info "Setting up configurations"
CURRENT_PWD=$(pwd)
cd $BASE
cd "$BASE" || exit 2
setup_api
setup
cd $CURRENT_PWD
cd "$CURRENT_PWD" || exit 2
log_info "Environment is ready in $BASE"
}
main
+2
View File
@@ -162,8 +162,10 @@ install_crowdsec() {
[ ! -f "$CROWDSEC_CONFIG_DIR"/console.yaml ] && install -v -m 644 -D ./config/console.yaml "$CROWDSEC_CONFIG_DIR" >/dev/null || exit
[ ! -f "$CROWDSEC_DATA_DIR"/detect.yaml ] && install -v -m 600 -D ./config/detect.yaml "$CROWDSEC_DATA_DIR" >/dev/null || exit
# shellcheck disable=SC2016
DATA="$CROWDSEC_DATA_DIR" CFG="$CROWDSEC_CONFIG_DIR" envsubst '$CFG $DATA' < ./config/user.yaml > "$CROWDSEC_CONFIG_DIR"/user.yaml || log_fatal "unable to generate user configuration file"
if [ "$DOCKER_MODE" = "false" ]; then
# shellcheck disable=SC2016
CFG="$CROWDSEC_CONFIG_DIR" BIN="$CROWDSEC_BIN_INSTALLED" envsubst '$CFG $BIN' < ./config/crowdsec.service > "${SYSTEMD_PATH_FILE}" || log_fatal "unable to crowdsec systemd file"
fi
install_bins