controller.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package controllers
  2. import (
  3. "context"
  4. "net/http"
  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. )
  12. type Controller struct {
  13. Ectx context.Context
  14. DBClient *database.Client
  15. Router *gin.Engine
  16. Profiles []*csconfig.ProfileCfg
  17. CAPIChan chan []*models.Alert
  18. Log *log.Logger
  19. }
  20. func (c *Controller) Init() error {
  21. if err := c.NewV1(); err != nil {
  22. return err
  23. }
  24. /* if we have a V2, just add
  25. if err := c.NewV2(); err != nil {
  26. return err
  27. }
  28. */
  29. return nil
  30. }
  31. func (c *Controller) NewV1() error {
  32. handlerV1, err := v1.New(c.DBClient, c.Ectx, c.Profiles, c.CAPIChan)
  33. if err != nil {
  34. return err
  35. }
  36. c.Router.Use(v1.PrometheusMiddleware())
  37. c.Router.HandleMethodNotAllowed = true
  38. c.Router.NoRoute(func(ctx *gin.Context) {
  39. ctx.AbortWithStatus(http.StatusNotFound)
  40. })
  41. c.Router.NoMethod(func(ctx *gin.Context) {
  42. ctx.AbortWithStatus(http.StatusMethodNotAllowed)
  43. })
  44. groupV1 := c.Router.Group("/v1")
  45. groupV1.POST("/watchers", handlerV1.CreateMachine)
  46. groupV1.POST("/watchers/login", handlerV1.Middlewares.JWT.Middleware.LoginHandler)
  47. jwtAuth := groupV1.Group("")
  48. jwtAuth.GET("/refresh_token", handlerV1.Middlewares.JWT.Middleware.RefreshHandler)
  49. jwtAuth.Use(handlerV1.Middlewares.JWT.Middleware.MiddlewareFunc(), v1.PrometheusMachinesMiddleware())
  50. {
  51. jwtAuth.POST("/alerts", handlerV1.CreateAlert)
  52. jwtAuth.GET("/alerts", handlerV1.FindAlerts)
  53. jwtAuth.HEAD("/alerts", handlerV1.FindAlerts)
  54. jwtAuth.GET("/alerts/:alert_id", handlerV1.FindAlertByID)
  55. jwtAuth.HEAD("/alerts/:alert_id", handlerV1.FindAlertByID)
  56. jwtAuth.DELETE("/alerts", handlerV1.DeleteAlerts)
  57. jwtAuth.DELETE("/decisions", handlerV1.DeleteDecisions)
  58. jwtAuth.DELETE("/decisions/:decision_id", handlerV1.DeleteDecisionById)
  59. }
  60. apiKeyAuth := groupV1.Group("")
  61. apiKeyAuth.Use(handlerV1.Middlewares.APIKey.MiddlewareFunc(), v1.PrometheusBouncersMiddleware())
  62. {
  63. apiKeyAuth.GET("/decisions", handlerV1.GetDecision)
  64. apiKeyAuth.HEAD("/decisions", handlerV1.GetDecision)
  65. apiKeyAuth.GET("/decisions/stream", handlerV1.StreamDecision)
  66. apiKeyAuth.HEAD("/decisions/stream", handlerV1.StreamDecision)
  67. }
  68. return nil
  69. }
  70. /*
  71. func (c *Controller) NewV2() error {
  72. handlerV2, err := v2.New(c.DBClient, c.Ectx)
  73. if err != nil {
  74. return err
  75. }
  76. v2 := c.Router.Group("/v2")
  77. v2.POST("/watchers", handlerV2.CreateMachine)
  78. v2.POST("/watchers/login", handlerV2.Middlewares.JWT.Middleware.LoginHandler)
  79. jwtAuth := v2.Group("")
  80. jwtAuth.GET("/refresh_token", handlerV2.Middlewares.JWT.Middleware.RefreshHandler)
  81. jwtAuth.Use(handlerV2.Middlewares.JWT.Middleware.MiddlewareFunc())
  82. {
  83. jwtAuth.POST("/alerts", handlerV2.CreateAlert)
  84. jwtAuth.GET("/alerts", handlerV2.FindAlerts)
  85. jwtAuth.DELETE("/alerts", handlerV2.DeleteAlerts)
  86. jwtAuth.DELETE("/decisions", handlerV2.DeleteDecisions)
  87. jwtAuth.DELETE("/decisions/:decision_id", handlerV2.DeleteDecisionById)
  88. }
  89. apiKeyAuth := v2.Group("")
  90. apiKeyAuth.Use(handlerV2.Middlewares.APIKey.MiddlewareFuncV2())
  91. {
  92. apiKeyAuth.GET("/decisions", handlerV2.GetDecision)
  93. apiKeyAuth.GET("/decisions/stream", handlerV2.StreamDecision)
  94. }
  95. return nil
  96. }
  97. */