mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-11 20:36:12 +02:00
refactor pkg/database, pkg/models (#3022)
* pkg/models: Source.String() * pkg/models: Alert.FormatAsStrings() * cscli alerts list: sort remediation keys avoid printing "ban: ... captcha: ..." in one line, and "captcha: ... ban: ..." in another * remove unused methods; drop else branch * lint
This commit is contained in:
parent
73e03ef556
commit
9e859c0c8c
3 changed files with 109 additions and 110 deletions
|
@ -1,27 +1,33 @@
|
|||
package models
|
||||
|
||||
func (a *Alert) HasRemediation() bool {
|
||||
return true
|
||||
}
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
// these are duplicated from pkg/types
|
||||
// TODO XXX: de-duplicate
|
||||
Ip = "Ip"
|
||||
Range = "Range"
|
||||
CscliImportOrigin = "cscli-import"
|
||||
)
|
||||
|
||||
func (a *Alert) GetScope() string {
|
||||
if a.Source.Scope == nil {
|
||||
return ""
|
||||
}
|
||||
return *a.Source.Scope
|
||||
return a.Source.GetScope()
|
||||
}
|
||||
|
||||
func (a *Alert) GetValue() string {
|
||||
if a.Source.Value == nil {
|
||||
return ""
|
||||
}
|
||||
return *a.Source.Value
|
||||
return a.Source.GetValue()
|
||||
}
|
||||
|
||||
func (a *Alert) GetScenario() string {
|
||||
if a.Scenario == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return *a.Scenario
|
||||
}
|
||||
|
||||
|
@ -29,6 +35,7 @@ func (a *Alert) GetEventsCount() int32 {
|
|||
if a.EventsCount == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return *a.EventsCount
|
||||
}
|
||||
|
||||
|
@ -38,6 +45,7 @@ func (e *Event) GetMeta(key string) string {
|
|||
return meta.Value
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
|
@ -47,6 +55,7 @@ func (a *Alert) GetMeta(key string) string {
|
|||
return meta.Value
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
|
@ -54,6 +63,7 @@ func (s Source) GetValue() string {
|
|||
if s.Value == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return *s.Value
|
||||
}
|
||||
|
||||
|
@ -61,6 +71,7 @@ func (s Source) GetScope() string {
|
|||
if s.Scope == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return *s.Scope
|
||||
}
|
||||
|
||||
|
@ -69,8 +80,88 @@ func (s Source) GetAsNumberName() string {
|
|||
if s.AsNumber != "0" {
|
||||
ret += s.AsNumber
|
||||
}
|
||||
|
||||
if s.AsName != "" {
|
||||
ret += " " + s.AsName
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func (s *Source) String() string {
|
||||
if s == nil || s.Scope == nil || *s.Scope == "" {
|
||||
return "empty source"
|
||||
}
|
||||
|
||||
cn := s.Cn
|
||||
|
||||
if s.AsNumber != "" {
|
||||
cn += "/" + s.AsNumber
|
||||
}
|
||||
|
||||
if cn != "" {
|
||||
cn = " (" + cn + ")"
|
||||
}
|
||||
|
||||
switch *s.Scope {
|
||||
case Ip:
|
||||
return "ip " + *s.Value + cn
|
||||
case Range:
|
||||
return "range " + *s.Value + cn
|
||||
default:
|
||||
return *s.Scope + " " + *s.Value
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Alert) FormatAsStrings(machineID string, logger *log.Logger) []string {
|
||||
src := a.Source.String()
|
||||
|
||||
msg := "empty scenario"
|
||||
if a.Scenario != nil && *a.Scenario != "" {
|
||||
msg = *a.Scenario
|
||||
} else if a.Message != nil && *a.Message != "" {
|
||||
msg = *a.Message
|
||||
}
|
||||
|
||||
reason := fmt.Sprintf("%s by %s", msg, src)
|
||||
|
||||
if len(a.Decisions) == 0 {
|
||||
return []string{fmt.Sprintf("(%s) alert : %s", machineID, reason)}
|
||||
}
|
||||
|
||||
var retStr []string
|
||||
|
||||
if a.Decisions[0].Origin != nil && *a.Decisions[0].Origin == CscliImportOrigin {
|
||||
return []string{fmt.Sprintf("(%s) alert : %s", machineID, reason)}
|
||||
}
|
||||
|
||||
for i, decisionItem := range a.Decisions {
|
||||
decision := ""
|
||||
if a.Simulated != nil && *a.Simulated {
|
||||
decision = "(simulated alert)"
|
||||
} else if decisionItem.Simulated != nil && *decisionItem.Simulated {
|
||||
decision = "(simulated decision)"
|
||||
}
|
||||
|
||||
if logger.GetLevel() >= log.DebugLevel {
|
||||
/*spew is expensive*/
|
||||
logger.Debug(spew.Sdump(decisionItem))
|
||||
}
|
||||
|
||||
if len(a.Decisions) > 1 {
|
||||
reason = fmt.Sprintf("%s for %d/%d decisions", msg, i+1, len(a.Decisions))
|
||||
}
|
||||
|
||||
origin := *decisionItem.Origin
|
||||
if machineID != "" {
|
||||
origin = machineID + "/" + origin
|
||||
}
|
||||
|
||||
decision += fmt.Sprintf("%s %s on %s %s", *decisionItem.Duration,
|
||||
*decisionItem.Type, *decisionItem.Scope, *decisionItem.Value)
|
||||
retStr = append(retStr,
|
||||
fmt.Sprintf("(%s) %s : %s", origin, reason, decision))
|
||||
}
|
||||
|
||||
return retStr
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue