postoverflows.go 6.5 KB

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