cscli decisions list -o json => [] instead of null; same for alerts (#2397)

This commit is contained in:
mmetc 2023-08-03 12:51:50 +02:00 committed by GitHub
parent 6ba682a32f
commit 644c767019
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 43 additions and 31 deletions

View file

@ -126,6 +126,12 @@ func AlertsToTable(alerts *models.GetAlertsResponse, printMachine bool) error {
}
csvwriter.Flush()
} else if csConfig.Cscli.Output == "json" {
if *alerts == nil {
// avoid returning "null" in json
// could be cleaner if we used slice of alerts directly
fmt.Println("[]")
return nil
}
x, _ := json.MarshalIndent(alerts, "", " ")
fmt.Printf("%s", string(x))
} else if csConfig.Cscli.Output == "human" {

View file

@ -81,6 +81,12 @@ func DecisionsToTable(alerts *models.GetAlertsResponse, printMachine bool) error
}
csvwriter.Flush()
} else if csConfig.Cscli.Output == "json" {
if *alerts == nil {
// avoid returning "null" in `json"
// could be cleaner if we used slice of alerts directly
fmt.Println("[]")
return nil
}
x, _ := json.MarshalIndent(alerts, "", " ")
fmt.Printf("%s", string(x))
} else if csConfig.Cscli.Output == "human" {

View file

@ -34,7 +34,7 @@ setup() {
./instance-crowdsec start
for ((i=0; i<15; i++)); do
sleep 2
[[ $(cscli alerts list -a -o json 2>/dev/null || cscli alerts list -o json) != "null" ]] && break
[[ $(cscli alerts list -a -o json) != "[]" ]] && break
done
rune -0 cscli alerts list -a -o json

View file

@ -66,7 +66,7 @@ setup() {
@test "1.1.1.172 has not been banned (range/NOT-contained: -r 1.1.2.0/24)" {
rune -0 cscli decisions list -r 1.1.2.0/24 -o json
assert_output 'null'
assert_json '[]'
}
@test "1.1.1.172 has been banned (exact: -i 1.1.1.172)" {
@ -77,5 +77,5 @@ setup() {
@test "1.1.1.173 has not been banned (exact: -i 1.1.1.173)" {
rune -0 cscli decisions list -i 1.1.1.173 -o json
assert_output 'null'
assert_json '[]'
}

View file

@ -54,7 +54,7 @@ setup() {
rune -0 cscli simulation enable crowdsecurity/ssh-bf
fake_log | "${CROWDSEC}" -dsn file:///dev/fd/0 -type syslog -no-api
rune -0 cscli decisions list --no-simu -o json
assert_output 'null'
assert_json '[]'
}
@test "global simulation, listing non-simulated: expect no decision" {
@ -62,5 +62,5 @@ setup() {
rune -0 cscli simulation enable --global
fake_log | "${CROWDSEC}" -dsn file:///dev/fd/0 -type syslog -no-api
rune -0 cscli decisions list --no-simu -o json
assert_output 'null'
assert_json '[]'
}

View file

@ -118,7 +118,7 @@ teardown() {
rune -0 cscli alerts list --until 200d -o human
assert_output "No active alerts"
rune -0 cscli alerts list --until 200d -o json
assert_output "null"
assert_json "[]"
rune -0 cscli alerts list --until 200d -o raw
assert_output "id,scope,value,reason,country,as,decisions,created_at"
rune -0 cscli alerts list --until 200d -o raw --machine
@ -172,7 +172,7 @@ teardown() {
rune -0 cscli alerts delete -i 1.2.3.4
assert_stderr --partial 'alert(s) deleted'
rune -0 cscli decisions list -o json
assert_output null
assert_json '[]'
}
@test "cscli alerts delete (must ignore the query limit)" {

View file

@ -33,7 +33,7 @@ api() {
# delete community pull
rune -0 cscli decisions delete --all
rune -0 cscli decisions list -o json
assert_output 'null'
assert_json '[]'
}
@test "API - first decisions list: must be empty" {
@ -74,7 +74,7 @@ api() {
@test "CLI - decision for 1.2.3.5" {
rune -0 cscli decisions list -i '1.2.3.5' -o json
assert_output 'null'
assert_json '[]'
}
@test "API - decision for 1.2.3.5" {
@ -86,7 +86,7 @@ api() {
@test "CLI - decision for 1.2.3.0/24" {
rune -0 cscli decisions list -r '1.2.3.0/24' -o json
assert_output 'null'
assert_json '[]'
}
@test "API - decision for 1.2.3.0/24" {

View file

@ -33,7 +33,7 @@ api() {
# delete community pull
rune -0 cscli decisions delete --all
rune -0 cscli decisions list -o json
assert_output 'null'
assert_json '[]'
}
@test "adding decision for ip 1111:2222:3333:4444:5555:6666:7777:8888" {
@ -67,7 +67,7 @@ api() {
@test "CLI - decisions for ip 1211:2222:3333:4444:5555:6666:7777:8888" {
rune -0 cscli decisions list -i '1211:2222:3333:4444:5555:6666:7777:8888' -o json
assert_output 'null'
assert_json '[]'
}
@test "API - decisions for ip 1211:2222:3333:4444:5555:6666:7777:888" {
@ -77,7 +77,7 @@ api() {
@test "CLI - decisions for ip 1111:2222:3333:4444:5555:6666:7777:8887" {
rune -0 cscli decisions list -i '1111:2222:3333:4444:5555:6666:7777:8887' -o json
assert_output 'null'
assert_json '[]'
}
@test "API - decisions for ip 1111:2222:3333:4444:5555:6666:7777:8887" {
@ -87,7 +87,7 @@ api() {
@test "CLI - decisions for range 1111:2222:3333:4444:5555:6666:7777:8888/48" {
rune -0 cscli decisions list -r '1111:2222:3333:4444:5555:6666:7777:8888/48' -o json
assert_output 'null'
assert_json '[]'
}
@test "API - decisions for range 1111:2222:3333:4444:5555:6666:7777:8888/48" {
@ -109,7 +109,7 @@ api() {
@test "CLI - decisions for range 1111:2222:3333:4444:5555:6666:7777:8888/64" {
rune -0 cscli decisions list -r '1111:2222:3333:4444:5555:6666:7777:8888/64' -o json
assert_output 'null'
assert_json '[]'
}
@test "API - decisions for range 1111:2222:3333:4444:5555:6666:7777:8888/64" {
@ -141,7 +141,7 @@ api() {
@test "CLI - decisions for ip 1111:2222:3333:4444:5555:6666:7777:8889 after delete" {
rune -0 cscli decisions list -i '1111:2222:3333:4444:5555:6666:7777:8889' -o json
assert_output 'null'
assert_json '[]'
}
@test "deleting decision for range 1111:2222:3333:4444:5555:6666:7777:8888/64" {
@ -151,5 +151,5 @@ api() {
@test "CLI - decisions for ip/range in 1111:2222:3333:4444:5555:6666:7777:8888/64 after delete" {
rune -0 cscli decisions list -r '1111:2222:3333:4444:5555:6666:7777:8888/64' -o json --contained
assert_output 'null'
assert_json '[]'
}

View file

@ -33,7 +33,7 @@ api() {
# delete community pull
rune -0 cscli decisions delete --all
rune -0 cscli decisions list -o json
assert_output 'null'
assert_json '[]'
}
@test "adding decision for range 4.4.4.0/24" {
@ -69,7 +69,7 @@ api() {
@test "CLI - decisions for ip contained in 4.4.4." {
rune -0 cscli decisions list -i '4.4.4.4' -o json --contained
assert_output 'null'
assert_json '[]'
}
@test "API - decisions for ip contained in 4.4.4." {
@ -79,7 +79,7 @@ api() {
@test "CLI - decisions for ip 5.4.4." {
rune -0 cscli decisions list -i '5.4.4.3' -o json
assert_output 'null'
assert_json '[]'
}
@test "API - decisions for ip 5.4.4." {
@ -89,7 +89,7 @@ api() {
@test "CLI - decisions for range 4.4.0.0/1" {
rune -0 cscli decisions list -r '4.4.0.0/16' -o json
assert_output 'null'
assert_json '[]'
}
@test "API - decisions for range 4.4.0.0/1" {
@ -125,7 +125,7 @@ api() {
@test "CLI - decisions for range 4.4.3.2/2" {
rune -0 cscli decisions list -r '4.4.3.2/28' -o json
assert_output 'null'
assert_json '[]'
}
@test "API - decisions for range 4.4.3.2/2" {

View file

@ -33,7 +33,7 @@ api() {
# delete community pull
rune -0 cscli decisions delete --all
rune -0 cscli decisions list -o json
assert_output 'null'
assert_json '[]'
}
@test "adding decision for range aaaa:2222:3333:4444::/64" {
@ -69,7 +69,7 @@ api() {
@test "CLI - decisions for ip aaaa:2222:3333:4445:5555:6666:7777:8888" {
rune -0 cscli decisions list -i 'aaaa:2222:3333:4445:5555:6666:7777:8888' -o json
assert_output 'null'
assert_json '[]'
}
@test "API - decisions for ip aaaa:2222:3333:4445:5555:6666:7777:8888" {
@ -79,7 +79,7 @@ api() {
@test "CLI - decisions for ip aaa1:2222:3333:4444:5555:6666:7777:8887" {
rune -0 cscli decisions list -i 'aaa1:2222:3333:4444:5555:6666:7777:8887' -o json
assert_output 'null'
assert_json '[]'
}
@test "API - decisions for ip aaa1:2222:3333:4444:5555:6666:7777:8887" {
@ -103,7 +103,7 @@ api() {
@test "CLI - decisions for range aaaa:2222:3333:4441:5555::/80" {
rune -0 cscli decisions list -r 'aaaa:2222:3333:4441:5555::/80' -o json
assert_output 'null'
assert_json '[]'
}
@ -114,7 +114,7 @@ api() {
@test "CLI - decisions for range aaa1:2222:3333:4444:5555::/80" {
rune -0 cscli decisions list -r 'aaa1:2222:3333:4444:5555::/80' -o json
assert_output 'null'
assert_json '[]'
}
@test "API - decisions for range aaa1:2222:3333:4444:5555::/80" {
@ -126,7 +126,7 @@ api() {
@test "CLI - decisions for range aaaa:2222:3333:4444:5555:6666:7777:8888/48" {
rune -0 cscli decisions list -r 'aaaa:2222:3333:4444:5555:6666:7777:8888/48' -o json
assert_output 'null'
assert_json '[]'
}
@test "API - decisions for range aaaa:2222:3333:4444:5555:6666:7777:8888/48" {
@ -148,7 +148,7 @@ api() {
@test "CLI - decisions for ip/range in aaaa:2222:3333:4445:5555:6666:7777:8888/48" {
rune -0 cscli decisions list -r 'aaaa:2222:3333:4445:5555:6666:7777:8888/48' -o json
assert_output 'null'
assert_json '[]'
}
@test "API - decisions for ip/range in aaaa:2222:3333:4445:5555:6666:7777:8888/48" {
@ -177,7 +177,7 @@ api() {
@test "CLI - decisions for ip bbbb:db8:0000:0000:0000:8fff:ffff:ffff" {
rune -0 cscli decisions list -o json -i 'bbbb:db8:0000:0000:0000:8fff:ffff:ffff'
assert_output 'null'
assert_json '[]'
}
@test "API - decisions for ip in bbbb:db8:0000:0000:0000:8fff:ffff:ffff" {
@ -192,7 +192,7 @@ api() {
@test "CLI - decisions for range aaaa:2222:3333:4444::/64 after delete" {
rune -0 cscli decisions list -o json -r 'aaaa:2222:3333:4444::/64'
assert_output 'null'
assert_json '[]'
}
@test "adding decision for ip bbbb:db8:0000:0000:0000:8fff:ffff:ffff" {