123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- package csconfig
- import (
- "path/filepath"
- "testing"
- "github.com/stretchr/testify/require"
- "github.com/crowdsecurity/go-cs-lib/cstest"
- "github.com/crowdsecurity/go-cs-lib/ptr"
- )
- func TestLoadCrowdsec(t *testing.T) {
- acquisFullPath, err := filepath.Abs("./testdata/acquis.yaml")
- require.NoError(t, err)
- acquisInDirFullPath, err := filepath.Abs("./testdata/acquis/acquis.yaml")
- require.NoError(t, err)
- acquisDirFullPath, err := filepath.Abs("./testdata/acquis")
- require.NoError(t, err)
- contextFileFullPath, err := filepath.Abs("./testdata/context.yaml")
- require.NoError(t, err)
- tests := []struct {
- name string
- input *Config
- expected *CrowdsecServiceCfg
- expectedErr string
- }{
- {
- name: "basic valid configuration",
- input: &Config{
- ConfigPaths: &ConfigurationPaths{
- ConfigDir: "./testdata",
- DataDir: "./data",
- HubDir: "./hub",
- },
- API: &APICfg{
- Client: &LocalApiClientCfg{
- CredentialsFilePath: "./testdata/lapi-secrets.yaml",
- },
- },
- Crowdsec: &CrowdsecServiceCfg{
- AcquisitionFilePath: "./testdata/acquis.yaml",
- SimulationFilePath: "./testdata/simulation.yaml",
- ConsoleContextPath: "./testdata/context.yaml",
- ConsoleContextValueLength: 2500,
- },
- },
- expected: &CrowdsecServiceCfg{
- Enable: ptr.Of(true),
- AcquisitionDirPath: "",
- ConsoleContextPath: contextFileFullPath,
- AcquisitionFilePath: acquisFullPath,
- ConfigDir: "./testdata",
- DataDir: "./data",
- HubDir: "./hub",
- // XXX: need to ensure a default here
- HubIndexFile: "",
- BucketsRoutinesCount: 1,
- ParserRoutinesCount: 1,
- OutputRoutinesCount: 1,
- ConsoleContextValueLength: 2500,
- AcquisitionFiles: []string{acquisFullPath},
- SimulationFilePath: "./testdata/simulation.yaml",
- ContextToSend: map[string][]string{
- "source_ip": {"evt.Parsed.source_ip"},
- },
- SimulationConfig: &SimulationConfig{
- Simulation: ptr.Of(false),
- },
- },
- },
- {
- name: "basic valid configuration with acquisition dir",
- input: &Config{
- ConfigPaths: &ConfigurationPaths{
- ConfigDir: "./testdata",
- DataDir: "./data",
- HubDir: "./hub",
- },
- API: &APICfg{
- Client: &LocalApiClientCfg{
- CredentialsFilePath: "./testdata/lapi-secrets.yaml",
- },
- },
- Crowdsec: &CrowdsecServiceCfg{
- AcquisitionFilePath: "./testdata/acquis.yaml",
- AcquisitionDirPath: "./testdata/acquis/",
- SimulationFilePath: "./testdata/simulation.yaml",
- ConsoleContextPath: "./testdata/context.yaml",
- },
- },
- expected: &CrowdsecServiceCfg{
- Enable: ptr.Of(true),
- AcquisitionDirPath: acquisDirFullPath,
- AcquisitionFilePath: acquisFullPath,
- ConsoleContextPath: contextFileFullPath,
- ConfigDir: "./testdata",
- // XXX: need to ensure a default here
- HubIndexFile: "",
- DataDir: "./data",
- HubDir: "./hub",
- BucketsRoutinesCount: 1,
- ParserRoutinesCount: 1,
- OutputRoutinesCount: 1,
- ConsoleContextValueLength: 0,
- AcquisitionFiles: []string{acquisFullPath, acquisInDirFullPath},
- ContextToSend: map[string][]string{
- "source_ip": {"evt.Parsed.source_ip"},
- },
- SimulationFilePath: "./testdata/simulation.yaml",
- SimulationConfig: &SimulationConfig{
- Simulation: ptr.Of(false),
- },
- },
- },
- {
- name: "no acquisition file and dir",
- input: &Config{
- ConfigPaths: &ConfigurationPaths{
- ConfigDir: "./testdata",
- DataDir: "./data",
- HubDir: "./hub",
- },
- API: &APICfg{
- Client: &LocalApiClientCfg{
- CredentialsFilePath: "./testdata/lapi-secrets.yaml",
- },
- },
- Crowdsec: &CrowdsecServiceCfg{
- ConsoleContextPath: "./testdata/context.yaml",
- ConsoleContextValueLength: 10,
- },
- },
- expected: &CrowdsecServiceCfg{
- Enable: ptr.Of(true),
- AcquisitionDirPath: "",
- AcquisitionFilePath: "",
- ConfigDir: "./testdata",
- // XXX: need to ensure a default here
- HubIndexFile: "",
- DataDir: "./data",
- HubDir: "./hub",
- ConsoleContextPath: contextFileFullPath,
- BucketsRoutinesCount: 1,
- ParserRoutinesCount: 1,
- OutputRoutinesCount: 1,
- ConsoleContextValueLength: 10,
- AcquisitionFiles: []string{},
- SimulationFilePath: "",
- ContextToSend: map[string][]string{
- "source_ip": {"evt.Parsed.source_ip"},
- },
- SimulationConfig: &SimulationConfig{
- Simulation: ptr.Of(false),
- },
- },
- },
- {
- name: "non existing acquisition file",
- input: &Config{
- ConfigPaths: &ConfigurationPaths{
- ConfigDir: "./testdata",
- DataDir: "./data",
- HubDir: "./hub",
- },
- API: &APICfg{
- Client: &LocalApiClientCfg{
- CredentialsFilePath: "./testdata/lapi-secrets.yaml",
- },
- },
- Crowdsec: &CrowdsecServiceCfg{
- ConsoleContextPath: "",
- AcquisitionFilePath: "./testdata/acquis_not_exist.yaml",
- },
- },
- expectedErr: cstest.FileNotFoundMessage,
- },
- {
- name: "agent disabled",
- input: &Config{
- ConfigPaths: &ConfigurationPaths{
- ConfigDir: "./testdata",
- DataDir: "./data",
- HubDir: "./hub",
- },
- },
- expected: nil,
- },
- }
- 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
- }
- require.Equal(t, tc.expected, tc.input.Crowdsec)
- })
- }
- }
|