2021-03-24 17:16:17 +00:00
|
|
|
package csconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2023-10-09 09:10:51 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-07-28 14:35:08 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/cstest"
|
|
|
|
"github.com/crowdsecurity/go-cs-lib/ptr"
|
2021-03-24 17:16:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoadCrowdsec(t *testing.T) {
|
2023-10-09 09:10:51 +00:00
|
|
|
acquisFullPath, err := filepath.Abs("./testdata/acquis.yaml")
|
2022-10-17 15:32:08 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-24 17:16:17 +00:00
|
|
|
|
2023-10-09 09:10:51 +00:00
|
|
|
acquisInDirFullPath, err := filepath.Abs("./testdata/acquis/acquis.yaml")
|
2022-10-17 15:32:08 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-24 17:16:17 +00:00
|
|
|
|
2023-10-09 09:10:51 +00:00
|
|
|
acquisDirFullPath, err := filepath.Abs("./testdata/acquis")
|
2022-10-17 15:32:08 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-24 17:16:17 +00:00
|
|
|
|
2023-10-09 09:10:51 +00:00
|
|
|
contextFileFullPath, err := filepath.Abs("./testdata/context.yaml")
|
2023-01-04 15:50:02 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
tests := []struct {
|
2023-10-09 09:10:51 +00:00
|
|
|
name string
|
|
|
|
input *Config
|
|
|
|
expected *CrowdsecServiceCfg
|
|
|
|
expectedErr string
|
2021-03-24 17:16:17 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic valid configuration",
|
2022-10-17 15:32:08 +00:00
|
|
|
input: &Config{
|
2021-03-24 17:16:17 +00:00
|
|
|
ConfigPaths: &ConfigurationPaths{
|
2023-10-09 09:10:51 +00:00
|
|
|
ConfigDir: "./testdata",
|
2021-03-24 17:16:17 +00:00
|
|
|
DataDir: "./data",
|
|
|
|
HubDir: "./hub",
|
|
|
|
},
|
|
|
|
API: &APICfg{
|
|
|
|
Client: &LocalApiClientCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
CredentialsFilePath: "./testdata/lapi-secrets.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Crowdsec: &CrowdsecServiceCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
AcquisitionFilePath: "./testdata/acquis.yaml",
|
|
|
|
SimulationFilePath: "./testdata/simulation.yaml",
|
|
|
|
ConsoleContextPath: "./testdata/context.yaml",
|
2023-01-04 15:50:02 +00:00
|
|
|
ConsoleContextValueLength: 2500,
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
2023-10-09 09:10:51 +00:00
|
|
|
expected: &CrowdsecServiceCfg{
|
2023-05-25 13:37:44 +00:00
|
|
|
Enable: ptr.Of(true),
|
2023-01-04 15:50:02 +00:00
|
|
|
AcquisitionDirPath: "",
|
|
|
|
ConsoleContextPath: contextFileFullPath,
|
|
|
|
AcquisitionFilePath: acquisFullPath,
|
|
|
|
BucketsRoutinesCount: 1,
|
|
|
|
ParserRoutinesCount: 1,
|
|
|
|
OutputRoutinesCount: 1,
|
|
|
|
ConsoleContextValueLength: 2500,
|
|
|
|
AcquisitionFiles: []string{acquisFullPath},
|
2023-10-09 09:10:51 +00:00
|
|
|
SimulationFilePath: "./testdata/simulation.yaml",
|
2023-12-07 15:20:13 +00:00
|
|
|
// context is loaded in pkg/alertcontext
|
|
|
|
// ContextToSend: map[string][]string{
|
|
|
|
// "source_ip": {"evt.Parsed.source_ip"},
|
|
|
|
// },
|
2021-05-17 09:54:28 +00:00
|
|
|
SimulationConfig: &SimulationConfig{
|
2023-05-25 13:37:44 +00:00
|
|
|
Simulation: ptr.Of(false),
|
2021-05-17 09:54:28 +00:00
|
|
|
},
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "basic valid configuration with acquisition dir",
|
2022-10-17 15:32:08 +00:00
|
|
|
input: &Config{
|
2021-03-24 17:16:17 +00:00
|
|
|
ConfigPaths: &ConfigurationPaths{
|
2023-10-09 09:10:51 +00:00
|
|
|
ConfigDir: "./testdata",
|
2021-03-24 17:16:17 +00:00
|
|
|
DataDir: "./data",
|
|
|
|
HubDir: "./hub",
|
|
|
|
},
|
|
|
|
API: &APICfg{
|
|
|
|
Client: &LocalApiClientCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
CredentialsFilePath: "./testdata/lapi-secrets.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Crowdsec: &CrowdsecServiceCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
AcquisitionFilePath: "./testdata/acquis.yaml",
|
|
|
|
AcquisitionDirPath: "./testdata/acquis/",
|
|
|
|
SimulationFilePath: "./testdata/simulation.yaml",
|
|
|
|
ConsoleContextPath: "./testdata/context.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
2023-10-09 09:10:51 +00:00
|
|
|
expected: &CrowdsecServiceCfg{
|
2023-05-25 13:37:44 +00:00
|
|
|
Enable: ptr.Of(true),
|
2023-01-04 15:50:02 +00:00
|
|
|
AcquisitionDirPath: acquisDirFullPath,
|
|
|
|
AcquisitionFilePath: acquisFullPath,
|
|
|
|
ConsoleContextPath: contextFileFullPath,
|
|
|
|
BucketsRoutinesCount: 1,
|
|
|
|
ParserRoutinesCount: 1,
|
|
|
|
OutputRoutinesCount: 1,
|
|
|
|
ConsoleContextValueLength: 0,
|
|
|
|
AcquisitionFiles: []string{acquisFullPath, acquisInDirFullPath},
|
2023-12-07 15:20:13 +00:00
|
|
|
// context is loaded in pkg/alertcontext
|
|
|
|
// ContextToSend: map[string][]string{
|
|
|
|
// "source_ip": {"evt.Parsed.source_ip"},
|
|
|
|
// },
|
2023-10-09 09:10:51 +00:00
|
|
|
SimulationFilePath: "./testdata/simulation.yaml",
|
2021-05-17 09:54:28 +00:00
|
|
|
SimulationConfig: &SimulationConfig{
|
2023-05-25 13:37:44 +00:00
|
|
|
Simulation: ptr.Of(false),
|
2021-05-17 09:54:28 +00:00
|
|
|
},
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no acquisition file and dir",
|
2022-10-17 15:32:08 +00:00
|
|
|
input: &Config{
|
2021-03-24 17:16:17 +00:00
|
|
|
ConfigPaths: &ConfigurationPaths{
|
2023-10-09 09:10:51 +00:00
|
|
|
ConfigDir: "./testdata",
|
2021-03-24 17:16:17 +00:00
|
|
|
DataDir: "./data",
|
|
|
|
HubDir: "./hub",
|
|
|
|
},
|
|
|
|
API: &APICfg{
|
|
|
|
Client: &LocalApiClientCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
CredentialsFilePath: "./testdata/lapi-secrets.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
2023-01-04 15:50:02 +00:00
|
|
|
Crowdsec: &CrowdsecServiceCfg{
|
2023-11-24 14:57:32 +00:00
|
|
|
ConsoleContextPath: "./testdata/context.yaml",
|
2023-01-04 15:50:02 +00:00
|
|
|
ConsoleContextValueLength: 10,
|
|
|
|
},
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
2023-10-09 09:10:51 +00:00
|
|
|
expected: &CrowdsecServiceCfg{
|
2023-05-25 13:37:44 +00:00
|
|
|
Enable: ptr.Of(true),
|
2023-01-04 15:50:02 +00:00
|
|
|
AcquisitionDirPath: "",
|
|
|
|
AcquisitionFilePath: "",
|
|
|
|
ConsoleContextPath: contextFileFullPath,
|
|
|
|
BucketsRoutinesCount: 1,
|
|
|
|
ParserRoutinesCount: 1,
|
|
|
|
OutputRoutinesCount: 1,
|
|
|
|
ConsoleContextValueLength: 10,
|
|
|
|
AcquisitionFiles: []string{},
|
|
|
|
SimulationFilePath: "",
|
2023-12-07 15:20:13 +00:00
|
|
|
// context is loaded in pkg/alertcontext
|
|
|
|
// ContextToSend: map[string][]string{
|
|
|
|
// "source_ip": {"evt.Parsed.source_ip"},
|
|
|
|
// },
|
2021-10-04 15:14:52 +00:00
|
|
|
SimulationConfig: &SimulationConfig{
|
2023-05-25 13:37:44 +00:00
|
|
|
Simulation: ptr.Of(false),
|
2021-10-04 15:14:52 +00:00
|
|
|
},
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "non existing acquisition file",
|
2022-10-17 15:32:08 +00:00
|
|
|
input: &Config{
|
2021-03-24 17:16:17 +00:00
|
|
|
ConfigPaths: &ConfigurationPaths{
|
2023-10-09 09:10:51 +00:00
|
|
|
ConfigDir: "./testdata",
|
2021-03-24 17:16:17 +00:00
|
|
|
DataDir: "./data",
|
|
|
|
HubDir: "./hub",
|
|
|
|
},
|
|
|
|
API: &APICfg{
|
|
|
|
Client: &LocalApiClientCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
CredentialsFilePath: "./testdata/lapi-secrets.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Crowdsec: &CrowdsecServiceCfg{
|
2023-01-04 15:50:02 +00:00
|
|
|
ConsoleContextPath: "",
|
2023-10-09 09:10:51 +00:00
|
|
|
AcquisitionFilePath: "./testdata/acquis_not_exist.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
2022-10-17 15:32:08 +00:00
|
|
|
expectedErr: cstest.FileNotFoundMessage,
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "agent disabled",
|
2022-10-17 15:32:08 +00:00
|
|
|
input: &Config{
|
2021-03-24 17:16:17 +00:00
|
|
|
ConfigPaths: &ConfigurationPaths{
|
2023-10-09 09:10:51 +00:00
|
|
|
ConfigDir: "./testdata",
|
2021-03-24 17:16:17 +00:00
|
|
|
DataDir: "./data",
|
|
|
|
HubDir: "./hub",
|
|
|
|
},
|
|
|
|
},
|
2023-10-09 09:10:51 +00:00
|
|
|
expected: nil,
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-10-17 15:32:08 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
err := tc.input.LoadCrowdsec()
|
|
|
|
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
|
|
|
if tc.expectedErr != "" {
|
|
|
|
return
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 09:10:51 +00:00
|
|
|
require.Equal(t, tc.expected, tc.input.Crowdsec)
|
2022-10-17 15:32:08 +00:00
|
|
|
})
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
}
|