api_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package csconfig
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "testing"
  8. "github.com/crowdsecurity/crowdsec/pkg/types"
  9. "github.com/stretchr/testify/assert"
  10. "gopkg.in/yaml.v2"
  11. )
  12. func TestLoadLocalApiClientCfg(t *testing.T) {
  13. True := true
  14. tests := []struct {
  15. name string
  16. Input *LocalApiClientCfg
  17. expectedResult *ApiCredentialsCfg
  18. err string
  19. }{
  20. {
  21. name: "basic valid configuration",
  22. Input: &LocalApiClientCfg{
  23. CredentialsFilePath: "./tests/lapi-secrets.yaml",
  24. },
  25. expectedResult: &ApiCredentialsCfg{
  26. URL: "http://localhost:8080/",
  27. Login: "test",
  28. Password: "testpassword",
  29. },
  30. },
  31. {
  32. name: "invalid configuration",
  33. Input: &LocalApiClientCfg{
  34. CredentialsFilePath: "./tests/bad_lapi-secrets.yaml",
  35. },
  36. expectedResult: &ApiCredentialsCfg{},
  37. },
  38. {
  39. name: "invalid configuration filepath",
  40. Input: &LocalApiClientCfg{
  41. CredentialsFilePath: "./tests/nonexist_lapi-secrets.yaml",
  42. },
  43. expectedResult: nil,
  44. },
  45. {
  46. name: "valid configuration with insecure skip verify",
  47. Input: &LocalApiClientCfg{
  48. CredentialsFilePath: "./tests/lapi-secrets.yaml",
  49. InsecureSkipVerify: &True,
  50. },
  51. expectedResult: &ApiCredentialsCfg{
  52. URL: "http://localhost:8080/",
  53. Login: "test",
  54. Password: "testpassword",
  55. },
  56. },
  57. }
  58. for idx, test := range tests {
  59. fmt.Printf("TEST '%s'\n", test.name)
  60. err := test.Input.Load()
  61. if err == nil && test.err != "" {
  62. t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests))
  63. } else if test.err != "" {
  64. if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) {
  65. t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
  66. test.err,
  67. fmt.Sprintf("%s", err))
  68. }
  69. }
  70. isOk := assert.Equal(t, test.expectedResult, test.Input.Credentials)
  71. if !isOk {
  72. t.Fatalf("test '%s' failed", test.name)
  73. }
  74. }
  75. }
  76. func TestLoadOnlineApiClientCfg(t *testing.T) {
  77. tests := []struct {
  78. name string
  79. Input *OnlineApiClientCfg
  80. expectedResult *ApiCredentialsCfg
  81. err string
  82. }{
  83. {
  84. name: "basic valid configuration",
  85. Input: &OnlineApiClientCfg{
  86. CredentialsFilePath: "./tests/online-api-secrets.yaml",
  87. },
  88. expectedResult: &ApiCredentialsCfg{
  89. URL: "http://crowdsec.api",
  90. Login: "test",
  91. Password: "testpassword",
  92. },
  93. },
  94. {
  95. name: "invalid configuration",
  96. Input: &OnlineApiClientCfg{
  97. CredentialsFilePath: "./tests/bad_lapi-secrets.yaml",
  98. },
  99. expectedResult: &ApiCredentialsCfg{},
  100. err: "failed unmarshaling api server credentials",
  101. },
  102. {
  103. name: "missing field configuration",
  104. Input: &OnlineApiClientCfg{
  105. CredentialsFilePath: "./tests/bad_online-api-secrets.yaml",
  106. },
  107. expectedResult: nil,
  108. },
  109. {
  110. name: "invalid configuration filepath",
  111. Input: &OnlineApiClientCfg{
  112. CredentialsFilePath: "./tests/nonexist_online-api-secrets.yaml",
  113. },
  114. expectedResult: &ApiCredentialsCfg{},
  115. err: "failed to read api server credentials",
  116. },
  117. }
  118. for idx, test := range tests {
  119. err := test.Input.Load()
  120. if err == nil && test.err != "" {
  121. fmt.Printf("TEST '%s': NOK\n", test.name)
  122. t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests))
  123. } else if test.err != "" {
  124. if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) {
  125. fmt.Printf("TEST '%s': NOK\n", test.name)
  126. t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
  127. test.err,
  128. fmt.Sprintf("%s", err))
  129. }
  130. }
  131. isOk := assert.Equal(t, test.expectedResult, test.Input.Credentials)
  132. if !isOk {
  133. t.Fatalf("TEST '%s': NOK", test.name)
  134. } else {
  135. fmt.Printf("TEST '%s': OK\n", test.name)
  136. }
  137. }
  138. }
  139. func TestLoadAPIServer(t *testing.T) {
  140. tmpLAPI := &LocalApiServerCfg{
  141. ProfilesPath: "./tests/profiles.yaml",
  142. }
  143. if err := tmpLAPI.LoadProfiles(); err != nil {
  144. t.Fatalf("loading tmp profiles: %+v", err)
  145. }
  146. LogDirFullPath, err := filepath.Abs("./tests")
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. config := &Config{}
  151. fcontent, err := os.ReadFile("./tests/config.yaml")
  152. if err != nil {
  153. t.Fatal(err)
  154. }
  155. configData := os.ExpandEnv(string(fcontent))
  156. err = yaml.UnmarshalStrict([]byte(configData), &config)
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. tests := []struct {
  161. name string
  162. Input *Config
  163. expectedResult *LocalApiServerCfg
  164. err string
  165. }{
  166. {
  167. name: "basic valid configuration",
  168. Input: &Config{
  169. Self: []byte(configData),
  170. API: &APICfg{
  171. Server: &LocalApiServerCfg{
  172. ListenURI: "http://crowdsec.api",
  173. OnlineClient: &OnlineApiClientCfg{
  174. CredentialsFilePath: "./tests/online-api-secrets.yaml",
  175. },
  176. ProfilesPath: "./tests/profiles.yaml",
  177. },
  178. },
  179. DbConfig: &DatabaseCfg{
  180. Type: "sqlite",
  181. DbPath: "./tests/test.db",
  182. },
  183. Common: &CommonCfg{
  184. LogDir: "./tests/",
  185. LogMedia: "stdout",
  186. },
  187. DisableAPI: false,
  188. },
  189. expectedResult: &LocalApiServerCfg{
  190. Enable: types.BoolPtr(true),
  191. ListenURI: "http://crowdsec.api",
  192. TLS: nil,
  193. DbConfig: &DatabaseCfg{
  194. DbPath: "./tests/test.db",
  195. Type: "sqlite",
  196. MaxOpenConns: types.IntPtr(DEFAULT_MAX_OPEN_CONNS),
  197. },
  198. ConsoleConfigPath: DefaultConfigPath("console.yaml"),
  199. ConsoleConfig: &ConsoleConfig{
  200. ShareManualDecisions: types.BoolPtr(false),
  201. ShareTaintedScenarios: types.BoolPtr(true),
  202. ShareCustomScenarios: types.BoolPtr(true),
  203. ShareContext: types.BoolPtr(false),
  204. },
  205. LogDir: LogDirFullPath,
  206. LogMedia: "stdout",
  207. OnlineClient: &OnlineApiClientCfg{
  208. CredentialsFilePath: "./tests/online-api-secrets.yaml",
  209. Credentials: &ApiCredentialsCfg{
  210. URL: "http://crowdsec.api",
  211. Login: "test",
  212. Password: "testpassword",
  213. },
  214. },
  215. Profiles: tmpLAPI.Profiles,
  216. ProfilesPath: "./tests/profiles.yaml",
  217. UseForwardedForHeaders: false,
  218. },
  219. err: "",
  220. },
  221. {
  222. name: "basic invalid configuration",
  223. Input: &Config{
  224. Self: []byte(configData),
  225. API: &APICfg{
  226. Server: &LocalApiServerCfg{},
  227. },
  228. Common: &CommonCfg{
  229. LogDir: "./tests/",
  230. LogMedia: "stdout",
  231. },
  232. DisableAPI: false,
  233. },
  234. expectedResult: &LocalApiServerCfg{
  235. Enable: types.BoolPtr(true),
  236. LogDir: LogDirFullPath,
  237. LogMedia: "stdout",
  238. },
  239. err: "while loading profiles for LAPI",
  240. },
  241. }
  242. for idx, test := range tests {
  243. err := test.Input.LoadAPIServer()
  244. if err == nil && test.err != "" {
  245. fmt.Printf("TEST '%s': NOK\n", test.name)
  246. t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests))
  247. } else if test.err != "" {
  248. if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) {
  249. fmt.Printf("TEST '%s': NOK\n", test.name)
  250. t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
  251. test.err,
  252. fmt.Sprintf("%s", err))
  253. }
  254. }
  255. isOk := assert.Equal(t, test.expectedResult, test.Input.API.Server)
  256. if !isOk {
  257. t.Fatalf("TEST '%s': NOK", test.name)
  258. } else {
  259. fmt.Printf("TEST '%s': OK\n", test.name)
  260. }
  261. }
  262. }