controller.go 3.8 KB

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