postoverflows.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/crowdsecurity/crowdsec/pkg/cwhub"
  5. log "github.com/sirupsen/logrus"
  6. "github.com/spf13/cobra"
  7. )
  8. func NewPostOverflowsCmd() *cobra.Command {
  9. var cmdPostOverflows = &cobra.Command{
  10. Use: "postoverflows [action] [config]",
  11. Short: "Install/Remove/Upgrade/Inspect postoverflow(s) from hub",
  12. Example: `cscli postoverflows install crowdsecurity/cdn-whitelist
  13. cscli postoverflows inspect crowdsecurity/cdn-whitelist
  14. cscli postoverflows upgrade crowdsecurity/cdn-whitelist
  15. cscli postoverflows list
  16. cscli postoverflows remove crowdsecurity/cdn-whitelist`,
  17. Args: cobra.MinimumNArgs(1),
  18. Aliases: []string{"postoverflow"},
  19. DisableAutoGenTag: true,
  20. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  21. if err := csConfig.LoadHub(); err != nil {
  22. log.Fatal(err)
  23. }
  24. if csConfig.Hub == nil {
  25. return fmt.Errorf("you must configure cli before interacting with hub")
  26. }
  27. if err := cwhub.SetHubBranch(); err != nil {
  28. return fmt.Errorf("error while setting hub branch: %s", err)
  29. }
  30. if err := cwhub.GetHubIdx(csConfig.Hub); err != nil {
  31. log.Info("Run 'sudo cscli hub update' to get the hub index")
  32. log.Fatalf("Failed to get Hub index : %v", err)
  33. }
  34. return nil
  35. },
  36. PersistentPostRun: func(cmd *cobra.Command, args []string) {
  37. if cmd.Name() == "inspect" || cmd.Name() == "list" {
  38. return
  39. }
  40. log.Infof(ReloadMessage())
  41. },
  42. }
  43. var ignoreError bool
  44. var 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. Run: func(cmd *cobra.Command, args []string) {
  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. log.Errorf("Error while installing '%s': %s", name, err)
  65. } else {
  66. log.Fatalf("Error while installing '%s': %s", name, err)
  67. }
  68. }
  69. }
  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. cmdPostOverflows.AddCommand(cmdPostOverflowsInstall)
  76. var cmdPostOverflowsRemove = &cobra.Command{
  77. Use: "remove [config]",
  78. Short: "Remove given postoverflow(s)",
  79. Long: `remove given postoverflow(s)`,
  80. Example: `cscli postoverflows remove crowdsec/xxx crowdsec/xyz`,
  81. DisableAutoGenTag: true,
  82. Aliases: []string{"delete"},
  83. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  84. return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
  85. },
  86. Run: func(cmd *cobra.Command, args []string) {
  87. if all {
  88. cwhub.RemoveMany(csConfig, cwhub.PARSERS_OVFLW, "", all, purge, forceAction)
  89. return
  90. }
  91. if len(args) == 0 {
  92. log.Fatalf("Specify at least one postoverflow to remove or '--all' flag.")
  93. }
  94. for _, name := range args {
  95. cwhub.RemoveMany(csConfig, cwhub.PARSERS_OVFLW, name, all, purge, forceAction)
  96. }
  97. },
  98. }
  99. cmdPostOverflowsRemove.PersistentFlags().BoolVar(&purge, "purge", false, "Delete source file too")
  100. cmdPostOverflowsRemove.PersistentFlags().BoolVar(&forceAction, "force", false, "Force remove : Remove tainted and outdated files")
  101. cmdPostOverflowsRemove.PersistentFlags().BoolVar(&all, "all", false, "Delete all the postoverflows")
  102. cmdPostOverflows.AddCommand(cmdPostOverflowsRemove)
  103. var cmdPostOverflowsUpgrade = &cobra.Command{
  104. Use: "upgrade [config]",
  105. Short: "Upgrade given postoverflow(s)",
  106. Long: `Fetch and Upgrade given postoverflow(s) from hub`,
  107. Example: `cscli postoverflows upgrade crowdsec/xxx crowdsec/xyz`,
  108. DisableAutoGenTag: true,
  109. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  110. return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
  111. },
  112. Run: func(cmd *cobra.Command, args []string) {
  113. if all {
  114. cwhub.UpgradeConfig(csConfig, cwhub.PARSERS_OVFLW, "", forceAction)
  115. } else {
  116. if len(args) == 0 {
  117. log.Fatalf("no target postoverflow to upgrade")
  118. }
  119. for _, name := range args {
  120. cwhub.UpgradeConfig(csConfig, cwhub.PARSERS_OVFLW, name, forceAction)
  121. }
  122. }
  123. },
  124. }
  125. cmdPostOverflowsUpgrade.PersistentFlags().BoolVarP(&all, "all", "a", false, "Upgrade all the postoverflows")
  126. cmdPostOverflowsUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files")
  127. cmdPostOverflows.AddCommand(cmdPostOverflowsUpgrade)
  128. var cmdPostOverflowsInspect = &cobra.Command{
  129. Use: "inspect [config]",
  130. Short: "Inspect given postoverflow",
  131. Long: `Inspect given postoverflow`,
  132. Example: `cscli postoverflows inspect crowdsec/xxx crowdsec/xyz`,
  133. DisableAutoGenTag: true,
  134. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  135. return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
  136. },
  137. Args: cobra.MinimumNArgs(1),
  138. Run: func(cmd *cobra.Command, args []string) {
  139. InspectItem(args[0], cwhub.PARSERS_OVFLW)
  140. },
  141. }
  142. cmdPostOverflows.AddCommand(cmdPostOverflowsInspect)
  143. var cmdPostOverflowsList = &cobra.Command{
  144. Use: "list [config]",
  145. Short: "List all postoverflows or given one",
  146. Long: `List all postoverflows or given one`,
  147. Example: `cscli postoverflows list
  148. cscli postoverflows list crowdsecurity/xxx`,
  149. DisableAutoGenTag: true,
  150. Run: func(cmd *cobra.Command, args []string) {
  151. ListItems([]string{cwhub.PARSERS_OVFLW}, args, false, true, all)
  152. },
  153. }
  154. cmdPostOverflowsList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List disabled items as well")
  155. cmdPostOverflows.AddCommand(cmdPostOverflowsList)
  156. return cmdPostOverflows
  157. }