2021-03-24 17:16:17 +00:00
|
|
|
package csconfig
|
|
|
|
|
|
|
|
import (
|
2023-10-02 09:42:17 +00:00
|
|
|
"net"
|
2021-03-24 17:16:17 +00:00
|
|
|
"os"
|
2023-01-31 13:47:44 +00:00
|
|
|
"strings"
|
2021-03-24 17:16:17 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-01-31 13:47:44 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-03-24 17:16:17 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-10-09 09:10:51 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2024-03-04 13:22:53 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2023-01-26 16:13:31 +00:00
|
|
|
|
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 TestLoadLocalApiClientCfg(t *testing.T) {
|
|
|
|
tests := []struct {
|
2023-01-26 16:13:31 +00:00
|
|
|
name string
|
|
|
|
input *LocalApiClientCfg
|
|
|
|
expected *ApiCredentialsCfg
|
|
|
|
expectedErr string
|
2021-03-24 17:16:17 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic valid configuration",
|
2023-01-26 16:13:31 +00:00
|
|
|
input: &LocalApiClientCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
CredentialsFilePath: "./testdata/lapi-secrets.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
2023-01-26 16:13:31 +00:00
|
|
|
expected: &ApiCredentialsCfg{
|
2021-03-24 17:16:17 +00:00
|
|
|
URL: "http://localhost:8080/",
|
|
|
|
Login: "test",
|
|
|
|
Password: "testpassword",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid configuration",
|
2023-01-26 16:13:31 +00:00
|
|
|
input: &LocalApiClientCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
CredentialsFilePath: "./testdata/bad_lapi-secrets.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
2023-01-26 16:13:31 +00:00
|
|
|
expected: &ApiCredentialsCfg{},
|
|
|
|
expectedErr: "field unknown_key not found in type csconfig.ApiCredentialsCfg",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid configuration filepath",
|
2023-01-26 16:13:31 +00:00
|
|
|
input: &LocalApiClientCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
CredentialsFilePath: "./testdata/nonexist_lapi-secrets.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
2023-01-26 16:13:31 +00:00
|
|
|
expected: nil,
|
2023-10-09 09:10:51 +00:00
|
|
|
expectedErr: "open ./testdata/nonexist_lapi-secrets.yaml: " + cstest.FileNotFoundMessage,
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid configuration with insecure skip verify",
|
2023-01-26 16:13:31 +00:00
|
|
|
input: &LocalApiClientCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
CredentialsFilePath: "./testdata/lapi-secrets.yaml",
|
2023-05-25 13:37:44 +00:00
|
|
|
InsecureSkipVerify: ptr.Of(false),
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
2023-01-26 16:13:31 +00:00
|
|
|
expected: &ApiCredentialsCfg{
|
2021-03-24 17:16:17 +00:00
|
|
|
URL: "http://localhost:8080/",
|
|
|
|
Login: "test",
|
|
|
|
Password: "testpassword",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-01-26 16:13:31 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
err := tc.input.Load()
|
|
|
|
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
2024-03-04 13:22:53 +00:00
|
|
|
|
2023-01-26 16:13:31 +00:00
|
|
|
if tc.expectedErr != "" {
|
|
|
|
return
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
|
2023-01-26 16:13:31 +00:00
|
|
|
assert.Equal(t, tc.expected, tc.input.Credentials)
|
|
|
|
})
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadOnlineApiClientCfg(t *testing.T) {
|
|
|
|
tests := []struct {
|
2023-01-26 16:13:31 +00:00
|
|
|
name string
|
|
|
|
input *OnlineApiClientCfg
|
|
|
|
expected *ApiCredentialsCfg
|
|
|
|
expectedErr string
|
2021-03-24 17:16:17 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic valid configuration",
|
2023-01-26 16:13:31 +00:00
|
|
|
input: &OnlineApiClientCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
CredentialsFilePath: "./testdata/online-api-secrets.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
2023-01-26 16:13:31 +00:00
|
|
|
expected: &ApiCredentialsCfg{
|
2021-03-24 17:16:17 +00:00
|
|
|
URL: "http://crowdsec.api",
|
|
|
|
Login: "test",
|
|
|
|
Password: "testpassword",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid configuration",
|
2023-01-26 16:13:31 +00:00
|
|
|
input: &OnlineApiClientCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
CredentialsFilePath: "./testdata/bad_lapi-secrets.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
2023-01-26 16:13:31 +00:00
|
|
|
expected: &ApiCredentialsCfg{},
|
|
|
|
expectedErr: "failed unmarshaling api server credentials",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing field configuration",
|
2023-01-26 16:13:31 +00:00
|
|
|
input: &OnlineApiClientCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
CredentialsFilePath: "./testdata/bad_online-api-secrets.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
2023-01-26 16:13:31 +00:00
|
|
|
expected: nil,
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid configuration filepath",
|
2023-01-26 16:13:31 +00:00
|
|
|
input: &OnlineApiClientCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
CredentialsFilePath: "./testdata/nonexist_online-api-secrets.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
2023-01-26 16:13:31 +00:00
|
|
|
expected: &ApiCredentialsCfg{},
|
2024-01-12 13:41:36 +00:00
|
|
|
expectedErr: "open ./testdata/nonexist_online-api-secrets.yaml: " + cstest.FileNotFoundMessage,
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-01-26 16:13:31 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
err := tc.input.Load()
|
|
|
|
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
2024-03-04 13:22:53 +00:00
|
|
|
|
2023-01-26 16:13:31 +00:00
|
|
|
if tc.expectedErr != "" {
|
|
|
|
return
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
|
2023-01-26 16:13:31 +00:00
|
|
|
assert.Equal(t, tc.expected, tc.input.Credentials)
|
|
|
|
})
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadAPIServer(t *testing.T) {
|
|
|
|
tmpLAPI := &LocalApiServerCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
ProfilesPath: "./testdata/profiles.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
2023-10-09 09:10:51 +00:00
|
|
|
err := tmpLAPI.LoadProfiles()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-01-31 13:47:44 +00:00
|
|
|
logLevel := log.InfoLevel
|
2021-03-24 17:16:17 +00:00
|
|
|
config := &Config{}
|
2023-10-09 09:10:51 +00:00
|
|
|
fcontent, err := os.ReadFile("./testdata/config.yaml")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
configData := os.ExpandEnv(string(fcontent))
|
2024-03-04 13:22:53 +00:00
|
|
|
|
|
|
|
dec := yaml.NewDecoder(strings.NewReader(configData))
|
|
|
|
dec.KnownFields(true)
|
|
|
|
|
|
|
|
err = dec.Decode(&config)
|
2023-10-09 09:10:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
tests := []struct {
|
2023-01-26 16:13:31 +00:00
|
|
|
name string
|
|
|
|
input *Config
|
|
|
|
expected *LocalApiServerCfg
|
|
|
|
expectedErr string
|
2021-03-24 17:16:17 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic valid configuration",
|
2023-01-26 16:13:31 +00:00
|
|
|
input: &Config{
|
2021-03-24 17:16:17 +00:00
|
|
|
Self: []byte(configData),
|
|
|
|
API: &APICfg{
|
|
|
|
Server: &LocalApiServerCfg{
|
|
|
|
ListenURI: "http://crowdsec.api",
|
|
|
|
OnlineClient: &OnlineApiClientCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
CredentialsFilePath: "./testdata/online-api-secrets.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
2023-10-09 09:10:51 +00:00
|
|
|
ProfilesPath: "./testdata/profiles.yaml",
|
2023-01-31 13:47:44 +00:00
|
|
|
PapiLogLevel: &logLevel,
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
DbConfig: &DatabaseCfg{
|
|
|
|
Type: "sqlite",
|
2023-10-09 09:10:51 +00:00
|
|
|
DbPath: "./testdata/test.db",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
Common: &CommonCfg{
|
2023-11-24 14:57:32 +00:00
|
|
|
LogDir: "./testdata",
|
2021-03-24 17:16:17 +00:00
|
|
|
LogMedia: "stdout",
|
|
|
|
},
|
|
|
|
DisableAPI: false,
|
|
|
|
},
|
2023-01-26 16:13:31 +00:00
|
|
|
expected: &LocalApiServerCfg{
|
2023-05-25 13:37:44 +00:00
|
|
|
Enable: ptr.Of(true),
|
2021-03-24 17:16:17 +00:00
|
|
|
ListenURI: "http://crowdsec.api",
|
|
|
|
TLS: nil,
|
|
|
|
DbConfig: &DatabaseCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
DbPath: "./testdata/test.db",
|
|
|
|
Type: "sqlite",
|
|
|
|
MaxOpenConns: ptr.Of(DEFAULT_MAX_OPEN_CONNS),
|
2024-03-07 13:04:50 +00:00
|
|
|
UseWal: ptr.Of(true), // autodetected
|
2023-10-09 09:10:51 +00:00
|
|
|
DecisionBulkSize: defaultDecisionBulkSize,
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
2022-02-01 09:34:53 +00:00
|
|
|
ConsoleConfigPath: DefaultConfigPath("console.yaml"),
|
2022-01-13 15:46:16 +00:00
|
|
|
ConsoleConfig: &ConsoleConfig{
|
2023-05-25 13:37:44 +00:00
|
|
|
ShareManualDecisions: ptr.Of(false),
|
|
|
|
ShareTaintedScenarios: ptr.Of(true),
|
|
|
|
ShareCustomScenarios: ptr.Of(true),
|
|
|
|
ShareContext: ptr.Of(false),
|
|
|
|
ConsoleManagement: ptr.Of(false),
|
2022-01-13 15:46:16 +00:00
|
|
|
},
|
2023-11-24 14:57:32 +00:00
|
|
|
LogDir: "./testdata",
|
2021-03-24 17:16:17 +00:00
|
|
|
LogMedia: "stdout",
|
|
|
|
OnlineClient: &OnlineApiClientCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
CredentialsFilePath: "./testdata/online-api-secrets.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
Credentials: &ApiCredentialsCfg{
|
|
|
|
URL: "http://crowdsec.api",
|
|
|
|
Login: "test",
|
|
|
|
Password: "testpassword",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Profiles: tmpLAPI.Profiles,
|
2023-10-09 09:10:51 +00:00
|
|
|
ProfilesPath: "./testdata/profiles.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
UseForwardedForHeaders: false,
|
2023-01-31 13:47:44 +00:00
|
|
|
PapiLogLevel: &logLevel,
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-22 09:29:52 +00:00
|
|
|
name: "basic invalid configuration",
|
2023-01-26 16:13:31 +00:00
|
|
|
input: &Config{
|
2021-03-24 17:16:17 +00:00
|
|
|
Self: []byte(configData),
|
|
|
|
API: &APICfg{
|
2023-12-14 13:54:11 +00:00
|
|
|
Server: &LocalApiServerCfg{
|
|
|
|
ListenURI: "http://crowdsec.api",
|
|
|
|
},
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
Common: &CommonCfg{
|
2023-10-09 09:10:51 +00:00
|
|
|
LogDir: "./testdata/",
|
2021-03-24 17:16:17 +00:00
|
|
|
LogMedia: "stdout",
|
|
|
|
},
|
|
|
|
DisableAPI: false,
|
|
|
|
},
|
2023-01-26 16:13:31 +00:00
|
|
|
expected: &LocalApiServerCfg{
|
2023-10-02 09:42:17 +00:00
|
|
|
Enable: ptr.Of(true),
|
2023-01-31 13:47:44 +00:00
|
|
|
PapiLogLevel: &logLevel,
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
2023-01-31 13:47:44 +00:00
|
|
|
expectedErr: "no database configuration provided",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-10-09 09:10:51 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2024-01-30 10:07:53 +00:00
|
|
|
err := tc.input.LoadAPIServer(false)
|
2023-10-09 09:10:51 +00:00
|
|
|
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
2024-03-04 13:22:53 +00:00
|
|
|
|
2023-10-09 09:10:51 +00:00
|
|
|
if tc.expectedErr != "" {
|
|
|
|
return
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 09:10:51 +00:00
|
|
|
assert.Equal(t, tc.expected, tc.input.API.Server)
|
|
|
|
})
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-02 09:42:17 +00:00
|
|
|
|
2023-10-09 09:10:51 +00:00
|
|
|
func mustParseCIDRNet(t *testing.T, s string) *net.IPNet {
|
2023-10-02 09:42:17 +00:00
|
|
|
_, ipNet, err := net.ParseCIDR(s)
|
2023-10-09 09:10:51 +00:00
|
|
|
require.NoError(t, err)
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2023-10-02 09:42:17 +00:00
|
|
|
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{},
|
2023-10-09 09:10:51 +00:00
|
|
|
Cidrs: []*net.IPNet{mustParseCIDRNet(t, "1.2.3.0/24")},
|
2023-10-02 09:42:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2024-03-04 13:22:53 +00:00
|
|
|
|
2023-10-02 09:42:17 +00:00
|
|
|
if tc.expectedErr != "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, tc.expected, wl)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|