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"
|
|
|
|
./instance-data load
|
|
|
|
./instance-crowdsec start
|
|
|
|
}
|
|
|
|
|
|
|
|
teardown() {
|
|
|
|
./instance-crowdsec stop
|
|
|
|
}
|
|
|
|
|
|
|
|
#----------
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "we can list collections" {
|
2022-03-09 13:45:36 +00:00
|
|
|
run -0 cscli collections list
|
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "there are 2 collections (linux and sshd)" {
|
2023-02-21 21:12:08 +00:00
|
|
|
rune -0 cscli collections list -o json
|
|
|
|
rune -0 jq '.collections | length' <(output)
|
2022-03-09 13:45:36 +00:00
|
|
|
assert_output 2
|
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "can install a collection (as a regular user) and remove it" {
|
2022-08-05 08:54:49 +00:00
|
|
|
# collection is not installed
|
2023-02-21 21:12:08 +00:00
|
|
|
rune -0 cscli collections list -o json
|
|
|
|
rune -0 jq -r '.collections[].name' <(output)
|
2022-08-05 08:54:49 +00:00
|
|
|
refute_line "crowdsecurity/mysql"
|
|
|
|
|
|
|
|
# we install it
|
2023-02-21 21:12:08 +00:00
|
|
|
rune -0 cscli collections install crowdsecurity/mysql -o human
|
2022-12-30 14:57:24 +00:00
|
|
|
assert_stderr --partial "Enabled crowdsecurity/mysql"
|
2022-08-05 08:54:49 +00:00
|
|
|
|
|
|
|
# it has been installed
|
2023-02-21 21:12:08 +00:00
|
|
|
rune -0 cscli collections list -o json
|
|
|
|
rune -0 jq -r '.collections[].name' <(output)
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_line "crowdsecurity/mysql"
|
|
|
|
|
|
|
|
# we install it
|
2023-02-21 21:12:08 +00:00
|
|
|
rune -0 cscli collections remove crowdsecurity/mysql -o human
|
|
|
|
assert_stderr --partial "Removed symlink [crowdsecurity/mysql]"
|
2022-08-05 08:54:49 +00:00
|
|
|
|
|
|
|
# it has been removed
|
2023-02-21 21:12:08 +00:00
|
|
|
rune -0 cscli collections list -o json
|
|
|
|
rune -0 jq -r '.collections[].name' <(output)
|
2022-08-05 08:54:49 +00:00
|
|
|
refute_line "crowdsecurity/mysql"
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:10:51 +00:00
|
|
|
@test "must use --force to remove a collection that belongs to another, which becomes tainted" {
|
|
|
|
# we expect no error since we may have multiple collections, some removed and some not
|
2023-02-21 21:12:08 +00:00
|
|
|
rune -0 cscli collections remove crowdsecurity/sshd
|
2022-10-25 12:10:51 +00:00
|
|
|
assert_stderr --partial "crowdsecurity/sshd belongs to other collections"
|
|
|
|
assert_stderr --partial "[crowdsecurity/linux]"
|
|
|
|
|
2023-02-21 21:12:08 +00:00
|
|
|
rune -0 cscli collections remove crowdsecurity/sshd --force
|
2022-10-25 12:10:51 +00:00
|
|
|
assert_stderr --partial "Removed symlink [crowdsecurity/sshd]"
|
2023-02-21 21:12:08 +00:00
|
|
|
rune -0 cscli collections inspect crowdsecurity/linux -o json
|
|
|
|
rune -0 jq -r '.tainted' <(output)
|
2022-10-25 12:10:51 +00:00
|
|
|
assert_output "true"
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "can remove a collection" {
|
2023-02-21 21:12:08 +00:00
|
|
|
rune -0 cscli collections remove crowdsecurity/linux
|
|
|
|
assert_stderr --partial "Removed"
|
|
|
|
assert_stderr --regexp ".*for the new configuration to be effective."
|
|
|
|
rune -0 cscli collections inspect crowdsecurity/linux -o human
|
2022-10-25 12:10:51 +00:00
|
|
|
assert_line 'installed: false'
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "collections delete is an alias for collections remove" {
|
2023-02-21 21:12:08 +00:00
|
|
|
rune -0 cscli collections delete crowdsecurity/linux
|
|
|
|
assert_stderr --partial "Removed"
|
|
|
|
assert_stderr --regexp ".*for the new configuration to be effective."
|
2022-10-25 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "removing a collection that does not exist is noop" {
|
2023-02-21 21:12:08 +00:00
|
|
|
rune -0 cscli collections remove crowdsecurity/apache2
|
|
|
|
refute_stderr --partial "Removed"
|
|
|
|
assert_stderr --regexp ".*for the new configuration to be effective."
|
2022-10-25 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "can remove a removed collection" {
|
2023-02-21 21:12:08 +00:00
|
|
|
rune -0 cscli collections install crowdsecurity/mysql
|
|
|
|
rune -0 cscli collections remove crowdsecurity/mysql
|
|
|
|
assert_stderr --partial "Removed"
|
|
|
|
rune -0 cscli collections remove crowdsecurity/mysql
|
|
|
|
refute_stderr --partial "Removed"
|
2022-10-25 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "can remove all collections" {
|
2022-10-31 09:02:51 +00:00
|
|
|
# we may have this too, from package installs
|
2023-02-21 21:12:08 +00:00
|
|
|
rune cscli parsers delete crowdsecurity/whitelists
|
|
|
|
rune -0 cscli collections remove --all
|
|
|
|
assert_stderr --partial "Removed symlink [crowdsecurity/sshd]"
|
|
|
|
assert_stderr --partial "Removed symlink [crowdsecurity/linux]"
|
|
|
|
rune -0 --separate-stderr cscli hub list -o json
|
2022-10-25 12:10:51 +00:00
|
|
|
assert_json '{collections:[],parsers:[],postoverflows:[],scenarios:[]}'
|
2023-02-21 21:12:08 +00:00
|
|
|
rune -0 cscli collections remove --all
|
|
|
|
assert_stderr --partial 'Disabled 0 items'
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "a taint bubbles up to the top collection" {
|
|
|
|
coll=crowdsecurity/nginx
|
|
|
|
subcoll=crowdsecurity/base-http-scenarios
|
|
|
|
scenario=crowdsecurity/http-crawl-non_statics
|
|
|
|
|
|
|
|
# install a collection with dependencies
|
|
|
|
rune -0 cscli collections install "$coll"
|
|
|
|
|
|
|
|
# the collection, subcollection and scenario are installed and not tainted
|
|
|
|
# we have to default to false because tainted is (as of 1.4.6) returned
|
|
|
|
# only when true
|
|
|
|
rune -0 cscli collections inspect "$coll" -o json
|
|
|
|
rune -0 jq -e '(.installed,.tainted|false)==(true,false)' <(output)
|
|
|
|
rune -0 cscli collections inspect "$subcoll" -o json
|
|
|
|
rune -0 jq -e '(.installed,.tainted|false)==(true,false)' <(output)
|
|
|
|
rune -0 cscli scenarios inspect "$scenario" -o json
|
|
|
|
rune -0 jq -e '(.installed,.tainted|false)==(true,false)' <(output)
|
|
|
|
|
|
|
|
# we taint the scenario
|
|
|
|
HUB_DIR=$(config_get '.config_paths.hub_dir')
|
|
|
|
yq e '.description="I am tainted"' -i "$HUB_DIR/scenarios/$scenario.yaml"
|
|
|
|
|
|
|
|
# the collection, subcollection and scenario are now tainted
|
|
|
|
rune -0 cscli scenarios inspect "$scenario" -o json
|
|
|
|
rune -0 jq -e '(.installed,.tainted)==(true,true)' <(output)
|
|
|
|
rune -0 cscli collections inspect "$subcoll" -o json
|
|
|
|
rune -0 jq -e '(.installed,.tainted)==(true,true)' <(output)
|
|
|
|
rune -0 cscli collections inspect "$coll" -o json
|
|
|
|
rune -0 jq -e '(.installed,.tainted)==(true,true)' <(output)
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
2022-08-05 08:54:49 +00:00
|
|
|
|
|
|
|
# TODO test download-only
|