parsers.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/fatih/color"
  5. log "github.com/sirupsen/logrus"
  6. "github.com/spf13/cobra"
  7. "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
  8. "github.com/crowdsecurity/crowdsec/pkg/cwhub"
  9. )
  10. func NewParsersCmd() *cobra.Command {
  11. cmdParsers := &cobra.Command{
  12. Use: "parsers <action> [parser]...",
  13. Short: "Manage hub parsers",
  14. Example: `cscli parsers list -a
  15. cscli parsers install crowdsecurity/caddy-logs crowdsecurity/sshd-logs
  16. cscli parsers inspect crowdsecurity/caddy-logs crowdsecurity/sshd-logs
  17. cscli parsers upgrade crowdsecurity/caddy-logs crowdsecurity/sshd-logs
  18. cscli parsers remove crowdsecurity/caddy-logs crowdsecurity/sshd-logs
  19. `,
  20. Args: cobra.MinimumNArgs(1),
  21. Aliases: []string{"parser"},
  22. DisableAutoGenTag: true,
  23. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  24. if err := require.Hub(csConfig); err != nil {
  25. return err
  26. }
  27. return nil
  28. },
  29. PersistentPostRun: func(cmd *cobra.Command, args []string) {
  30. if cmd.Name() == "inspect" || cmd.Name() == "list" {
  31. return
  32. }
  33. log.Infof(ReloadMessage())
  34. },
  35. }
  36. cmdParsers.AddCommand(NewParsersInstallCmd())
  37. cmdParsers.AddCommand(NewParsersRemoveCmd())
  38. cmdParsers.AddCommand(NewParsersUpgradeCmd())
  39. cmdParsers.AddCommand(NewParsersInspectCmd())
  40. cmdParsers.AddCommand(NewParsersListCmd())
  41. return cmdParsers
  42. }
  43. func runParsersInstall(cmd *cobra.Command, args []string) error {
  44. flags := cmd.Flags()
  45. downloadOnly, err := flags.GetBool("download-only")
  46. if err != nil {
  47. return err
  48. }
  49. force, err := flags.GetBool("force")
  50. if err != nil {
  51. return err
  52. }
  53. ignoreError, err := flags.GetBool("ignore")
  54. if err != nil {
  55. return err
  56. }
  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, force, downloadOnly); err != nil {
  65. if !ignoreError {
  66. return fmt.Errorf("error while installing '%s': %w", name, err)
  67. }
  68. log.Errorf("Error while installing '%s': %s", name, err)
  69. }
  70. }
  71. return nil
  72. }
  73. func NewParsersInstallCmd() *cobra.Command {
  74. cmdParsersInstall := &cobra.Command{
  75. Use: "install <parser>...",
  76. Short: "Install given parser(s)",
  77. Long: `Fetch and install one or more parsers from the hub`,
  78. Example: `cscli parsers install crowdsecurity/caddy-logs crowdsecurity/sshd-logs`,
  79. Args: cobra.MinimumNArgs(1),
  80. DisableAutoGenTag: true,
  81. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  82. return compAllItems(cwhub.PARSERS, args, toComplete)
  83. },
  84. RunE: runParsersInstall,
  85. }
  86. flags := cmdParsersInstall.Flags()
  87. flags.BoolP("download-only", "d", false, "Only download packages, don't enable")
  88. flags.Bool("force", false, "Force install: overwrite tainted and outdated files")
  89. flags.Bool("ignore", false, "Ignore errors when installing multiple parsers")
  90. return cmdParsersInstall
  91. }
  92. func runParsersRemove(cmd *cobra.Command, args []string) error {
  93. flags := cmd.Flags()
  94. purge, err := flags.GetBool("purge")
  95. if err != nil {
  96. return err
  97. }
  98. force, err := flags.GetBool("force")
  99. if err != nil {
  100. return err
  101. }
  102. all, err := flags.GetBool("all")
  103. if err != nil {
  104. return err
  105. }
  106. if all {
  107. err := cwhub.RemoveMany(csConfig, cwhub.PARSERS, "", all, purge, force)
  108. if err != nil {
  109. return err
  110. }
  111. return nil
  112. }
  113. if len(args) == 0 {
  114. return fmt.Errorf("specify at least one parser to remove or '--all'")
  115. }
  116. for _, name := range args {
  117. err := cwhub.RemoveMany(csConfig, cwhub.PARSERS, name, all, purge, force)
  118. if err != nil {
  119. return err
  120. }
  121. }
  122. return nil
  123. }
  124. func NewParsersRemoveCmd() *cobra.Command {
  125. cmdParsersRemove := &cobra.Command{
  126. Use: "remove <parser>...",
  127. Short: "Remove given parser(s)",
  128. Long: `Remove one or more parsers`,
  129. Example: `cscli parsers remove crowdsecurity/caddy-logs crowdsecurity/sshd-logs`,
  130. Aliases: []string{"delete"},
  131. DisableAutoGenTag: true,
  132. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  133. return compInstalledItems(cwhub.PARSERS, args, toComplete)
  134. },
  135. RunE: runParsersRemove,
  136. }
  137. flags := cmdParsersRemove.Flags()
  138. flags.Bool("purge", false, "Delete source file too")
  139. flags.Bool("force", false, "Force remove: remove tainted and outdated files")
  140. flags.Bool("all", false, "Remove all the parsers")
  141. return cmdParsersRemove
  142. }
  143. func runParsersUpgrade(cmd *cobra.Command, args []string) error {
  144. flags := cmd.Flags()
  145. force, err := flags.GetBool("force")
  146. if err != nil {
  147. return err
  148. }
  149. all, err := flags.GetBool("all")
  150. if err != nil {
  151. return err
  152. }
  153. if all {
  154. if err := cwhub.UpgradeConfig(csConfig, cwhub.PARSERS, "", force); err != nil {
  155. return err
  156. }
  157. return nil
  158. }
  159. if len(args) == 0 {
  160. return fmt.Errorf("specify at least one parser to upgrade or '--all'")
  161. }
  162. for _, name := range args {
  163. if err := cwhub.UpgradeConfig(csConfig, cwhub.PARSERS, name, force); err != nil {
  164. return err
  165. }
  166. }
  167. return nil
  168. }
  169. func NewParsersUpgradeCmd() *cobra.Command {
  170. cmdParsersUpgrade := &cobra.Command{
  171. Use: "upgrade <parser>...",
  172. Short: "Upgrade given parser(s)",
  173. Long: `Fetch and upgrade one or more parsers from the hub`,
  174. Example: `cscli parsers upgrade crowdsecurity/caddy-logs crowdsecurity/sshd-logs`,
  175. DisableAutoGenTag: true,
  176. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  177. return compInstalledItems(cwhub.PARSERS, args, toComplete)
  178. },
  179. RunE: runParsersUpgrade,
  180. }
  181. flags := cmdParsersUpgrade.Flags()
  182. flags.BoolP("all", "a", false, "Upgrade all the parsers")
  183. flags.Bool("force", false, "Force upgrade: overwrite tainted and outdated files")
  184. return cmdParsersUpgrade
  185. }
  186. func runParsersInspect(cmd *cobra.Command, args []string) error {
  187. flags := cmd.Flags()
  188. url, err := flags.GetString("url")
  189. if err != nil {
  190. return err
  191. }
  192. if url != "" {
  193. csConfig.Cscli.PrometheusUrl = url
  194. }
  195. noMetrics, err := flags.GetBool("no-metrics")
  196. if err != nil {
  197. return err
  198. }
  199. for _, name := range args {
  200. if err = InspectItem(name, cwhub.PARSERS, noMetrics); err != nil {
  201. return err
  202. }
  203. }
  204. return nil
  205. }
  206. func NewParsersInspectCmd() *cobra.Command {
  207. cmdParsersInspect := &cobra.Command{
  208. Use: "inspect <parser>",
  209. Short: "Inspect a parser",
  210. Long: `Inspect a parser`,
  211. Example: `cscli parsers inspect crowdsecurity/httpd-logs crowdsecurity/sshd-logs`,
  212. Args: cobra.MinimumNArgs(1),
  213. DisableAutoGenTag: true,
  214. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  215. return compInstalledItems(cwhub.PARSERS, args, toComplete)
  216. },
  217. RunE: runParsersInspect,
  218. }
  219. flags := cmdParsersInspect.Flags()
  220. flags.StringP("url", "u", "", "Prometheus url")
  221. flags.Bool("no-metrics", false, "Don't show metrics (when cscli.output=human)")
  222. return cmdParsersInspect
  223. }
  224. func runParsersList(cmd *cobra.Command, args []string) error {
  225. flags := cmd.Flags()
  226. all, err := flags.GetBool("all")
  227. if err != nil {
  228. return err
  229. }
  230. // XXX: will happily ignore missing parsers
  231. ListItems(color.Output, []string{cwhub.PARSERS}, args, false, true, all)
  232. return nil
  233. }
  234. func NewParsersListCmd() *cobra.Command {
  235. cmdParsersList := &cobra.Command{
  236. Use: "list [parser... | -a]",
  237. Short: "List parsers",
  238. Long: `List of installed/available/specified parsers`,
  239. Example: `cscli parsers list
  240. cscli parsers list -a
  241. cscli parsers list crowdsecurity/caddy-logs crowdsecurity/sshd-logs`,
  242. DisableAutoGenTag: true,
  243. RunE: runParsersList,
  244. }
  245. flags := cmdParsersList.Flags()
  246. flags.BoolP("all", "a", false, "List disabled items as well")
  247. return cmdParsersList
  248. }