Skip to content

Commit

Permalink
print missing "AS" values as empty strings instead of "0 " (#1867)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc authored Nov 14, 2022
1 parent b0889d7 commit 3beb84b
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/alerts_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/decisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/decisions_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
11 changes: 11 additions & 0 deletions pkg/models/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions tests/bats/80_alerts.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit 3beb84b

Please sign in to comment.