parsers.go 6.1 KB

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