decouple bouncer dependencies: use go-cs-lib in test code (#2229)
This commit is contained in:
parent
364b833d67
commit
b2d3520519
1
.github/workflows/bats-sqlite-coverage.yml
vendored
1
.github/workflows/bats-sqlite-coverage.yml
vendored
|
@ -72,7 +72,6 @@ jobs:
|
||||||
-e '/plugins/notifications' \
|
-e '/plugins/notifications' \
|
||||||
-e '/pkg/protobufs' \
|
-e '/pkg/protobufs' \
|
||||||
-e '/pkg/cwversions' \
|
-e '/pkg/cwversions' \
|
||||||
-e '/pkg/cstest' \
|
|
||||||
-e '/pkg/models' \
|
-e '/pkg/models' \
|
||||||
< coverage-bats-raw.out \
|
< coverage-bats-raw.out \
|
||||||
> coverage-bats.out
|
> coverage-bats.out
|
||||||
|
|
|
@ -13,9 +13,10 @@ import (
|
||||||
tomb "gopkg.in/tomb.v2"
|
tomb "gopkg.in/tomb.v2"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
|
"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -9,9 +9,10 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
|
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
|
@ -11,7 +11,8 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
dockerTypes "github.com/docker/docker/api/types"
|
dockerTypes "github.com/docker/docker/api/types"
|
||||||
dockerContainer "github.com/docker/docker/api/types/container"
|
dockerContainer "github.com/docker/docker/api/types/container"
|
||||||
|
|
|
@ -13,8 +13,9 @@ import (
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"gopkg.in/tomb.v2"
|
"gopkg.in/tomb.v2"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
|
|
||||||
fileacquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/file"
|
fileacquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/file"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,8 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/sirupsen/logrus/hooks/test"
|
"github.com/sirupsen/logrus/hooks/test"
|
||||||
|
|
|
@ -8,7 +8,8 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
"github.com/segmentio/kafka-go"
|
"github.com/segmentio/kafka-go"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
|
@ -12,10 +12,11 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
"github.com/aws/aws-sdk-go/aws/session"
|
"github.com/aws/aws-sdk-go/aws/session"
|
||||||
"github.com/aws/aws-sdk-go/service/kinesis"
|
"github.com/aws/aws-sdk-go/service/kinesis"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
|
@ -4,8 +4,9 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPri(t *testing.T) {
|
func TestPri(t *testing.T) {
|
||||||
|
|
|
@ -7,7 +7,8 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"gopkg.in/tomb.v2"
|
"gopkg.in/tomb.v2"
|
||||||
|
|
|
@ -20,11 +20,12 @@ import (
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"gopkg.in/tomb.v2"
|
"gopkg.in/tomb.v2"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/ptr"
|
||||||
"github.com/crowdsecurity/go-cs-lib/pkg/version"
|
"github.com/crowdsecurity/go-cs-lib/pkg/version"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/database"
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/database/ent/decision"
|
"github.com/crowdsecurity/crowdsec/pkg/database/ent/decision"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/database/ent/machine"
|
"github.com/crowdsecurity/crowdsec/pkg/database/ent/machine"
|
||||||
|
@ -60,10 +61,10 @@ func getAPIC(t *testing.T) *apic {
|
||||||
metricsTomb: tomb.Tomb{},
|
metricsTomb: tomb.Tomb{},
|
||||||
scenarioList: make([]string, 0),
|
scenarioList: make([]string, 0),
|
||||||
consoleConfig: &csconfig.ConsoleConfig{
|
consoleConfig: &csconfig.ConsoleConfig{
|
||||||
ShareManualDecisions: types.BoolPtr(false),
|
ShareManualDecisions: ptr.Of(false),
|
||||||
ShareTaintedScenarios: types.BoolPtr(false),
|
ShareTaintedScenarios: ptr.Of(false),
|
||||||
ShareCustomScenarios: types.BoolPtr(false),
|
ShareCustomScenarios: ptr.Of(false),
|
||||||
ShareContext: types.BoolPtr(false),
|
ShareContext: ptr.Of(false),
|
||||||
},
|
},
|
||||||
isPulling: make(chan bool, 1),
|
isPulling: make(chan bool, 1),
|
||||||
}
|
}
|
||||||
|
@ -266,11 +267,11 @@ func TestAPICHandleDeletedDecisions(t *testing.T) {
|
||||||
assertTotalDecisionCount(t, api.dbClient, 2)
|
assertTotalDecisionCount(t, api.dbClient, 2)
|
||||||
|
|
||||||
nbDeleted, err := api.HandleDeletedDecisions([]*models.Decision{{
|
nbDeleted, err := api.HandleDeletedDecisions([]*models.Decision{{
|
||||||
Value: types.StrPtr("1.2.3.4"),
|
Value: ptr.Of("1.2.3.4"),
|
||||||
Origin: types.StrPtr(types.CAPIOrigin),
|
Origin: ptr.Of(types.CAPIOrigin),
|
||||||
Type: &decision1.Type,
|
Type: &decision1.Type,
|
||||||
Scenario: types.StrPtr("crowdsec/test"),
|
Scenario: ptr.Of("crowdsec/test"),
|
||||||
Scope: types.StrPtr("IP"),
|
Scope: ptr.Of("IP"),
|
||||||
}}, deleteCounters)
|
}}, deleteCounters)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
@ -294,7 +295,7 @@ func TestAPICGetMetrics(t *testing.T) {
|
||||||
machineIDs: []string{},
|
machineIDs: []string{},
|
||||||
bouncers: []string{},
|
bouncers: []string{},
|
||||||
expectedMetric: &models.Metrics{
|
expectedMetric: &models.Metrics{
|
||||||
ApilVersion: types.StrPtr(version.String()),
|
ApilVersion: ptr.Of(version.String()),
|
||||||
Bouncers: []*models.MetricsBouncerInfo{},
|
Bouncers: []*models.MetricsBouncerInfo{},
|
||||||
Machines: []*models.MetricsAgentInfo{},
|
Machines: []*models.MetricsAgentInfo{},
|
||||||
},
|
},
|
||||||
|
@ -304,7 +305,7 @@ func TestAPICGetMetrics(t *testing.T) {
|
||||||
machineIDs: []string{"a", "b", "c"},
|
machineIDs: []string{"a", "b", "c"},
|
||||||
bouncers: []string{"1", "2", "3"},
|
bouncers: []string{"1", "2", "3"},
|
||||||
expectedMetric: &models.Metrics{
|
expectedMetric: &models.Metrics{
|
||||||
ApilVersion: types.StrPtr(version.String()),
|
ApilVersion: ptr.Of(version.String()),
|
||||||
Bouncers: []*models.MetricsBouncerInfo{
|
Bouncers: []*models.MetricsBouncerInfo{
|
||||||
{
|
{
|
||||||
CustomName: "1",
|
CustomName: "1",
|
||||||
|
@ -375,23 +376,23 @@ func TestAPICGetMetrics(t *testing.T) {
|
||||||
|
|
||||||
func TestCreateAlertsForDecision(t *testing.T) {
|
func TestCreateAlertsForDecision(t *testing.T) {
|
||||||
httpBfDecisionList := &models.Decision{
|
httpBfDecisionList := &models.Decision{
|
||||||
Origin: types.StrPtr(types.ListOrigin),
|
Origin: ptr.Of(types.ListOrigin),
|
||||||
Scenario: types.StrPtr("crowdsecurity/http-bf"),
|
Scenario: ptr.Of("crowdsecurity/http-bf"),
|
||||||
}
|
}
|
||||||
|
|
||||||
sshBfDecisionList := &models.Decision{
|
sshBfDecisionList := &models.Decision{
|
||||||
Origin: types.StrPtr(types.ListOrigin),
|
Origin: ptr.Of(types.ListOrigin),
|
||||||
Scenario: types.StrPtr("crowdsecurity/ssh-bf"),
|
Scenario: ptr.Of("crowdsecurity/ssh-bf"),
|
||||||
}
|
}
|
||||||
|
|
||||||
httpBfDecisionCommunity := &models.Decision{
|
httpBfDecisionCommunity := &models.Decision{
|
||||||
Origin: types.StrPtr(types.CAPIOrigin),
|
Origin: ptr.Of(types.CAPIOrigin),
|
||||||
Scenario: types.StrPtr("crowdsecurity/http-bf"),
|
Scenario: ptr.Of("crowdsecurity/http-bf"),
|
||||||
}
|
}
|
||||||
|
|
||||||
sshBfDecisionCommunity := &models.Decision{
|
sshBfDecisionCommunity := &models.Decision{
|
||||||
Origin: types.StrPtr(types.CAPIOrigin),
|
Origin: ptr.Of(types.CAPIOrigin),
|
||||||
Scenario: types.StrPtr("crowdsecurity/ssh-bf"),
|
Scenario: ptr.Of("crowdsecurity/ssh-bf"),
|
||||||
}
|
}
|
||||||
type args struct {
|
type args struct {
|
||||||
decisions []*models.Decision
|
decisions []*models.Decision
|
||||||
|
@ -454,27 +455,27 @@ func TestCreateAlertsForDecision(t *testing.T) {
|
||||||
|
|
||||||
func TestFillAlertsWithDecisions(t *testing.T) {
|
func TestFillAlertsWithDecisions(t *testing.T) {
|
||||||
httpBfDecisionCommunity := &models.Decision{
|
httpBfDecisionCommunity := &models.Decision{
|
||||||
Origin: types.StrPtr(types.CAPIOrigin),
|
Origin: ptr.Of(types.CAPIOrigin),
|
||||||
Scenario: types.StrPtr("crowdsecurity/http-bf"),
|
Scenario: ptr.Of("crowdsecurity/http-bf"),
|
||||||
Scope: types.StrPtr("ip"),
|
Scope: ptr.Of("ip"),
|
||||||
}
|
}
|
||||||
|
|
||||||
sshBfDecisionCommunity := &models.Decision{
|
sshBfDecisionCommunity := &models.Decision{
|
||||||
Origin: types.StrPtr(types.CAPIOrigin),
|
Origin: ptr.Of(types.CAPIOrigin),
|
||||||
Scenario: types.StrPtr("crowdsecurity/ssh-bf"),
|
Scenario: ptr.Of("crowdsecurity/ssh-bf"),
|
||||||
Scope: types.StrPtr("ip"),
|
Scope: ptr.Of("ip"),
|
||||||
}
|
}
|
||||||
|
|
||||||
httpBfDecisionList := &models.Decision{
|
httpBfDecisionList := &models.Decision{
|
||||||
Origin: types.StrPtr(types.ListOrigin),
|
Origin: ptr.Of(types.ListOrigin),
|
||||||
Scenario: types.StrPtr("crowdsecurity/http-bf"),
|
Scenario: ptr.Of("crowdsecurity/http-bf"),
|
||||||
Scope: types.StrPtr("ip"),
|
Scope: ptr.Of("ip"),
|
||||||
}
|
}
|
||||||
|
|
||||||
sshBfDecisionList := &models.Decision{
|
sshBfDecisionList := &models.Decision{
|
||||||
Origin: types.StrPtr(types.ListOrigin),
|
Origin: ptr.Of(types.ListOrigin),
|
||||||
Scenario: types.StrPtr("crowdsecurity/ssh-bf"),
|
Scenario: ptr.Of("crowdsecurity/ssh-bf"),
|
||||||
Scope: types.StrPtr("ip"),
|
Scope: ptr.Of("ip"),
|
||||||
}
|
}
|
||||||
type args struct {
|
type args struct {
|
||||||
alerts []*models.Alert
|
alerts []*models.Alert
|
||||||
|
@ -573,58 +574,58 @@ func TestAPICWhitelists(t *testing.T) {
|
||||||
"9.9.9.9", // This is already present in DB
|
"9.9.9.9", // This is already present in DB
|
||||||
"9.1.9.9", // This not present in DB
|
"9.1.9.9", // This not present in DB
|
||||||
},
|
},
|
||||||
Scope: types.StrPtr("Ip"),
|
Scope: ptr.Of("Ip"),
|
||||||
}, // This is already present in DB
|
}, // This is already present in DB
|
||||||
},
|
},
|
||||||
New: modelscapi.GetDecisionsStreamResponseNew{
|
New: modelscapi.GetDecisionsStreamResponseNew{
|
||||||
&modelscapi.GetDecisionsStreamResponseNewItem{
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
||||||
Scenario: types.StrPtr("crowdsecurity/test1"),
|
Scenario: ptr.Of("crowdsecurity/test1"),
|
||||||
Scope: types.StrPtr("Ip"),
|
Scope: ptr.Of("Ip"),
|
||||||
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
||||||
{
|
{
|
||||||
Value: types.StrPtr("13.2.3.4"), //wl by cidr
|
Value: ptr.Of("13.2.3.4"), //wl by cidr
|
||||||
Duration: types.StrPtr("24h"),
|
Duration: ptr.Of("24h"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
&modelscapi.GetDecisionsStreamResponseNewItem{
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
||||||
Scenario: types.StrPtr("crowdsecurity/test1"),
|
Scenario: ptr.Of("crowdsecurity/test1"),
|
||||||
Scope: types.StrPtr("Ip"),
|
Scope: ptr.Of("Ip"),
|
||||||
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
||||||
{
|
{
|
||||||
Value: types.StrPtr("2.2.3.4"),
|
Value: ptr.Of("2.2.3.4"),
|
||||||
Duration: types.StrPtr("24h"),
|
Duration: ptr.Of("24h"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
&modelscapi.GetDecisionsStreamResponseNewItem{
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
||||||
Scenario: types.StrPtr("crowdsecurity/test2"),
|
Scenario: ptr.Of("crowdsecurity/test2"),
|
||||||
Scope: types.StrPtr("Ip"),
|
Scope: ptr.Of("Ip"),
|
||||||
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
||||||
{
|
{
|
||||||
Value: types.StrPtr("13.2.3.5"), //wl by cidr
|
Value: ptr.Of("13.2.3.5"), //wl by cidr
|
||||||
Duration: types.StrPtr("24h"),
|
Duration: ptr.Of("24h"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, // These two are from community list.
|
}, // These two are from community list.
|
||||||
&modelscapi.GetDecisionsStreamResponseNewItem{
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
||||||
Scenario: types.StrPtr("crowdsecurity/test1"),
|
Scenario: ptr.Of("crowdsecurity/test1"),
|
||||||
Scope: types.StrPtr("Ip"),
|
Scope: ptr.Of("Ip"),
|
||||||
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
||||||
{
|
{
|
||||||
Value: types.StrPtr("6.2.3.4"),
|
Value: ptr.Of("6.2.3.4"),
|
||||||
Duration: types.StrPtr("24h"),
|
Duration: ptr.Of("24h"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
&modelscapi.GetDecisionsStreamResponseNewItem{
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
||||||
Scenario: types.StrPtr("crowdsecurity/test1"),
|
Scenario: ptr.Of("crowdsecurity/test1"),
|
||||||
Scope: types.StrPtr("Ip"),
|
Scope: ptr.Of("Ip"),
|
||||||
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
||||||
{
|
{
|
||||||
Value: types.StrPtr("9.2.3.4"), //wl by ip
|
Value: ptr.Of("9.2.3.4"), //wl by ip
|
||||||
Duration: types.StrPtr("24h"),
|
Duration: ptr.Of("24h"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -632,18 +633,18 @@ func TestAPICWhitelists(t *testing.T) {
|
||||||
Links: &modelscapi.GetDecisionsStreamResponseLinks{
|
Links: &modelscapi.GetDecisionsStreamResponseLinks{
|
||||||
Blocklists: []*modelscapi.BlocklistLink{
|
Blocklists: []*modelscapi.BlocklistLink{
|
||||||
{
|
{
|
||||||
URL: types.StrPtr("http://api.crowdsec.net/blocklist1"),
|
URL: ptr.Of("http://api.crowdsec.net/blocklist1"),
|
||||||
Name: types.StrPtr("blocklist1"),
|
Name: ptr.Of("blocklist1"),
|
||||||
Scope: types.StrPtr("Ip"),
|
Scope: ptr.Of("Ip"),
|
||||||
Remediation: types.StrPtr("ban"),
|
Remediation: ptr.Of("ban"),
|
||||||
Duration: types.StrPtr("24h"),
|
Duration: ptr.Of("24h"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
URL: types.StrPtr("http://api.crowdsec.net/blocklist2"),
|
URL: ptr.Of("http://api.crowdsec.net/blocklist2"),
|
||||||
Name: types.StrPtr("blocklist2"),
|
Name: ptr.Of("blocklist2"),
|
||||||
Scope: types.StrPtr("Ip"),
|
Scope: ptr.Of("Ip"),
|
||||||
Remediation: types.StrPtr("ban"),
|
Remediation: ptr.Of("ban"),
|
||||||
Duration: types.StrPtr("24h"),
|
Duration: ptr.Of("24h"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -735,27 +736,27 @@ func TestAPICPullTop(t *testing.T) {
|
||||||
"9.9.9.9", // This is already present in DB
|
"9.9.9.9", // This is already present in DB
|
||||||
"9.1.9.9", // This not present in DB
|
"9.1.9.9", // This not present in DB
|
||||||
},
|
},
|
||||||
Scope: types.StrPtr("Ip"),
|
Scope: ptr.Of("Ip"),
|
||||||
}, // This is already present in DB
|
}, // This is already present in DB
|
||||||
},
|
},
|
||||||
New: modelscapi.GetDecisionsStreamResponseNew{
|
New: modelscapi.GetDecisionsStreamResponseNew{
|
||||||
&modelscapi.GetDecisionsStreamResponseNewItem{
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
||||||
Scenario: types.StrPtr("crowdsecurity/test1"),
|
Scenario: ptr.Of("crowdsecurity/test1"),
|
||||||
Scope: types.StrPtr("Ip"),
|
Scope: ptr.Of("Ip"),
|
||||||
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
||||||
{
|
{
|
||||||
Value: types.StrPtr("1.2.3.4"),
|
Value: ptr.Of("1.2.3.4"),
|
||||||
Duration: types.StrPtr("24h"),
|
Duration: ptr.Of("24h"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
&modelscapi.GetDecisionsStreamResponseNewItem{
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
||||||
Scenario: types.StrPtr("crowdsecurity/test2"),
|
Scenario: ptr.Of("crowdsecurity/test2"),
|
||||||
Scope: types.StrPtr("Ip"),
|
Scope: ptr.Of("Ip"),
|
||||||
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
||||||
{
|
{
|
||||||
Value: types.StrPtr("1.2.3.5"),
|
Value: ptr.Of("1.2.3.5"),
|
||||||
Duration: types.StrPtr("24h"),
|
Duration: ptr.Of("24h"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, // These two are from community list.
|
}, // These two are from community list.
|
||||||
|
@ -763,18 +764,18 @@ func TestAPICPullTop(t *testing.T) {
|
||||||
Links: &modelscapi.GetDecisionsStreamResponseLinks{
|
Links: &modelscapi.GetDecisionsStreamResponseLinks{
|
||||||
Blocklists: []*modelscapi.BlocklistLink{
|
Blocklists: []*modelscapi.BlocklistLink{
|
||||||
{
|
{
|
||||||
URL: types.StrPtr("http://api.crowdsec.net/blocklist1"),
|
URL: ptr.Of("http://api.crowdsec.net/blocklist1"),
|
||||||
Name: types.StrPtr("blocklist1"),
|
Name: ptr.Of("blocklist1"),
|
||||||
Scope: types.StrPtr("Ip"),
|
Scope: ptr.Of("Ip"),
|
||||||
Remediation: types.StrPtr("ban"),
|
Remediation: ptr.Of("ban"),
|
||||||
Duration: types.StrPtr("24h"),
|
Duration: ptr.Of("24h"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
URL: types.StrPtr("http://api.crowdsec.net/blocklist2"),
|
URL: ptr.Of("http://api.crowdsec.net/blocklist2"),
|
||||||
Name: types.StrPtr("blocklist2"),
|
Name: ptr.Of("blocklist2"),
|
||||||
Scope: types.StrPtr("Ip"),
|
Scope: ptr.Of("Ip"),
|
||||||
Remediation: types.StrPtr("ban"),
|
Remediation: ptr.Of("ban"),
|
||||||
Duration: types.StrPtr("24h"),
|
Duration: ptr.Of("24h"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -841,12 +842,12 @@ func TestAPICPullTopBLCacheFirstCall(t *testing.T) {
|
||||||
modelscapi.GetDecisionsStreamResponse{
|
modelscapi.GetDecisionsStreamResponse{
|
||||||
New: modelscapi.GetDecisionsStreamResponseNew{
|
New: modelscapi.GetDecisionsStreamResponseNew{
|
||||||
&modelscapi.GetDecisionsStreamResponseNewItem{
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
||||||
Scenario: types.StrPtr("crowdsecurity/test1"),
|
Scenario: ptr.Of("crowdsecurity/test1"),
|
||||||
Scope: types.StrPtr("Ip"),
|
Scope: ptr.Of("Ip"),
|
||||||
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
||||||
{
|
{
|
||||||
Value: types.StrPtr("1.2.3.4"),
|
Value: ptr.Of("1.2.3.4"),
|
||||||
Duration: types.StrPtr("24h"),
|
Duration: ptr.Of("24h"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -854,11 +855,11 @@ func TestAPICPullTopBLCacheFirstCall(t *testing.T) {
|
||||||
Links: &modelscapi.GetDecisionsStreamResponseLinks{
|
Links: &modelscapi.GetDecisionsStreamResponseLinks{
|
||||||
Blocklists: []*modelscapi.BlocklistLink{
|
Blocklists: []*modelscapi.BlocklistLink{
|
||||||
{
|
{
|
||||||
URL: types.StrPtr("http://api.crowdsec.net/blocklist1"),
|
URL: ptr.Of("http://api.crowdsec.net/blocklist1"),
|
||||||
Name: types.StrPtr("blocklist1"),
|
Name: ptr.Of("blocklist1"),
|
||||||
Scope: types.StrPtr("Ip"),
|
Scope: ptr.Of("Ip"),
|
||||||
Remediation: types.StrPtr("ban"),
|
Remediation: ptr.Of("ban"),
|
||||||
Duration: types.StrPtr("24h"),
|
Duration: ptr.Of("24h"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -884,7 +885,7 @@ func TestAPICPullTopBLCacheFirstCall(t *testing.T) {
|
||||||
err = api.PullTop(false)
|
err = api.PullTop(false)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
blocklistConfigItemName := fmt.Sprintf("blocklist:%s:last_pull", *types.StrPtr("blocklist1"))
|
blocklistConfigItemName := "blocklist:blocklist1:last_pull"
|
||||||
lastPullTimestamp, err := api.dbClient.GetConfigItem(blocklistConfigItemName)
|
lastPullTimestamp, err := api.dbClient.GetConfigItem(blocklistConfigItemName)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.NotEqual(t, "", *lastPullTimestamp)
|
assert.NotEqual(t, "", *lastPullTimestamp)
|
||||||
|
@ -928,12 +929,12 @@ func TestAPICPullTopBLCacheForceCall(t *testing.T) {
|
||||||
modelscapi.GetDecisionsStreamResponse{
|
modelscapi.GetDecisionsStreamResponse{
|
||||||
New: modelscapi.GetDecisionsStreamResponseNew{
|
New: modelscapi.GetDecisionsStreamResponseNew{
|
||||||
&modelscapi.GetDecisionsStreamResponseNewItem{
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
||||||
Scenario: types.StrPtr("crowdsecurity/test1"),
|
Scenario: ptr.Of("crowdsecurity/test1"),
|
||||||
Scope: types.StrPtr("Ip"),
|
Scope: ptr.Of("Ip"),
|
||||||
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
||||||
{
|
{
|
||||||
Value: types.StrPtr("1.2.3.4"),
|
Value: ptr.Of("1.2.3.4"),
|
||||||
Duration: types.StrPtr("24h"),
|
Duration: ptr.Of("24h"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -941,11 +942,11 @@ func TestAPICPullTopBLCacheForceCall(t *testing.T) {
|
||||||
Links: &modelscapi.GetDecisionsStreamResponseLinks{
|
Links: &modelscapi.GetDecisionsStreamResponseLinks{
|
||||||
Blocklists: []*modelscapi.BlocklistLink{
|
Blocklists: []*modelscapi.BlocklistLink{
|
||||||
{
|
{
|
||||||
URL: types.StrPtr("http://api.crowdsec.net/blocklist1"),
|
URL: ptr.Of("http://api.crowdsec.net/blocklist1"),
|
||||||
Name: types.StrPtr("blocklist1"),
|
Name: ptr.Of("blocklist1"),
|
||||||
Scope: types.StrPtr("Ip"),
|
Scope: ptr.Of("Ip"),
|
||||||
Remediation: types.StrPtr("ban"),
|
Remediation: ptr.Of("ban"),
|
||||||
Duration: types.StrPtr("24h"),
|
Duration: ptr.Of("24h"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -982,10 +983,10 @@ func TestAPICPush(t *testing.T) {
|
||||||
name: "simple single alert",
|
name: "simple single alert",
|
||||||
alerts: []*models.Alert{
|
alerts: []*models.Alert{
|
||||||
{
|
{
|
||||||
Scenario: types.StrPtr("crowdsec/test"),
|
Scenario: ptr.Of("crowdsec/test"),
|
||||||
ScenarioHash: types.StrPtr("certified"),
|
ScenarioHash: ptr.Of("certified"),
|
||||||
ScenarioVersion: types.StrPtr("v1.0"),
|
ScenarioVersion: ptr.Of("v1.0"),
|
||||||
Simulated: types.BoolPtr(false),
|
Simulated: ptr.Of(false),
|
||||||
Source: &models.Source{},
|
Source: &models.Source{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -995,10 +996,10 @@ func TestAPICPush(t *testing.T) {
|
||||||
name: "simulated alert is not pushed",
|
name: "simulated alert is not pushed",
|
||||||
alerts: []*models.Alert{
|
alerts: []*models.Alert{
|
||||||
{
|
{
|
||||||
Scenario: types.StrPtr("crowdsec/test"),
|
Scenario: ptr.Of("crowdsec/test"),
|
||||||
ScenarioHash: types.StrPtr("certified"),
|
ScenarioHash: ptr.Of("certified"),
|
||||||
ScenarioVersion: types.StrPtr("v1.0"),
|
ScenarioVersion: ptr.Of("v1.0"),
|
||||||
Simulated: types.BoolPtr(true),
|
Simulated: ptr.Of(true),
|
||||||
Source: &models.Source{},
|
Source: &models.Source{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1011,10 +1012,10 @@ func TestAPICPush(t *testing.T) {
|
||||||
alerts := make([]*models.Alert, 100)
|
alerts := make([]*models.Alert, 100)
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < 100; i++ {
|
||||||
alerts[i] = &models.Alert{
|
alerts[i] = &models.Alert{
|
||||||
Scenario: types.StrPtr("crowdsec/test"),
|
Scenario: ptr.Of("crowdsec/test"),
|
||||||
ScenarioHash: types.StrPtr("certified"),
|
ScenarioHash: ptr.Of("certified"),
|
||||||
ScenarioVersion: types.StrPtr("v1.0"),
|
ScenarioVersion: ptr.Of("v1.0"),
|
||||||
Simulated: types.BoolPtr(false),
|
Simulated: ptr.Of(false),
|
||||||
Source: &models.Source{},
|
Source: &models.Source{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1189,12 +1190,12 @@ func TestAPICPull(t *testing.T) {
|
||||||
modelscapi.GetDecisionsStreamResponse{
|
modelscapi.GetDecisionsStreamResponse{
|
||||||
New: modelscapi.GetDecisionsStreamResponseNew{
|
New: modelscapi.GetDecisionsStreamResponseNew{
|
||||||
&modelscapi.GetDecisionsStreamResponseNewItem{
|
&modelscapi.GetDecisionsStreamResponseNewItem{
|
||||||
Scenario: types.StrPtr("crowdsecurity/ssh-bf"),
|
Scenario: ptr.Of("crowdsecurity/ssh-bf"),
|
||||||
Scope: types.StrPtr("Ip"),
|
Scope: ptr.Of("Ip"),
|
||||||
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
Decisions: []*modelscapi.GetDecisionsStreamResponseNewItemDecisionsItems0{
|
||||||
{
|
{
|
||||||
Value: types.StrPtr("1.2.3.5"),
|
Value: ptr.Of("1.2.3.5"),
|
||||||
Duration: types.StrPtr("24h"),
|
Duration: ptr.Of("24h"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1229,29 +1230,29 @@ func TestShouldShareAlert(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "custom alert should be shared if config enables it",
|
name: "custom alert should be shared if config enables it",
|
||||||
consoleConfig: &csconfig.ConsoleConfig{
|
consoleConfig: &csconfig.ConsoleConfig{
|
||||||
ShareCustomScenarios: types.BoolPtr(true),
|
ShareCustomScenarios: ptr.Of(true),
|
||||||
},
|
},
|
||||||
alert: &models.Alert{Simulated: types.BoolPtr(false)},
|
alert: &models.Alert{Simulated: ptr.Of(false)},
|
||||||
expectedRet: true,
|
expectedRet: true,
|
||||||
expectedTrust: "custom",
|
expectedTrust: "custom",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "custom alert should not be shared if config disables it",
|
name: "custom alert should not be shared if config disables it",
|
||||||
consoleConfig: &csconfig.ConsoleConfig{
|
consoleConfig: &csconfig.ConsoleConfig{
|
||||||
ShareCustomScenarios: types.BoolPtr(false),
|
ShareCustomScenarios: ptr.Of(false),
|
||||||
},
|
},
|
||||||
alert: &models.Alert{Simulated: types.BoolPtr(false)},
|
alert: &models.Alert{Simulated: ptr.Of(false)},
|
||||||
expectedRet: false,
|
expectedRet: false,
|
||||||
expectedTrust: "custom",
|
expectedTrust: "custom",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "manual alert should be shared if config enables it",
|
name: "manual alert should be shared if config enables it",
|
||||||
consoleConfig: &csconfig.ConsoleConfig{
|
consoleConfig: &csconfig.ConsoleConfig{
|
||||||
ShareManualDecisions: types.BoolPtr(true),
|
ShareManualDecisions: ptr.Of(true),
|
||||||
},
|
},
|
||||||
alert: &models.Alert{
|
alert: &models.Alert{
|
||||||
Simulated: types.BoolPtr(false),
|
Simulated: ptr.Of(false),
|
||||||
Decisions: []*models.Decision{{Origin: types.StrPtr(types.CscliOrigin)}},
|
Decisions: []*models.Decision{{Origin: ptr.Of(types.CscliOrigin)}},
|
||||||
},
|
},
|
||||||
expectedRet: true,
|
expectedRet: true,
|
||||||
expectedTrust: "manual",
|
expectedTrust: "manual",
|
||||||
|
@ -1259,11 +1260,11 @@ func TestShouldShareAlert(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "manual alert should not be shared if config disables it",
|
name: "manual alert should not be shared if config disables it",
|
||||||
consoleConfig: &csconfig.ConsoleConfig{
|
consoleConfig: &csconfig.ConsoleConfig{
|
||||||
ShareManualDecisions: types.BoolPtr(false),
|
ShareManualDecisions: ptr.Of(false),
|
||||||
},
|
},
|
||||||
alert: &models.Alert{
|
alert: &models.Alert{
|
||||||
Simulated: types.BoolPtr(false),
|
Simulated: ptr.Of(false),
|
||||||
Decisions: []*models.Decision{{Origin: types.StrPtr(types.CscliOrigin)}},
|
Decisions: []*models.Decision{{Origin: ptr.Of(types.CscliOrigin)}},
|
||||||
},
|
},
|
||||||
expectedRet: false,
|
expectedRet: false,
|
||||||
expectedTrust: "manual",
|
expectedTrust: "manual",
|
||||||
|
@ -1271,11 +1272,11 @@ func TestShouldShareAlert(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "manual alert should be shared if config enables it",
|
name: "manual alert should be shared if config enables it",
|
||||||
consoleConfig: &csconfig.ConsoleConfig{
|
consoleConfig: &csconfig.ConsoleConfig{
|
||||||
ShareTaintedScenarios: types.BoolPtr(true),
|
ShareTaintedScenarios: ptr.Of(true),
|
||||||
},
|
},
|
||||||
alert: &models.Alert{
|
alert: &models.Alert{
|
||||||
Simulated: types.BoolPtr(false),
|
Simulated: ptr.Of(false),
|
||||||
ScenarioHash: types.StrPtr("whateverHash"),
|
ScenarioHash: ptr.Of("whateverHash"),
|
||||||
},
|
},
|
||||||
expectedRet: true,
|
expectedRet: true,
|
||||||
expectedTrust: "tainted",
|
expectedTrust: "tainted",
|
||||||
|
@ -1283,11 +1284,11 @@ func TestShouldShareAlert(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "manual alert should not be shared if config disables it",
|
name: "manual alert should not be shared if config disables it",
|
||||||
consoleConfig: &csconfig.ConsoleConfig{
|
consoleConfig: &csconfig.ConsoleConfig{
|
||||||
ShareTaintedScenarios: types.BoolPtr(false),
|
ShareTaintedScenarios: ptr.Of(false),
|
||||||
},
|
},
|
||||||
alert: &models.Alert{
|
alert: &models.Alert{
|
||||||
Simulated: types.BoolPtr(false),
|
Simulated: ptr.Of(false),
|
||||||
ScenarioHash: types.StrPtr("whateverHash"),
|
ScenarioHash: ptr.Of("whateverHash"),
|
||||||
},
|
},
|
||||||
expectedRet: false,
|
expectedRet: false,
|
||||||
expectedTrust: "tainted",
|
expectedTrust: "tainted",
|
||||||
|
|
|
@ -7,12 +7,12 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/ptr"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLoadLocalApiClientCfg(t *testing.T) {
|
func TestLoadLocalApiClientCfg(t *testing.T) {
|
||||||
|
@ -53,7 +53,7 @@ func TestLoadLocalApiClientCfg(t *testing.T) {
|
||||||
name: "valid configuration with insecure skip verify",
|
name: "valid configuration with insecure skip verify",
|
||||||
input: &LocalApiClientCfg{
|
input: &LocalApiClientCfg{
|
||||||
CredentialsFilePath: "./tests/lapi-secrets.yaml",
|
CredentialsFilePath: "./tests/lapi-secrets.yaml",
|
||||||
InsecureSkipVerify: types.BoolPtr(false),
|
InsecureSkipVerify: ptr.Of(false),
|
||||||
},
|
},
|
||||||
expected: &ApiCredentialsCfg{
|
expected: &ApiCredentialsCfg{
|
||||||
URL: "http://localhost:8080/",
|
URL: "http://localhost:8080/",
|
||||||
|
@ -188,21 +188,21 @@ func TestLoadAPIServer(t *testing.T) {
|
||||||
DisableAPI: false,
|
DisableAPI: false,
|
||||||
},
|
},
|
||||||
expected: &LocalApiServerCfg{
|
expected: &LocalApiServerCfg{
|
||||||
Enable: types.BoolPtr(true),
|
Enable: ptr.Of(true),
|
||||||
ListenURI: "http://crowdsec.api",
|
ListenURI: "http://crowdsec.api",
|
||||||
TLS: nil,
|
TLS: nil,
|
||||||
DbConfig: &DatabaseCfg{
|
DbConfig: &DatabaseCfg{
|
||||||
DbPath: "./tests/test.db",
|
DbPath: "./tests/test.db",
|
||||||
Type: "sqlite",
|
Type: "sqlite",
|
||||||
MaxOpenConns: types.IntPtr(DEFAULT_MAX_OPEN_CONNS),
|
MaxOpenConns: ptr.Of(DEFAULT_MAX_OPEN_CONNS),
|
||||||
},
|
},
|
||||||
ConsoleConfigPath: DefaultConfigPath("console.yaml"),
|
ConsoleConfigPath: DefaultConfigPath("console.yaml"),
|
||||||
ConsoleConfig: &ConsoleConfig{
|
ConsoleConfig: &ConsoleConfig{
|
||||||
ShareManualDecisions: types.BoolPtr(false),
|
ShareManualDecisions: ptr.Of(false),
|
||||||
ShareTaintedScenarios: types.BoolPtr(true),
|
ShareTaintedScenarios: ptr.Of(true),
|
||||||
ShareCustomScenarios: types.BoolPtr(true),
|
ShareCustomScenarios: ptr.Of(true),
|
||||||
ShareContext: types.BoolPtr(false),
|
ShareContext: ptr.Of(false),
|
||||||
ConsoleManagement: types.BoolPtr(false),
|
ConsoleManagement: ptr.Of(false),
|
||||||
},
|
},
|
||||||
LogDir: LogDirFullPath,
|
LogDir: LogDirFullPath,
|
||||||
LogMedia: "stdout",
|
LogMedia: "stdout",
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNormalLoad(t *testing.T) {
|
func TestNormalLoad(t *testing.T) {
|
||||||
|
|
|
@ -5,13 +5,13 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/go-cs-lib/pkg/ptr"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLoadCrowdsec(t *testing.T) {
|
func TestLoadCrowdsec(t *testing.T) {
|
||||||
falseBoolPtr := false
|
|
||||||
acquisFullPath, err := filepath.Abs("./tests/acquis.yaml")
|
acquisFullPath, err := filepath.Abs("./tests/acquis.yaml")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ func TestLoadCrowdsec(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedResult: &CrowdsecServiceCfg{
|
expectedResult: &CrowdsecServiceCfg{
|
||||||
Enable: types.BoolPtr(true),
|
Enable: ptr.Of(true),
|
||||||
AcquisitionDirPath: "",
|
AcquisitionDirPath: "",
|
||||||
ConsoleContextPath: contextFileFullPath,
|
ConsoleContextPath: contextFileFullPath,
|
||||||
AcquisitionFilePath: acquisFullPath,
|
AcquisitionFilePath: acquisFullPath,
|
||||||
|
@ -81,7 +81,7 @@ func TestLoadCrowdsec(t *testing.T) {
|
||||||
"source_ip": {"evt.Parsed.source_ip"},
|
"source_ip": {"evt.Parsed.source_ip"},
|
||||||
},
|
},
|
||||||
SimulationConfig: &SimulationConfig{
|
SimulationConfig: &SimulationConfig{
|
||||||
Simulation: &falseBoolPtr,
|
Simulation: ptr.Of(false),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -106,7 +106,7 @@ func TestLoadCrowdsec(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedResult: &CrowdsecServiceCfg{
|
expectedResult: &CrowdsecServiceCfg{
|
||||||
Enable: types.BoolPtr(true),
|
Enable: ptr.Of(true),
|
||||||
AcquisitionDirPath: acquisDirFullPath,
|
AcquisitionDirPath: acquisDirFullPath,
|
||||||
AcquisitionFilePath: acquisFullPath,
|
AcquisitionFilePath: acquisFullPath,
|
||||||
ConsoleContextPath: contextFileFullPath,
|
ConsoleContextPath: contextFileFullPath,
|
||||||
|
@ -124,7 +124,7 @@ func TestLoadCrowdsec(t *testing.T) {
|
||||||
},
|
},
|
||||||
SimulationFilePath: "./tests/simulation.yaml",
|
SimulationFilePath: "./tests/simulation.yaml",
|
||||||
SimulationConfig: &SimulationConfig{
|
SimulationConfig: &SimulationConfig{
|
||||||
Simulation: &falseBoolPtr,
|
Simulation: ptr.Of(false),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -147,7 +147,7 @@ func TestLoadCrowdsec(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedResult: &CrowdsecServiceCfg{
|
expectedResult: &CrowdsecServiceCfg{
|
||||||
Enable: types.BoolPtr(true),
|
Enable: ptr.Of(true),
|
||||||
AcquisitionDirPath: "",
|
AcquisitionDirPath: "",
|
||||||
AcquisitionFilePath: "",
|
AcquisitionFilePath: "",
|
||||||
ConfigDir: configDirFullPath,
|
ConfigDir: configDirFullPath,
|
||||||
|
@ -165,7 +165,7 @@ func TestLoadCrowdsec(t *testing.T) {
|
||||||
"source_ip": {"evt.Parsed.source_ip"},
|
"source_ip": {"evt.Parsed.source_ip"},
|
||||||
},
|
},
|
||||||
SimulationConfig: &SimulationConfig{
|
SimulationConfig: &SimulationConfig{
|
||||||
Simulation: &falseBoolPtr,
|
Simulation: ptr.Of(false),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSimulationLoading(t *testing.T) {
|
func TestSimulationLoading(t *testing.T) {
|
||||||
|
|
|
@ -19,8 +19,9 @@ import (
|
||||||
"gopkg.in/tomb.v2"
|
"gopkg.in/tomb.v2"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -15,8 +15,9 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"gopkg.in/tomb.v2"
|
"gopkg.in/tomb.v2"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
//go:build unix || linux || freebsd || netbsd || openbsd || solaris
|
|
||||||
|
|
||||||
package cstest
|
|
||||||
|
|
||||||
const FileNotFoundMessage = "no such file or directory"
|
|
|
@ -1,5 +0,0 @@
|
||||||
//go:build windows
|
|
||||||
|
|
||||||
package cstest
|
|
||||||
|
|
||||||
const FileNotFoundMessage = "The system cannot find the file specified."
|
|
|
@ -1,99 +0,0 @@
|
||||||
package cstest
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"text/template"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
|
|
||||||
logtest "github.com/sirupsen/logrus/hooks/test"
|
|
||||||
)
|
|
||||||
|
|
||||||
func AssertErrorContains(t *testing.T, err error, expectedErr string) {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
if expectedErr != "" {
|
|
||||||
assert.ErrorContains(t, err, expectedErr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func AssertErrorMessage(t *testing.T, err error, expectedErr string) {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
if expectedErr != "" {
|
|
||||||
errmsg := ""
|
|
||||||
if err != nil {
|
|
||||||
errmsg = err.Error()
|
|
||||||
}
|
|
||||||
assert.Equal(t, expectedErr, errmsg)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func RequireErrorContains(t *testing.T, err error, expectedErr string) {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
if expectedErr != "" {
|
|
||||||
require.ErrorContains(t, err, expectedErr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func RequireErrorMessage(t *testing.T, err error, expectedErr string) {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
if expectedErr != "" {
|
|
||||||
errmsg := ""
|
|
||||||
if err != nil {
|
|
||||||
errmsg = err.Error()
|
|
||||||
}
|
|
||||||
require.Equal(t, expectedErr, errmsg)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func RequireLogContains(t *testing.T, hook *logtest.Hook, expected string) {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
// look for a log entry that matches the expected message
|
|
||||||
for _, entry := range hook.AllEntries() {
|
|
||||||
if strings.Contains(entry.Message, expected) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// show all hook entries, in case the test fails we'll need them
|
|
||||||
for _, entry := range hook.AllEntries() {
|
|
||||||
t.Logf("log entry: %s", entry.Message)
|
|
||||||
}
|
|
||||||
|
|
||||||
require.Fail(t, "no log entry found with message", expected)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Interpolate fills a string template with the given values, can be map or struct.
|
|
||||||
// example: Interpolate("{{.Name}}", map[string]string{"Name": "JohnDoe"})
|
|
||||||
func Interpolate(s string, data interface{}) (string, error) {
|
|
||||||
tmpl, err := template.New("").Parse(s)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
var b strings.Builder
|
|
||||||
err = tmpl.Execute(&b, data)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.String(), nil
|
|
||||||
}
|
|
|
@ -13,8 +13,10 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/ptr"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/database"
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
|
@ -220,7 +222,7 @@ func TestRegexpCacheBehavior(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
//cache with no TTL
|
//cache with no TTL
|
||||||
err = RegexpCacheInit(filename, types.DataSource{Type: "regex", Size: types.IntPtr(1)})
|
err = RegexpCacheInit(filename, types.DataSource{Type: "regex", Size: ptr.Of(1)})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
ret, _ := RegexpInFile("crowdsec", filename)
|
ret, _ := RegexpInFile("crowdsec", filename)
|
||||||
|
@ -233,7 +235,7 @@ func TestRegexpCacheBehavior(t *testing.T) {
|
||||||
|
|
||||||
//cache with TTL
|
//cache with TTL
|
||||||
ttl := 500 * time.Millisecond
|
ttl := 500 * time.Millisecond
|
||||||
err = RegexpCacheInit(filename, types.DataSource{Type: "regex", Size: types.IntPtr(2), TTL: &ttl})
|
err = RegexpCacheInit(filename, types.DataSource{Type: "regex", Size: ptr.Of(2), TTL: &ttl})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
ret, _ = RegexpInFile("crowdsec", filename)
|
ret, _ = RegexpInFile("crowdsec", filename)
|
||||||
|
|
|
@ -9,7 +9,8 @@ import (
|
||||||
logtest "github.com/sirupsen/logrus/hooks/test"
|
logtest "github.com/sirupsen/logrus/hooks/test"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/fflag"
|
"github.com/crowdsecurity/crowdsec/pkg/fflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,8 @@ import (
|
||||||
"github.com/lithammer/dedent"
|
"github.com/lithammer/dedent"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/setup"
|
"github.com/crowdsecurity/crowdsec/pkg/setup"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue