api_test.go 6.0 KB

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