controller.go 3.8 KB

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