controller.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package controllers
  2. import (
  3. "context"
  4. "net"
  5. "net/http"
  6. "github.com/alexliesenfeld/health"
  7. v1 "github.com/crowdsecurity/crowdsec/pkg/apiserver/controllers/v1"
  8. "github.com/crowdsecurity/crowdsec/pkg/csconfig"
  9. "github.com/crowdsecurity/crowdsec/pkg/csplugin"
  10. "github.com/crowdsecurity/crowdsec/pkg/database"
  11. "github.com/crowdsecurity/crowdsec/pkg/models"
  12. "github.com/gin-gonic/gin"
  13. log "github.com/sirupsen/logrus"
  14. )
  15. type Controller struct {
  16. Ectx context.Context
  17. DBClient *database.Client
  18. Router *gin.Engine
  19. Profiles []*csconfig.ProfileCfg
  20. AlertsAddChan chan []*models.Alert
  21. DecisionDeleteChan chan []*models.Decision
  22. PluginChannel chan csplugin.ProfileAlert
  23. Log *log.Logger
  24. ConsoleConfig *csconfig.ConsoleConfig
  25. TrustedIPs []net.IPNet
  26. HandlerV1 *v1.Controller
  27. }
  28. func (c *Controller) Init() error {
  29. if err := c.NewV1(); err != nil {
  30. return err
  31. }
  32. /* if we have a V2, just add
  33. if err := c.NewV2(); err != nil {
  34. return err
  35. }
  36. */
  37. return nil
  38. }
  39. // endpoint for health checking
  40. func serveHealth() http.HandlerFunc {
  41. checker := health.NewChecker(
  42. // just simple up/down status is enough
  43. health.WithDisabledDetails(),
  44. // no caching required
  45. health.WithDisabledCache(),
  46. )
  47. return health.NewHandler(checker)
  48. }
  49. func (c *Controller) NewV1() error {
  50. var err error
  51. v1Config := v1.ControllerV1Config{
  52. DbClient: c.DBClient,
  53. Ctx: c.Ectx,
  54. ProfilesCfg: c.Profiles,
  55. DecisionDeleteChan: c.DecisionDeleteChan,
  56. AlertsAddChan: c.AlertsAddChan,
  57. PluginChannel: c.PluginChannel,
  58. ConsoleConfig: *c.ConsoleConfig,
  59. TrustedIPs: c.TrustedIPs,
  60. }
  61. c.HandlerV1, err = v1.New(&v1Config)
  62. if err != nil {
  63. return err
  64. }
  65. c.Router.GET("/health", gin.WrapF(serveHealth()))
  66. c.Router.Use(v1.PrometheusMiddleware())
  67. c.Router.HandleMethodNotAllowed = true
  68. c.Router.NoRoute(func(ctx *gin.Context) {
  69. ctx.AbortWithStatus(http.StatusNotFound)
  70. })
  71. c.Router.NoMethod(func(ctx *gin.Context) {
  72. ctx.AbortWithStatus(http.StatusMethodNotAllowed)
  73. })
  74. groupV1 := c.Router.Group("/v1")
  75. groupV1.POST("/watchers", c.HandlerV1.CreateMachine)
  76. groupV1.POST("/watchers/login", c.HandlerV1.Middlewares.JWT.Middleware.LoginHandler)
  77. jwtAuth := groupV1.Group("")
  78. jwtAuth.GET("/refresh_token", c.HandlerV1.Middlewares.JWT.Middleware.RefreshHandler)
  79. jwtAuth.Use(c.HandlerV1.Middlewares.JWT.Middleware.MiddlewareFunc(), v1.PrometheusMachinesMiddleware())
  80. {
  81. jwtAuth.POST("/alerts", c.HandlerV1.CreateAlert)
  82. jwtAuth.GET("/alerts", c.HandlerV1.FindAlerts)
  83. jwtAuth.HEAD("/alerts", c.HandlerV1.FindAlerts)
  84. jwtAuth.GET("/alerts/:alert_id", c.HandlerV1.FindAlertByID)
  85. jwtAuth.HEAD("/alerts/:alert_id", c.HandlerV1.FindAlertByID)
  86. jwtAuth.DELETE("/alerts/:alert_id", c.HandlerV1.DeleteAlertByID)
  87. jwtAuth.DELETE("/alerts", c.HandlerV1.DeleteAlerts)
  88. jwtAuth.DELETE("/decisions", c.HandlerV1.DeleteDecisions)
  89. jwtAuth.DELETE("/decisions/:decision_id", c.HandlerV1.DeleteDecisionById)
  90. jwtAuth.GET("/heartbeat", c.HandlerV1.HeartBeat)
  91. }
  92. apiKeyAuth := groupV1.Group("")
  93. apiKeyAuth.Use(c.HandlerV1.Middlewares.APIKey.MiddlewareFunc(), v1.PrometheusBouncersMiddleware())
  94. {
  95. apiKeyAuth.GET("/decisions", c.HandlerV1.GetDecision)
  96. apiKeyAuth.HEAD("/decisions", c.HandlerV1.GetDecision)
  97. apiKeyAuth.GET("/decisions/stream", c.HandlerV1.StreamDecision)
  98. apiKeyAuth.HEAD("/decisions/stream", c.HandlerV1.StreamDecision)
  99. }
  100. return nil
  101. }
  102. /*
  103. func (c *Controller) NewV2() error {
  104. handlerV2, err := v2.New(c.DBClient, c.Ectx)
  105. if err != nil {
  106. return err
  107. }
  108. v2 := c.Router.Group("/v2")
  109. v2.POST("/watchers", handlerV2.CreateMachine)
  110. v2.POST("/watchers/login", handlerV2.Middlewares.JWT.Middleware.LoginHandler)
  111. jwtAuth := v2.Group("")
  112. jwtAuth.GET("/refresh_token", handlerV2.Middlewares.JWT.Middleware.RefreshHandler)
  113. jwtAuth.Use(handlerV2.Middlewares.JWT.Middleware.MiddlewareFunc())
  114. {
  115. jwtAuth.POST("/alerts", handlerV2.CreateAlert)
  116. jwtAuth.GET("/alerts", handlerV2.FindAlerts)
  117. jwtAuth.DELETE("/alerts", handlerV2.DeleteAlerts)
  118. jwtAuth.DELETE("/decisions", handlerV2.DeleteDecisions)
  119. jwtAuth.DELETE("/decisions/:decision_id", handlerV2.DeleteDecisionById)
  120. }
  121. apiKeyAuth := v2.Group("")
  122. apiKeyAuth.Use(handlerV2.Middlewares.APIKey.MiddlewareFuncV2())
  123. {
  124. apiKeyAuth.GET("/decisions", handlerV2.GetDecision)
  125. apiKeyAuth.GET("/decisions/stream", handlerV2.StreamDecision)
  126. }
  127. return nil
  128. }
  129. */