api_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package csconfig
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. "github.com/crowdsecurity/crowdsec/pkg/types"
  10. "github.com/stretchr/testify/assert"
  11. "gopkg.in/yaml.v2"
  12. )
  13. func TestLoadLocalApiClientCfg(t *testing.T) {
  14. True := true
  15. tests := []struct {
  16. name string
  17. Input *LocalApiClientCfg
  18. expectedResult *ApiCredentialsCfg
  19. err string
  20. }{
  21. {
  22. name: "basic valid configuration",
  23. Input: &LocalApiClientCfg{
  24. CredentialsFilePath: "./tests/lapi-secrets.yaml",
  25. },
  26. expectedResult: &ApiCredentialsCfg{
  27. URL: "http://localhost:8080/",
  28. Login: "test",
  29. Password: "testpassword",
  30. },
  31. },
  32. {
  33. name: "invalid configuration",
  34. Input: &LocalApiClientCfg{
  35. CredentialsFilePath: "./tests/bad_lapi-secrets.yaml",
  36. },
  37. expectedResult: &ApiCredentialsCfg{},
  38. },
  39. {
  40. name: "invalid configuration filepath",
  41. Input: &LocalApiClientCfg{
  42. CredentialsFilePath: "./tests/nonexist_lapi-secrets.yaml",
  43. },
  44. expectedResult: nil,
  45. },
  46. {
  47. name: "valid configuration with insecure skip verify",
  48. Input: &LocalApiClientCfg{
  49. CredentialsFilePath: "./tests/lapi-secrets.yaml",
  50. InsecureSkipVerify: &True,
  51. },
  52. expectedResult: &ApiCredentialsCfg{
  53. URL: "http://localhost:8080/",
  54. Login: "test",
  55. Password: "testpassword",
  56. },
  57. },
  58. }
  59. for idx, test := range tests {
  60. fmt.Printf("TEST '%s'\n", test.name)
  61. err := test.Input.Load()
  62. if err == nil && test.err != "" {
  63. t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests))
  64. } else if test.err != "" {
  65. if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) {
  66. t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
  67. test.err,
  68. fmt.Sprintf("%s", err))
  69. }
  70. }
  71. isOk := assert.Equal(t, test.expectedResult, test.Input.Credentials)
  72. if !isOk {
  73. t.Fatalf("test '%s' failed", test.name)
  74. }
  75. }
  76. }
  77. func TestLoadOnlineApiClientCfg(t *testing.T) {
  78. tests := []struct {
  79. name string
  80. Input *OnlineApiClientCfg
  81. expectedResult *ApiCredentialsCfg
  82. err string
  83. }{
  84. {
  85. name: "basic valid configuration",
  86. Input: &OnlineApiClientCfg{
  87. CredentialsFilePath: "./tests/online-api-secrets.yaml",
  88. },
  89. expectedResult: &ApiCredentialsCfg{
  90. URL: "http://crowdsec.api",
  91. Login: "test",
  92. Password: "testpassword",
  93. },
  94. },
  95. {
  96. name: "invalid configuration",
  97. Input: &OnlineApiClientCfg{
  98. CredentialsFilePath: "./tests/bad_lapi-secrets.yaml",
  99. },
  100. expectedResult: &ApiCredentialsCfg{},
  101. err: "failed unmarshaling api server credentials",
  102. },
  103. {
  104. name: "missing field configuration",
  105. Input: &OnlineApiClientCfg{
  106. CredentialsFilePath: "./tests/bad_online-api-secrets.yaml",
  107. },
  108. expectedResult: nil,
  109. },
  110. {
  111. name: "invalid configuration filepath",
  112. Input: &OnlineApiClientCfg{
  113. CredentialsFilePath: "./tests/nonexist_online-api-secrets.yaml",
  114. },
  115. expectedResult: &ApiCredentialsCfg{},
  116. err: "failed to read api server credentials",
  117. },
  118. }
  119. for idx, test := range tests {
  120. err := test.Input.Load()
  121. if err == nil && test.err != "" {
  122. fmt.Printf("TEST '%s': NOK\n", test.name)
  123. t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests))
  124. } else if test.err != "" {
  125. if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) {
  126. fmt.Printf("TEST '%s': NOK\n", test.name)
  127. t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
  128. test.err,
  129. fmt.Sprintf("%s", err))
  130. }
  131. }
  132. isOk := assert.Equal(t, test.expectedResult, test.Input.Credentials)
  133. if !isOk {
  134. t.Fatalf("TEST '%s': NOK", test.name)
  135. } else {
  136. fmt.Printf("TEST '%s': OK\n", test.name)
  137. }
  138. }
  139. }
  140. func TestLoadAPIServer(t *testing.T) {
  141. tmpLAPI := &LocalApiServerCfg{
  142. ProfilesPath: "./tests/profiles.yaml",
  143. }
  144. if err := tmpLAPI.LoadProfiles(); err != nil {
  145. t.Fatalf("loading tmp profiles: %+v", err)
  146. }
  147. LogDirFullPath, err := filepath.Abs("./tests")
  148. if err != nil {
  149. t.Fatalf(err.Error())
  150. }
  151. config := &Config{}
  152. fcontent, err := ioutil.ReadFile("./tests/config.yaml")
  153. if err != nil {
  154. t.Fatalf(err.Error())
  155. }
  156. configData := os.ExpandEnv(string(fcontent))
  157. err = yaml.UnmarshalStrict([]byte(configData), &config)
  158. if err != nil {
  159. t.Fatalf(err.Error())
  160. }
  161. tests := []struct {
  162. name string
  163. Input *Config
  164. expectedResult *LocalApiServerCfg
  165. err string
  166. }{
  167. {
  168. name: "basic valid configuration",
  169. Input: &Config{
  170. Self: []byte(configData),
  171. API: &APICfg{
  172. Server: &LocalApiServerCfg{
  173. ListenURI: "http://crowdsec.api",
  174. OnlineClient: &OnlineApiClientCfg{
  175. CredentialsFilePath: "./tests/online-api-secrets.yaml",
  176. },
  177. ProfilesPath: "./tests/profiles.yaml",
  178. },
  179. },
  180. DbConfig: &DatabaseCfg{
  181. Type: "sqlite",
  182. DbPath: "./tests/test.db",
  183. },
  184. Common: &CommonCfg{
  185. LogDir: "./tests/",
  186. LogMedia: "stdout",
  187. },
  188. DisableAPI: false,
  189. },
  190. expectedResult: &LocalApiServerCfg{
  191. ListenURI: "http://crowdsec.api",
  192. TLS: nil,
  193. DbConfig: &DatabaseCfg{
  194. DbPath: "./tests/test.db",
  195. Type: "sqlite",
  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 valid 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. }