postoverflows.go 6.9 KB

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