2021-08-25 09:43:29 +00:00
|
|
|
package csprofiles
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
2023-03-28 08:49:01 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
|
2021-08-25 09:43:29 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
2022-06-22 09:29:52 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2021-08-25 09:43:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
scope = "Country"
|
|
|
|
typ = "ban"
|
2022-06-22 09:29:52 +00:00
|
|
|
boolFalse = false
|
|
|
|
boolTrue = true
|
2021-08-25 09:43:29 +00:00
|
|
|
duration = "1h"
|
|
|
|
|
|
|
|
value = "CH"
|
|
|
|
scenario = "ssh-bf"
|
|
|
|
)
|
|
|
|
|
2022-06-22 09:29:52 +00:00
|
|
|
func TestNewProfile(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
profileCfg *csconfig.ProfileCfg
|
|
|
|
expectedNbProfile int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "filter ok and duration_expr ok",
|
|
|
|
profileCfg: &csconfig.ProfileCfg{
|
|
|
|
Filters: []string{
|
|
|
|
"1==1",
|
|
|
|
},
|
|
|
|
DurationExpr: "1==1",
|
|
|
|
Debug: &boolFalse,
|
|
|
|
Decisions: []models.Decision{
|
|
|
|
{Type: &typ, Scope: &scope, Simulated: &boolTrue, Duration: &duration},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedNbProfile: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "filter NOK and duration_expr ok",
|
|
|
|
profileCfg: &csconfig.ProfileCfg{
|
|
|
|
Filters: []string{
|
|
|
|
"1==1",
|
|
|
|
"unknownExprHelper() == 'foo'",
|
|
|
|
},
|
|
|
|
DurationExpr: "1==1",
|
|
|
|
Debug: &boolFalse,
|
|
|
|
Decisions: []models.Decision{
|
|
|
|
{Type: &typ, Scope: &scope, Simulated: &boolFalse, Duration: &duration},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedNbProfile: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "filter ok and duration_expr NOK",
|
|
|
|
profileCfg: &csconfig.ProfileCfg{
|
|
|
|
Filters: []string{
|
|
|
|
"1==1",
|
|
|
|
},
|
|
|
|
DurationExpr: "unknownExprHelper() == 'foo'",
|
|
|
|
Debug: &boolFalse,
|
|
|
|
Decisions: []models.Decision{
|
|
|
|
{Type: &typ, Scope: &scope, Simulated: &boolFalse, Duration: &duration},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedNbProfile: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "filter ok and duration_expr ok + DEBUG",
|
|
|
|
profileCfg: &csconfig.ProfileCfg{
|
|
|
|
Filters: []string{
|
|
|
|
"1==1",
|
|
|
|
},
|
|
|
|
DurationExpr: "1==1",
|
|
|
|
Debug: &boolTrue,
|
|
|
|
Decisions: []models.Decision{
|
|
|
|
{Type: &typ, Scope: &scope, Simulated: &boolFalse, Duration: &duration},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedNbProfile: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2022-10-10 08:48:26 +00:00
|
|
|
test := test
|
2022-06-22 09:29:52 +00:00
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
profilesCfg := []*csconfig.ProfileCfg{
|
|
|
|
test.profileCfg,
|
|
|
|
}
|
|
|
|
profile, _ := NewProfile(profilesCfg)
|
|
|
|
fmt.Printf("expected : %+v | result : %+v", test.expectedNbProfile, len(profile))
|
|
|
|
assert.Equal(t, test.expectedNbProfile, len(profile))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 09:43:29 +00:00
|
|
|
func TestEvaluateProfile(t *testing.T) {
|
|
|
|
type args struct {
|
2022-06-22 09:29:52 +00:00
|
|
|
profileCfg *csconfig.ProfileCfg
|
|
|
|
Alert *models.Alert
|
2021-08-25 09:43:29 +00:00
|
|
|
}
|
2023-03-28 08:49:01 +00:00
|
|
|
|
|
|
|
exprhelpers.Init(nil)
|
|
|
|
|
2021-08-25 09:43:29 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
expectedDecisionCount int // count of expected decisions
|
2022-06-22 09:29:52 +00:00
|
|
|
expectedDuration string
|
2021-08-25 09:43:29 +00:00
|
|
|
expectedMatchStatus bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple pass single expr",
|
|
|
|
args: args{
|
2022-06-22 09:29:52 +00:00
|
|
|
profileCfg: &csconfig.ProfileCfg{
|
|
|
|
Filters: []string{fmt.Sprintf("Alert.GetScenario() == \"%s\"", scenario)},
|
|
|
|
Debug: &boolFalse,
|
2021-08-25 09:43:29 +00:00
|
|
|
},
|
|
|
|
Alert: &models.Alert{Remediation: true, Scenario: &scenario},
|
|
|
|
},
|
|
|
|
expectedDecisionCount: 0,
|
|
|
|
expectedMatchStatus: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple fail single expr",
|
|
|
|
args: args{
|
2022-06-22 09:29:52 +00:00
|
|
|
profileCfg: &csconfig.ProfileCfg{
|
|
|
|
Filters: []string{"Alert.GetScenario() == \"Foo\""},
|
2021-08-25 09:43:29 +00:00
|
|
|
},
|
|
|
|
Alert: &models.Alert{Remediation: true},
|
|
|
|
},
|
|
|
|
expectedDecisionCount: 0,
|
|
|
|
expectedMatchStatus: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "1 expr fail 1 expr pass should still eval to match",
|
|
|
|
args: args{
|
2022-06-22 09:29:52 +00:00
|
|
|
profileCfg: &csconfig.ProfileCfg{
|
|
|
|
Filters: []string{"1==1", "1!=1"},
|
2021-08-25 09:43:29 +00:00
|
|
|
},
|
|
|
|
Alert: &models.Alert{Remediation: true},
|
|
|
|
},
|
|
|
|
expectedDecisionCount: 0,
|
|
|
|
expectedMatchStatus: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple filter with 2 decision",
|
|
|
|
args: args{
|
2022-06-22 09:29:52 +00:00
|
|
|
profileCfg: &csconfig.ProfileCfg{
|
|
|
|
Filters: []string{"1==1"},
|
2021-08-25 09:43:29 +00:00
|
|
|
Decisions: []models.Decision{
|
2022-06-22 09:29:52 +00:00
|
|
|
{Type: &typ, Scope: &scope, Simulated: &boolTrue, Duration: &duration},
|
|
|
|
{Type: &typ, Scope: &scope, Simulated: &boolFalse, Duration: &duration},
|
2021-08-25 09:43:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Alert: &models.Alert{Remediation: true, Scenario: &scenario, Source: &models.Source{Value: &value}},
|
|
|
|
},
|
|
|
|
expectedDecisionCount: 2,
|
|
|
|
expectedMatchStatus: true,
|
|
|
|
},
|
2022-06-22 09:29:52 +00:00
|
|
|
{
|
|
|
|
name: "simple filter with decision_expr",
|
|
|
|
args: args{
|
|
|
|
profileCfg: &csconfig.ProfileCfg{
|
|
|
|
Filters: []string{"1==1"},
|
|
|
|
Decisions: []models.Decision{
|
|
|
|
{Type: &typ, Scope: &scope, Simulated: &boolFalse},
|
|
|
|
},
|
|
|
|
DurationExpr: "Sprintf('%dh', 4*4)",
|
|
|
|
},
|
|
|
|
Alert: &models.Alert{Remediation: true, Scenario: &scenario, Source: &models.Source{Value: &value}},
|
|
|
|
},
|
|
|
|
expectedDecisionCount: 1,
|
|
|
|
expectedDuration: "16h",
|
|
|
|
expectedMatchStatus: true,
|
|
|
|
},
|
2021-08-25 09:43:29 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2022-10-10 08:48:26 +00:00
|
|
|
tt := tt
|
2021-08-25 09:43:29 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-06-22 09:29:52 +00:00
|
|
|
profilesCfg := []*csconfig.ProfileCfg{
|
|
|
|
tt.args.profileCfg,
|
2021-08-25 09:43:29 +00:00
|
|
|
}
|
2022-06-22 09:29:52 +00:00
|
|
|
profile, err := NewProfile(profilesCfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get newProfile : %+v", err)
|
|
|
|
}
|
|
|
|
got, got1, _ := profile[0].EvaluateProfile(tt.args.Alert)
|
2021-08-25 09:43:29 +00:00
|
|
|
if !reflect.DeepEqual(len(got), tt.expectedDecisionCount) {
|
|
|
|
t.Errorf("EvaluateProfile() got = %+v, want %+v", got, tt.expectedDecisionCount)
|
|
|
|
}
|
|
|
|
if got1 != tt.expectedMatchStatus {
|
|
|
|
t.Errorf("EvaluateProfile() got1 = %v, want %v", got1, tt.expectedMatchStatus)
|
|
|
|
}
|
2022-06-22 09:29:52 +00:00
|
|
|
if tt.expectedDuration != "" {
|
|
|
|
assert.Equal(t, tt.expectedDuration, *got[0].Duration, "The two durations should be the same")
|
|
|
|
}
|
2021-08-25 09:43:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|