From 3beb84bcfe05885fdd9a00f3124b4a949e45ce82 Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Mon, 14 Nov 2022 09:55:53 +0100 Subject: [PATCH] print missing "AS" values as empty strings instead of "0 " (#1867) --- cmd/crowdsec-cli/alerts.go | 2 +- cmd/crowdsec-cli/alerts_table.go | 2 +- cmd/crowdsec-cli/decisions.go | 2 +- cmd/crowdsec-cli/decisions_table.go | 2 +- pkg/models/helpers.go | 11 +++++++++++ tests/bats/80_alerts.bats | 4 ++-- 6 files changed, 17 insertions(+), 6 deletions(-) diff --git a/cmd/crowdsec-cli/alerts.go b/cmd/crowdsec-cli/alerts.go index c7061570f..2ca5b07bf 100644 --- a/cmd/crowdsec-cli/alerts.go +++ b/cmd/crowdsec-cli/alerts.go @@ -65,7 +65,7 @@ func AlertsToTable(alerts *models.GetAlertsResponse, printMachine bool) error { *alertItem.Source.Value, *alertItem.Scenario, alertItem.Source.Cn, - alertItem.Source.AsNumber + " " + alertItem.Source.AsName, + alertItem.Source.GetAsNumberName(), DecisionsFromAlert(alertItem), *alertItem.StartAt, } diff --git a/cmd/crowdsec-cli/alerts_table.go b/cmd/crowdsec-cli/alerts_table.go index 2c9d12d3d..6bca95bed 100644 --- a/cmd/crowdsec-cli/alerts_table.go +++ b/cmd/crowdsec-cli/alerts_table.go @@ -32,7 +32,7 @@ func alertsTable(out io.Writer, alerts *models.GetAlertsResponse, printMachine b displayVal, *alertItem.Scenario, alertItem.Source.Cn, - alertItem.Source.AsNumber + " " + alertItem.Source.AsName, + alertItem.Source.GetAsNumberName(), DecisionsFromAlert(alertItem), *alertItem.StartAt, } diff --git a/cmd/crowdsec-cli/decisions.go b/cmd/crowdsec-cli/decisions.go index 22563f714..fab83a7e4 100644 --- a/cmd/crowdsec-cli/decisions.go +++ b/cmd/crowdsec-cli/decisions.go @@ -72,7 +72,7 @@ func DecisionsToTable(alerts *models.GetAlertsResponse, printMachine bool) error *decisionItem.Scenario, *decisionItem.Type, alertItem.Source.Cn, - alertItem.Source.AsNumber + " " + alertItem.Source.AsName, + alertItem.Source.GetAsNumberName(), fmt.Sprintf("%d", *alertItem.EventsCount), *decisionItem.Duration, fmt.Sprintf("%t", *decisionItem.Simulated), diff --git a/cmd/crowdsec-cli/decisions_table.go b/cmd/crowdsec-cli/decisions_table.go index 00087e63b..d8d5e0325 100644 --- a/cmd/crowdsec-cli/decisions_table.go +++ b/cmd/crowdsec-cli/decisions_table.go @@ -29,7 +29,7 @@ func decisionsTable(out io.Writer, alerts *models.GetAlertsResponse, printMachin *decisionItem.Scenario, *decisionItem.Type, alertItem.Source.Cn, - alertItem.Source.AsNumber + " " + alertItem.Source.AsName, + alertItem.Source.GetAsNumberName(), strconv.Itoa(int(*alertItem.EventsCount)), *decisionItem.Duration, strconv.Itoa(int(alertItem.ID)), diff --git a/pkg/models/helpers.go b/pkg/models/helpers.go index ffca2c9dd..8c082550d 100644 --- a/pkg/models/helpers.go +++ b/pkg/models/helpers.go @@ -63,3 +63,14 @@ func (s Source) GetScope() string { } return *s.Scope } + +func (s Source) GetAsNumberName() string { + ret := "" + if s.AsNumber != "0" { + ret += s.AsNumber + } + if s.AsName != "" { + ret += " " + s.AsName + } + return ret +} diff --git a/tests/bats/80_alerts.bats b/tests/bats/80_alerts.bats index 94f761f8b..520c65874 100644 --- a/tests/bats/80_alerts.bats +++ b/tests/bats/80_alerts.bats @@ -58,11 +58,11 @@ teardown() { run -0 cscli alerts list -o raw assert_line "id,scope,value,reason,country,as,decisions,created_at" - assert_line --regexp ".*,Ip,10.20.30.40,manual 'ban' from 'githubciXXXXXXXXXXXXXXXXXXXXXXXX([a-zA-Z0-9]{16})?',,\" \",ban:1,.*" + assert_line --regexp ".*,Ip,10.20.30.40,manual 'ban' from 'githubciXXXXXXXXXXXXXXXXXXXXXXXX([a-zA-Z0-9]{16})?',,,ban:1,.*" run -0 cscli alerts list -o raw --machine assert_line "id,scope,value,reason,country,as,decisions,created_at,machine" - assert_line --regexp "^[0-9]+,Ip,10.20.30.40,manual 'ban' from 'githubciXXXXXXXXXXXXXXXXXXXXXXXX([a-zA-Z0-9]{16})?',,\" \",ban:1,.*,githubciXXXXXXXXXXXXXXXXXXXXXXXX([a-zA-Z0-9]{16})?$" + assert_line --regexp "^[0-9]+,Ip,10.20.30.40,manual 'ban' from 'githubciXXXXXXXXXXXXXXXXXXXXXXXX([a-zA-Z0-9]{16})?',,,ban:1,.*,githubciXXXXXXXXXXXXXXXXXXXXXXXX([a-zA-Z0-9]{16})?$" } @test "cscli alerts inspect" {