parsers.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 NewParsersCmd() *cobra.Command {
  10. var cmdParsers = &cobra.Command{
  11. Use: "parsers [action] [config]",
  12. Short: "Install/Remove/Upgrade/Inspect parser(s) from hub",
  13. Example: `cscli parsers install crowdsecurity/sshd-logs
  14. cscli parsers inspect crowdsecurity/sshd-logs
  15. cscli parsers upgrade crowdsecurity/sshd-logs
  16. cscli parsers list
  17. cscli parsers remove crowdsecurity/sshd-logs
  18. `,
  19. Args: cobra.MinimumNArgs(1),
  20. Aliases: []string{"parser"},
  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 fmt.Errorf("error while setting hub branch: %s", err)
  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 cmdParsersInstall = &cobra.Command{
  47. Use: "install [config]",
  48. Short: "Install given parser(s)",
  49. Long: `Fetch and install given parser(s) from hub`,
  50. Example: `cscli parsers install crowdsec/xxx crowdsec/xyz`,
  51. Args: cobra.MinimumNArgs(1),
  52. DisableAutoGenTag: true,
  53. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  54. return compAllItems(cwhub.PARSERS, args, toComplete)
  55. },
  56. Run: func(cmd *cobra.Command, args []string) {
  57. for _, name := range args {
  58. t := cwhub.GetItem(cwhub.PARSERS, name)
  59. if t == nil {
  60. nearestItem, score := GetDistance(cwhub.PARSERS, name)
  61. Suggest(cwhub.PARSERS, name, nearestItem.Name, score, ignoreError)
  62. continue
  63. }
  64. if err := cwhub.InstallItem(csConfig, name, cwhub.PARSERS, 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. cmdParsersInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable")
  75. cmdParsersInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files")
  76. cmdParsersInstall.PersistentFlags().BoolVar(&ignoreError, "ignore", false, "Ignore errors when installing multiple parsers")
  77. cmdParsers.AddCommand(cmdParsersInstall)
  78. var cmdParsersRemove = &cobra.Command{
  79. Use: "remove [config]",
  80. Short: "Remove given parser(s)",
  81. Long: `Remove given parse(s) from hub`,
  82. Aliases: []string{"delete"},
  83. Example: `cscli parsers remove crowdsec/xxx crowdsec/xyz`,
  84. DisableAutoGenTag: true,
  85. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  86. return compInstalledItems(cwhub.PARSERS, args, toComplete)
  87. },
  88. Run: func(cmd *cobra.Command, args []string) {
  89. if all {
  90. cwhub.RemoveMany(csConfig, cwhub.PARSERS, "", all, purge, forceAction)
  91. return
  92. }
  93. if len(args) == 0 {
  94. log.Fatalf("Specify at least one parser to remove or '--all' flag.")
  95. }
  96. for _, name := range args {
  97. cwhub.RemoveMany(csConfig, cwhub.PARSERS, name, all, purge, forceAction)
  98. }
  99. },
  100. }
  101. cmdParsersRemove.PersistentFlags().BoolVar(&purge, "purge", false, "Delete source file too")
  102. cmdParsersRemove.PersistentFlags().BoolVar(&forceAction, "force", false, "Force remove : Remove tainted and outdated files")
  103. cmdParsersRemove.PersistentFlags().BoolVar(&all, "all", false, "Delete all the parsers")
  104. cmdParsers.AddCommand(cmdParsersRemove)
  105. var cmdParsersUpgrade = &cobra.Command{
  106. Use: "upgrade [config]",
  107. Short: "Upgrade given parser(s)",
  108. Long: `Fetch and upgrade given parser(s) from hub`,
  109. Example: `cscli parsers upgrade crowdsec/xxx crowdsec/xyz`,
  110. DisableAutoGenTag: true,
  111. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  112. return compInstalledItems(cwhub.PARSERS, args, toComplete)
  113. },
  114. Run: func(cmd *cobra.Command, args []string) {
  115. if all {
  116. cwhub.UpgradeConfig(csConfig, cwhub.PARSERS, "", forceAction)
  117. } else {
  118. if len(args) == 0 {
  119. log.Fatalf("no target parser to upgrade")
  120. }
  121. for _, name := range args {
  122. cwhub.UpgradeConfig(csConfig, cwhub.PARSERS, name, forceAction)
  123. }
  124. }
  125. },
  126. }
  127. cmdParsersUpgrade.PersistentFlags().BoolVar(&all, "all", false, "Upgrade all the parsers")
  128. cmdParsersUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files")
  129. cmdParsers.AddCommand(cmdParsersUpgrade)
  130. var cmdParsersInspect = &cobra.Command{
  131. Use: "inspect [name]",
  132. Short: "Inspect given parser",
  133. Long: `Inspect given parser`,
  134. Example: `cscli parsers inspect crowdsec/xxx`,
  135. DisableAutoGenTag: true,
  136. Args: cobra.MinimumNArgs(1),
  137. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  138. return compInstalledItems(cwhub.PARSERS, args, toComplete)
  139. },
  140. Run: func(cmd *cobra.Command, args []string) {
  141. InspectItem(args[0], cwhub.PARSERS)
  142. },
  143. }
  144. cmdParsersInspect.PersistentFlags().StringVarP(&prometheusURL, "url", "u", "", "Prometheus url")
  145. cmdParsers.AddCommand(cmdParsersInspect)
  146. var cmdParsersList = &cobra.Command{
  147. Use: "list [name]",
  148. Short: "List all parsers or given one",
  149. Long: `List all parsers or given one`,
  150. Example: `cscli parsers list
  151. cscli parser list crowdsecurity/xxx`,
  152. DisableAutoGenTag: true,
  153. Run: func(cmd *cobra.Command, args []string) {
  154. ListItems(colorable.NewColorableStdout(), []string{cwhub.PARSERS}, args, false, true, all)
  155. },
  156. }
  157. cmdParsersList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List disabled items as well")
  158. cmdParsers.AddCommand(cmdParsersList)
  159. return cmdParsers
  160. }