File: 70_http_plugin.bats

package info (click to toggle)
crowdsec 1.4.6-10.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,500 kB
  • sloc: sh: 2,870; makefile: 386; python: 74
file content (86 lines) | stat: -rw-r--r-- 2,201 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/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"
    # eval "$(debug)"
    ./instance-data load

    MOCK_OUT="${LOG_DIR}/mock-http.out"
    export MOCK_OUT
    MOCK_PORT="9999"
    MOCK_URL="http://localhost:${MOCK_PORT}"
    export MOCK_URL
    PLUGIN_DIR=$(config_get '.config_paths.plugin_dir')
    # could have a trailing slash
    PLUGIN_DIR=$(realpath "${PLUGIN_DIR}")
    export PLUGIN_DIR

    # https://mikefarah.gitbook.io/yq/operators/env-variable-operators
    config_set "$(config_get '.config_paths.notification_dir')/http.yaml" '
        .url=strenv(MOCK_URL) |
        .group_wait="5s" |
        .group_threshold=2
    '

    config_set "$(config_get '.api.server.profiles_path')" '
        .notifications=["http_default"] |
        .filters=["Alert.GetScope() == \"Ip\""]
    '

    config_set '
        .plugin_config.user="" |
        .plugin_config.group=""
    '

    rm -f -- "${MOCK_OUT}"

    ./instance-crowdsec start
    ./instance-mock-http start "${MOCK_PORT}"
}

teardown_file() {
    load "../lib/teardown_file.sh"
    ./instance-crowdsec stop
    ./instance-mock-http stop
}

setup() {
    load "../lib/setup.sh"
}

#----------

@test "add two bans" {
    run -0 cscli decisions add --ip 1.2.3.4 --duration 30s
    assert_output --partial 'Decision successfully added'

    run -0 cscli decisions add --ip 1.2.3.5 --duration 30s
    assert_output --partial 'Decision successfully added'
    sleep 5
}

@test "expected 1 log line from http server" {
    run -0 wc -l <"${MOCK_OUT}"
    # wc can pad with spaces on some platforms
    run -0 tr -d ' ' < <(output)
    assert_output 1
}

@test "expected to receive 2 alerts in the request body from plugin" {
    run -0 jq -r '.request_body' <"${MOCK_OUT}"
    run -0 jq -r 'length' <(output)
    assert_output 2
}

@test "expected to receive IP 1.2.3.4 as value of first decision" {
    run -0 jq -r '.request_body[0].decisions[0].value' <"${MOCK_OUT}"
    assert_output 1.2.3.4
}

@test "expected to receive IP 1.2.3.5 as value of second decision" {
    run -0 jq -r '.request_body[1].decisions[0].value' <"${MOCK_OUT}"
    assert_output 1.2.3.5
}