controller.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package v1
  2. import (
  3. "context"
  4. "net"
  5. //"github.com/crowdsecurity/crowdsec/pkg/apiserver/controllers"
  6. middlewares "github.com/crowdsecurity/crowdsec/pkg/apiserver/middlewares/v1"
  7. "github.com/crowdsecurity/crowdsec/pkg/csconfig"
  8. "github.com/crowdsecurity/crowdsec/pkg/csplugin"
  9. "github.com/crowdsecurity/crowdsec/pkg/csprofiles"
  10. "github.com/crowdsecurity/crowdsec/pkg/database"
  11. "github.com/crowdsecurity/crowdsec/pkg/models"
  12. "github.com/pkg/errors"
  13. )
  14. type Controller struct {
  15. Ectx context.Context
  16. DBClient *database.Client
  17. APIKeyHeader string
  18. Middlewares *middlewares.Middlewares
  19. Profiles []*csprofiles.Runtime
  20. AlertsAddChan chan []*models.Alert
  21. DecisionDeleteChan chan []*models.Decision
  22. PluginChannel chan csplugin.ProfileAlert
  23. ConsoleConfig csconfig.ConsoleConfig
  24. TrustedIPs []net.IPNet
  25. }
  26. type ControllerV1Config struct {
  27. DbClient *database.Client
  28. Ctx context.Context
  29. ProfilesCfg []*csconfig.ProfileCfg
  30. AlertsAddChan chan []*models.Alert
  31. DecisionDeleteChan chan []*models.Decision
  32. PluginChannel chan csplugin.ProfileAlert
  33. ConsoleConfig csconfig.ConsoleConfig
  34. TrustedIPs []net.IPNet
  35. }
  36. func New(cfg *ControllerV1Config) (*Controller, error) {
  37. var err error
  38. profiles, err := csprofiles.NewProfile(cfg.ProfilesCfg)
  39. if err != nil {
  40. return &Controller{}, errors.Wrapf(err, "failed to compile profiles")
  41. }
  42. v1 := &Controller{
  43. Ectx: cfg.Ctx,
  44. DBClient: cfg.DbClient,
  45. APIKeyHeader: middlewares.APIKeyHeader,
  46. Profiles: profiles,
  47. AlertsAddChan: cfg.AlertsAddChan,
  48. DecisionDeleteChan: cfg.DecisionDeleteChan,
  49. PluginChannel: cfg.PluginChannel,
  50. ConsoleConfig: cfg.ConsoleConfig,
  51. TrustedIPs: cfg.TrustedIPs,
  52. }
  53. v1.Middlewares, err = middlewares.NewMiddlewares(cfg.DbClient)
  54. if err != nil {
  55. return v1, err
  56. }
  57. return v1, nil
  58. }