123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- package csconfig
- import (
- "fmt"
- "io/ioutil"
- "os"
- "path/filepath"
- "strings"
- "testing"
- "github.com/crowdsecurity/crowdsec/pkg/types"
- "github.com/stretchr/testify/assert"
- "gopkg.in/yaml.v2"
- )
- func TestLoadLocalApiClientCfg(t *testing.T) {
- True := true
- tests := []struct {
- name string
- Input *LocalApiClientCfg
- expectedResult *ApiCredentialsCfg
- err string
- }{
- {
- name: "basic valid configuration",
- Input: &LocalApiClientCfg{
- CredentialsFilePath: "./tests/lapi-secrets.yaml",
- },
- expectedResult: &ApiCredentialsCfg{
- URL: "http://localhost:8080/",
- Login: "test",
- Password: "testpassword",
- },
- },
- {
- name: "invalid configuration",
- Input: &LocalApiClientCfg{
- CredentialsFilePath: "./tests/bad_lapi-secrets.yaml",
- },
- expectedResult: &ApiCredentialsCfg{},
- },
- {
- name: "invalid configuration filepath",
- Input: &LocalApiClientCfg{
- CredentialsFilePath: "./tests/nonexist_lapi-secrets.yaml",
- },
- expectedResult: nil,
- },
- {
- name: "valid configuration with insecure skip verify",
- Input: &LocalApiClientCfg{
- CredentialsFilePath: "./tests/lapi-secrets.yaml",
- InsecureSkipVerify: &True,
- },
- expectedResult: &ApiCredentialsCfg{
- URL: "http://localhost:8080/",
- Login: "test",
- Password: "testpassword",
- },
- },
- }
- for idx, test := range tests {
- fmt.Printf("TEST '%s'\n", test.name)
- err := test.Input.Load()
- if err == nil && test.err != "" {
- t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests))
- } else if test.err != "" {
- if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) {
- t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
- test.err,
- fmt.Sprintf("%s", err))
- }
- }
- isOk := assert.Equal(t, test.expectedResult, test.Input.Credentials)
- if !isOk {
- t.Fatalf("test '%s' failed", test.name)
- }
- }
- }
- func TestLoadOnlineApiClientCfg(t *testing.T) {
- tests := []struct {
- name string
- Input *OnlineApiClientCfg
- expectedResult *ApiCredentialsCfg
- err string
- }{
- {
- name: "basic valid configuration",
- Input: &OnlineApiClientCfg{
- CredentialsFilePath: "./tests/online-api-secrets.yaml",
- },
- expectedResult: &ApiCredentialsCfg{
- URL: "http://crowdsec.api",
- Login: "test",
- Password: "testpassword",
- },
- },
- {
- name: "invalid configuration",
- Input: &OnlineApiClientCfg{
- CredentialsFilePath: "./tests/bad_lapi-secrets.yaml",
- },
- expectedResult: &ApiCredentialsCfg{},
- err: "failed unmarshaling api server credentials",
- },
- {
- name: "missing field configuration",
- Input: &OnlineApiClientCfg{
- CredentialsFilePath: "./tests/bad_online-api-secrets.yaml",
- },
- expectedResult: nil,
- },
- {
- name: "invalid configuration filepath",
- Input: &OnlineApiClientCfg{
- CredentialsFilePath: "./tests/nonexist_online-api-secrets.yaml",
- },
- expectedResult: &ApiCredentialsCfg{},
- err: "failed to read api server credentials",
- },
- }
- for idx, test := range tests {
- err := test.Input.Load()
- if err == nil && test.err != "" {
- fmt.Printf("TEST '%s': NOK\n", test.name)
- t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests))
- } else if test.err != "" {
- if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) {
- fmt.Printf("TEST '%s': NOK\n", test.name)
- t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
- test.err,
- fmt.Sprintf("%s", err))
- }
- }
- isOk := assert.Equal(t, test.expectedResult, test.Input.Credentials)
- if !isOk {
- t.Fatalf("TEST '%s': NOK", test.name)
- } else {
- fmt.Printf("TEST '%s': OK\n", test.name)
- }
- }
- }
- func TestLoadAPIServer(t *testing.T) {
- tmpLAPI := &LocalApiServerCfg{
- ProfilesPath: "./tests/profiles.yaml",
- }
- if err := tmpLAPI.LoadProfiles(); err != nil {
- t.Fatalf("loading tmp profiles: %+v", err)
- }
- LogDirFullPath, err := filepath.Abs("./tests")
- if err != nil {
- t.Fatalf(err.Error())
- }
- config := &Config{}
- fcontent, err := ioutil.ReadFile("./tests/config.yaml")
- if err != nil {
- t.Fatalf(err.Error())
- }
- configData := os.ExpandEnv(string(fcontent))
- err = yaml.UnmarshalStrict([]byte(configData), &config)
- if err != nil {
- t.Fatalf(err.Error())
- }
- tests := []struct {
- name string
- Input *Config
- expectedResult *LocalApiServerCfg
- err string
- }{
- {
- name: "basic valid configuration",
- Input: &Config{
- Self: []byte(configData),
- API: &APICfg{
- Server: &LocalApiServerCfg{
- ListenURI: "http://crowdsec.api",
- OnlineClient: &OnlineApiClientCfg{
- CredentialsFilePath: "./tests/online-api-secrets.yaml",
- },
- ProfilesPath: "./tests/profiles.yaml",
- },
- },
- DbConfig: &DatabaseCfg{
- Type: "sqlite",
- DbPath: "./tests/test.db",
- },
- Common: &CommonCfg{
- LogDir: "./tests/",
- LogMedia: "stdout",
- },
- DisableAPI: false,
- },
- expectedResult: &LocalApiServerCfg{
- ListenURI: "http://crowdsec.api",
- TLS: nil,
- DbConfig: &DatabaseCfg{
- DbPath: "./tests/test.db",
- Type: "sqlite",
- },
- ConsoleConfigPath: DefaultConfigPath("console.yaml"),
- ConsoleConfig: &ConsoleConfig{
- ShareManualDecisions: types.BoolPtr(false),
- ShareTaintedScenarios: types.BoolPtr(true),
- ShareCustomScenarios: types.BoolPtr(true),
- },
- LogDir: LogDirFullPath,
- LogMedia: "stdout",
- OnlineClient: &OnlineApiClientCfg{
- CredentialsFilePath: "./tests/online-api-secrets.yaml",
- Credentials: &ApiCredentialsCfg{
- URL: "http://crowdsec.api",
- Login: "test",
- Password: "testpassword",
- },
- },
- Profiles: tmpLAPI.Profiles,
- ProfilesPath: "./tests/profiles.yaml",
- UseForwardedForHeaders: false,
- },
- err: "",
- },
- {
- name: "basic valid configuration",
- Input: &Config{
- Self: []byte(configData),
- API: &APICfg{
- Server: &LocalApiServerCfg{},
- },
- Common: &CommonCfg{
- LogDir: "./tests/",
- LogMedia: "stdout",
- },
- DisableAPI: false,
- },
- expectedResult: &LocalApiServerCfg{
- LogDir: LogDirFullPath,
- LogMedia: "stdout",
- },
- err: "while loading profiles for LAPI",
- },
- }
- for idx, test := range tests {
- err := test.Input.LoadAPIServer()
- if err == nil && test.err != "" {
- fmt.Printf("TEST '%s': NOK\n", test.name)
- t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests))
- } else if test.err != "" {
- if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) {
- fmt.Printf("TEST '%s': NOK\n", test.name)
- t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
- test.err,
- fmt.Sprintf("%s", err))
- }
- }
- isOk := assert.Equal(t, test.expectedResult, test.Input.API.Server)
- if !isOk {
- t.Fatalf("TEST '%s': NOK", test.name)
- } else {
- fmt.Printf("TEST '%s': OK\n", test.name)
- }
- }
- }
|