postoverflows.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 NewPostOverflowsCmd() *cobra.Command {
  10. var 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. log.Fatal(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("error while setting hub branch: %s", 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. log.Fatalf("Failed to get Hub index : %v", 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. var ignoreError bool
  45. var cmdPostOverflowsInstall = &cobra.Command{
  46. Use: "install [config]",
  47. Short: "Install given postoverflow(s)",
  48. Long: `Fetch and install given postoverflow(s) from hub`,
  49. Example: `cscli postoverflows install crowdsec/xxx crowdsec/xyz`,
  50. Args: cobra.MinimumNArgs(1),
  51. DisableAutoGenTag: true,
  52. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  53. return compAllItems(cwhub.PARSERS_OVFLW, args, toComplete)
  54. },
  55. Run: func(cmd *cobra.Command, args []string) {
  56. for _, name := range args {
  57. t := cwhub.GetItem(cwhub.PARSERS_OVFLW, name)
  58. if t == nil {
  59. nearestItem, score := GetDistance(cwhub.PARSERS_OVFLW, name)
  60. Suggest(cwhub.PARSERS_OVFLW, name, nearestItem.Name, score, ignoreError)
  61. continue
  62. }
  63. if err := cwhub.InstallItem(csConfig, name, cwhub.PARSERS_OVFLW, forceAction, downloadOnly); err != nil {
  64. if ignoreError {
  65. log.Errorf("Error while installing '%s': %s", name, err)
  66. } else {
  67. log.Fatalf("Error while installing '%s': %s", name, err)
  68. }
  69. }
  70. }
  71. },
  72. }
  73. cmdPostOverflowsInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable")
  74. cmdPostOverflowsInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files")
  75. cmdPostOverflowsInstall.PersistentFlags().BoolVar(&ignoreError, "ignore", false, "Ignore errors when installing multiple postoverflows")
  76. cmdPostOverflows.AddCommand(cmdPostOverflowsInstall)
  77. var cmdPostOverflowsRemove = &cobra.Command{
  78. Use: "remove [config]",
  79. Short: "Remove given postoverflow(s)",
  80. Long: `remove given postoverflow(s)`,
  81. Example: `cscli postoverflows remove crowdsec/xxx crowdsec/xyz`,
  82. DisableAutoGenTag: true,
  83. Aliases: []string{"delete"},
  84. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  85. return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
  86. },
  87. Run: func(cmd *cobra.Command, args []string) {
  88. if all {
  89. cwhub.RemoveMany(csConfig, cwhub.PARSERS_OVFLW, "", all, purge, forceAction)
  90. return
  91. }
  92. if len(args) == 0 {
  93. log.Fatalf("Specify at least one postoverflow to remove or '--all' flag.")
  94. }
  95. for _, name := range args {
  96. cwhub.RemoveMany(csConfig, cwhub.PARSERS_OVFLW, name, all, purge, forceAction)
  97. }
  98. },
  99. }
  100. cmdPostOverflowsRemove.PersistentFlags().BoolVar(&purge, "purge", false, "Delete source file too")
  101. cmdPostOverflowsRemove.PersistentFlags().BoolVar(&forceAction, "force", false, "Force remove : Remove tainted and outdated files")
  102. cmdPostOverflowsRemove.PersistentFlags().BoolVar(&all, "all", false, "Delete all the postoverflows")
  103. cmdPostOverflows.AddCommand(cmdPostOverflowsRemove)
  104. var cmdPostOverflowsUpgrade = &cobra.Command{
  105. Use: "upgrade [config]",
  106. Short: "Upgrade given postoverflow(s)",
  107. Long: `Fetch and Upgrade given postoverflow(s) from hub`,
  108. Example: `cscli postoverflows upgrade crowdsec/xxx crowdsec/xyz`,
  109. DisableAutoGenTag: true,
  110. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  111. return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
  112. },
  113. Run: func(cmd *cobra.Command, args []string) {
  114. if all {
  115. cwhub.UpgradeConfig(csConfig, cwhub.PARSERS_OVFLW, "", forceAction)
  116. } else {
  117. if len(args) == 0 {
  118. log.Fatalf("no target postoverflow to upgrade")
  119. }
  120. for _, name := range args {
  121. cwhub.UpgradeConfig(csConfig, cwhub.PARSERS_OVFLW, name, forceAction)
  122. }
  123. }
  124. },
  125. }
  126. cmdPostOverflowsUpgrade.PersistentFlags().BoolVarP(&all, "all", "a", false, "Upgrade all the postoverflows")
  127. cmdPostOverflowsUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files")
  128. cmdPostOverflows.AddCommand(cmdPostOverflowsUpgrade)
  129. var cmdPostOverflowsInspect = &cobra.Command{
  130. Use: "inspect [config]",
  131. Short: "Inspect given postoverflow",
  132. Long: `Inspect given postoverflow`,
  133. Example: `cscli postoverflows inspect crowdsec/xxx crowdsec/xyz`,
  134. DisableAutoGenTag: true,
  135. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  136. return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
  137. },
  138. Args: cobra.MinimumNArgs(1),
  139. Run: func(cmd *cobra.Command, args []string) {
  140. InspectItem(args[0], cwhub.PARSERS_OVFLW)
  141. },
  142. }
  143. cmdPostOverflows.AddCommand(cmdPostOverflowsInspect)
  144. var cmdPostOverflowsList = &cobra.Command{
  145. Use: "list [config]",
  146. Short: "List all postoverflows or given one",
  147. Long: `List all postoverflows or given one`,
  148. Example: `cscli postoverflows list
  149. cscli postoverflows list crowdsecurity/xxx`,
  150. DisableAutoGenTag: true,
  151. Run: func(cmd *cobra.Command, args []string) {
  152. ListItems(colorable.NewColorableStdout(), []string{cwhub.PARSERS_OVFLW}, args, false, true, all)
  153. },
  154. }
  155. cmdPostOverflowsList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List disabled items as well")
  156. cmdPostOverflows.AddCommand(cmdPostOverflowsList)
  157. return cmdPostOverflows
  158. }