123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- package csconfig
- import (
- "net"
- "os"
- "strings"
- "testing"
- log "github.com/sirupsen/logrus"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "gopkg.in/yaml.v2"
- "github.com/crowdsecurity/go-cs-lib/cstest"
- "github.com/crowdsecurity/go-cs-lib/ptr"
- )
- func TestLoadLocalApiClientCfg(t *testing.T) {
- tests := []struct {
- name string
- input *LocalApiClientCfg
- expected *ApiCredentialsCfg
- expectedErr string
- }{
- {
- name: "basic valid configuration",
- input: &LocalApiClientCfg{
- CredentialsFilePath: "./testdata/lapi-secrets.yaml",
- },
- expected: &ApiCredentialsCfg{
- URL: "http://localhost:8080/",
- Login: "test",
- Password: "testpassword",
- },
- },
- {
- name: "invalid configuration",
- input: &LocalApiClientCfg{
- CredentialsFilePath: "./testdata/bad_lapi-secrets.yaml",
- },
- expected: &ApiCredentialsCfg{},
- expectedErr: "field unknown_key not found in type csconfig.ApiCredentialsCfg",
- },
- {
- name: "invalid configuration filepath",
- input: &LocalApiClientCfg{
- CredentialsFilePath: "./testdata/nonexist_lapi-secrets.yaml",
- },
- expected: nil,
- expectedErr: "open ./testdata/nonexist_lapi-secrets.yaml: " + cstest.FileNotFoundMessage,
- },
- {
- name: "valid configuration with insecure skip verify",
- input: &LocalApiClientCfg{
- CredentialsFilePath: "./testdata/lapi-secrets.yaml",
- InsecureSkipVerify: ptr.Of(false),
- },
- expected: &ApiCredentialsCfg{
- URL: "http://localhost:8080/",
- Login: "test",
- Password: "testpassword",
- },
- },
- }
- for _, tc := range tests {
- tc := tc
- t.Run(tc.name, func(t *testing.T) {
- err := tc.input.Load()
- cstest.RequireErrorContains(t, err, tc.expectedErr)
- if tc.expectedErr != "" {
- return
- }
- assert.Equal(t, tc.expected, tc.input.Credentials)
- })
- }
- }
- func TestLoadOnlineApiClientCfg(t *testing.T) {
- tests := []struct {
- name string
- input *OnlineApiClientCfg
- expected *ApiCredentialsCfg
- expectedErr string
- }{
- {
- name: "basic valid configuration",
- input: &OnlineApiClientCfg{
- CredentialsFilePath: "./testdata/online-api-secrets.yaml",
- },
- expected: &ApiCredentialsCfg{
- URL: "http://crowdsec.api",
- Login: "test",
- Password: "testpassword",
- },
- },
- {
- name: "invalid configuration",
- input: &OnlineApiClientCfg{
- CredentialsFilePath: "./testdata/bad_lapi-secrets.yaml",
- },
- expected: &ApiCredentialsCfg{},
- expectedErr: "failed unmarshaling api server credentials",
- },
- {
- name: "missing field configuration",
- input: &OnlineApiClientCfg{
- CredentialsFilePath: "./testdata/bad_online-api-secrets.yaml",
- },
- expected: nil,
- },
- {
- name: "invalid configuration filepath",
- input: &OnlineApiClientCfg{
- CredentialsFilePath: "./testdata/nonexist_online-api-secrets.yaml",
- },
- expected: &ApiCredentialsCfg{},
- expectedErr: "failed to read api server credentials",
- },
- }
- for _, tc := range tests {
- tc := tc
- t.Run(tc.name, func(t *testing.T) {
- err := tc.input.Load()
- cstest.RequireErrorContains(t, err, tc.expectedErr)
- if tc.expectedErr != "" {
- return
- }
- assert.Equal(t, tc.expected, tc.input.Credentials)
- })
- }
- }
- func TestLoadAPIServer(t *testing.T) {
- tmpLAPI := &LocalApiServerCfg{
- ProfilesPath: "./testdata/profiles.yaml",
- }
- err := tmpLAPI.LoadProfiles()
- require.NoError(t, err)
- logLevel := log.InfoLevel
- config := &Config{}
- fcontent, err := os.ReadFile("./testdata/config.yaml")
- require.NoError(t, err)
- configData := os.ExpandEnv(string(fcontent))
- err = yaml.UnmarshalStrict([]byte(configData), &config)
- require.NoError(t, err)
- tests := []struct {
- name string
- input *Config
- expected *LocalApiServerCfg
- expectedErr string
- }{
- {
- name: "basic valid configuration",
- input: &Config{
- Self: []byte(configData),
- API: &APICfg{
- Server: &LocalApiServerCfg{
- ListenURI: "http://crowdsec.api",
- OnlineClient: &OnlineApiClientCfg{
- CredentialsFilePath: "./testdata/online-api-secrets.yaml",
- },
- ProfilesPath: "./testdata/profiles.yaml",
- PapiLogLevel: &logLevel,
- },
- },
- DbConfig: &DatabaseCfg{
- Type: "sqlite",
- DbPath: "./testdata/test.db",
- },
- Common: &CommonCfg{
- LogDir: "./testdata",
- LogMedia: "stdout",
- },
- DisableAPI: false,
- },
- expected: &LocalApiServerCfg{
- Enable: ptr.Of(true),
- ListenURI: "http://crowdsec.api",
- TLS: nil,
- DbConfig: &DatabaseCfg{
- DbPath: "./testdata/test.db",
- Type: "sqlite",
- MaxOpenConns: ptr.Of(DEFAULT_MAX_OPEN_CONNS),
- DecisionBulkSize: defaultDecisionBulkSize,
- },
- ConsoleConfigPath: DefaultConfigPath("console.yaml"),
- ConsoleConfig: &ConsoleConfig{
- ShareManualDecisions: ptr.Of(false),
- ShareTaintedScenarios: ptr.Of(true),
- ShareCustomScenarios: ptr.Of(true),
- ShareContext: ptr.Of(false),
- ConsoleManagement: ptr.Of(false),
- },
- LogDir: "./testdata",
- LogMedia: "stdout",
- OnlineClient: &OnlineApiClientCfg{
- CredentialsFilePath: "./testdata/online-api-secrets.yaml",
- Credentials: &ApiCredentialsCfg{
- URL: "http://crowdsec.api",
- Login: "test",
- Password: "testpassword",
- },
- },
- Profiles: tmpLAPI.Profiles,
- ProfilesPath: "./testdata/profiles.yaml",
- UseForwardedForHeaders: false,
- PapiLogLevel: &logLevel,
- },
- },
- {
- name: "basic invalid configuration",
- input: &Config{
- Self: []byte(configData),
- API: &APICfg{
- Server: &LocalApiServerCfg{},
- },
- Common: &CommonCfg{
- LogDir: "./testdata/",
- LogMedia: "stdout",
- },
- DisableAPI: false,
- },
- expected: &LocalApiServerCfg{
- Enable: ptr.Of(true),
- PapiLogLevel: &logLevel,
- },
- expectedErr: "no database configuration provided",
- },
- }
- for _, tc := range tests {
- tc := tc
- t.Run(tc.name, func(t *testing.T) {
- err := tc.input.LoadAPIServer()
- cstest.RequireErrorContains(t, err, tc.expectedErr)
- if tc.expectedErr != "" {
- return
- }
- assert.Equal(t, tc.expected, tc.input.API.Server)
- })
- }
- }
- func mustParseCIDRNet(t *testing.T, s string) *net.IPNet {
- _, ipNet, err := net.ParseCIDR(s)
- require.NoError(t, err)
- return ipNet
- }
- func TestParseCapiWhitelists(t *testing.T) {
- tests := []struct {
- name string
- input string
- expected *CapiWhitelist
- expectedErr string
- }{
- {
- name: "empty file",
- input: "",
- expected: &CapiWhitelist{
- Ips: []net.IP{},
- Cidrs: []*net.IPNet{},
- },
- expectedErr: "empty file",
- },
- {
- name: "empty ip and cidr",
- input: `{"ips": [], "cidrs": []}`,
- expected: &CapiWhitelist{
- Ips: []net.IP{},
- Cidrs: []*net.IPNet{},
- },
- },
- {
- name: "some ip",
- input: `{"ips": ["1.2.3.4"]}`,
- expected: &CapiWhitelist{
- Ips: []net.IP{net.IPv4(1, 2, 3, 4)},
- Cidrs: []*net.IPNet{},
- },
- },
- {
- name: "some cidr",
- input: `{"cidrs": ["1.2.3.0/24"]}`,
- expected: &CapiWhitelist{
- Ips: []net.IP{},
- Cidrs: []*net.IPNet{mustParseCIDRNet(t, "1.2.3.0/24")},
- },
- },
- }
- for _, tc := range tests {
- tc := tc
- t.Run(tc.name, func(t *testing.T) {
- wl, err := parseCapiWhitelists(strings.NewReader(tc.input))
- cstest.RequireErrorContains(t, err, tc.expectedErr)
- if tc.expectedErr != "" {
- return
- }
- assert.Equal(t, tc.expected, wl)
- })
- }
- }
|