2022-05-19 11:27:24 +00: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 20:45:55 +00:00
PLUGIN_DIR=$(config_get '.config_paths.plugin_dir')
2022-07-01 08:16:45 +00:00
# could have a trailing slash
2022-07-13 08:10:08 +00:00
PLUGIN_DIR=$(realpath "${PLUGIN_DIR}")
2022-05-19 11:27:24 +00:00
export PLUGIN_DIR
2022-07-01 20:45:55 +00:00
PROFILES_PATH=$(config_get '.api.server.profiles_path')
2022-05-19 11:27:24 +00: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 09:03:40 +00:00
@test "misconfigured plugin, only user is empty" {
2022-07-01 20:45:55 +00:00
config_set '.plugin_config.user="" | .plugin_config.group="nogroup"'
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2023-06-01 07:42:27 +00:00
rune -1 timeout 2s "${CROWDSEC}"
2022-08-05 08:54:49 +00:00
assert_stderr --partial "api server init: unable to run local API: while loading plugin: while getting process attributes: both plugin user and group must be set"
2022-05-19 11:27:24 +00:00
}
2022-07-01 09:03:40 +00:00
@test "misconfigured plugin, only group is empty" {
2022-07-01 20:45:55 +00:00
config_set '(.plugin_config.user="nobody") | (.plugin_config.group="")'
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2023-06-01 07:42:27 +00:00
rune -1 timeout 2s "${CROWDSEC}"
2022-08-05 08:54:49 +00:00
assert_stderr --partial "api server init: unable to run local API: while loading plugin: while getting process attributes: both plugin user and group must be set"
2022-05-19 11:27:24 +00:00
}
2022-07-01 09:03:40 +00:00
@test "misconfigured plugin, user does not exist" {
2022-07-01 20:45:55 +00:00
config_set '(.plugin_config.user="userdoesnotexist") | (.plugin_config.group="groupdoesnotexist")'
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2023-06-01 07:42:27 +00:00
rune -1 timeout 2s "${CROWDSEC}"
2022-08-05 08:54:49 +00:00
assert_stderr --partial "api server init: unable to run local API: while loading plugin: while getting process attributes: user: unknown user userdoesnotexist"
2022-05-19 11:27:24 +00:00
}
2022-07-01 09:03:40 +00:00
@test "misconfigured plugin, group does not exist" {
2022-07-01 20:45:55 +00:00
config_set '(.plugin_config.user=strenv(USER)) | (.plugin_config.group="groupdoesnotexist")'
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2023-06-01 07:42:27 +00:00
rune -1 timeout 2s "${CROWDSEC}"
2022-08-05 08:54:49 +00:00
assert_stderr --partial "api server init: unable to run local API: while loading plugin: while getting process attributes: group: unknown group groupdoesnotexist"
2022-05-19 11:27:24 +00:00
}
2022-07-01 09:03:40 +00:00
@test "bad plugin name" {
2022-07-01 20:45:55 +00:00
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2022-05-19 11:27:24 +00:00
cp "${PLUGIN_DIR}"/notification-http "${PLUGIN_DIR}"/badname
2023-06-01 07:42:27 +00:00
rune -1 timeout 2s "${CROWDSEC}"
2022-08-05 08:54:49 +00:00
assert_stderr --partial "api server init: unable to run local API: while loading plugin: plugin name ${PLUGIN_DIR}/badname is invalid. Name should be like {type-name}"
2022-05-19 11:27:24 +00:00
}
2022-07-01 09:03:40 +00:00
@test "bad plugin permission (group writable)" {
2022-07-01 20:45:55 +00:00
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2022-05-19 11:27:24 +00:00
chmod g+w "${PLUGIN_DIR}"/notification-http
2023-06-01 07:42:27 +00:00
rune -1 timeout 2s "${CROWDSEC}"
2022-08-05 08:54:49 +00:00
assert_stderr --partial "api server init: unable to run local API: while loading plugin: plugin at ${PLUGIN_DIR}/notification-http is group writable, group writable plugins are invalid"
2022-05-19 11:27:24 +00:00
}
2022-07-01 09:03:40 +00:00
@test "bad plugin permission (world writable)" {
2022-07-01 20:45:55 +00:00
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2022-05-19 11:27:24 +00:00
chmod o+w "${PLUGIN_DIR}"/notification-http
2023-06-01 07:42:27 +00:00
rune -1 timeout 2s "${CROWDSEC}"
2022-08-05 08:54:49 +00:00
assert_stderr --partial "api server init: unable to run local API: while loading plugin: plugin at ${PLUGIN_DIR}/notification-http is world writable, world writable plugins are invalid"
2022-05-19 11:27:24 +00:00
}
2022-07-01 09:03:40 +00:00
@test "config.yaml: missing .plugin_config section" {
2022-07-01 20:45:55 +00:00
config_set 'del(.plugin_config)'
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2023-06-01 07:42:27 +00:00
rune -1 timeout 2s "${CROWDSEC}"
2022-08-05 08:54:49 +00:00
assert_stderr --partial "api server init: plugins are enabled, but the plugin_config section is missing in the configuration"
2022-06-06 13:24:48 +00:00
}
2022-07-01 09:03:40 +00:00
@test "config.yaml: missing config_paths.notification_dir" {
2022-07-01 20:45:55 +00:00
config_set 'del(.config_paths.notification_dir)'
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2023-06-01 07:42:27 +00:00
rune -1 timeout 2s "${CROWDSEC}"
2022-08-05 08:54:49 +00:00
assert_stderr --partial "api server init: plugins are enabled, but config_paths.notification_dir is not defined"
2022-06-06 13:24:48 +00:00
}
2022-07-01 09:03:40 +00:00
@test "config.yaml: missing config_paths.plugin_dir" {
2022-07-01 20:45:55 +00:00
config_set 'del(.config_paths.plugin_dir)'
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2023-06-01 07:42:27 +00:00
rune -1 timeout 2s "${CROWDSEC}"
2022-08-05 08:54:49 +00:00
assert_stderr --partial "api server init: plugins are enabled, but config_paths.plugin_dir is not defined"
2022-06-06 13:24:48 +00:00
}
2022-07-01 09:03:40 +00:00
@test "unable to run local API: while reading plugin config" {
2022-07-01 20:45:55 +00:00
config_set '.config_paths.notification_dir="/this/path/does/not/exist"'
config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
2023-06-01 07:42:27 +00:00
rune -1 timeout 2s "${CROWDSEC}"
2022-08-05 08:54:49 +00:00
assert_stderr --partial "api server init: unable to run local API: while loading plugin config: open /this/path/does/not/exist: no such file or directory"
2022-06-06 13:24:48 +00:00
}