postoverflows.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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> [postoverflow]...",
  13. Short: "Manage hub postoverflows",
  14. Example: `cscli postoverflows list -a
  15. cscli postoverflows install crowdsecurity/cdn-whitelist crowdsecurity/rdns
  16. cscli postoverflows inspect crowdsecurity/cdn-whitelist crowdsecurity/rdns
  17. cscli postoverflows upgrade crowdsecurity/cdn-whitelist crowdsecurity/rdns
  18. cscli postoverflows remove crowdsecurity/cdn-whitelist crowdsecurity/rdns
  19. `,
  20. Args: cobra.MinimumNArgs(1),
  21. Aliases: []string{"postoverflow"},
  22. DisableAutoGenTag: true,
  23. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  24. if err := require.Hub(csConfig); err != nil {
  25. return err
  26. }
  27. return nil
  28. },
  29. PersistentPostRun: func(cmd *cobra.Command, args []string) {
  30. if cmd.Name() == "inspect" || cmd.Name() == "list" {
  31. return
  32. }
  33. log.Infof(ReloadMessage())
  34. },
  35. }
  36. cmdPostOverflows.AddCommand(NewPostOverflowsInstallCmd())
  37. cmdPostOverflows.AddCommand(NewPostOverflowsRemoveCmd())
  38. cmdPostOverflows.AddCommand(NewPostOverflowsUpgradeCmd())
  39. cmdPostOverflows.AddCommand(NewPostOverflowsInspectCmd())
  40. cmdPostOverflows.AddCommand(NewPostOverflowsListCmd())
  41. return cmdPostOverflows
  42. }
  43. func runPostOverflowsInstall(cmd *cobra.Command, args []string) error {
  44. flags := cmd.Flags()
  45. downloadOnly, err := flags.GetBool("download-only")
  46. if err != nil {
  47. return err
  48. }
  49. force, err := flags.GetBool("force")
  50. if err != nil {
  51. return err
  52. }
  53. ignoreError, err := flags.GetBool("ignore")
  54. if err != nil {
  55. return err
  56. }
  57. for _, name := range args {
  58. t := cwhub.GetItem(cwhub.PARSERS_OVFLW, name)
  59. if t == nil {
  60. nearestItem, score := GetDistance(cwhub.PARSERS_OVFLW, name)
  61. Suggest(cwhub.PARSERS_OVFLW, name, nearestItem.Name, score, ignoreError)
  62. continue
  63. }
  64. if err := cwhub.InstallItem(csConfig, name, cwhub.PARSERS_OVFLW, force, downloadOnly); err != nil {
  65. if !ignoreError {
  66. return fmt.Errorf("error while installing '%s': %w", name, err)
  67. }
  68. log.Errorf("Error while installing '%s': %s", name, err)
  69. }
  70. }
  71. return nil
  72. }
  73. func NewPostOverflowsInstallCmd() *cobra.Command {
  74. cmdPostOverflowsInstall := &cobra.Command{
  75. Use: "install <postoverflow>...",
  76. Short: "Install given postoverflow(s)",
  77. Long: `Fetch and install one or more postoverflows from the hub`,
  78. Example: `cscli postoverflows install crowdsecurity/cdn-whitelist crowdsecurity/rdns`,
  79. Args: cobra.MinimumNArgs(1),
  80. DisableAutoGenTag: true,
  81. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  82. return compAllItems(cwhub.PARSERS_OVFLW, args, toComplete)
  83. },
  84. RunE: runPostOverflowsInstall,
  85. }
  86. flags := cmdPostOverflowsInstall.Flags()
  87. flags.BoolP("download-only", "d", false, "Only download packages, don't enable")
  88. flags.Bool("force", false, "Force install: overwrite tainted and outdated files")
  89. flags.Bool("ignore", false, "Ignore errors when installing multiple postoverflows")
  90. return cmdPostOverflowsInstall
  91. }
  92. func runPostOverflowsRemove(cmd *cobra.Command, args []string) error {
  93. flags := cmd.Flags()
  94. purge, err := flags.GetBool("purge")
  95. if err != nil {
  96. return err
  97. }
  98. force, err := flags.GetBool("force")
  99. if err != nil {
  100. return err
  101. }
  102. all, err := flags.GetBool("all")
  103. if err != nil {
  104. return err
  105. }
  106. if all {
  107. err := cwhub.RemoveMany(csConfig, cwhub.PARSERS_OVFLW, "", all, purge, force)
  108. if err != nil {
  109. return err
  110. }
  111. return nil
  112. }
  113. if len(args) == 0 {
  114. return fmt.Errorf("specify at least one postoverflow to remove or '--all'")
  115. }
  116. for _, name := range args {
  117. err := cwhub.RemoveMany(csConfig, cwhub.PARSERS_OVFLW, name, all, purge, force)
  118. if err != nil {
  119. return err
  120. }
  121. }
  122. return nil
  123. }
  124. func NewPostOverflowsRemoveCmd() *cobra.Command {
  125. cmdPostOverflowsRemove := &cobra.Command{
  126. Use: "remove <postoverflow>...",
  127. Short: "Remove given postoverflow(s)",
  128. Long: `remove one or more postoverflows from the hub`,
  129. Example: `cscli postoverflows remove crowdsecurity/cdn-whitelist crowdsecurity/rdns`,
  130. Aliases: []string{"delete"},
  131. DisableAutoGenTag: true,
  132. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  133. return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
  134. },
  135. RunE: runPostOverflowsRemove,
  136. }
  137. flags := cmdPostOverflowsRemove.Flags()
  138. flags.Bool("purge", false, "Delete source file too")
  139. flags.Bool("force", false, "Force remove: remove tainted and outdated files")
  140. flags.Bool("all", false, "Delete all the postoverflows")
  141. return cmdPostOverflowsRemove
  142. }
  143. func runPostOverflowUpgrade(cmd *cobra.Command, args []string) error {
  144. flags := cmd.Flags()
  145. force, err := flags.GetBool("force")
  146. if err != nil {
  147. return err
  148. }
  149. all, err := flags.GetBool("all")
  150. if err != nil {
  151. return err
  152. }
  153. if all {
  154. if err := cwhub.UpgradeConfig(csConfig, cwhub.PARSERS_OVFLW, "", force); err != nil {
  155. return err
  156. }
  157. return nil
  158. }
  159. if len(args) == 0 {
  160. return fmt.Errorf("specify at least one postoverflow to upgrade or '--all'")
  161. }
  162. for _, name := range args {
  163. if err := cwhub.UpgradeConfig(csConfig, cwhub.PARSERS_OVFLW, name, force); err != nil {
  164. return err
  165. }
  166. }
  167. return nil
  168. }
  169. func NewPostOverflowsUpgradeCmd() *cobra.Command {
  170. cmdPostOverflowsUpgrade := &cobra.Command{
  171. Use: "upgrade <postoverflow>...",
  172. Short: "Upgrade given postoverflow(s)",
  173. Long: `Fetch and upgrade one or more postoverflows from the hub`,
  174. Example: `cscli postoverflows upgrade crowdsecurity/cdn-whitelist crowdsecurity/rdns`,
  175. DisableAutoGenTag: true,
  176. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  177. return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
  178. },
  179. RunE: runPostOverflowUpgrade,
  180. }
  181. flags := cmdPostOverflowsUpgrade.Flags()
  182. flags.BoolP("all", "a", false, "Upgrade all the postoverflows")
  183. flags.Bool("force", false, "Force upgrade: overwrite tainted and outdated files")
  184. return cmdPostOverflowsUpgrade
  185. }
  186. func runPostOverflowsInspect(cmd *cobra.Command, args []string) error {
  187. flags := cmd.Flags()
  188. url, err := flags.GetString("url")
  189. if err != nil {
  190. return err
  191. }
  192. if url != "" {
  193. csConfig.Cscli.PrometheusUrl = url
  194. }
  195. noMetrics, err := flags.GetBool("no-metrics")
  196. if err != nil {
  197. return err
  198. }
  199. for _, name := range args {
  200. if err = InspectItem(name, cwhub.PARSERS_OVFLW, noMetrics); err != nil {
  201. return err
  202. }
  203. }
  204. return nil
  205. }
  206. func NewPostOverflowsInspectCmd() *cobra.Command {
  207. cmdPostOverflowsInspect := &cobra.Command{
  208. Use: "inspect <postoverflow>",
  209. Short: "Inspect a postoverflow",
  210. Long: `Inspect a postoverflow`,
  211. Example: `cscli postoverflows inspect crowdsecurity/cdn-whitelist crowdsecurity/rdns`,
  212. Args: cobra.MinimumNArgs(1),
  213. DisableAutoGenTag: true,
  214. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  215. return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
  216. },
  217. RunE: runPostOverflowsInspect,
  218. }
  219. flags := cmdPostOverflowsInspect.Flags()
  220. // XXX: is this needed for postoverflows?
  221. flags.StringP("url", "u", "", "Prometheus url")
  222. flags.Bool("no-metrics", false, "Don't show metrics (when cscli.output=human)")
  223. return cmdPostOverflowsInspect
  224. }
  225. func runPostOverflowsList(cmd *cobra.Command, args []string) error {
  226. flags := cmd.Flags()
  227. all, err := flags.GetBool("all")
  228. if err != nil {
  229. return err
  230. }
  231. // XXX: will happily ignore missing postoverflows
  232. ListItems(color.Output, []string{cwhub.PARSERS_OVFLW}, args, false, true, all)
  233. return nil
  234. }
  235. func NewPostOverflowsListCmd() *cobra.Command {
  236. cmdPostOverflowsList := &cobra.Command{
  237. Use: "list [postoverflow]...",
  238. Short: "List postoverflows",
  239. Long: `List of installed/available/specified postoverflows`,
  240. Example: `cscli postoverflows list
  241. cscli postoverflows list -a
  242. cscli postoverflows list crowdsecurity/cdn-whitelist crowdsecurity/rdns`,
  243. DisableAutoGenTag: true,
  244. RunE: runPostOverflowsList,
  245. }
  246. flags := cmdPostOverflowsList.Flags()
  247. flags.BoolP("all", "a", false, "List disabled items as well")
  248. return cmdPostOverflowsList
  249. }