controller.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. DisableRemoteLapiRegistration bool
  28. }
  29. func (c *Controller) Init() error {
  30. if err := c.NewV1(); err != nil {
  31. return err
  32. }
  33. /* if we have a V2, just add
  34. if err := c.NewV2(); err != nil {
  35. return err
  36. }
  37. */
  38. return nil
  39. }
  40. // endpoint for health checking
  41. func serveHealth() http.HandlerFunc {
  42. checker := health.NewChecker(
  43. // just simple up/down status is enough
  44. health.WithDisabledDetails(),
  45. // no caching required
  46. health.WithDisabledCache(),
  47. )
  48. return health.NewHandler(checker)
  49. }
  50. func (c *Controller) NewV1() error {
  51. var err error
  52. v1Config := v1.ControllerV1Config{
  53. DbClient: c.DBClient,
  54. Ctx: c.Ectx,
  55. ProfilesCfg: c.Profiles,
  56. DecisionDeleteChan: c.DecisionDeleteChan,
  57. AlertsAddChan: c.AlertsAddChan,
  58. PluginChannel: c.PluginChannel,
  59. ConsoleConfig: *c.ConsoleConfig,
  60. TrustedIPs: c.TrustedIPs,
  61. }
  62. c.HandlerV1, err = v1.New(&v1Config)
  63. if err != nil {
  64. return err
  65. }
  66. c.Router.GET("/health", gin.WrapF(serveHealth()))
  67. c.Router.Use(v1.PrometheusMiddleware())
  68. c.Router.HandleMethodNotAllowed = true
  69. c.Router.NoRoute(func(ctx *gin.Context) {
  70. ctx.AbortWithStatus(http.StatusNotFound)
  71. })
  72. c.Router.NoMethod(func(ctx *gin.Context) {
  73. ctx.AbortWithStatus(http.StatusMethodNotAllowed)
  74. })
  75. groupV1 := c.Router.Group("/v1")
  76. groupV1.POST("/watchers", c.HandlerV1.AbortRemoteIf(c.DisableRemoteLapiRegistration), c.HandlerV1.CreateMachine)
  77. groupV1.POST("/watchers/login", c.HandlerV1.Middlewares.JWT.Middleware.LoginHandler)
  78. jwtAuth := groupV1.Group("")
  79. jwtAuth.GET("/refresh_token", c.HandlerV1.Middlewares.JWT.Middleware.RefreshHandler)
  80. jwtAuth.Use(c.HandlerV1.Middlewares.JWT.Middleware.MiddlewareFunc(), v1.PrometheusMachinesMiddleware())
  81. {
  82. jwtAuth.POST("/alerts", c.HandlerV1.CreateAlert)
  83. jwtAuth.GET("/alerts", c.HandlerV1.FindAlerts)
  84. jwtAuth.HEAD("/alerts", c.HandlerV1.FindAlerts)
  85. jwtAuth.GET("/alerts/:alert_id", c.HandlerV1.FindAlertByID)
  86. jwtAuth.HEAD("/alerts/:alert_id", c.HandlerV1.FindAlertByID)
  87. jwtAuth.DELETE("/alerts/:alert_id", c.HandlerV1.DeleteAlertByID)
  88. jwtAuth.DELETE("/alerts", c.HandlerV1.DeleteAlerts)
  89. jwtAuth.DELETE("/decisions", c.HandlerV1.DeleteDecisions)
  90. jwtAuth.DELETE("/decisions/:decision_id", c.HandlerV1.DeleteDecisionById)
  91. jwtAuth.GET("/heartbeat", c.HandlerV1.HeartBeat)
  92. }
  93. apiKeyAuth := groupV1.Group("")
  94. apiKeyAuth.Use(c.HandlerV1.Middlewares.APIKey.MiddlewareFunc(), v1.PrometheusBouncersMiddleware())
  95. {
  96. apiKeyAuth.GET("/decisions", c.HandlerV1.GetDecision)
  97. apiKeyAuth.HEAD("/decisions", c.HandlerV1.GetDecision)
  98. apiKeyAuth.GET("/decisions/stream", c.HandlerV1.StreamDecision)
  99. apiKeyAuth.HEAD("/decisions/stream", c.HandlerV1.StreamDecision)
  100. }
  101. return nil
  102. }
  103. /*
  104. func (c *Controller) NewV2() error {
  105. handlerV2, err := v2.New(c.DBClient, c.Ectx)
  106. if err != nil {
  107. return err
  108. }
  109. v2 := c.Router.Group("/v2")
  110. v2.POST("/watchers", handlerV2.CreateMachine)
  111. v2.POST("/watchers/login", handlerV2.Middlewares.JWT.Middleware.LoginHandler)
  112. jwtAuth := v2.Group("")
  113. jwtAuth.GET("/refresh_token", handlerV2.Middlewares.JWT.Middleware.RefreshHandler)
  114. jwtAuth.Use(handlerV2.Middlewares.JWT.Middleware.MiddlewareFunc())
  115. {
  116. jwtAuth.POST("/alerts", handlerV2.CreateAlert)
  117. jwtAuth.GET("/alerts", handlerV2.FindAlerts)
  118. jwtAuth.DELETE("/alerts", handlerV2.DeleteAlerts)
  119. jwtAuth.DELETE("/decisions", handlerV2.DeleteDecisions)
  120. jwtAuth.DELETE("/decisions/:decision_id", handlerV2.DeleteDecisionById)
  121. }
  122. apiKeyAuth := v2.Group("")
  123. apiKeyAuth.Use(handlerV2.Middlewares.APIKey.MiddlewareFuncV2())
  124. {
  125. apiKeyAuth.GET("/decisions", handlerV2.GetDecision)
  126. apiKeyAuth.GET("/decisions/stream", handlerV2.StreamDecision)
  127. }
  128. return nil
  129. }
  130. */