api_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.Fatalf(err.Error())
  149. }
  150. config := &Config{}
  151. fcontent, err := os.ReadFile("./tests/config.yaml")
  152. if err != nil {
  153. t.Fatalf(err.Error())
  154. }
  155. configData := os.ExpandEnv(string(fcontent))
  156. err = yaml.UnmarshalStrict([]byte(configData), &config)
  157. if err != nil {
  158. t.Fatalf(err.Error())
  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. ListenURI: "http://crowdsec.api",
  191. TLS: nil,
  192. DbConfig: &DatabaseCfg{
  193. DbPath: "./tests/test.db",
  194. Type: "sqlite",
  195. MaxOpenConns: types.IntPtr(DEFAULT_MAX_OPEN_CONNS),
  196. },
  197. ConsoleConfigPath: DefaultConfigPath("console.yaml"),
  198. ConsoleConfig: &ConsoleConfig{
  199. ShareManualDecisions: types.BoolPtr(false),
  200. ShareTaintedScenarios: types.BoolPtr(true),
  201. ShareCustomScenarios: types.BoolPtr(true),
  202. },
  203. LogDir: LogDirFullPath,
  204. LogMedia: "stdout",
  205. OnlineClient: &OnlineApiClientCfg{
  206. CredentialsFilePath: "./tests/online-api-secrets.yaml",
  207. Credentials: &ApiCredentialsCfg{
  208. URL: "http://crowdsec.api",
  209. Login: "test",
  210. Password: "testpassword",
  211. },
  212. },
  213. Profiles: tmpLAPI.Profiles,
  214. ProfilesPath: "./tests/profiles.yaml",
  215. UseForwardedForHeaders: false,
  216. },
  217. err: "",
  218. },
  219. {
  220. name: "basic invalid configuration",
  221. Input: &Config{
  222. Self: []byte(configData),
  223. API: &APICfg{
  224. Server: &LocalApiServerCfg{},
  225. },
  226. Common: &CommonCfg{
  227. LogDir: "./tests/",
  228. LogMedia: "stdout",
  229. },
  230. DisableAPI: false,
  231. },
  232. expectedResult: &LocalApiServerCfg{
  233. LogDir: LogDirFullPath,
  234. LogMedia: "stdout",
  235. },
  236. err: "while loading profiles for LAPI",
  237. },
  238. }
  239. for idx, test := range tests {
  240. err := test.Input.LoadAPIServer()
  241. if err == nil && test.err != "" {
  242. fmt.Printf("TEST '%s': NOK\n", test.name)
  243. t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests))
  244. } else if test.err != "" {
  245. if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) {
  246. fmt.Printf("TEST '%s': NOK\n", test.name)
  247. t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
  248. test.err,
  249. fmt.Sprintf("%s", err))
  250. }
  251. }
  252. isOk := assert.Equal(t, test.expectedResult, test.Input.API.Server)
  253. if !isOk {
  254. t.Fatalf("TEST '%s': NOK", test.name)
  255. } else {
  256. fmt.Printf("TEST '%s': OK\n", test.name)
  257. }
  258. }
  259. }