controller.go 4.2 KB

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