scenarios.go 6.4 KB

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