parsers.go 6.4 KB

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