From 644c767019c55244067039d1aadb8a8d6c781a33 Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Thu, 3 Aug 2023 12:51:50 +0200 Subject: [PATCH] cscli decisions list -o json => [] instead of null; same for alerts (#2397) --- cmd/crowdsec-cli/alerts.go | 6 ++++++ cmd/crowdsec-cli/decisions.go | 6 ++++++ test/bats/04_capi.bats | 2 +- test/bats/40_cold-logs.bats | 4 ++-- test/bats/50_simulation.bats | 4 ++-- test/bats/80_alerts.bats | 4 ++-- test/bats/97_ipv4_single.bats | 6 +++--- test/bats/97_ipv6_single.bats | 14 +++++++------- test/bats/98_ipv4_range.bats | 10 +++++----- test/bats/98_ipv6_range.bats | 18 +++++++++--------- 10 files changed, 43 insertions(+), 31 deletions(-) diff --git a/cmd/crowdsec-cli/alerts.go b/cmd/crowdsec-cli/alerts.go index c37d09b1e..a97322b56 100644 --- a/cmd/crowdsec-cli/alerts.go +++ b/cmd/crowdsec-cli/alerts.go @@ -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" { diff --git a/cmd/crowdsec-cli/decisions.go b/cmd/crowdsec-cli/decisions.go index 49d6ddd60..985b4d2d8 100644 --- a/cmd/crowdsec-cli/decisions.go +++ b/cmd/crowdsec-cli/decisions.go @@ -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" { diff --git a/test/bats/04_capi.bats b/test/bats/04_capi.bats index 2cfabc7b7..04ddf49ae 100644 --- a/test/bats/04_capi.bats +++ b/test/bats/04_capi.bats @@ -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 diff --git a/test/bats/40_cold-logs.bats b/test/bats/40_cold-logs.bats index ad4d5233c..21c0615c7 100644 --- a/test/bats/40_cold-logs.bats +++ b/test/bats/40_cold-logs.bats @@ -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 '[]' } diff --git a/test/bats/50_simulation.bats b/test/bats/50_simulation.bats index 578dcf81a..0add1e816 100644 --- a/test/bats/50_simulation.bats +++ b/test/bats/50_simulation.bats @@ -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 '[]' } diff --git a/test/bats/80_alerts.bats b/test/bats/80_alerts.bats index 0f70223b6..e0fdcb022 100644 --- a/test/bats/80_alerts.bats +++ b/test/bats/80_alerts.bats @@ -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)" { diff --git a/test/bats/97_ipv4_single.bats b/test/bats/97_ipv4_single.bats index c42836071..1ada1c464 100644 --- a/test/bats/97_ipv4_single.bats +++ b/test/bats/97_ipv4_single.bats @@ -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" { diff --git a/test/bats/97_ipv6_single.bats b/test/bats/97_ipv6_single.bats index 41948fb25..ffbfc125b 100644 --- a/test/bats/97_ipv6_single.bats +++ b/test/bats/97_ipv6_single.bats @@ -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 '[]' } diff --git a/test/bats/98_ipv4_range.bats b/test/bats/98_ipv4_range.bats index 1983225b9..b0f6f4829 100644 --- a/test/bats/98_ipv4_range.bats +++ b/test/bats/98_ipv4_range.bats @@ -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" { diff --git a/test/bats/98_ipv6_range.bats b/test/bats/98_ipv6_range.bats index b85f0dfcd..d3c347583 100644 --- a/test/bats/98_ipv6_range.bats +++ b/test/bats/98_ipv6_range.bats @@ -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" {