require.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package require
  2. import (
  3. "fmt"
  4. "io"
  5. "github.com/sirupsen/logrus"
  6. "github.com/crowdsecurity/crowdsec/pkg/csconfig"
  7. "github.com/crowdsecurity/crowdsec/pkg/cwhub"
  8. )
  9. func LAPI(c *csconfig.Config) error {
  10. if err := c.LoadAPIServer(); err != nil {
  11. return fmt.Errorf("failed to load Local API: %w", err)
  12. }
  13. if c.DisableAPI {
  14. return fmt.Errorf("local API is disabled -- this command must be run on the local API machine")
  15. }
  16. return nil
  17. }
  18. func CAPI(c *csconfig.Config) error {
  19. if c.API.Server.OnlineClient == nil {
  20. return fmt.Errorf("no configuration for Central API (CAPI) in '%s'", *c.FilePath)
  21. }
  22. return nil
  23. }
  24. func PAPI(c *csconfig.Config) error {
  25. if c.API.Server.OnlineClient.Credentials.PapiURL == "" {
  26. return fmt.Errorf("no PAPI URL in configuration")
  27. }
  28. return nil
  29. }
  30. func CAPIRegistered(c *csconfig.Config) error {
  31. if c.API.Server.OnlineClient.Credentials == nil {
  32. return fmt.Errorf("the Central API (CAPI) must be configured with 'cscli capi register'")
  33. }
  34. return nil
  35. }
  36. func DB(c *csconfig.Config) error {
  37. if err := c.LoadDBConfig(); err != nil {
  38. return fmt.Errorf("this command requires direct database access (must be run on the local API machine): %w", err)
  39. }
  40. return nil
  41. }
  42. func Notifications(c *csconfig.Config) error {
  43. if c.ConfigPaths.NotificationDir == "" {
  44. return fmt.Errorf("config_paths.notification_dir is not set in crowdsec config")
  45. }
  46. return nil
  47. }
  48. // RemoteHub returns the configuration required to download hub index and items: url, branch, etc.
  49. func RemoteHub(c *csconfig.Config) *cwhub.RemoteHubCfg {
  50. // set branch in config, and log if necessary
  51. branch := HubBranch(c)
  52. urlTemplate := HubURLTemplate(c)
  53. remote := &cwhub.RemoteHubCfg{
  54. Branch: branch,
  55. URLTemplate: urlTemplate,
  56. IndexPath: ".index.json",
  57. }
  58. return remote
  59. }
  60. // Hub initializes the hub. If a remote configuration is provided, it can be used to download the index and items.
  61. // If no remote parameter is provided, the hub can only be used for local operations.
  62. func Hub(c *csconfig.Config, remote *cwhub.RemoteHubCfg, logger *logrus.Logger) (*cwhub.Hub, error) {
  63. local := c.Hub
  64. if local == nil {
  65. return nil, fmt.Errorf("you must configure cli before interacting with hub")
  66. }
  67. if logger == nil {
  68. logger = logrus.New()
  69. logger.SetOutput(io.Discard)
  70. }
  71. hub, err := cwhub.NewHub(local, remote, false, logger)
  72. if err != nil {
  73. return nil, fmt.Errorf("failed to read Hub index: %w. Run 'sudo cscli hub update' to download the index again", err)
  74. }
  75. return hub, nil
  76. }