hub.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package main
  2. import (
  3. "fmt"
  4. colorable "github.com/mattn/go-colorable"
  5. log "github.com/sirupsen/logrus"
  6. "github.com/spf13/cobra"
  7. "github.com/crowdsecurity/crowdsec/pkg/cwhub"
  8. )
  9. func NewHubCmd() *cobra.Command {
  10. /* ---- HUB COMMAND */
  11. var cmdHub = &cobra.Command{
  12. Use: "hub [action]",
  13. Short: "Manage Hub",
  14. Long: `
  15. Hub management
  16. List/update parsers/scenarios/postoverflows/collections from [Crowdsec Hub](https://hub.crowdsec.net).
  17. The Hub is managed by cscli, to get the latest hub files from [Crowdsec Hub](https://hub.crowdsec.net), you need to update.
  18. `,
  19. Example: `
  20. cscli hub list # List all installed configurations
  21. cscli hub update # Download list of available configurations from the hub
  22. `,
  23. Args: cobra.ExactArgs(0),
  24. DisableAutoGenTag: true,
  25. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  26. if csConfig.Cscli == nil {
  27. return fmt.Errorf("you must configure cli before interacting with hub")
  28. }
  29. return nil
  30. },
  31. }
  32. cmdHub.PersistentFlags().StringVarP(&cwhub.HubBranch, "branch", "b", "", "Use given branch from hub")
  33. var cmdHubList = &cobra.Command{
  34. Use: "list [-a]",
  35. Short: "List installed configs",
  36. Args: cobra.ExactArgs(0),
  37. DisableAutoGenTag: true,
  38. Run: func(cmd *cobra.Command, args []string) {
  39. if err := csConfig.LoadHub(); err != nil {
  40. log.Fatal(err)
  41. }
  42. if err := cwhub.GetHubIdx(csConfig.Hub); err != nil {
  43. log.Info("Run 'sudo cscli hub update' to get the hub index")
  44. log.Fatalf("Failed to get Hub index : %v", err)
  45. }
  46. //use LocalSync to get warnings about tainted / outdated items
  47. _, warn := cwhub.LocalSync(csConfig.Hub)
  48. for _, v := range warn {
  49. log.Info(v)
  50. }
  51. cwhub.DisplaySummary()
  52. ListItems(colorable.NewColorableStdout(), []string{
  53. cwhub.COLLECTIONS, cwhub.PARSERS, cwhub.SCENARIOS, cwhub.PARSERS_OVFLW,
  54. }, args, true, false, all)
  55. },
  56. }
  57. cmdHubList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List disabled items as well")
  58. cmdHub.AddCommand(cmdHubList)
  59. var cmdHubUpdate = &cobra.Command{
  60. Use: "update",
  61. Short: "Fetch available configs from hub",
  62. Long: `
  63. Fetches the [.index.json](https://github.com/crowdsecurity/hub/blob/master/.index.json) file from hub, containing the list of available configs.
  64. `,
  65. Args: cobra.ExactArgs(0),
  66. DisableAutoGenTag: true,
  67. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  68. if csConfig.Cscli == nil {
  69. return fmt.Errorf("you must configure cli before interacting with hub")
  70. }
  71. if err := cwhub.SetHubBranch(); err != nil {
  72. return fmt.Errorf("error while setting hub branch: %s", err)
  73. }
  74. return nil
  75. },
  76. Run: func(cmd *cobra.Command, args []string) {
  77. if err := csConfig.LoadHub(); err != nil {
  78. log.Fatal(err)
  79. }
  80. if err := cwhub.UpdateHubIdx(csConfig.Hub); err != nil {
  81. log.Fatalf("Failed to get Hub index : %v", err)
  82. }
  83. //use LocalSync to get warnings about tainted / outdated items
  84. _, warn := cwhub.LocalSync(csConfig.Hub)
  85. for _, v := range warn {
  86. log.Info(v)
  87. }
  88. },
  89. }
  90. cmdHub.AddCommand(cmdHubUpdate)
  91. var cmdHubUpgrade = &cobra.Command{
  92. Use: "upgrade",
  93. Short: "Upgrade all configs installed from hub",
  94. Long: `
  95. Upgrade all configs installed from Crowdsec Hub. Run 'sudo cscli hub update' if you want the latest versions available.
  96. `,
  97. Args: cobra.ExactArgs(0),
  98. DisableAutoGenTag: true,
  99. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  100. if csConfig.Cscli == nil {
  101. return fmt.Errorf("you must configure cli before interacting with hub")
  102. }
  103. if err := cwhub.SetHubBranch(); err != nil {
  104. return fmt.Errorf("error while setting hub branch: %s", err)
  105. }
  106. return nil
  107. },
  108. Run: func(cmd *cobra.Command, args []string) {
  109. if err := csConfig.LoadHub(); err != nil {
  110. log.Fatal(err)
  111. }
  112. if err := cwhub.GetHubIdx(csConfig.Hub); err != nil {
  113. log.Info("Run 'sudo cscli hub update' to get the hub index")
  114. log.Fatalf("Failed to get Hub index : %v", err)
  115. }
  116. log.Infof("Upgrading collections")
  117. cwhub.UpgradeConfig(csConfig, cwhub.COLLECTIONS, "", forceAction)
  118. log.Infof("Upgrading parsers")
  119. cwhub.UpgradeConfig(csConfig, cwhub.PARSERS, "", forceAction)
  120. log.Infof("Upgrading scenarios")
  121. cwhub.UpgradeConfig(csConfig, cwhub.SCENARIOS, "", forceAction)
  122. log.Infof("Upgrading postoverflows")
  123. cwhub.UpgradeConfig(csConfig, cwhub.PARSERS_OVFLW, "", forceAction)
  124. },
  125. }
  126. cmdHubUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files")
  127. cmdHub.AddCommand(cmdHubUpgrade)
  128. return cmdHub
  129. }