2022-05-19 13:27:24 +02:00
#!/usr/bin/env bats
# vim: ft=bats:list:ts=8:sts=4:sw=4:et:ai:si:
set -u
setup_file() {
load "../lib/setup_file.sh"
2022-07-01 22:45:55 +02:00
PLUGIN_DIR=$(config_get '.config_paths.plugin_dir')
2022-07-01 10:16:45 +02:00
# could have a trailing slash
2022-07-13 10:10:08 +02:00
PLUGIN_DIR=$(realpath "${PLUGIN_DIR}")
2022-05-19 13:27:24 +02:00
export PLUGIN_DIR
2022-07-01 22:45:55 +02:00
PROFILES_PATH=$(config_get '.api.server.profiles_path')
2022-05-19 13:27:24 +02:00
export PROFILES_PATH
}
teardown_file() {
load "../lib/teardown_file.sh"
}
setup() {
load "../lib/setup.sh"
./instance-data load
}
teardown() {
./instance-crowdsec stop
rm -f "${PLUGIN_DIR}"/badname
chmod go-w "${PLUGIN_DIR}"/notification-http
}
#----------
2022-07-01 11:03:40 +02:00
@test "misconfigured plugin, only user is empty" {
2022-07-01 22:45:55 +02:00
config_set '.plugin_config.user="" | .plugin_config.group="nogroup"'
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2023-06-01 09:42:27 +02:00
rune -1 timeout 2s "${CROWDSEC}"
2023-06-22 15:01:34 +02:00
assert_stderr --partial "api server init: unable to run plugin broker: while loading plugin: while getting process attributes: both plugin user and group must be set"
2022-05-19 13:27:24 +02:00
}
2022-07-01 11:03:40 +02:00
@test "misconfigured plugin, only group is empty" {
2022-07-01 22:45:55 +02:00
config_set '(.plugin_config.user="nobody") | (.plugin_config.group="")'
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2023-06-01 09:42:27 +02:00
rune -1 timeout 2s "${CROWDSEC}"
2023-06-22 15:01:34 +02:00
assert_stderr --partial "api server init: unable to run plugin broker: while loading plugin: while getting process attributes: both plugin user and group must be set"
2022-05-19 13:27:24 +02:00
}
2022-07-01 11:03:40 +02:00
@test "misconfigured plugin, user does not exist" {
2022-07-01 22:45:55 +02:00
config_set '(.plugin_config.user="userdoesnotexist") | (.plugin_config.group="groupdoesnotexist")'
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2023-06-01 09:42:27 +02:00
rune -1 timeout 2s "${CROWDSEC}"
2023-06-22 15:01:34 +02:00
assert_stderr --partial "api server init: unable to run plugin broker: while loading plugin: while getting process attributes: user: unknown user userdoesnotexist"
2022-05-19 13:27:24 +02:00
}
2022-07-01 11:03:40 +02:00
@test "misconfigured plugin, group does not exist" {
2022-07-01 22:45:55 +02:00
config_set '(.plugin_config.user=strenv(USER)) | (.plugin_config.group="groupdoesnotexist")'
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2023-06-01 09:42:27 +02:00
rune -1 timeout 2s "${CROWDSEC}"
2023-06-22 15:01:34 +02:00
assert_stderr --partial "api server init: unable to run plugin broker: while loading plugin: while getting process attributes: group: unknown group groupdoesnotexist"
2022-05-19 13:27:24 +02:00
}
2022-07-01 11:03:40 +02:00
@test "bad plugin name" {
2022-07-01 22:45:55 +02:00
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2022-05-19 13:27:24 +02:00
cp "${PLUGIN_DIR}"/notification-http "${PLUGIN_DIR}"/badname
2023-06-01 09:42:27 +02:00
rune -1 timeout 2s "${CROWDSEC}"
2023-06-22 15:01:34 +02:00
assert_stderr --partial "api server init: unable to run plugin broker: while loading plugin: plugin name ${PLUGIN_DIR}/badname is invalid. Name should be like {type-name}"
2022-05-19 13:27:24 +02:00
}
2023-06-02 14:41:50 +02:00
@test "duplicate notification config" {
CONFIG_DIR=$(dirname "$CONFIG_YAML")
# email_default has two configurations
rune -0 yq -i '.name="email_default"' "$CONFIG_DIR/notifications/http.yaml"
# enable a notification, otherwise plugins are ignored
config_set "${PROFILES_PATH}" '.notifications=["slack_default"]'
# we want to check the logs
config_set '.common.log_media="stdout"'
# the command will fail because slack_deault is not working
run -1 --separate-stderr timeout 2s "${CROWDSEC}"
# but we have what we wanted
assert_stderr --partial "notification 'email_default' is defined multiple times"
}
2022-07-01 11:03:40 +02:00
@test "bad plugin permission (group writable)" {
2022-07-01 22:45:55 +02:00
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2022-05-19 13:27:24 +02:00
chmod g+w "${PLUGIN_DIR}"/notification-http
2023-06-01 09:42:27 +02:00
rune -1 timeout 2s "${CROWDSEC}"
2023-06-22 15:01:34 +02:00
assert_stderr --partial "api server init: unable to run plugin broker: while loading plugin: plugin at ${PLUGIN_DIR}/notification-http is group writable, group writable plugins are invalid"
2022-05-19 13:27:24 +02:00
}
2022-07-01 11:03:40 +02:00
@test "bad plugin permission (world writable)" {
2022-07-01 22:45:55 +02:00
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2022-05-19 13:27:24 +02:00
chmod o+w "${PLUGIN_DIR}"/notification-http
2023-06-01 09:42:27 +02:00
rune -1 timeout 2s "${CROWDSEC}"
2023-06-22 15:01:34 +02:00
assert_stderr --partial "api server init: unable to run plugin broker: while loading plugin: plugin at ${PLUGIN_DIR}/notification-http is world writable, world writable plugins are invalid"
2022-05-19 13:27:24 +02:00
}
2022-07-01 11:03:40 +02:00
@test "config.yaml: missing .plugin_config section" {
2022-07-01 22:45:55 +02:00
config_set 'del(.plugin_config)'
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2023-06-01 09:42:27 +02:00
rune -1 timeout 2s "${CROWDSEC}"
2022-08-05 10:54:49 +02:00
assert_stderr --partial "api server init: plugins are enabled, but the plugin_config section is missing in the configuration"
2022-06-06 15:24:48 +02:00
}
2022-07-01 11:03:40 +02:00
@test "config.yaml: missing config_paths.notification_dir" {
2022-07-01 22:45:55 +02:00
config_set 'del(.config_paths.notification_dir)'
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2023-06-01 09:42:27 +02:00
rune -1 timeout 2s "${CROWDSEC}"
2022-08-05 10:54:49 +02:00
assert_stderr --partial "api server init: plugins are enabled, but config_paths.notification_dir is not defined"
2022-06-06 15:24:48 +02:00
}
2022-07-01 11:03:40 +02:00
@test "config.yaml: missing config_paths.plugin_dir" {
2022-07-01 22:45:55 +02:00
config_set 'del(.config_paths.plugin_dir)'
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2023-06-01 09:42:27 +02:00
rune -1 timeout 2s "${CROWDSEC}"
2022-08-05 10:54:49 +02:00
assert_stderr --partial "api server init: plugins are enabled, but config_paths.plugin_dir is not defined"
2022-06-06 15:24:48 +02:00
}
2023-06-22 15:01:34 +02:00
@test "unable to run plugin broker: while reading plugin config" {
2022-07-01 22:45:55 +02:00
config_set '.config_paths.notification_dir="/this/path/does/not/exist"'
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2023-06-01 09:42:27 +02:00
rune -1 timeout 2s "${CROWDSEC}"
2023-06-22 15:01:34 +02:00
assert_stderr --partial "api server init: unable to run plugin broker: while loading plugin config: open /this/path/does/not/exist: no such file or directory"
2022-06-06 15:24:48 +02:00
}