file_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. package fileacquisition_test
  2. import (
  3. "fmt"
  4. "os"
  5. "runtime"
  6. "testing"
  7. "time"
  8. fileacquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/file"
  9. "github.com/crowdsecurity/crowdsec/pkg/cstest"
  10. "github.com/crowdsecurity/crowdsec/pkg/types"
  11. log "github.com/sirupsen/logrus"
  12. "github.com/sirupsen/logrus/hooks/test"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. "gopkg.in/tomb.v2"
  16. )
  17. func TestBadConfiguration(t *testing.T) {
  18. tests := []struct {
  19. name string
  20. config string
  21. expectedErr string
  22. }{
  23. {
  24. name: "extra configuration key",
  25. config: "foobar: asd.log",
  26. expectedErr: "line 1: field foobar not found in type fileacquisition.FileConfiguration",
  27. },
  28. {
  29. name: "missing filenames",
  30. config: "mode: tail",
  31. expectedErr: "no filename or filenames configuration provided",
  32. },
  33. {
  34. name: "glob syntax error",
  35. config: `filename: "[asd-.log"`,
  36. expectedErr: "Glob failure: syntax error in pattern",
  37. },
  38. {
  39. name: "bad exclude regexp",
  40. config: `filenames: ["asd.log"]
  41. exclude_regexps: ["as[a-$d"]`,
  42. expectedErr: "Could not compile regexp as",
  43. },
  44. }
  45. subLogger := log.WithFields(log.Fields{
  46. "type": "file",
  47. })
  48. for _, tc := range tests {
  49. tc := tc
  50. t.Run(tc.name, func(t *testing.T) {
  51. f := fileacquisition.FileSource{}
  52. err := f.Configure([]byte(tc.config), subLogger)
  53. cstest.RequireErrorContains(t, err, tc.expectedErr)
  54. })
  55. }
  56. }
  57. func TestConfigureDSN(t *testing.T) {
  58. file := "/etc/passwd"
  59. if runtime.GOOS == "windows" {
  60. file = `C:\Windows\System32\drivers\etc\hosts`
  61. }
  62. tests := []struct {
  63. dsn string
  64. expectedErr string
  65. }{
  66. {
  67. dsn: "asd://",
  68. expectedErr: "invalid DSN asd:// for file source, must start with file://",
  69. },
  70. {
  71. dsn: "file://",
  72. expectedErr: "empty file:// DSN",
  73. },
  74. {
  75. dsn: fmt.Sprintf("file://%s?log_level=warn", file),
  76. },
  77. {
  78. dsn: fmt.Sprintf("file://%s?log_level=foobar", file),
  79. expectedErr: "unknown level foobar: not a valid logrus Level:",
  80. },
  81. }
  82. subLogger := log.WithFields(log.Fields{
  83. "type": "file",
  84. })
  85. for _, tc := range tests {
  86. tc := tc
  87. t.Run(tc.dsn, func(t *testing.T) {
  88. f := fileacquisition.FileSource{}
  89. err := f.ConfigureByDSN(tc.dsn, map[string]string{"type": "testtype"}, subLogger)
  90. cstest.RequireErrorContains(t, err, tc.expectedErr)
  91. })
  92. }
  93. }
  94. func TestOneShot(t *testing.T) {
  95. permDeniedFile := "/etc/shadow"
  96. permDeniedError := "failed opening /etc/shadow: open /etc/shadow: permission denied"
  97. if runtime.GOOS == "windows" {
  98. // Technically, this is not a permission denied error, but we just want to test what happens
  99. // if we do not have access to the file
  100. permDeniedFile = `C:\Windows\System32\config\SAM`
  101. permDeniedError = `failed opening C:\Windows\System32\config\SAM: open C:\Windows\System32\config\SAM: The process cannot access the file because it is being used by another process.`
  102. }
  103. tests := []struct {
  104. name string
  105. config string
  106. expectedConfigErr string
  107. expectedErr string
  108. expectedOutput string
  109. expectedLines int
  110. logLevel log.Level
  111. setup func()
  112. afterConfigure func()
  113. teardown func()
  114. }{
  115. {
  116. name: "permission denied",
  117. config: fmt.Sprintf(`
  118. mode: cat
  119. filename: %s`, permDeniedFile),
  120. expectedErr: permDeniedError,
  121. logLevel: log.WarnLevel,
  122. expectedLines: 0,
  123. },
  124. {
  125. name: "ignored directory",
  126. config: `
  127. mode: cat
  128. filename: /`,
  129. expectedOutput: "/ is a directory, ignoring it",
  130. logLevel: log.WarnLevel,
  131. expectedLines: 0,
  132. },
  133. {
  134. name: "glob syntax error",
  135. config: `
  136. mode: cat
  137. filename: "[*-.log"`,
  138. expectedConfigErr: "Glob failure: syntax error in pattern",
  139. logLevel: log.WarnLevel,
  140. expectedLines: 0,
  141. },
  142. {
  143. name: "no matching files",
  144. config: `
  145. mode: cat
  146. filename: /do/not/exist`,
  147. expectedOutput: "No matching files for pattern /do/not/exist",
  148. logLevel: log.WarnLevel,
  149. expectedLines: 0,
  150. },
  151. {
  152. name: "test.log",
  153. config: `
  154. mode: cat
  155. filename: test_files/test.log`,
  156. expectedLines: 5,
  157. logLevel: log.WarnLevel,
  158. },
  159. {
  160. name: "test.log.gz",
  161. config: `
  162. mode: cat
  163. filename: test_files/test.log.gz`,
  164. expectedLines: 5,
  165. logLevel: log.WarnLevel,
  166. },
  167. {
  168. name: "unexpected end of gzip stream",
  169. config: `
  170. mode: cat
  171. filename: test_files/bad.gz`,
  172. expectedErr: "failed to read gz test_files/bad.gz: unexpected EOF",
  173. expectedLines: 0,
  174. logLevel: log.WarnLevel,
  175. },
  176. {
  177. name: "deleted file",
  178. config: `
  179. mode: cat
  180. filename: test_files/test_delete.log`,
  181. setup: func() {
  182. f, _ := os.Create("test_files/test_delete.log")
  183. f.Close()
  184. },
  185. afterConfigure: func() {
  186. os.Remove("test_files/test_delete.log")
  187. },
  188. expectedErr: "could not stat file test_files/test_delete.log",
  189. },
  190. }
  191. for _, tc := range tests {
  192. tc := tc
  193. t.Run(tc.name, func(t *testing.T) {
  194. logger, hook := test.NewNullLogger()
  195. logger.SetLevel(tc.logLevel)
  196. subLogger := logger.WithFields(log.Fields{
  197. "type": "file",
  198. })
  199. tomb := tomb.Tomb{}
  200. out := make(chan types.Event)
  201. f := fileacquisition.FileSource{}
  202. if tc.setup != nil {
  203. tc.setup()
  204. }
  205. err := f.Configure([]byte(tc.config), subLogger)
  206. cstest.RequireErrorContains(t, err, tc.expectedConfigErr)
  207. if tc.expectedConfigErr != "" {
  208. return
  209. }
  210. if tc.afterConfigure != nil {
  211. tc.afterConfigure()
  212. }
  213. actualLines := 0
  214. if tc.expectedLines != 0 {
  215. go func() {
  216. for {
  217. select {
  218. case <-out:
  219. actualLines++
  220. case <-time.After(2 * time.Second):
  221. return
  222. }
  223. }
  224. }()
  225. }
  226. err = f.OneShotAcquisition(out, &tomb)
  227. cstest.RequireErrorContains(t, err, tc.expectedErr)
  228. if tc.expectedLines != 0 {
  229. assert.Equal(t, tc.expectedLines, actualLines)
  230. }
  231. if tc.expectedOutput != "" {
  232. assert.Contains(t, hook.LastEntry().Message, tc.expectedOutput)
  233. hook.Reset()
  234. }
  235. if tc.teardown != nil {
  236. tc.teardown()
  237. }
  238. })
  239. }
  240. }
  241. func TestLiveAcquisition(t *testing.T) {
  242. permDeniedFile := "/etc/shadow"
  243. permDeniedError := "unable to read /etc/shadow : open /etc/shadow: permission denied"
  244. testPattern := "test_files/*.log"
  245. if runtime.GOOS == "windows" {
  246. // Technically, this is not a permission denied error, but we just want to test what happens
  247. // if we do not have access to the file
  248. permDeniedFile = `C:\Windows\System32\config\SAM`
  249. permDeniedError = `unable to read C:\Windows\System32\config\SAM : open C:\Windows\System32\config\SAM: The process cannot access the file because it is being used by another process`
  250. testPattern = `test_files\\*.log` // the \ must be escaped for the yaml config
  251. }
  252. tests := []struct {
  253. name string
  254. config string
  255. expectedErr string
  256. expectedOutput string
  257. expectedLines int
  258. logLevel log.Level
  259. setup func()
  260. afterConfigure func()
  261. teardown func()
  262. }{
  263. {
  264. config: fmt.Sprintf(`
  265. mode: tail
  266. filename: %s`, permDeniedFile),
  267. expectedOutput: permDeniedError,
  268. logLevel: log.InfoLevel,
  269. expectedLines: 0,
  270. name: "PermissionDenied",
  271. },
  272. {
  273. config: `
  274. mode: tail
  275. filename: /`,
  276. expectedOutput: "/ is a directory, ignoring it",
  277. logLevel: log.WarnLevel,
  278. expectedLines: 0,
  279. name: "Directory",
  280. },
  281. {
  282. config: `
  283. mode: tail
  284. filename: /do/not/exist`,
  285. expectedOutput: "No matching files for pattern /do/not/exist",
  286. logLevel: log.WarnLevel,
  287. expectedLines: 0,
  288. name: "badPattern",
  289. },
  290. {
  291. config: fmt.Sprintf(`
  292. mode: tail
  293. filenames:
  294. - %s
  295. force_inotify: true`, testPattern),
  296. expectedLines: 5,
  297. logLevel: log.DebugLevel,
  298. name: "basicGlob",
  299. },
  300. {
  301. config: fmt.Sprintf(`
  302. mode: tail
  303. filenames:
  304. - %s
  305. force_inotify: true`, testPattern),
  306. expectedLines: 0,
  307. logLevel: log.DebugLevel,
  308. name: "GlobInotify",
  309. afterConfigure: func() {
  310. f, _ := os.Create("test_files/a.log")
  311. f.Close()
  312. time.Sleep(1 * time.Second)
  313. os.Remove("test_files/a.log")
  314. },
  315. },
  316. {
  317. config: fmt.Sprintf(`
  318. mode: tail
  319. filenames:
  320. - %s
  321. force_inotify: true`, testPattern),
  322. expectedLines: 5,
  323. logLevel: log.DebugLevel,
  324. name: "GlobInotifyChmod",
  325. afterConfigure: func() {
  326. f, _ := os.Create("test_files/a.log")
  327. f.Close()
  328. time.Sleep(1 * time.Second)
  329. os.Chmod("test_files/a.log", 0o000)
  330. },
  331. teardown: func() {
  332. os.Chmod("test_files/a.log", 0o644)
  333. os.Remove("test_files/a.log")
  334. },
  335. },
  336. {
  337. config: fmt.Sprintf(`
  338. mode: tail
  339. filenames:
  340. - %s
  341. force_inotify: true`, testPattern),
  342. expectedLines: 5,
  343. logLevel: log.DebugLevel,
  344. name: "InotifyMkDir",
  345. afterConfigure: func() {
  346. os.Mkdir("test_files/pouet/", 0o700)
  347. },
  348. teardown: func() {
  349. os.Remove("test_files/pouet/")
  350. },
  351. },
  352. }
  353. for _, tc := range tests {
  354. tc := tc
  355. t.Run(tc.name, func(t *testing.T) {
  356. logger, hook := test.NewNullLogger()
  357. logger.SetLevel(tc.logLevel)
  358. subLogger := logger.WithFields(log.Fields{
  359. "type": "file",
  360. })
  361. tomb := tomb.Tomb{}
  362. out := make(chan types.Event)
  363. f := fileacquisition.FileSource{}
  364. if tc.setup != nil {
  365. tc.setup()
  366. }
  367. err := f.Configure([]byte(tc.config), subLogger)
  368. require.NoError(t, err)
  369. if tc.afterConfigure != nil {
  370. tc.afterConfigure()
  371. }
  372. actualLines := 0
  373. if tc.expectedLines != 0 {
  374. go func() {
  375. for {
  376. select {
  377. case <-out:
  378. actualLines++
  379. case <-time.After(2 * time.Second):
  380. return
  381. }
  382. }
  383. }()
  384. }
  385. err = f.StreamingAcquisition(out, &tomb)
  386. cstest.RequireErrorContains(t, err, tc.expectedErr)
  387. if tc.expectedLines != 0 {
  388. fd, err := os.Create("test_files/stream.log")
  389. if err != nil {
  390. t.Fatalf("could not create test file : %s", err)
  391. }
  392. for i := 0; i < 5; i++ {
  393. _, err = fmt.Fprintf(fd, "%d\n", i)
  394. if err != nil {
  395. t.Fatalf("could not write test file : %s", err)
  396. os.Remove("test_files/stream.log")
  397. }
  398. }
  399. fd.Close()
  400. // we sleep to make sure we detect the new file
  401. time.Sleep(1 * time.Second)
  402. os.Remove("test_files/stream.log")
  403. assert.Equal(t, tc.expectedLines, actualLines)
  404. }
  405. if tc.expectedOutput != "" {
  406. if hook.LastEntry() == nil {
  407. t.Fatalf("expected output %s, but got nothing", tc.expectedOutput)
  408. }
  409. assert.Contains(t, hook.LastEntry().Message, tc.expectedOutput)
  410. hook.Reset()
  411. }
  412. if tc.teardown != nil {
  413. tc.teardown()
  414. }
  415. tomb.Kill(nil)
  416. })
  417. }
  418. }
  419. func TestExclusion(t *testing.T) {
  420. config := `filenames: ["test_files/*.log*"]
  421. exclude_regexps: ["\\.gz$"]`
  422. logger, hook := test.NewNullLogger()
  423. // logger.SetLevel(ts.logLevel)
  424. subLogger := logger.WithFields(log.Fields{
  425. "type": "file",
  426. })
  427. f := fileacquisition.FileSource{}
  428. if err := f.Configure([]byte(config), subLogger); err != nil {
  429. subLogger.Fatalf("unexpected error: %s", err)
  430. }
  431. expectedLogOutput := "Skipping file test_files/test.log.gz as it matches exclude pattern"
  432. if runtime.GOOS == "windows" {
  433. expectedLogOutput = `Skipping file test_files\test.log.gz as it matches exclude pattern \.gz`
  434. }
  435. if hook.LastEntry() == nil {
  436. t.Fatalf("expected output %s, but got nothing", expectedLogOutput)
  437. }
  438. assert.Contains(t, hook.LastEntry().Message, expectedLogOutput)
  439. hook.Reset()
  440. }