github.com/crowdsecurity/crowdsec@v1.6.1/pkg/csconfig/crowdsec_service_test.go (about) 1 package csconfig 2 3 import ( 4 "path/filepath" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/crowdsecurity/go-cs-lib/cstest" 10 "github.com/crowdsecurity/go-cs-lib/ptr" 11 ) 12 13 func TestLoadCrowdsec(t *testing.T) { 14 acquisFullPath, err := filepath.Abs("./testdata/acquis.yaml") 15 require.NoError(t, err) 16 17 acquisInDirFullPath, err := filepath.Abs("./testdata/acquis/acquis.yaml") 18 require.NoError(t, err) 19 20 acquisDirFullPath, err := filepath.Abs("./testdata/acquis") 21 require.NoError(t, err) 22 23 contextFileFullPath, err := filepath.Abs("./testdata/context.yaml") 24 require.NoError(t, err) 25 26 tests := []struct { 27 name string 28 input *Config 29 expected *CrowdsecServiceCfg 30 expectedErr string 31 }{ 32 { 33 name: "basic valid configuration", 34 input: &Config{ 35 ConfigPaths: &ConfigurationPaths{ 36 ConfigDir: "./testdata", 37 DataDir: "./data", 38 HubDir: "./hub", 39 }, 40 API: &APICfg{ 41 Client: &LocalApiClientCfg{ 42 CredentialsFilePath: "./testdata/lapi-secrets.yaml", 43 }, 44 }, 45 Crowdsec: &CrowdsecServiceCfg{ 46 AcquisitionFilePath: "./testdata/acquis.yaml", 47 SimulationFilePath: "./testdata/simulation.yaml", 48 ConsoleContextPath: "./testdata/context.yaml", 49 ConsoleContextValueLength: 2500, 50 }, 51 }, 52 expected: &CrowdsecServiceCfg{ 53 Enable: ptr.Of(true), 54 AcquisitionDirPath: "", 55 ConsoleContextPath: contextFileFullPath, 56 AcquisitionFilePath: acquisFullPath, 57 BucketsRoutinesCount: 1, 58 ParserRoutinesCount: 1, 59 OutputRoutinesCount: 1, 60 ConsoleContextValueLength: 2500, 61 AcquisitionFiles: []string{acquisFullPath}, 62 SimulationFilePath: "./testdata/simulation.yaml", 63 // context is loaded in pkg/alertcontext 64 // ContextToSend: map[string][]string{ 65 // "source_ip": {"evt.Parsed.source_ip"}, 66 // }, 67 SimulationConfig: &SimulationConfig{ 68 Simulation: ptr.Of(false), 69 }, 70 }, 71 }, 72 { 73 name: "basic valid configuration with acquisition dir", 74 input: &Config{ 75 ConfigPaths: &ConfigurationPaths{ 76 ConfigDir: "./testdata", 77 DataDir: "./data", 78 HubDir: "./hub", 79 }, 80 API: &APICfg{ 81 Client: &LocalApiClientCfg{ 82 CredentialsFilePath: "./testdata/lapi-secrets.yaml", 83 }, 84 }, 85 Crowdsec: &CrowdsecServiceCfg{ 86 AcquisitionFilePath: "./testdata/acquis.yaml", 87 AcquisitionDirPath: "./testdata/acquis/", 88 SimulationFilePath: "./testdata/simulation.yaml", 89 ConsoleContextPath: "./testdata/context.yaml", 90 }, 91 }, 92 expected: &CrowdsecServiceCfg{ 93 Enable: ptr.Of(true), 94 AcquisitionDirPath: acquisDirFullPath, 95 AcquisitionFilePath: acquisFullPath, 96 ConsoleContextPath: contextFileFullPath, 97 BucketsRoutinesCount: 1, 98 ParserRoutinesCount: 1, 99 OutputRoutinesCount: 1, 100 ConsoleContextValueLength: 0, 101 AcquisitionFiles: []string{acquisFullPath, acquisInDirFullPath}, 102 // context is loaded in pkg/alertcontext 103 // ContextToSend: map[string][]string{ 104 // "source_ip": {"evt.Parsed.source_ip"}, 105 // }, 106 SimulationFilePath: "./testdata/simulation.yaml", 107 SimulationConfig: &SimulationConfig{ 108 Simulation: ptr.Of(false), 109 }, 110 }, 111 }, 112 { 113 name: "no acquisition file and dir", 114 input: &Config{ 115 ConfigPaths: &ConfigurationPaths{ 116 ConfigDir: "./testdata", 117 DataDir: "./data", 118 HubDir: "./hub", 119 }, 120 API: &APICfg{ 121 Client: &LocalApiClientCfg{ 122 CredentialsFilePath: "./testdata/lapi-secrets.yaml", 123 }, 124 }, 125 Crowdsec: &CrowdsecServiceCfg{ 126 ConsoleContextPath: "./testdata/context.yaml", 127 ConsoleContextValueLength: 10, 128 }, 129 }, 130 expected: &CrowdsecServiceCfg{ 131 Enable: ptr.Of(true), 132 AcquisitionDirPath: "", 133 AcquisitionFilePath: "", 134 ConsoleContextPath: contextFileFullPath, 135 BucketsRoutinesCount: 1, 136 ParserRoutinesCount: 1, 137 OutputRoutinesCount: 1, 138 ConsoleContextValueLength: 10, 139 AcquisitionFiles: []string{}, 140 SimulationFilePath: "", 141 // context is loaded in pkg/alertcontext 142 // ContextToSend: map[string][]string{ 143 // "source_ip": {"evt.Parsed.source_ip"}, 144 // }, 145 SimulationConfig: &SimulationConfig{ 146 Simulation: ptr.Of(false), 147 }, 148 }, 149 }, 150 { 151 name: "non existing acquisition file", 152 input: &Config{ 153 ConfigPaths: &ConfigurationPaths{ 154 ConfigDir: "./testdata", 155 DataDir: "./data", 156 HubDir: "./hub", 157 }, 158 API: &APICfg{ 159 Client: &LocalApiClientCfg{ 160 CredentialsFilePath: "./testdata/lapi-secrets.yaml", 161 }, 162 }, 163 Crowdsec: &CrowdsecServiceCfg{ 164 ConsoleContextPath: "", 165 AcquisitionFilePath: "./testdata/acquis_not_exist.yaml", 166 }, 167 }, 168 expectedErr: cstest.FileNotFoundMessage, 169 }, 170 { 171 name: "agent disabled", 172 input: &Config{ 173 ConfigPaths: &ConfigurationPaths{ 174 ConfigDir: "./testdata", 175 DataDir: "./data", 176 HubDir: "./hub", 177 }, 178 }, 179 expected: nil, 180 }, 181 } 182 183 for _, tc := range tests { 184 tc := tc 185 t.Run(tc.name, func(t *testing.T) { 186 err := tc.input.LoadCrowdsec() 187 cstest.RequireErrorContains(t, err, tc.expectedErr) 188 if tc.expectedErr != "" { 189 return 190 } 191 192 require.Equal(t, tc.expected, tc.input.Crowdsec) 193 }) 194 } 195 }