postoverflows.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/fatih/color"
  5. log "github.com/sirupsen/logrus"
  6. "github.com/spf13/cobra"
  7. "github.com/crowdsecurity/crowdsec/pkg/cwhub"
  8. )
  9. func NewPostOverflowsCmd() *cobra.Command {
  10. cmdPostOverflows := &cobra.Command{
  11. Use: "postoverflows [action] [config]",
  12. Short: "Install/Remove/Upgrade/Inspect postoverflow(s) from hub",
  13. Example: `cscli postoverflows install crowdsecurity/cdn-whitelist
  14. cscli postoverflows inspect crowdsecurity/cdn-whitelist
  15. cscli postoverflows upgrade crowdsecurity/cdn-whitelist
  16. cscli postoverflows list
  17. cscli postoverflows remove crowdsecurity/cdn-whitelist`,
  18. Args: cobra.MinimumNArgs(1),
  19. Aliases: []string{"postoverflow"},
  20. DisableAutoGenTag: true,
  21. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  22. if err := csConfig.LoadHub(); err != nil {
  23. return err
  24. }
  25. if csConfig.Hub == nil {
  26. return fmt.Errorf("you must configure cli before interacting with hub")
  27. }
  28. if err := cwhub.SetHubBranch(); err != nil {
  29. return fmt.Errorf("while setting hub branch: %w", err)
  30. }
  31. if err := cwhub.GetHubIdx(csConfig.Hub); err != nil {
  32. log.Info("Run 'sudo cscli hub update' to get the hub index")
  33. return fmt.Errorf("failed to get hub index: %w", err)
  34. }
  35. return nil
  36. },
  37. PersistentPostRun: func(cmd *cobra.Command, args []string) {
  38. if cmd.Name() == "inspect" || cmd.Name() == "list" {
  39. return
  40. }
  41. log.Infof(ReloadMessage())
  42. },
  43. }
  44. cmdPostOverflows.AddCommand(NewPostOverflowsInstallCmd())
  45. cmdPostOverflows.AddCommand(NewPostOverflowsRemoveCmd())
  46. cmdPostOverflows.AddCommand(NewPostOverflowsUpgradeCmd())
  47. cmdPostOverflows.AddCommand(NewPostOverflowsInspectCmd())
  48. cmdPostOverflows.AddCommand(NewPostOverflowsListCmd())
  49. return cmdPostOverflows
  50. }
  51. func NewPostOverflowsInstallCmd() *cobra.Command {
  52. var ignoreError bool
  53. cmdPostOverflowsInstall := &cobra.Command{
  54. Use: "install [config]",
  55. Short: "Install given postoverflow(s)",
  56. Long: `Fetch and install given postoverflow(s) from hub`,
  57. Example: `cscli postoverflows install crowdsec/xxx crowdsec/xyz`,
  58. Args: cobra.MinimumNArgs(1),
  59. DisableAutoGenTag: true,
  60. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  61. return compAllItems(cwhub.PARSERS_OVFLW, args, toComplete)
  62. },
  63. RunE: func(cmd *cobra.Command, args []string) error {
  64. for _, name := range args {
  65. t := cwhub.GetItem(cwhub.PARSERS_OVFLW, name)
  66. if t == nil {
  67. nearestItem, score := GetDistance(cwhub.PARSERS_OVFLW, name)
  68. Suggest(cwhub.PARSERS_OVFLW, name, nearestItem.Name, score, ignoreError)
  69. continue
  70. }
  71. if err := cwhub.InstallItem(csConfig, name, cwhub.PARSERS_OVFLW, forceAction, downloadOnly); err != nil {
  72. if !ignoreError {
  73. return fmt.Errorf("error while installing '%s': %w", name, err)
  74. }
  75. log.Errorf("Error while installing '%s': %s", name, err)
  76. }
  77. }
  78. return nil
  79. },
  80. }
  81. cmdPostOverflowsInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable")
  82. cmdPostOverflowsInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files")
  83. cmdPostOverflowsInstall.PersistentFlags().BoolVar(&ignoreError, "ignore", false, "Ignore errors when installing multiple postoverflows")
  84. return cmdPostOverflowsInstall
  85. }
  86. func NewPostOverflowsRemoveCmd() *cobra.Command {
  87. cmdPostOverflowsRemove := &cobra.Command{
  88. Use: "remove [config]",
  89. Short: "Remove given postoverflow(s)",
  90. Long: `remove given postoverflow(s)`,
  91. Example: `cscli postoverflows remove crowdsec/xxx crowdsec/xyz`,
  92. Aliases: []string{"delete"},
  93. DisableAutoGenTag: true,
  94. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  95. return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
  96. },
  97. RunE: func(cmd *cobra.Command, args []string) error {
  98. if all {
  99. cwhub.RemoveMany(csConfig, cwhub.PARSERS_OVFLW, "", all, purge, forceAction)
  100. return nil
  101. }
  102. if len(args) == 0 {
  103. return fmt.Errorf("specify at least one postoverflow to remove or '--all'")
  104. }
  105. for _, name := range args {
  106. cwhub.RemoveMany(csConfig, cwhub.PARSERS_OVFLW, name, all, purge, forceAction)
  107. }
  108. return nil
  109. },
  110. }
  111. cmdPostOverflowsRemove.PersistentFlags().BoolVar(&purge, "purge", false, "Delete source file too")
  112. cmdPostOverflowsRemove.PersistentFlags().BoolVar(&forceAction, "force", false, "Force remove : Remove tainted and outdated files")
  113. cmdPostOverflowsRemove.PersistentFlags().BoolVar(&all, "all", false, "Delete all the postoverflows")
  114. return cmdPostOverflowsRemove
  115. }
  116. func NewPostOverflowsUpgradeCmd() *cobra.Command {
  117. cmdPostOverflowsUpgrade := &cobra.Command{
  118. Use: "upgrade [config]",
  119. Short: "Upgrade given postoverflow(s)",
  120. Long: `Fetch and Upgrade given postoverflow(s) from hub`,
  121. Example: `cscli postoverflows upgrade crowdsec/xxx crowdsec/xyz`,
  122. DisableAutoGenTag: true,
  123. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  124. return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
  125. },
  126. RunE: func(cmd *cobra.Command, args []string) error {
  127. if all {
  128. cwhub.UpgradeConfig(csConfig, cwhub.PARSERS_OVFLW, "", forceAction)
  129. } else {
  130. if len(args) == 0 {
  131. return fmt.Errorf("specify at least one postoverflow to upgrade or '--all'")
  132. }
  133. for _, name := range args {
  134. cwhub.UpgradeConfig(csConfig, cwhub.PARSERS_OVFLW, name, forceAction)
  135. }
  136. }
  137. return nil
  138. },
  139. }
  140. cmdPostOverflowsUpgrade.PersistentFlags().BoolVarP(&all, "all", "a", false, "Upgrade all the postoverflows")
  141. cmdPostOverflowsUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files")
  142. return cmdPostOverflowsUpgrade
  143. }
  144. func NewPostOverflowsInspectCmd() *cobra.Command {
  145. cmdPostOverflowsInspect := &cobra.Command{
  146. Use: "inspect [config]",
  147. Short: "Inspect given postoverflow",
  148. Long: `Inspect given postoverflow`,
  149. Example: `cscli postoverflows inspect crowdsec/xxx crowdsec/xyz`,
  150. DisableAutoGenTag: true,
  151. Args: cobra.MinimumNArgs(1),
  152. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  153. return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
  154. },
  155. Run: func(cmd *cobra.Command, args []string) {
  156. InspectItem(args[0], cwhub.PARSERS_OVFLW)
  157. },
  158. }
  159. return cmdPostOverflowsInspect
  160. }
  161. func NewPostOverflowsListCmd() *cobra.Command {
  162. cmdPostOverflowsList := &cobra.Command{
  163. Use: "list [config]",
  164. Short: "List all postoverflows or given one",
  165. Long: `List all postoverflows or given one`,
  166. Example: `cscli postoverflows list
  167. cscli postoverflows list crowdsecurity/xxx`,
  168. DisableAutoGenTag: true,
  169. Run: func(cmd *cobra.Command, args []string) {
  170. ListItems(color.Output, []string{cwhub.PARSERS_OVFLW}, args, false, true, all)
  171. },
  172. }
  173. cmdPostOverflowsList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List disabled items as well")
  174. return cmdPostOverflowsList
  175. }