postoverflows.go 7.2 KB

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