signals.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package cwapi
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "time"
  8. "github.com/crowdsecurity/crowdsec/pkg/types"
  9. log "github.com/sirupsen/logrus"
  10. )
  11. func (ctx *ApiCtx) AppendSignal(sig types.SignalOccurence) error {
  12. ctx.toPush = append(ctx.toPush, types.Event{Overflow: sig})
  13. log.Debugf("api append signal: adding new signal (cache size : %d): %+v", len(ctx.toPush), sig)
  14. return nil
  15. }
  16. func (ctx *ApiCtx) pushSignals() error {
  17. if len(ctx.toPush) == 0 {
  18. return nil
  19. }
  20. req, err := ctx.Http.New().Put(ctx.PushPath).BodyJSON(&ctx.toPush).Request()
  21. if err != nil {
  22. return fmt.Errorf("api push signal: HTTP request creation failed: %s", err)
  23. }
  24. log.Debugf("api push: URL: '%s'", req.URL)
  25. httpClient := http.Client{Timeout: 20 * time.Second}
  26. resp, err := httpClient.Do(req)
  27. if err != nil {
  28. return fmt.Errorf("api push signal: API call failed : %s", err)
  29. }
  30. defer resp.Body.Close()
  31. body, err := ioutil.ReadAll(resp.Body)
  32. if err != nil {
  33. return fmt.Errorf("failed to read body : %s", err)
  34. }
  35. log.Debugf("api push signal: HTTP Code: %+v | Body: %s \n", resp.StatusCode, string(body))
  36. if resp.StatusCode != 200 {
  37. if resp.StatusCode == 401 && !ctx.tokenExpired {
  38. log.Printf("api push signal: expired token, resigning to API")
  39. ctx.tokenExpired = true
  40. err := ctx.Signin()
  41. if err != nil {
  42. return err
  43. }
  44. log.Printf("api push signal: token renewed. Pushing signals")
  45. err = ctx.pushSignals()
  46. if err != nil {
  47. return fmt.Errorf("api push signal: unable to renew api session token: %s", err.Error())
  48. }
  49. } else {
  50. return fmt.Errorf("api push signal: return bad HTTP code (%d): %s", resp.StatusCode, string(body))
  51. }
  52. }
  53. if len(ctx.toPush) > 0 {
  54. log.Infof("api push signal: pushed %d signals successfully", len(ctx.toPush))
  55. }
  56. ctx.toPush = make([]types.Event, 0)
  57. ctx.tokenExpired = false
  58. return nil
  59. }
  60. func (ctx *ApiCtx) Flush() error {
  61. /*flag can be activated to dump to local file*/
  62. if ctx.DebugDump {
  63. log.Warningf("api flush: dumping api cache to ./api-dump.json")
  64. x, err := json.MarshalIndent(ctx.toPush, "", " ")
  65. if err != nil {
  66. return fmt.Errorf("api flush: failed to marshal data: %s", err)
  67. }
  68. if err := ioutil.WriteFile("./api-dump.json", x, 0755); err != nil {
  69. return fmt.Errorf("api flush: failed to write marshaled data : %s", err)
  70. }
  71. }
  72. //pretend we did stuff
  73. if ctx.Muted {
  74. return nil
  75. }
  76. if err := ctx.pushSignals(); err != nil {
  77. log.Errorf("api flush: fail to push signals: %s", err)
  78. }
  79. return nil
  80. }
  81. //This one is called on a regular basis (decided by init) and push stacked events to API
  82. func (ctx *ApiCtx) pushLoop() error {
  83. log.Debugf("api push loop: running with a ticker every 2 minutes")
  84. ticker := time.NewTicker(2 * time.Minute)
  85. for {
  86. select {
  87. case <-ticker.C: //push data.
  88. if len(ctx.toPush) == 0 {
  89. log.Debugf("api push loop: nothing to push")
  90. continue
  91. }
  92. err := ctx.Flush()
  93. if err != nil {
  94. log.Errorf("api push loop: %s", err.Error())
  95. }
  96. case <-ctx.PusherTomb.Dying(): //we are being killed by main
  97. log.Infof("Killing api routine")
  98. return nil
  99. }
  100. }
  101. }