2022-03-09 13:45:36 +00:00
|
|
|
#!/usr/bin/env bats
|
|
|
|
# vim: ft=bats:list:ts=8:sts=4:sw=4:et:ai:si:
|
|
|
|
|
|
|
|
set -u
|
|
|
|
|
|
|
|
setup_file() {
|
2022-03-18 09:13:12 +00:00
|
|
|
load "../lib/setup_file.sh"
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
teardown_file() {
|
2022-03-18 09:13:12 +00:00
|
|
|
load "../lib/teardown_file.sh"
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setup() {
|
|
|
|
load "../lib/setup.sh"
|
2022-06-06 13:24:48 +00:00
|
|
|
load "../lib/bats-file/load.bash"
|
2022-03-09 13:45:36 +00:00
|
|
|
./instance-data load
|
|
|
|
./instance-crowdsec start
|
|
|
|
}
|
|
|
|
|
|
|
|
teardown() {
|
|
|
|
./instance-crowdsec stop
|
|
|
|
}
|
|
|
|
|
|
|
|
# to silence shellcheck
|
|
|
|
declare stderr
|
|
|
|
|
|
|
|
#----------
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "cscli - usage" {
|
2022-03-31 14:50:38 +00:00
|
|
|
run -0 cscli
|
|
|
|
assert_output --partial "Usage:"
|
|
|
|
assert_output --partial "cscli [command]"
|
|
|
|
assert_output --partial "Available Commands:"
|
2022-05-19 11:42:44 +00:00
|
|
|
|
|
|
|
# no "usage" output after every error
|
|
|
|
run -1 --separate-stderr cscli blahblah
|
|
|
|
# error is displayed as log entry, not with print
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr --partial 'level=fatal msg="unknown command \"blahblah\" for \"cscli\""'
|
|
|
|
refute_stderr --partial 'unknown command "blahblah" for "cscli"'
|
2022-03-31 14:50:38 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "cscli version" {
|
2022-03-09 13:45:36 +00:00
|
|
|
run -0 cscli version
|
|
|
|
assert_output --partial "version:"
|
|
|
|
assert_output --partial "Codename:"
|
|
|
|
assert_output --partial "BuildDate:"
|
|
|
|
assert_output --partial "GoVersion:"
|
|
|
|
assert_output --partial "Platform:"
|
|
|
|
assert_output --partial "Constraint_parser:"
|
|
|
|
assert_output --partial "Constraint_scenario:"
|
|
|
|
assert_output --partial "Constraint_api:"
|
|
|
|
assert_output --partial "Constraint_acquis:"
|
2022-04-13 13:44:23 +00:00
|
|
|
|
|
|
|
# should work without configuration file
|
|
|
|
rm "${CONFIG_YAML}"
|
|
|
|
run -0 cscli version
|
|
|
|
assert_output --partial "version:"
|
|
|
|
}
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "cscli help" {
|
2022-04-13 13:44:23 +00:00
|
|
|
run -0 cscli help
|
|
|
|
assert_line "Available Commands:"
|
|
|
|
assert_line --regexp ".* help .* Help about any command"
|
|
|
|
|
|
|
|
# should work without configuration file
|
|
|
|
rm "${CONFIG_YAML}"
|
|
|
|
run -0 cscli help
|
|
|
|
assert_line "Available Commands:"
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "cscli config show -o human" {
|
2022-03-09 13:45:36 +00:00
|
|
|
run -0 cscli config show -o human
|
|
|
|
assert_output --partial "Global:"
|
|
|
|
assert_output --partial "Crowdsec:"
|
|
|
|
assert_output --partial "cscli:"
|
|
|
|
assert_output --partial "Local API Server:"
|
|
|
|
}
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "cscli config show -o json" {
|
2022-03-09 13:45:36 +00:00
|
|
|
run -0 cscli config show -o json
|
|
|
|
assert_output --partial '"API":'
|
|
|
|
assert_output --partial '"Common":'
|
|
|
|
assert_output --partial '"ConfigPaths":'
|
|
|
|
assert_output --partial '"Crowdsec":'
|
|
|
|
assert_output --partial '"Cscli":'
|
|
|
|
assert_output --partial '"DbConfig":'
|
|
|
|
assert_output --partial '"Hub":'
|
|
|
|
assert_output --partial '"PluginConfig":'
|
|
|
|
assert_output --partial '"Prometheus":'
|
|
|
|
}
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "cscli config show -o raw" {
|
2022-03-09 13:45:36 +00:00
|
|
|
run -0 cscli config show -o raw
|
|
|
|
assert_line "api:"
|
|
|
|
assert_line "common:"
|
|
|
|
assert_line "config_paths:"
|
|
|
|
assert_line "crowdsec_service:"
|
|
|
|
assert_line "cscli:"
|
|
|
|
assert_line "db_config:"
|
|
|
|
assert_line "plugin_config:"
|
|
|
|
assert_line "prometheus:"
|
|
|
|
}
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "cscli config show --key" {
|
2022-03-09 13:45:36 +00:00
|
|
|
run -0 cscli config show --key Config.API.Server.ListenURI
|
|
|
|
assert_output "127.0.0.1:8080"
|
|
|
|
}
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "cscli config backup / restore" {
|
2022-06-06 13:24:48 +00:00
|
|
|
# test that we need a valid path
|
|
|
|
# disabled because in CI, the empty string is not passed as a parameter
|
|
|
|
## run -1 --separate-stderr cscli config backup ""
|
2022-08-05 08:54:49 +00:00
|
|
|
## assert_stderr --partial "Failed to backup configurations: directory path can't be empty"
|
2022-06-06 13:24:48 +00:00
|
|
|
|
|
|
|
run -1 --separate-stderr cscli config backup "/dev/null/blah"
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr --partial "Failed to backup configurations: while creating /dev/null/blah: mkdir /dev/null/blah: not a directory"
|
2022-06-06 13:24:48 +00:00
|
|
|
|
|
|
|
# pick a dirpath
|
2022-03-10 11:19:02 +00:00
|
|
|
backupdir=$(TMPDIR="${BATS_TEST_TMPDIR}" mktemp -u)
|
2022-06-06 13:24:48 +00:00
|
|
|
|
|
|
|
# succeed the first time
|
2022-03-10 11:19:02 +00:00
|
|
|
run -0 cscli config backup "${backupdir}"
|
2022-03-09 13:45:36 +00:00
|
|
|
assert_output --partial "Starting configuration backup"
|
|
|
|
|
2022-06-06 13:24:48 +00:00
|
|
|
# don't overwrite an existing backup
|
|
|
|
run -1 --separate-stderr cscli config backup "${backupdir}"
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr --partial "Failed to backup configurations"
|
|
|
|
assert_stderr --partial "file exists"
|
2022-06-06 13:24:48 +00:00
|
|
|
|
2022-07-01 20:45:55 +00:00
|
|
|
SIMULATION_YAML="$(config_get '.config_paths.simulation_path')"
|
2022-06-06 13:24:48 +00:00
|
|
|
|
|
|
|
# restore
|
|
|
|
rm "${SIMULATION_YAML}"
|
|
|
|
run -0 cscli config restore "${backupdir}"
|
|
|
|
assert_file_exist "${SIMULATION_YAML}"
|
|
|
|
|
|
|
|
# cleanup
|
|
|
|
rm -rf -- "${backupdir:?}"
|
|
|
|
|
|
|
|
# backup: detect missing files
|
|
|
|
rm "${SIMULATION_YAML}"
|
|
|
|
run -1 --separate-stderr cscli config backup "${backupdir}"
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr --regexp "Failed to backup configurations: failed copy .* to .*: stat .*: no such file or directory"
|
2022-03-10 11:19:02 +00:00
|
|
|
rm -rf -- "${backupdir:?}"
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "cscli lapi status" {
|
2022-03-09 13:45:36 +00:00
|
|
|
run -0 --separate-stderr cscli lapi status
|
|
|
|
|
2022-05-19 08:48:08 +00:00
|
|
|
run -0 echo "${stderr}"
|
2022-03-09 13:45:36 +00:00
|
|
|
assert_output --partial "Loaded credentials from"
|
|
|
|
assert_output --partial "Trying to authenticate with username"
|
|
|
|
assert_output --partial " on http://127.0.0.1:8080/"
|
|
|
|
assert_output --partial "You can successfully interact with Local API (LAPI)"
|
|
|
|
}
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "cscli - missing LAPI credentials file" {
|
2022-07-01 20:45:55 +00:00
|
|
|
LOCAL_API_CREDENTIALS=$(config_get '.api.client.credentials_path')
|
2022-05-19 08:48:08 +00:00
|
|
|
rm -f "${LOCAL_API_CREDENTIALS}"
|
|
|
|
run -1 --separate-stderr cscli lapi status
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr --partial "loading api client: while reading yaml file: open ${LOCAL_API_CREDENTIALS}: no such file or directory"
|
2022-05-19 08:48:08 +00:00
|
|
|
|
|
|
|
run -1 --separate-stderr cscli alerts list
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr --partial "loading api client: while reading yaml file: open ${LOCAL_API_CREDENTIALS}: no such file or directory"
|
2022-05-19 08:48:08 +00:00
|
|
|
|
|
|
|
run -1 --separate-stderr cscli decisions list
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr --partial "loading api client: while reading yaml file: open ${LOCAL_API_CREDENTIALS}: no such file or directory"
|
2022-05-19 08:48:08 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "cscli - empty LAPI credentials file" {
|
2022-07-01 20:45:55 +00:00
|
|
|
LOCAL_API_CREDENTIALS=$(config_get '.api.client.credentials_path')
|
2022-05-19 08:48:08 +00:00
|
|
|
truncate -s 0 "${LOCAL_API_CREDENTIALS}"
|
|
|
|
run -1 --separate-stderr cscli lapi status
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr --partial "no credentials or URL found in api client configuration '${LOCAL_API_CREDENTIALS}'"
|
2022-05-19 08:48:08 +00:00
|
|
|
|
|
|
|
run -1 --separate-stderr cscli alerts list
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr --partial "no credentials or URL found in api client configuration '${LOCAL_API_CREDENTIALS}'"
|
2022-05-19 08:48:08 +00:00
|
|
|
|
|
|
|
run -1 --separate-stderr cscli decisions list
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr --partial "no credentials or URL found in api client configuration '${LOCAL_API_CREDENTIALS}'"
|
2022-05-19 08:48:08 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "cscli - missing LAPI client settings" {
|
2022-07-01 20:45:55 +00:00
|
|
|
config_set 'del(.api.client)'
|
2022-05-19 08:48:08 +00:00
|
|
|
run -1 --separate-stderr cscli lapi status
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr --partial "loading api client: no API client section in configuration"
|
2022-05-19 08:48:08 +00:00
|
|
|
|
|
|
|
run -1 --separate-stderr cscli alerts list
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr --partial "loading api client: no API client section in configuration"
|
2022-05-19 08:48:08 +00:00
|
|
|
|
|
|
|
run -1 --separate-stderr cscli decisions list
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr --partial "loading api client: no API client section in configuration"
|
2022-05-19 08:48:08 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "cscli - malformed LAPI url" {
|
2022-07-01 20:45:55 +00:00
|
|
|
LOCAL_API_CREDENTIALS=$(config_get '.api.client.credentials_path')
|
|
|
|
config_set "${LOCAL_API_CREDENTIALS}" '.url="https://127.0.0.1:-80"'
|
2022-05-19 08:48:08 +00:00
|
|
|
|
|
|
|
run -1 --separate-stderr cscli lapi status
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr --partial 'parsing api url'
|
|
|
|
assert_stderr --partial 'invalid port \":-80\" after host'
|
2022-05-19 08:48:08 +00:00
|
|
|
|
|
|
|
run -1 --separate-stderr cscli alerts list
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr --partial 'parsing api url'
|
|
|
|
assert_stderr --partial 'invalid port \":-80\" after host'
|
2022-05-19 08:48:08 +00:00
|
|
|
|
|
|
|
run -1 --separate-stderr cscli decisions list
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr --partial 'parsing api url'
|
|
|
|
assert_stderr --partial 'invalid port \":-80\" after host'
|
2022-05-19 08:48:08 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "cscli metrics" {
|
2022-03-09 13:45:36 +00:00
|
|
|
run -0 cscli lapi status
|
|
|
|
run -0 --separate-stderr cscli metrics
|
2022-10-07 09:05:35 +00:00
|
|
|
assert_output --partial "Route"
|
2022-03-09 13:45:36 +00:00
|
|
|
assert_output --partial '/v1/watchers/login'
|
2022-08-18 09:54:01 +00:00
|
|
|
assert_output --partial "Local Api Metrics:"
|
2022-03-09 13:45:36 +00:00
|
|
|
|
|
|
|
}
|
2022-03-10 13:57:14 +00:00
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "'cscli completion' with or without configuration file" {
|
2022-03-10 13:57:14 +00:00
|
|
|
run -0 cscli completion bash
|
|
|
|
assert_output --partial "# bash completion for cscli"
|
|
|
|
run -0 cscli completion zsh
|
|
|
|
assert_output --partial "# zsh completion for cscli"
|
2022-06-06 13:24:48 +00:00
|
|
|
run -0 cscli completion powershell
|
|
|
|
assert_output --partial "# powershell completion for cscli"
|
|
|
|
run -0 cscli completion fish
|
|
|
|
assert_output --partial "# fish completion for cscli"
|
2022-03-18 09:13:12 +00:00
|
|
|
|
|
|
|
rm "${CONFIG_YAML}"
|
2022-03-10 13:57:14 +00:00
|
|
|
run -0 cscli completion bash
|
|
|
|
assert_output --partial "# bash completion for cscli"
|
|
|
|
}
|
2022-04-13 13:44:23 +00:00
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "cscli hub list" {
|
2022-04-19 12:22:59 +00:00
|
|
|
# we check for the presence of some objects. There may be others when we
|
|
|
|
# use $PACKAGE_TESTING, so the order is not important.
|
|
|
|
|
2022-04-13 13:44:23 +00:00
|
|
|
run -0 cscli hub list -o human
|
|
|
|
assert_line --regexp '^ crowdsecurity/linux'
|
|
|
|
assert_line --regexp '^ crowdsecurity/sshd'
|
|
|
|
assert_line --regexp '^ crowdsecurity/dateparse-enrich'
|
|
|
|
assert_line --regexp '^ crowdsecurity/geoip-enrich'
|
|
|
|
assert_line --regexp '^ crowdsecurity/sshd-logs'
|
|
|
|
assert_line --regexp '^ crowdsecurity/syslog-logs'
|
|
|
|
assert_line --regexp '^ crowdsecurity/ssh-bf'
|
|
|
|
assert_line --regexp '^ crowdsecurity/ssh-slow-bf'
|
|
|
|
|
|
|
|
run -0 cscli hub list -o raw
|
|
|
|
assert_line --regexp '^crowdsecurity/linux,enabled,[0-9]+\.[0-9]+,core linux support : syslog\+geoip\+ssh,collections$'
|
|
|
|
assert_line --regexp '^crowdsecurity/sshd,enabled,[0-9]+\.[0-9]+,sshd support : parser and brute-force detection,collections$'
|
|
|
|
assert_line --regexp '^crowdsecurity/dateparse-enrich,enabled,[0-9]+\.[0-9]+,,parsers$'
|
|
|
|
assert_line --regexp '^crowdsecurity/geoip-enrich,enabled,[0-9]+\.[0-9]+,"Populate event with geoloc info : as, country, coords, source range.",parsers$'
|
|
|
|
assert_line --regexp '^crowdsecurity/sshd-logs,enabled,[0-9]+\.[0-9]+,Parse openSSH logs,parsers$'
|
|
|
|
assert_line --regexp '^crowdsecurity/syslog-logs,enabled,[0-9]+\.[0-9]+,,parsers$'
|
|
|
|
assert_line --regexp '^crowdsecurity/ssh-bf,enabled,[0-9]+\.[0-9]+,Detect ssh bruteforce,scenarios$'
|
|
|
|
assert_line --regexp '^crowdsecurity/ssh-slow-bf,enabled,[0-9]+\.[0-9]+,Detect slow ssh bruteforce,scenarios$'
|
|
|
|
|
|
|
|
run -0 cscli hub list -o json
|
2022-04-19 12:22:59 +00:00
|
|
|
run jq -r '.collections[].name, .parsers[].name, .scenarios[].name' <(output)
|
|
|
|
assert_line 'crowdsecurity/linux'
|
|
|
|
assert_line 'crowdsecurity/sshd'
|
|
|
|
assert_line 'crowdsecurity/dateparse-enrich'
|
|
|
|
assert_line 'crowdsecurity/geoip-enrich'
|
|
|
|
assert_line 'crowdsecurity/sshd-logs'
|
|
|
|
assert_line 'crowdsecurity/syslog-logs'
|
|
|
|
assert_line 'crowdsecurity/ssh-bf'
|
|
|
|
assert_line 'crowdsecurity/ssh-slow-bf'
|
2022-04-13 13:44:23 +00:00
|
|
|
}
|