controller.go 3.6 KB

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