2022-03-29 12:20:26 +00:00
|
|
|
package apiserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-03-21 10:50:10 +00:00
|
|
|
"net"
|
2023-02-13 14:06:14 +00:00
|
|
|
"net/http"
|
2022-03-29 12:20:26 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-10-28 11:55:59 +00:00
|
|
|
"github.com/jarcoal/httpmock"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"gopkg.in/tomb.v2"
|
|
|
|
|
2023-05-25 13:37:44 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
|
|
|
"github.com/crowdsecurity/go-cs-lib/pkg/ptr"
|
2023-05-23 08:52:47 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/pkg/version"
|
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/database/ent/decision"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/database/ent/machine"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
2023-02-06 13:06:14 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/modelscapi"
|
2022-03-29 12:20:26 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getDBClient(t *testing.T) *database.Client {
|
|
|
|
t.Helper()
|
|
|
|
dbPath, err := os.CreateTemp("", "*sqlite")
|
2022-09-30 14:01:42 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-29 12:20:26 +00:00
|
|
|
dbClient, err := database.NewClient(&csconfig.DatabaseCfg{
|
|
|
|
Type: "sqlite",
|
|
|
|
DbName: "crowdsec",
|
|
|
|
DbPath: dbPath.Name(),
|
|
|
|
})
|
2022-09-30 14:01:42 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-29 12:20:26 +00:00
|
|
|
return dbClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAPIC(t *testing.T) *apic {
|
|
|
|
t.Helper()
|
|
|
|
dbClient := getDBClient(t)
|
|
|
|
return &apic{
|
2023-01-31 13:47:44 +00:00
|
|
|
AlertsAddChan: make(chan []*models.Alert),
|
|
|
|
//DecisionDeleteChan: make(chan []*models.Decision),
|
2022-03-29 12:20:26 +00:00
|
|
|
dbClient: dbClient,
|
|
|
|
mu: sync.Mutex{},
|
|
|
|
startup: true,
|
|
|
|
pullTomb: tomb.Tomb{},
|
|
|
|
pushTomb: tomb.Tomb{},
|
|
|
|
metricsTomb: tomb.Tomb{},
|
|
|
|
scenarioList: make([]string, 0),
|
|
|
|
consoleConfig: &csconfig.ConsoleConfig{
|
2023-05-25 13:37:44 +00:00
|
|
|
ShareManualDecisions: ptr.Of(false),
|
|
|
|
ShareTaintedScenarios: ptr.Of(false),
|
|
|
|
ShareCustomScenarios: ptr.Of(false),
|
|
|
|
ShareContext: ptr.Of(false),
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
2023-03-21 13:06:19 +00:00
|
|
|
isPulling: make(chan bool, 1),
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func absDiff(a int, b int) (c int) {
|
|
|
|
if c = a - b; c < 0 {
|
|
|
|
return -1 * c
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertTotalDecisionCount(t *testing.T, dbClient *database.Client, count int) {
|
|
|
|
d := dbClient.Ent.Decision.Query().AllX(context.Background())
|
|
|
|
assert.Len(t, d, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertTotalValidDecisionCount(t *testing.T, dbClient *database.Client, count int) {
|
|
|
|
d := dbClient.Ent.Decision.Query().Where(
|
|
|
|
decision.UntilGT(time.Now()),
|
|
|
|
).AllX(context.Background())
|
|
|
|
assert.Len(t, d, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonMarshalX(v interface{}) []byte {
|
|
|
|
data, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertTotalAlertCount(t *testing.T, dbClient *database.Client, count int) {
|
|
|
|
d := dbClient.Ent.Alert.Query().AllX(context.Background())
|
|
|
|
assert.Len(t, d, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPICCAPIPullIsOld(t *testing.T) {
|
|
|
|
api := getAPIC(t)
|
|
|
|
|
2022-09-30 14:01:42 +00:00
|
|
|
isOld, err := api.CAPIPullIsOld()
|
|
|
|
require.NoError(t, err)
|
2022-03-29 12:20:26 +00:00
|
|
|
assert.True(t, isOld)
|
|
|
|
|
|
|
|
decision := api.dbClient.Ent.Decision.Create().
|
|
|
|
SetUntil(time.Now().Add(time.Hour)).
|
|
|
|
SetScenario("crowdsec/test").
|
|
|
|
SetType("IP").
|
|
|
|
SetScope("Country").
|
|
|
|
SetValue("Blah").
|
2023-01-31 13:47:44 +00:00
|
|
|
SetOrigin(types.CAPIOrigin).
|
2022-03-29 12:20:26 +00:00
|
|
|
SaveX(context.Background())
|
|
|
|
|
|
|
|
api.dbClient.Ent.Alert.Create().
|
|
|
|
SetCreatedAt(time.Now()).
|
|
|
|
SetScenario("crowdsec/test").
|
|
|
|
AddDecisions(
|
|
|
|
decision,
|
|
|
|
).
|
|
|
|
SaveX(context.Background())
|
|
|
|
|
|
|
|
isOld, err = api.CAPIPullIsOld()
|
2022-09-30 14:01:42 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-29 12:20:26 +00:00
|
|
|
|
|
|
|
assert.False(t, isOld)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPICFetchScenariosListFromDB(t *testing.T) {
|
2022-09-30 14:01:42 +00:00
|
|
|
tests := []struct {
|
2022-03-29 12:20:26 +00:00
|
|
|
name string
|
|
|
|
machineIDsWithScenarios map[string]string
|
|
|
|
expectedScenarios []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Simple one machine with two scenarios",
|
|
|
|
machineIDsWithScenarios: map[string]string{
|
|
|
|
"a": "crowdsecurity/http-bf,crowdsecurity/ssh-bf",
|
|
|
|
},
|
|
|
|
expectedScenarios: []string{"crowdsecurity/ssh-bf", "crowdsecurity/http-bf"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Multi machine with custom+hub scenarios",
|
|
|
|
machineIDsWithScenarios: map[string]string{
|
|
|
|
"a": "crowdsecurity/http-bf,crowdsecurity/ssh-bf,my_scenario",
|
|
|
|
"b": "crowdsecurity/http-bf,crowdsecurity/ssh-bf,foo_scenario",
|
|
|
|
},
|
|
|
|
expectedScenarios: []string{"crowdsecurity/ssh-bf", "crowdsecurity/http-bf", "my_scenario", "foo_scenario"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:01:42 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
2022-03-29 12:20:26 +00:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-09-30 14:01:42 +00:00
|
|
|
api := getAPIC(t)
|
2022-03-29 12:20:26 +00:00
|
|
|
for machineID, scenarios := range tc.machineIDsWithScenarios {
|
|
|
|
api.dbClient.Ent.Machine.Create().
|
|
|
|
SetMachineId(machineID).
|
|
|
|
SetPassword(testPassword.String()).
|
|
|
|
SetIpAddress("1.2.3.4").
|
|
|
|
SetScenarios(scenarios).
|
|
|
|
ExecX(context.Background())
|
|
|
|
}
|
2022-09-30 14:01:42 +00:00
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
scenarios, err := api.FetchScenariosListFromDB()
|
|
|
|
for machineID := range tc.machineIDsWithScenarios {
|
|
|
|
api.dbClient.Ent.Machine.Delete().Where(machine.MachineIdEQ(machineID)).ExecX(context.Background())
|
|
|
|
}
|
2022-09-30 14:01:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.ElementsMatch(t, tc.expectedScenarios, scenarios)
|
2022-03-29 12:20:26 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewAPIC(t *testing.T) {
|
|
|
|
var testConfig *csconfig.OnlineApiClientCfg
|
|
|
|
setConfig := func() {
|
|
|
|
testConfig = &csconfig.OnlineApiClientCfg{
|
|
|
|
Credentials: &csconfig.ApiCredentialsCfg{
|
2023-01-31 13:47:44 +00:00
|
|
|
URL: "http://foobar/",
|
2022-03-29 12:20:26 +00:00
|
|
|
Login: "foo",
|
|
|
|
Password: "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2023-01-31 13:47:44 +00:00
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
type args struct {
|
|
|
|
dbClient *database.Client
|
|
|
|
consoleConfig *csconfig.ConsoleConfig
|
|
|
|
}
|
|
|
|
tests := []struct {
|
2022-09-30 14:01:42 +00:00
|
|
|
name string
|
|
|
|
args args
|
|
|
|
expectedErr string
|
|
|
|
action func()
|
2022-03-29 12:20:26 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple",
|
|
|
|
action: func() {},
|
|
|
|
args: args{
|
|
|
|
dbClient: getDBClient(t),
|
2023-05-25 13:32:32 +00:00
|
|
|
consoleConfig: LoadTestConfig(t).API.Server.ConsoleConfig,
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error in parsing URL",
|
|
|
|
action: func() { testConfig.Credentials.URL = "foobar http://" },
|
|
|
|
args: args{
|
|
|
|
dbClient: getDBClient(t),
|
2023-05-25 13:32:32 +00:00
|
|
|
consoleConfig: LoadTestConfig(t).API.Server.ConsoleConfig,
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
2022-09-30 14:01:42 +00:00
|
|
|
expectedErr: "first path segment in URL cannot contain colon",
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
}
|
2022-09-30 14:01:42 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-03-29 12:20:26 +00:00
|
|
|
setConfig()
|
2023-01-31 13:47:44 +00:00
|
|
|
httpmock.Activate()
|
|
|
|
defer httpmock.DeactivateAndReset()
|
2023-02-06 13:06:14 +00:00
|
|
|
httpmock.RegisterResponder("POST", "http://foobar/v3/watchers/login", httpmock.NewBytesResponder(
|
2023-01-31 13:47:44 +00:00
|
|
|
200, jsonMarshalX(
|
|
|
|
models.WatcherAuthResponse{
|
|
|
|
Code: 200,
|
|
|
|
Expire: "2023-01-12T22:51:43Z",
|
|
|
|
Token: "MyToken",
|
|
|
|
},
|
|
|
|
),
|
|
|
|
))
|
2022-09-30 14:01:42 +00:00
|
|
|
tc.action()
|
2023-03-21 10:50:10 +00:00
|
|
|
_, err := NewAPIC(testConfig, tc.args.dbClient, tc.args.consoleConfig, nil)
|
2022-09-30 14:01:42 +00:00
|
|
|
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
2022-03-29 12:20:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPICHandleDeletedDecisions(t *testing.T) {
|
|
|
|
api := getAPIC(t)
|
|
|
|
_, deleteCounters := makeAddAndDeleteCounters()
|
|
|
|
|
|
|
|
decision1 := api.dbClient.Ent.Decision.Create().
|
|
|
|
SetUntil(time.Now().Add(time.Hour)).
|
|
|
|
SetScenario("crowdsec/test").
|
|
|
|
SetType("ban").
|
|
|
|
SetScope("IP").
|
|
|
|
SetValue("1.2.3.4").
|
2023-01-31 13:47:44 +00:00
|
|
|
SetOrigin(types.CAPIOrigin).
|
2022-03-29 12:20:26 +00:00
|
|
|
SaveX(context.Background())
|
|
|
|
|
|
|
|
api.dbClient.Ent.Decision.Create().
|
|
|
|
SetUntil(time.Now().Add(time.Hour)).
|
|
|
|
SetScenario("crowdsec/test").
|
|
|
|
SetType("ban").
|
|
|
|
SetScope("IP").
|
|
|
|
SetValue("1.2.3.4").
|
2023-01-31 13:47:44 +00:00
|
|
|
SetOrigin(types.CAPIOrigin).
|
2022-03-29 12:20:26 +00:00
|
|
|
SaveX(context.Background())
|
|
|
|
|
|
|
|
assertTotalDecisionCount(t, api.dbClient, 2)
|
|
|
|
|
|
|
|
nbDeleted, err := api.HandleDeletedDecisions([]*models.Decision{{
|
2023-05-25 13:37:44 +00:00
|
|
|
Value: ptr.Of("1.2.3.4"),
|
|
|
|
Origin: ptr.Of(types.CAPIOrigin),
|
2022-03-29 12:20:26 +00:00
|
|
|
Type: &decision1.Type,
|
2023-05-25 13:37:44 +00:00
|
|
|
Scenario: ptr.Of("crowdsec/test"),
|
|
|
|
Scope: ptr.Of("IP"),
|
2022-03-29 12:20:26 +00:00
|
|
|
}}, deleteCounters)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2022-09-30 14:01:42 +00:00
|
|
|
assert.Equal(t, 2, nbDeleted)
|
2023-01-31 13:47:44 +00:00
|
|
|
assert.Equal(t, 2, deleteCounters[types.CAPIOrigin]["all"])
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPICGetMetrics(t *testing.T) {
|
2022-09-30 14:01:42 +00:00
|
|
|
cleanUp := func(api *apic) {
|
2022-03-29 12:20:26 +00:00
|
|
|
api.dbClient.Ent.Bouncer.Delete().ExecX(context.Background())
|
|
|
|
api.dbClient.Ent.Machine.Delete().ExecX(context.Background())
|
|
|
|
}
|
2022-09-30 14:01:42 +00:00
|
|
|
tests := []struct {
|
2022-03-29 12:20:26 +00:00
|
|
|
name string
|
|
|
|
machineIDs []string
|
|
|
|
bouncers []string
|
|
|
|
expectedMetric *models.Metrics
|
|
|
|
}{
|
2023-02-17 13:57:46 +00:00
|
|
|
{
|
|
|
|
name: "no bouncers nor machines should still have bouncers/machines keys in output",
|
|
|
|
machineIDs: []string{},
|
|
|
|
bouncers: []string{},
|
|
|
|
expectedMetric: &models.Metrics{
|
2023-05-25 13:37:44 +00:00
|
|
|
ApilVersion: ptr.Of(version.String()),
|
2023-02-17 13:57:46 +00:00
|
|
|
Bouncers: []*models.MetricsBouncerInfo{},
|
|
|
|
Machines: []*models.MetricsAgentInfo{},
|
|
|
|
},
|
|
|
|
},
|
2022-03-29 12:20:26 +00:00
|
|
|
{
|
|
|
|
name: "simple",
|
|
|
|
machineIDs: []string{"a", "b", "c"},
|
|
|
|
bouncers: []string{"1", "2", "3"},
|
|
|
|
expectedMetric: &models.Metrics{
|
2023-05-25 13:37:44 +00:00
|
|
|
ApilVersion: ptr.Of(version.String()),
|
2022-03-29 12:20:26 +00:00
|
|
|
Bouncers: []*models.MetricsBouncerInfo{
|
|
|
|
{
|
|
|
|
CustomName: "1",
|
|
|
|
LastPull: time.Time{}.String(),
|
|
|
|
}, {
|
|
|
|
CustomName: "2",
|
|
|
|
LastPull: time.Time{}.String(),
|
|
|
|
}, {
|
|
|
|
CustomName: "3",
|
|
|
|
LastPull: time.Time{}.String(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Machines: []*models.MetricsAgentInfo{
|
|
|
|
{
|
|
|
|
Name: "a",
|
|
|
|
LastPush: time.Time{}.String(),
|
|
|
|
LastUpdate: time.Time{}.String(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "b",
|
|
|
|
LastPush: time.Time{}.String(),
|
|
|
|
LastUpdate: time.Time{}.String(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "c",
|
|
|
|
LastPush: time.Time{}.String(),
|
|
|
|
LastUpdate: time.Time{}.String(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-09-30 14:01:42 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
apiClient := getAPIC(t)
|
|
|
|
cleanUp(apiClient)
|
|
|
|
for i, machineID := range tc.machineIDs {
|
|
|
|
apiClient.dbClient.Ent.Machine.Create().
|
2022-03-29 12:20:26 +00:00
|
|
|
SetMachineId(machineID).
|
|
|
|
SetPassword(testPassword.String()).
|
|
|
|
SetIpAddress(fmt.Sprintf("1.2.3.%d", i)).
|
|
|
|
SetScenarios("crowdsecurity/test").
|
|
|
|
SetLastPush(time.Time{}).
|
|
|
|
SetUpdatedAt(time.Time{}).
|
|
|
|
ExecX(context.Background())
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:01:42 +00:00
|
|
|
for i, bouncerName := range tc.bouncers {
|
|
|
|
apiClient.dbClient.Ent.Bouncer.Create().
|
2022-03-29 12:20:26 +00:00
|
|
|
SetIPAddress(fmt.Sprintf("1.2.3.%d", i)).
|
|
|
|
SetName(bouncerName).
|
|
|
|
SetAPIKey("foobar").
|
|
|
|
SetRevoked(false).
|
|
|
|
SetLastPull(time.Time{}).
|
|
|
|
ExecX(context.Background())
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:01:42 +00:00
|
|
|
foundMetrics, err := apiClient.GetMetrics()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.expectedMetric.Bouncers, foundMetrics.Bouncers)
|
|
|
|
assert.Equal(t, tc.expectedMetric.Machines, foundMetrics.Machines)
|
2022-03-29 12:20:26 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateAlertsForDecision(t *testing.T) {
|
|
|
|
httpBfDecisionList := &models.Decision{
|
2023-05-25 13:37:44 +00:00
|
|
|
Origin: ptr.Of(types.ListOrigin),
|
|
|
|
Scenario: ptr.Of("crowdsecurity/http-bf"),
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sshBfDecisionList := &models.Decision{
|
2023-05-25 13:37:44 +00:00
|
|
|
Origin: ptr.Of(types.ListOrigin),
|
|
|
|
Scenario: ptr.Of("crowdsecurity/ssh-bf"),
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
httpBfDecisionCommunity := &models.Decision{
|
2023-05-25 13:37:44 +00:00
|
|
|
Origin: ptr.Of(types.CAPIOrigin),
|
|
|
|
Scenario: ptr.Of("crowdsecurity/http-bf"),
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sshBfDecisionCommunity := &models.Decision{
|
2023-05-25 13:37:44 +00:00
|
|
|
Origin: ptr.Of(types.CAPIOrigin),
|
|
|
|
Scenario: ptr.Of("crowdsecurity/ssh-bf"),
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
decisions []*models.Decision
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []*models.Alert
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "2 decisions CAPI List Decisions should create 2 alerts",
|
|
|
|
args: args{
|
|
|
|
decisions: []*models.Decision{
|
|
|
|
httpBfDecisionList,
|
|
|
|
sshBfDecisionList,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*models.Alert{
|
|
|
|
createAlertForDecision(httpBfDecisionList),
|
|
|
|
createAlertForDecision(sshBfDecisionList),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "2 decisions CAPI List same scenario decisions should create 1 alert",
|
|
|
|
args: args{
|
|
|
|
decisions: []*models.Decision{
|
|
|
|
httpBfDecisionList,
|
|
|
|
httpBfDecisionList,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*models.Alert{
|
|
|
|
createAlertForDecision(httpBfDecisionList),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "5 decisions from community list should create 1 alert",
|
|
|
|
args: args{
|
|
|
|
decisions: []*models.Decision{
|
|
|
|
httpBfDecisionCommunity,
|
|
|
|
httpBfDecisionCommunity,
|
|
|
|
sshBfDecisionCommunity,
|
|
|
|
sshBfDecisionCommunity,
|
|
|
|
sshBfDecisionCommunity,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*models.Alert{
|
|
|
|
createAlertForDecision(sshBfDecisionCommunity),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-09-30 14:01:42 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
if got := createAlertsForDecisions(tc.args.decisions); !reflect.DeepEqual(got, tc.want) {
|
|
|
|
t.Errorf("createAlertsForDecisions() = %v, want %v", got, tc.want)
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFillAlertsWithDecisions(t *testing.T) {
|
|
|
|
httpBfDecisionCommunity := &models.Decision{
|
2023-05-25 13:37:44 +00:00
|
|
|
Origin: ptr.Of(types.CAPIOrigin),
|
|
|
|
Scenario: ptr.Of("crowdsecurity/http-bf"),
|
|
|
|
Scope: ptr.Of("ip"),
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sshBfDecisionCommunity := &models.Decision{
|
2023-05-25 13:37:44 +00:00
|
|
|
Origin: ptr.Of(types.CAPIOrigin),
|
|
|
|
Scenario: ptr.Of("crowdsecurity/ssh-bf"),
|
|
|
|
Scope: ptr.Of("ip"),
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
httpBfDecisionList := &models.Decision{
|
2023-05-25 13:37:44 +00:00
|
|
|
Origin: ptr.Of(types.ListOrigin),
|
|
|
|
Scenario: ptr.Of("crowdsecurity/http-bf"),
|
|
|
|
Scope: ptr.Of("ip"),
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sshBfDecisionList := &models.Decision{
|
2023-05-25 13:37:44 +00:00
|
|
|
Origin: ptr.Of(types.ListOrigin),
|
|
|
|
Scenario: ptr.Of("crowdsecurity/ssh-bf"),
|
|
|
|
Scope: ptr.Of("ip"),
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
alerts []*models.Alert
|
|
|
|
decisions []*models.Decision
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []*models.Alert
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "1 CAPI alert should pair up with n CAPI decisions",
|
|
|
|
args: args{
|
|
|
|
alerts: []*models.Alert{createAlertForDecision(httpBfDecisionCommunity)},
|
|
|
|
decisions: []*models.Decision{httpBfDecisionCommunity, sshBfDecisionCommunity, sshBfDecisionCommunity, httpBfDecisionCommunity},
|
|
|
|
},
|
|
|
|
want: []*models.Alert{
|
|
|
|
func() *models.Alert {
|
|
|
|
a := createAlertForDecision(httpBfDecisionCommunity)
|
|
|
|
a.Decisions = []*models.Decision{httpBfDecisionCommunity, sshBfDecisionCommunity, sshBfDecisionCommunity, httpBfDecisionCommunity}
|
|
|
|
return a
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "List alert should pair up only with decisions having same scenario",
|
|
|
|
args: args{
|
|
|
|
alerts: []*models.Alert{createAlertForDecision(httpBfDecisionList), createAlertForDecision(sshBfDecisionList)},
|
|
|
|
decisions: []*models.Decision{httpBfDecisionList, httpBfDecisionList, sshBfDecisionList, sshBfDecisionList},
|
|
|
|
},
|
|
|
|
want: []*models.Alert{
|
|
|
|
func() *models.Alert {
|
|
|
|
a := createAlertForDecision(httpBfDecisionList)
|
|
|
|
a.Decisions = []*models.Decision{httpBfDecisionList, httpBfDecisionList}
|
|
|
|
return a
|
|
|
|
}(),
|
|
|
|
func() *models.Alert {
|
|
|
|
a := createAlertForDecision(sshBfDecisionList)
|
|
|
|
a.Decisions = []*models.Decision{sshBfDecisionList, sshBfDecisionList}
|
|
|
|
return a
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-09-30 14:01:42 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
addCounters, _ := makeAddAndDeleteCounters()
|
|
|
|
if got := fillAlertsWithDecisions(tc.args.alerts, tc.args.decisions, addCounters); !reflect.DeepEqual(got, tc.want) {
|
|
|
|
t.Errorf("fillAlertsWithDecisions() = %v, want %v", got, tc.want)
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 10:50:10 +00:00
|
|
|
func TestAPICWhitelists(t *testing.T) {
|
|
|
|
api := getAPIC(t)
|
|
|
|
//one whitelist on IP, one on CIDR
|
|
|
|
api.whitelists = &csconfig.CapiWhitelist{}
|
|
|
|
ipwl1 := "9.2.3.4"
|
|
|
|
ip := net.ParseIP(ipwl1)
|
|
|
|
api.whitelists.Ips = append(api.whitelists.Ips, ip)
|
|
|
|
ipwl1 = "7.2.3.4"
|
|
|
|
ip = net.ParseIP(ipwl1)
|
|
|
|
api.whitelists.Ips = append(api.whitelists.Ips, ip)
|
|
|
|
cidrwl1 := "13.2.3.0/24"
|
|
|
|
_, tnet, err := net.ParseCIDR(cidrwl1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to parse cidr : %s", err)
|
|
|
|
}
|
|
|
|
api.whitelists.Cidrs = append(api.whitelists.Cidrs, tnet)
|
|
|
|
cidrwl1 = "11.2.3.0/24"
|
|
|
|
_, tnet, err = net.ParseCIDR(cidrwl1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to parse cidr : %s", err)
|
|
|
|
}
|
|
|
|
api.whitelists.Cidrs = append(api.whitelists.Cidrs, tnet)
|
|
|
|
api.dbClient.Ent.Decision.Create().
|
|
|
|
SetOrigin(types.CAPIOrigin).
|
|
|
|
SetType("ban").
|
|
|
|
SetValue("9.9.9.9").
|
|
|
|
SetScope("Ip").
|
|
|
|
SetScenario("crowdsecurity/ssh-bf").
|
|
|
|
SetUntil(time.Now().Add(time.Hour)).
|
|
|
|
ExecX(context.Background())
|
|
|
|
assertTotalDecisionCount(t, api.dbClient, 1)
|
|
|
|
assertTotalValidDecisionCount(t, api.dbClient, 1)
|
|
|
|
httpmock.Activate()
|
|
|
|
defer httpmock.DeactivateAndReset()
|
|
|
|
httpmock.RegisterResponder("GET", "http://api.crowdsec.net/api/decisions/stream", httpmock.NewBytesResponder(
|
|
|
|
200, jsonMarshalX(
|
|
|
|
modelscapi.GetDecisionsStreamResponse{
|
|
|
|
Deleted: modelscapi.GetDecisionsStreamResponseDeleted{
|
|
|
|
&modelscapi.GetDecisionsStreamResponseDeletedItem{
|
|
|
|
Decisions: []string{
|
|
|
|
"9.9.9.9", // This is already present in DB
|
|
|
|
"9.1.9.9", // This not present in DB
|
|
|
|
},
|
2023-05-25 13:37:44 +00:00
|
|
|
Scope: ptr.Of("Ip"),
|
2023-03-21 10:50:10 +00:00
|
|
|
}, // This is already present in DB
|
|
|
|
},
|
|
|
|
New: modelscapi.GetDecisionsStreamResponseNew{
|
|
|
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
2023-05-25 13:37:44 +00:00
|
|
|
Scenario: ptr.Of("crowdsecurity/test1"),
|
|
|
|
Scope: ptr.Of("Ip"),
|
2023-03-21 10:50:10 +00:00
|
|
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
|
|
|
{
|
2023-05-25 13:37:44 +00:00
|
|
|
Value: ptr.Of("13.2.3.4"), //wl by cidr
|
|
|
|
Duration: ptr.Of("24h"),
|
2023-03-21 10:50:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
2023-05-25 13:37:44 +00:00
|
|
|
Scenario: ptr.Of("crowdsecurity/test1"),
|
|
|
|
Scope: ptr.Of("Ip"),
|
2023-03-21 10:50:10 +00:00
|
|
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
|
|
|
{
|
2023-05-25 13:37:44 +00:00
|
|
|
Value: ptr.Of("2.2.3.4"),
|
|
|
|
Duration: ptr.Of("24h"),
|
2023-03-21 10:50:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
2023-05-25 13:37:44 +00:00
|
|
|
Scenario: ptr.Of("crowdsecurity/test2"),
|
|
|
|
Scope: ptr.Of("Ip"),
|
2023-03-21 10:50:10 +00:00
|
|
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
|
|
|
{
|
2023-05-25 13:37:44 +00:00
|
|
|
Value: ptr.Of("13.2.3.5"), //wl by cidr
|
|
|
|
Duration: ptr.Of("24h"),
|
2023-03-21 10:50:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, // These two are from community list.
|
|
|
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
2023-05-25 13:37:44 +00:00
|
|
|
Scenario: ptr.Of("crowdsecurity/test1"),
|
|
|
|
Scope: ptr.Of("Ip"),
|
2023-03-21 10:50:10 +00:00
|
|
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
|
|
|
{
|
2023-05-25 13:37:44 +00:00
|
|
|
Value: ptr.Of("6.2.3.4"),
|
|
|
|
Duration: ptr.Of("24h"),
|
2023-03-21 10:50:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
2023-05-25 13:37:44 +00:00
|
|
|
Scenario: ptr.Of("crowdsecurity/test1"),
|
|
|
|
Scope: ptr.Of("Ip"),
|
2023-03-21 10:50:10 +00:00
|
|
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
|
|
|
{
|
2023-05-25 13:37:44 +00:00
|
|
|
Value: ptr.Of("9.2.3.4"), //wl by ip
|
|
|
|
Duration: ptr.Of("24h"),
|
2023-03-21 10:50:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Links: &modelscapi.GetDecisionsStreamResponseLinks{
|
|
|
|
Blocklists: []*modelscapi.BlocklistLink{
|
|
|
|
{
|
2023-05-25 13:37:44 +00:00
|
|
|
URL: ptr.Of("http://api.crowdsec.net/blocklist1"),
|
|
|
|
Name: ptr.Of("blocklist1"),
|
|
|
|
Scope: ptr.Of("Ip"),
|
|
|
|
Remediation: ptr.Of("ban"),
|
|
|
|
Duration: ptr.Of("24h"),
|
2023-03-21 10:50:10 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-25 13:37:44 +00:00
|
|
|
URL: ptr.Of("http://api.crowdsec.net/blocklist2"),
|
|
|
|
Name: ptr.Of("blocklist2"),
|
|
|
|
Scope: ptr.Of("Ip"),
|
|
|
|
Remediation: ptr.Of("ban"),
|
|
|
|
Duration: ptr.Of("24h"),
|
2023-03-21 10:50:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
))
|
|
|
|
httpmock.RegisterResponder("GET", "http://api.crowdsec.net/blocklist1", httpmock.NewStringResponder(
|
|
|
|
200, "1.2.3.6",
|
|
|
|
))
|
|
|
|
httpmock.RegisterResponder("GET", "http://api.crowdsec.net/blocklist2", httpmock.NewStringResponder(
|
|
|
|
200, "1.2.3.7",
|
|
|
|
))
|
|
|
|
url, err := url.ParseRequestURI("http://api.crowdsec.net/")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
apic, err := apiclient.NewDefaultClient(
|
|
|
|
url,
|
|
|
|
"/api",
|
2023-05-23 08:52:47 +00:00
|
|
|
fmt.Sprintf("crowdsec/%s", version.String()),
|
2023-03-21 10:50:10 +00:00
|
|
|
nil,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
api.apiClient = apic
|
2023-03-21 13:06:19 +00:00
|
|
|
err = api.PullTop(false)
|
2023-03-21 10:50:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assertTotalDecisionCount(t, api.dbClient, 5) //2 from FIRE + 2 from bl + 1 existing
|
|
|
|
assertTotalValidDecisionCount(t, api.dbClient, 4)
|
|
|
|
assertTotalAlertCount(t, api.dbClient, 3) // 2 for list sub , 1 for community list.
|
|
|
|
alerts := api.dbClient.Ent.Alert.Query().AllX(context.Background())
|
|
|
|
validDecisions := api.dbClient.Ent.Decision.Query().Where(
|
|
|
|
decision.UntilGT(time.Now())).
|
|
|
|
AllX(context.Background())
|
|
|
|
|
|
|
|
decisionScenarioFreq := make(map[string]int)
|
|
|
|
decisionIp := make(map[string]int)
|
|
|
|
|
|
|
|
alertScenario := make(map[string]int)
|
|
|
|
|
|
|
|
for _, alert := range alerts {
|
|
|
|
alertScenario[alert.SourceScope]++
|
|
|
|
}
|
|
|
|
assert.Equal(t, 3, len(alertScenario))
|
|
|
|
assert.Equal(t, 1, alertScenario[SCOPE_CAPI_ALIAS_ALIAS])
|
|
|
|
assert.Equal(t, 1, alertScenario["lists:blocklist1"])
|
|
|
|
assert.Equal(t, 1, alertScenario["lists:blocklist2"])
|
|
|
|
|
|
|
|
for _, decisions := range validDecisions {
|
|
|
|
decisionScenarioFreq[decisions.Scenario]++
|
|
|
|
decisionIp[decisions.Value]++
|
|
|
|
}
|
|
|
|
assert.Equal(t, 1, decisionIp["2.2.3.4"], 1)
|
|
|
|
assert.Equal(t, 1, decisionIp["6.2.3.4"], 1)
|
|
|
|
if _, ok := decisionIp["13.2.3.4"]; ok {
|
|
|
|
t.Errorf("13.2.3.4 is whitelisted")
|
|
|
|
}
|
|
|
|
if _, ok := decisionIp["13.2.3.5"]; ok {
|
|
|
|
t.Errorf("13.2.3.5 is whitelisted")
|
|
|
|
}
|
|
|
|
if _, ok := decisionIp["9.2.3.4"]; ok {
|
|
|
|
t.Errorf("9.2.3.4 is whitelisted")
|
|
|
|
}
|
|
|
|
assert.Equal(t, 1, decisionScenarioFreq["blocklist1"], 1)
|
|
|
|
assert.Equal(t, 1, decisionScenarioFreq["blocklist2"], 1)
|
|
|
|
assert.Equal(t, 2, decisionScenarioFreq["crowdsecurity/test1"], 2)
|
|
|
|
}
|
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
func TestAPICPullTop(t *testing.T) {
|
|
|
|
api := getAPIC(t)
|
|
|
|
api.dbClient.Ent.Decision.Create().
|
2023-02-06 13:06:14 +00:00
|
|
|
SetOrigin(types.CAPIOrigin).
|
2022-03-29 12:20:26 +00:00
|
|
|
SetType("ban").
|
|
|
|
SetValue("9.9.9.9").
|
|
|
|
SetScope("Ip").
|
|
|
|
SetScenario("crowdsecurity/ssh-bf").
|
|
|
|
SetUntil(time.Now().Add(time.Hour)).
|
|
|
|
ExecX(context.Background())
|
|
|
|
assertTotalDecisionCount(t, api.dbClient, 1)
|
|
|
|
assertTotalValidDecisionCount(t, api.dbClient, 1)
|
|
|
|
httpmock.Activate()
|
|
|
|
defer httpmock.DeactivateAndReset()
|
|
|
|
httpmock.RegisterResponder("GET", "http://api.crowdsec.net/api/decisions/stream", httpmock.NewBytesResponder(
|
|
|
|
200, jsonMarshalX(
|
2023-02-06 13:06:14 +00:00
|
|
|
modelscapi.GetDecisionsStreamResponse{
|
|
|
|
Deleted: modelscapi.GetDecisionsStreamResponseDeleted{
|
|
|
|
&modelscapi.GetDecisionsStreamResponseDeletedItem{
|
|
|
|
Decisions: []string{
|
|
|
|
"9.9.9.9", // This is already present in DB
|
|
|
|
"9.1.9.9", // This not present in DB
|
|
|
|
},
|
2023-05-25 13:37:44 +00:00
|
|
|
Scope: ptr.Of("Ip"),
|
2022-04-27 09:04:12 +00:00
|
|
|
}, // This is already present in DB
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
2023-02-06 13:06:14 +00:00
|
|
|
New: modelscapi.GetDecisionsStreamResponseNew{
|
|
|
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
2023-05-25 13:37:44 +00:00
|
|
|
Scenario: ptr.Of("crowdsecurity/test1"),
|
|
|
|
Scope: ptr.Of("Ip"),
|
2023-02-06 13:06:14 +00:00
|
|
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
|
|
|
{
|
2023-05-25 13:37:44 +00:00
|
|
|
Value: ptr.Of("1.2.3.4"),
|
|
|
|
Duration: ptr.Of("24h"),
|
2023-02-06 13:06:14 +00:00
|
|
|
},
|
|
|
|
},
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
2023-02-06 13:06:14 +00:00
|
|
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
2023-05-25 13:37:44 +00:00
|
|
|
Scenario: ptr.Of("crowdsecurity/test2"),
|
|
|
|
Scope: ptr.Of("Ip"),
|
2023-02-06 13:06:14 +00:00
|
|
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
|
|
|
{
|
2023-05-25 13:37:44 +00:00
|
|
|
Value: ptr.Of("1.2.3.5"),
|
|
|
|
Duration: ptr.Of("24h"),
|
2023-02-06 13:06:14 +00:00
|
|
|
},
|
|
|
|
},
|
2022-03-29 12:20:26 +00:00
|
|
|
}, // These two are from community list.
|
2023-02-06 13:06:14 +00:00
|
|
|
},
|
|
|
|
Links: &modelscapi.GetDecisionsStreamResponseLinks{
|
|
|
|
Blocklists: []*modelscapi.BlocklistLink{
|
|
|
|
{
|
2023-05-25 13:37:44 +00:00
|
|
|
URL: ptr.Of("http://api.crowdsec.net/blocklist1"),
|
|
|
|
Name: ptr.Of("blocklist1"),
|
|
|
|
Scope: ptr.Of("Ip"),
|
|
|
|
Remediation: ptr.Of("ban"),
|
|
|
|
Duration: ptr.Of("24h"),
|
2023-02-06 13:06:14 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-25 13:37:44 +00:00
|
|
|
URL: ptr.Of("http://api.crowdsec.net/blocklist2"),
|
|
|
|
Name: ptr.Of("blocklist2"),
|
|
|
|
Scope: ptr.Of("Ip"),
|
|
|
|
Remediation: ptr.Of("ban"),
|
|
|
|
Duration: ptr.Of("24h"),
|
2023-02-06 13:06:14 +00:00
|
|
|
},
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
))
|
2023-02-06 13:06:14 +00:00
|
|
|
httpmock.RegisterResponder("GET", "http://api.crowdsec.net/blocklist1", httpmock.NewStringResponder(
|
|
|
|
200, "1.2.3.6",
|
|
|
|
))
|
|
|
|
httpmock.RegisterResponder("GET", "http://api.crowdsec.net/blocklist2", httpmock.NewStringResponder(
|
|
|
|
200, "1.2.3.7",
|
|
|
|
))
|
2022-03-29 12:20:26 +00:00
|
|
|
url, err := url.ParseRequestURI("http://api.crowdsec.net/")
|
2022-09-30 14:01:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
apic, err := apiclient.NewDefaultClient(
|
|
|
|
url,
|
|
|
|
"/api",
|
2023-05-23 08:52:47 +00:00
|
|
|
fmt.Sprintf("crowdsec/%s", version.String()),
|
2022-03-29 12:20:26 +00:00
|
|
|
nil,
|
|
|
|
)
|
2022-09-30 14:01:42 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-29 12:20:26 +00:00
|
|
|
|
|
|
|
api.apiClient = apic
|
2023-03-21 13:06:19 +00:00
|
|
|
err = api.PullTop(false)
|
2022-09-30 14:01:42 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-29 12:20:26 +00:00
|
|
|
|
|
|
|
assertTotalDecisionCount(t, api.dbClient, 5)
|
|
|
|
assertTotalValidDecisionCount(t, api.dbClient, 4)
|
|
|
|
assertTotalAlertCount(t, api.dbClient, 3) // 2 for list sub , 1 for community list.
|
|
|
|
alerts := api.dbClient.Ent.Alert.Query().AllX(context.Background())
|
|
|
|
validDecisions := api.dbClient.Ent.Decision.Query().Where(
|
|
|
|
decision.UntilGT(time.Now())).
|
|
|
|
AllX(context.Background())
|
|
|
|
|
|
|
|
decisionScenarioFreq := make(map[string]int)
|
|
|
|
alertScenario := make(map[string]int)
|
|
|
|
|
|
|
|
for _, alert := range alerts {
|
|
|
|
alertScenario[alert.SourceScope]++
|
|
|
|
}
|
2022-09-30 14:01:42 +00:00
|
|
|
assert.Equal(t, 3, len(alertScenario))
|
2023-01-31 13:47:44 +00:00
|
|
|
assert.Equal(t, 1, alertScenario[SCOPE_CAPI_ALIAS_ALIAS])
|
2023-02-13 14:06:14 +00:00
|
|
|
assert.Equal(t, 1, alertScenario["lists:blocklist1"])
|
|
|
|
assert.Equal(t, 1, alertScenario["lists:blocklist2"])
|
2022-03-29 12:20:26 +00:00
|
|
|
|
|
|
|
for _, decisions := range validDecisions {
|
|
|
|
decisionScenarioFreq[decisions.Scenario]++
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:06:14 +00:00
|
|
|
assert.Equal(t, 1, decisionScenarioFreq["blocklist1"], 1)
|
|
|
|
assert.Equal(t, 1, decisionScenarioFreq["blocklist2"], 1)
|
2022-09-30 14:01:42 +00:00
|
|
|
assert.Equal(t, 1, decisionScenarioFreq["crowdsecurity/test1"], 1)
|
|
|
|
assert.Equal(t, 1, decisionScenarioFreq["crowdsecurity/test2"], 1)
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 14:06:14 +00:00
|
|
|
func TestAPICPullTopBLCacheFirstCall(t *testing.T) {
|
|
|
|
// no decision in db, no last modified parameter.
|
|
|
|
api := getAPIC(t)
|
|
|
|
httpmock.Activate()
|
|
|
|
defer httpmock.DeactivateAndReset()
|
|
|
|
httpmock.RegisterResponder("GET", "http://api.crowdsec.net/api/decisions/stream", httpmock.NewBytesResponder(
|
|
|
|
200, jsonMarshalX(
|
|
|
|
modelscapi.GetDecisionsStreamResponse{
|
|
|
|
New: modelscapi.GetDecisionsStreamResponseNew{
|
|
|
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
2023-05-25 13:37:44 +00:00
|
|
|
Scenario: ptr.Of("crowdsecurity/test1"),
|
|
|
|
Scope: ptr.Of("Ip"),
|
2023-02-13 14:06:14 +00:00
|
|
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
|
|
|
{
|
2023-05-25 13:37:44 +00:00
|
|
|
Value: ptr.Of("1.2.3.4"),
|
|
|
|
Duration: ptr.Of("24h"),
|
2023-02-13 14:06:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Links: &modelscapi.GetDecisionsStreamResponseLinks{
|
|
|
|
Blocklists: []*modelscapi.BlocklistLink{
|
|
|
|
{
|
2023-05-25 13:37:44 +00:00
|
|
|
URL: ptr.Of("http://api.crowdsec.net/blocklist1"),
|
|
|
|
Name: ptr.Of("blocklist1"),
|
|
|
|
Scope: ptr.Of("Ip"),
|
|
|
|
Remediation: ptr.Of("ban"),
|
|
|
|
Duration: ptr.Of("24h"),
|
2023-02-13 14:06:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
))
|
|
|
|
httpmock.RegisterResponder("GET", "http://api.crowdsec.net/blocklist1", func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.Equal(t, "", req.Header.Get("If-Modified-Since"))
|
|
|
|
return httpmock.NewStringResponse(200, "1.2.3.4"), nil
|
|
|
|
})
|
|
|
|
url, err := url.ParseRequestURI("http://api.crowdsec.net/")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
apic, err := apiclient.NewDefaultClient(
|
|
|
|
url,
|
|
|
|
"/api",
|
2023-05-23 08:52:47 +00:00
|
|
|
fmt.Sprintf("crowdsec/%s", version.String()),
|
2023-02-13 14:06:14 +00:00
|
|
|
nil,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
api.apiClient = apic
|
2023-03-21 13:06:19 +00:00
|
|
|
err = api.PullTop(false)
|
2023-02-13 14:06:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-05-25 13:37:44 +00:00
|
|
|
blocklistConfigItemName := "blocklist:blocklist1:last_pull"
|
2023-02-13 14:06:14 +00:00
|
|
|
lastPullTimestamp, err := api.dbClient.GetConfigItem(blocklistConfigItemName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotEqual(t, "", *lastPullTimestamp)
|
|
|
|
|
|
|
|
// new call should return 304 and should not change lastPullTimestamp
|
|
|
|
httpmock.RegisterResponder("GET", "http://api.crowdsec.net/blocklist1", func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.NotEqual(t, "", req.Header.Get("If-Modified-Since"))
|
|
|
|
return httpmock.NewStringResponse(304, ""), nil
|
|
|
|
})
|
2023-03-21 13:06:19 +00:00
|
|
|
err = api.PullTop(false)
|
2023-02-13 14:06:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
secondLastPullTimestamp, err := api.dbClient.GetConfigItem(blocklistConfigItemName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, *lastPullTimestamp, *secondLastPullTimestamp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPICPullTopBLCacheForceCall(t *testing.T) {
|
|
|
|
api := getAPIC(t)
|
|
|
|
httpmock.Activate()
|
|
|
|
defer httpmock.DeactivateAndReset()
|
|
|
|
// create a decision about to expire. It should force fetch
|
|
|
|
alertInstance := api.dbClient.Ent.Alert.
|
|
|
|
Create().
|
|
|
|
SetScenario("update list").
|
|
|
|
SetSourceScope("list:blocklist1").
|
|
|
|
SetSourceValue("list:blocklist1").
|
|
|
|
SaveX(context.Background())
|
|
|
|
|
|
|
|
api.dbClient.Ent.Decision.Create().
|
|
|
|
SetOrigin(types.ListOrigin).
|
|
|
|
SetType("ban").
|
|
|
|
SetValue("9.9.9.9").
|
|
|
|
SetScope("Ip").
|
|
|
|
SetScenario("blocklist1").
|
|
|
|
SetUntil(time.Now().Add(time.Hour)).
|
|
|
|
SetOwnerID(alertInstance.ID).
|
|
|
|
ExecX(context.Background())
|
|
|
|
|
|
|
|
httpmock.RegisterResponder("GET", "http://api.crowdsec.net/api/decisions/stream", httpmock.NewBytesResponder(
|
|
|
|
200, jsonMarshalX(
|
|
|
|
modelscapi.GetDecisionsStreamResponse{
|
|
|
|
New: modelscapi.GetDecisionsStreamResponseNew{
|
|
|
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
2023-05-25 13:37:44 +00:00
|
|
|
Scenario: ptr.Of("crowdsecurity/test1"),
|
|
|
|
Scope: ptr.Of("Ip"),
|
2023-02-13 14:06:14 +00:00
|
|
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
|
|
|
{
|
2023-05-25 13:37:44 +00:00
|
|
|
Value: ptr.Of("1.2.3.4"),
|
|
|
|
Duration: ptr.Of("24h"),
|
2023-02-13 14:06:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Links: &modelscapi.GetDecisionsStreamResponseLinks{
|
|
|
|
Blocklists: []*modelscapi.BlocklistLink{
|
|
|
|
{
|
2023-05-25 13:37:44 +00:00
|
|
|
URL: ptr.Of("http://api.crowdsec.net/blocklist1"),
|
|
|
|
Name: ptr.Of("blocklist1"),
|
|
|
|
Scope: ptr.Of("Ip"),
|
|
|
|
Remediation: ptr.Of("ban"),
|
|
|
|
Duration: ptr.Of("24h"),
|
2023-02-13 14:06:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
))
|
|
|
|
httpmock.RegisterResponder("GET", "http://api.crowdsec.net/blocklist1", func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.Equal(t, "", req.Header.Get("If-Modified-Since"))
|
|
|
|
return httpmock.NewStringResponse(304, ""), nil
|
|
|
|
})
|
|
|
|
url, err := url.ParseRequestURI("http://api.crowdsec.net/")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
apic, err := apiclient.NewDefaultClient(
|
|
|
|
url,
|
|
|
|
"/api",
|
2023-05-23 08:52:47 +00:00
|
|
|
fmt.Sprintf("crowdsec/%s", version.String()),
|
2023-02-13 14:06:14 +00:00
|
|
|
nil,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
api.apiClient = apic
|
2023-03-21 13:06:19 +00:00
|
|
|
err = api.PullTop(false)
|
2023-02-13 14:06:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
func TestAPICPush(t *testing.T) {
|
2022-09-30 14:01:42 +00:00
|
|
|
tests := []struct {
|
2022-03-29 12:20:26 +00:00
|
|
|
name string
|
|
|
|
alerts []*models.Alert
|
|
|
|
expectedCalls int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple single alert",
|
|
|
|
alerts: []*models.Alert{
|
|
|
|
{
|
2023-05-25 13:37:44 +00:00
|
|
|
Scenario: ptr.Of("crowdsec/test"),
|
|
|
|
ScenarioHash: ptr.Of("certified"),
|
|
|
|
ScenarioVersion: ptr.Of("v1.0"),
|
|
|
|
Simulated: ptr.Of(false),
|
2023-01-31 13:47:44 +00:00
|
|
|
Source: &models.Source{},
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedCalls: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simulated alert is not pushed",
|
|
|
|
alerts: []*models.Alert{
|
|
|
|
{
|
2023-05-25 13:37:44 +00:00
|
|
|
Scenario: ptr.Of("crowdsec/test"),
|
|
|
|
ScenarioHash: ptr.Of("certified"),
|
|
|
|
ScenarioVersion: ptr.Of("v1.0"),
|
|
|
|
Simulated: ptr.Of(true),
|
2023-01-31 13:47:44 +00:00
|
|
|
Source: &models.Source{},
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedCalls: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "1 request per 50 alerts",
|
|
|
|
expectedCalls: 2,
|
|
|
|
alerts: func() []*models.Alert {
|
|
|
|
alerts := make([]*models.Alert, 100)
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
alerts[i] = &models.Alert{
|
2023-05-25 13:37:44 +00:00
|
|
|
Scenario: ptr.Of("crowdsec/test"),
|
|
|
|
ScenarioHash: ptr.Of("certified"),
|
|
|
|
ScenarioVersion: ptr.Of("v1.0"),
|
|
|
|
Simulated: ptr.Of(false),
|
2023-01-31 13:47:44 +00:00
|
|
|
Source: &models.Source{},
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return alerts
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:01:42 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-03-29 12:20:26 +00:00
|
|
|
api := getAPIC(t)
|
|
|
|
api.pushInterval = time.Millisecond
|
2022-10-28 11:55:59 +00:00
|
|
|
api.pushIntervalFirst = time.Millisecond
|
2022-03-29 12:20:26 +00:00
|
|
|
url, err := url.ParseRequestURI("http://api.crowdsec.net/")
|
2022-09-30 14:01:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
httpmock.Activate()
|
|
|
|
defer httpmock.DeactivateAndReset()
|
|
|
|
apic, err := apiclient.NewDefaultClient(
|
|
|
|
url,
|
|
|
|
"/api",
|
2023-05-23 08:52:47 +00:00
|
|
|
fmt.Sprintf("crowdsec/%s", version.String()),
|
2022-03-29 12:20:26 +00:00
|
|
|
nil,
|
|
|
|
)
|
2022-09-30 14:01:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
api.apiClient = apic
|
|
|
|
httpmock.RegisterResponder("POST", "http://api.crowdsec.net/api/signals", httpmock.NewBytesResponder(200, []byte{}))
|
|
|
|
go func() {
|
2023-01-31 13:47:44 +00:00
|
|
|
api.AlertsAddChan <- tc.alerts
|
2022-03-29 12:20:26 +00:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
api.Shutdown()
|
|
|
|
}()
|
2022-09-30 14:01:42 +00:00
|
|
|
err = api.Push()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tc.expectedCalls, httpmock.GetTotalCallCount())
|
2022-03-29 12:20:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPICSendMetrics(t *testing.T) {
|
2022-09-30 14:01:42 +00:00
|
|
|
tests := []struct {
|
2022-03-29 12:20:26 +00:00
|
|
|
name string
|
|
|
|
duration time.Duration
|
|
|
|
expectedCalls int
|
2022-09-30 14:01:42 +00:00
|
|
|
setUp func(*apic)
|
2022-03-29 12:20:26 +00:00
|
|
|
metricsInterval time.Duration
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic",
|
2022-05-17 10:14:59 +00:00
|
|
|
duration: time.Millisecond * 30,
|
|
|
|
metricsInterval: time.Millisecond * 5,
|
2022-03-29 12:20:26 +00:00
|
|
|
expectedCalls: 5,
|
2022-09-30 14:01:42 +00:00
|
|
|
setUp: func(api *apic) {},
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "with some metrics",
|
2022-05-17 10:14:59 +00:00
|
|
|
duration: time.Millisecond * 30,
|
|
|
|
metricsInterval: time.Millisecond * 5,
|
2022-03-29 12:20:26 +00:00
|
|
|
expectedCalls: 5,
|
2022-09-30 14:01:42 +00:00
|
|
|
setUp: func(api *apic) {
|
|
|
|
api.dbClient.Ent.Machine.Delete().ExecX(context.Background())
|
2022-03-29 12:20:26 +00:00
|
|
|
api.dbClient.Ent.Machine.Create().
|
|
|
|
SetMachineId("1234").
|
|
|
|
SetPassword(testPassword.String()).
|
|
|
|
SetIpAddress("1.2.3.4").
|
|
|
|
SetScenarios("crowdsecurity/test").
|
|
|
|
SetLastPush(time.Time{}).
|
|
|
|
SetUpdatedAt(time.Time{}).
|
|
|
|
ExecX(context.Background())
|
|
|
|
|
2022-09-30 14:01:42 +00:00
|
|
|
api.dbClient.Ent.Bouncer.Delete().ExecX(context.Background())
|
2022-03-29 12:20:26 +00:00
|
|
|
api.dbClient.Ent.Bouncer.Create().
|
|
|
|
SetIPAddress("1.2.3.6").
|
|
|
|
SetName("someBouncer").
|
|
|
|
SetAPIKey("foobar").
|
|
|
|
SetRevoked(false).
|
|
|
|
SetLastPull(time.Time{}).
|
|
|
|
ExecX(context.Background())
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-09-30 14:01:42 +00:00
|
|
|
|
|
|
|
httpmock.RegisterResponder("POST", "http://api.crowdsec.net/api/metrics/", httpmock.NewBytesResponder(200, []byte{}))
|
|
|
|
httpmock.Activate()
|
|
|
|
defer httpmock.Deactivate()
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-03-29 12:20:26 +00:00
|
|
|
url, err := url.ParseRequestURI("http://api.crowdsec.net/")
|
2022-09-30 14:01:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
apiClient, err := apiclient.NewDefaultClient(
|
2022-03-29 12:20:26 +00:00
|
|
|
url,
|
|
|
|
"/api",
|
2023-05-23 08:52:47 +00:00
|
|
|
fmt.Sprintf("crowdsec/%s", version.String()),
|
2022-03-29 12:20:26 +00:00
|
|
|
nil,
|
|
|
|
)
|
2022-09-30 14:01:42 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-29 12:20:26 +00:00
|
|
|
|
2022-09-30 14:01:42 +00:00
|
|
|
api := getAPIC(t)
|
|
|
|
api.pushInterval = time.Millisecond
|
2022-10-28 11:55:59 +00:00
|
|
|
api.pushIntervalFirst = time.Millisecond
|
2022-09-30 14:01:42 +00:00
|
|
|
api.apiClient = apiClient
|
|
|
|
api.metricsInterval = tc.metricsInterval
|
2022-10-28 11:55:59 +00:00
|
|
|
api.metricsIntervalFirst = tc.metricsInterval
|
2022-09-30 14:01:42 +00:00
|
|
|
tc.setUp(api)
|
|
|
|
|
|
|
|
stop := make(chan bool)
|
|
|
|
httpmock.ZeroCallCounters()
|
|
|
|
go api.SendMetrics(stop)
|
|
|
|
time.Sleep(tc.duration)
|
|
|
|
stop <- true
|
|
|
|
|
|
|
|
info := httpmock.GetCallCountInfo()
|
|
|
|
noResponderCalls := info["NO_RESPONDER"]
|
|
|
|
responderCalls := info["POST http://api.crowdsec.net/api/metrics/"]
|
|
|
|
assert.LessOrEqual(t, absDiff(tc.expectedCalls, responderCalls), 2)
|
|
|
|
assert.Zero(t, noResponderCalls)
|
2022-03-29 12:20:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPICPull(t *testing.T) {
|
|
|
|
api := getAPIC(t)
|
2022-09-30 14:01:42 +00:00
|
|
|
tests := []struct {
|
2022-03-29 12:20:26 +00:00
|
|
|
name string
|
|
|
|
setUp func()
|
|
|
|
expectedDecisionCount int
|
|
|
|
logContains string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "test pull if no scenarios are present",
|
|
|
|
setUp: func() {},
|
|
|
|
logContains: "scenario list is empty, will not pull yet",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test pull",
|
|
|
|
setUp: func() {
|
|
|
|
api.dbClient.Ent.Machine.Create().
|
|
|
|
SetMachineId("1.2.3.4").
|
|
|
|
SetPassword(testPassword.String()).
|
|
|
|
SetIpAddress("1.2.3.4").
|
|
|
|
SetScenarios("crowdsecurity/ssh-bf").
|
|
|
|
ExecX(context.Background())
|
|
|
|
},
|
|
|
|
expectedDecisionCount: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:01:42 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-03-29 12:20:26 +00:00
|
|
|
api = getAPIC(t)
|
|
|
|
api.pullInterval = time.Millisecond
|
2022-10-28 11:55:59 +00:00
|
|
|
api.pullIntervalFirst = time.Millisecond
|
2022-03-29 12:20:26 +00:00
|
|
|
url, err := url.ParseRequestURI("http://api.crowdsec.net/")
|
2022-09-30 14:01:42 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-29 12:20:26 +00:00
|
|
|
httpmock.Activate()
|
|
|
|
defer httpmock.DeactivateAndReset()
|
|
|
|
apic, err := apiclient.NewDefaultClient(
|
|
|
|
url,
|
|
|
|
"/api",
|
2023-05-23 08:52:47 +00:00
|
|
|
fmt.Sprintf("crowdsec/%s", version.String()),
|
2022-03-29 12:20:26 +00:00
|
|
|
nil,
|
|
|
|
)
|
2022-09-30 14:01:42 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-29 12:20:26 +00:00
|
|
|
api.apiClient = apic
|
|
|
|
httpmock.RegisterNoResponder(httpmock.NewBytesResponder(200, jsonMarshalX(
|
2023-02-06 13:06:14 +00:00
|
|
|
modelscapi.GetDecisionsStreamResponse{
|
|
|
|
New: modelscapi.GetDecisionsStreamResponseNew{
|
|
|
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
2023-05-25 13:37:44 +00:00
|
|
|
Scenario: ptr.Of("crowdsecurity/ssh-bf"),
|
|
|
|
Scope: ptr.Of("Ip"),
|
2023-02-06 13:06:14 +00:00
|
|
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
|
|
|
{
|
2023-05-25 13:37:44 +00:00
|
|
|
Value: ptr.Of("1.2.3.5"),
|
|
|
|
Duration: ptr.Of("24h"),
|
2023-02-06 13:06:14 +00:00
|
|
|
},
|
|
|
|
},
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)))
|
2022-09-30 14:01:42 +00:00
|
|
|
tc.setUp()
|
2022-03-29 12:20:26 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
go func() {
|
|
|
|
logrus.SetOutput(&buf)
|
|
|
|
if err := api.Pull(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
2022-05-17 10:14:59 +00:00
|
|
|
//Slightly long because the CI runner for windows are slow, and this can lead to random failure
|
|
|
|
time.Sleep(time.Millisecond * 500)
|
2022-03-29 12:20:26 +00:00
|
|
|
logrus.SetOutput(os.Stderr)
|
2022-09-30 14:01:42 +00:00
|
|
|
assert.Contains(t, buf.String(), tc.logContains)
|
|
|
|
assertTotalDecisionCount(t, api.dbClient, tc.expectedDecisionCount)
|
2022-03-29 12:20:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldShareAlert(t *testing.T) {
|
2022-09-30 14:01:42 +00:00
|
|
|
tests := []struct {
|
2022-03-29 12:20:26 +00:00
|
|
|
name string
|
|
|
|
consoleConfig *csconfig.ConsoleConfig
|
|
|
|
alert *models.Alert
|
|
|
|
expectedRet bool
|
|
|
|
expectedTrust string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "custom alert should be shared if config enables it",
|
|
|
|
consoleConfig: &csconfig.ConsoleConfig{
|
2023-05-25 13:37:44 +00:00
|
|
|
ShareCustomScenarios: ptr.Of(true),
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
2023-05-25 13:37:44 +00:00
|
|
|
alert: &models.Alert{Simulated: ptr.Of(false)},
|
2022-03-29 12:20:26 +00:00
|
|
|
expectedRet: true,
|
|
|
|
expectedTrust: "custom",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "custom alert should not be shared if config disables it",
|
|
|
|
consoleConfig: &csconfig.ConsoleConfig{
|
2023-05-25 13:37:44 +00:00
|
|
|
ShareCustomScenarios: ptr.Of(false),
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
2023-05-25 13:37:44 +00:00
|
|
|
alert: &models.Alert{Simulated: ptr.Of(false)},
|
2022-03-29 12:20:26 +00:00
|
|
|
expectedRet: false,
|
|
|
|
expectedTrust: "custom",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "manual alert should be shared if config enables it",
|
|
|
|
consoleConfig: &csconfig.ConsoleConfig{
|
2023-05-25 13:37:44 +00:00
|
|
|
ShareManualDecisions: ptr.Of(true),
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
alert: &models.Alert{
|
2023-05-25 13:37:44 +00:00
|
|
|
Simulated: ptr.Of(false),
|
|
|
|
Decisions: []*models.Decision{{Origin: ptr.Of(types.CscliOrigin)}},
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
expectedRet: true,
|
|
|
|
expectedTrust: "manual",
|
|
|
|
},
|
|
|
|
{
|
2022-04-27 09:04:12 +00:00
|
|
|
name: "manual alert should not be shared if config disables it",
|
2022-03-29 12:20:26 +00:00
|
|
|
consoleConfig: &csconfig.ConsoleConfig{
|
2023-05-25 13:37:44 +00:00
|
|
|
ShareManualDecisions: ptr.Of(false),
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
alert: &models.Alert{
|
2023-05-25 13:37:44 +00:00
|
|
|
Simulated: ptr.Of(false),
|
|
|
|
Decisions: []*models.Decision{{Origin: ptr.Of(types.CscliOrigin)}},
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
expectedRet: false,
|
|
|
|
expectedTrust: "manual",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "manual alert should be shared if config enables it",
|
|
|
|
consoleConfig: &csconfig.ConsoleConfig{
|
2023-05-25 13:37:44 +00:00
|
|
|
ShareTaintedScenarios: ptr.Of(true),
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
alert: &models.Alert{
|
2023-05-25 13:37:44 +00:00
|
|
|
Simulated: ptr.Of(false),
|
|
|
|
ScenarioHash: ptr.Of("whateverHash"),
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
expectedRet: true,
|
|
|
|
expectedTrust: "tainted",
|
|
|
|
},
|
|
|
|
{
|
2022-04-27 09:04:12 +00:00
|
|
|
name: "manual alert should not be shared if config disables it",
|
2022-03-29 12:20:26 +00:00
|
|
|
consoleConfig: &csconfig.ConsoleConfig{
|
2023-05-25 13:37:44 +00:00
|
|
|
ShareTaintedScenarios: ptr.Of(false),
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
alert: &models.Alert{
|
2023-05-25 13:37:44 +00:00
|
|
|
Simulated: ptr.Of(false),
|
|
|
|
ScenarioHash: ptr.Of("whateverHash"),
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
expectedRet: false,
|
|
|
|
expectedTrust: "tainted",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:01:42 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
ret := shouldShareAlert(tc.alert, tc.consoleConfig)
|
|
|
|
assert.Equal(t, tc.expectedRet, ret)
|
2022-03-29 12:20:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|