parsers.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. hub, err := cwhub.GetHub()
  58. if err != nil {
  59. return err
  60. }
  61. for _, name := range args {
  62. t := hub.GetItem(cwhub.PARSERS, name)
  63. if t == nil {
  64. nearestItem, score := GetDistance(cwhub.PARSERS, name)
  65. Suggest(cwhub.PARSERS, name, nearestItem.Name, score, ignoreError)
  66. continue
  67. }
  68. if err := hub.InstallItem(name, cwhub.PARSERS, force, downloadOnly); err != nil {
  69. if !ignoreError {
  70. return fmt.Errorf("error while installing '%s': %w", name, err)
  71. }
  72. log.Errorf("Error while installing '%s': %s", name, err)
  73. }
  74. }
  75. return nil
  76. }
  77. func NewParsersInstallCmd() *cobra.Command {
  78. cmdParsersInstall := &cobra.Command{
  79. Use: "install <parser>...",
  80. Short: "Install given parser(s)",
  81. Long: `Fetch and install one or more parsers from the hub`,
  82. Example: `cscli parsers install crowdsecurity/caddy-logs crowdsecurity/sshd-logs`,
  83. Args: cobra.MinimumNArgs(1),
  84. DisableAutoGenTag: true,
  85. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  86. return compAllItems(cwhub.PARSERS, args, toComplete)
  87. },
  88. RunE: runParsersInstall,
  89. }
  90. flags := cmdParsersInstall.Flags()
  91. flags.BoolP("download-only", "d", false, "Only download packages, don't enable")
  92. flags.Bool("force", false, "Force install: overwrite tainted and outdated files")
  93. flags.Bool("ignore", false, "Ignore errors when installing multiple parsers")
  94. return cmdParsersInstall
  95. }
  96. func runParsersRemove(cmd *cobra.Command, args []string) error {
  97. flags := cmd.Flags()
  98. purge, err := flags.GetBool("purge")
  99. if err != nil {
  100. return err
  101. }
  102. force, err := flags.GetBool("force")
  103. if err != nil {
  104. return err
  105. }
  106. all, err := flags.GetBool("all")
  107. if err != nil {
  108. return err
  109. }
  110. hub, err := cwhub.GetHub()
  111. if err != nil {
  112. return err
  113. }
  114. if all {
  115. err := hub.RemoveMany(cwhub.PARSERS, "", all, purge, force)
  116. if err != nil {
  117. return err
  118. }
  119. return nil
  120. }
  121. if len(args) == 0 {
  122. return fmt.Errorf("specify at least one parser to remove or '--all'")
  123. }
  124. for _, name := range args {
  125. err := hub.RemoveMany(cwhub.PARSERS, name, all, purge, force)
  126. if err != nil {
  127. return err
  128. }
  129. }
  130. return nil
  131. }
  132. func NewParsersRemoveCmd() *cobra.Command {
  133. cmdParsersRemove := &cobra.Command{
  134. Use: "remove <parser>...",
  135. Short: "Remove given parser(s)",
  136. Long: `Remove one or more parsers`,
  137. Example: `cscli parsers remove crowdsecurity/caddy-logs crowdsecurity/sshd-logs`,
  138. Aliases: []string{"delete"},
  139. DisableAutoGenTag: true,
  140. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  141. return compInstalledItems(cwhub.PARSERS, args, toComplete)
  142. },
  143. RunE: runParsersRemove,
  144. }
  145. flags := cmdParsersRemove.Flags()
  146. flags.Bool("purge", false, "Delete source file too")
  147. flags.Bool("force", false, "Force remove: remove tainted and outdated files")
  148. flags.Bool("all", false, "Remove all the parsers")
  149. return cmdParsersRemove
  150. }
  151. func runParsersUpgrade(cmd *cobra.Command, args []string) error {
  152. flags := cmd.Flags()
  153. force, err := flags.GetBool("force")
  154. if err != nil {
  155. return err
  156. }
  157. all, err := flags.GetBool("all")
  158. if err != nil {
  159. return err
  160. }
  161. hub, err := cwhub.GetHub()
  162. if err != nil {
  163. return err
  164. }
  165. if all {
  166. if err := hub.UpgradeConfig(cwhub.PARSERS, "", force); err != nil {
  167. return err
  168. }
  169. return nil
  170. }
  171. if len(args) == 0 {
  172. return fmt.Errorf("specify at least one parser to upgrade or '--all'")
  173. }
  174. for _, name := range args {
  175. if err := hub.UpgradeConfig(cwhub.PARSERS, name, force); err != nil {
  176. return err
  177. }
  178. }
  179. return nil
  180. }
  181. func NewParsersUpgradeCmd() *cobra.Command {
  182. cmdParsersUpgrade := &cobra.Command{
  183. Use: "upgrade <parser>...",
  184. Short: "Upgrade given parser(s)",
  185. Long: `Fetch and upgrade one or more parsers from the hub`,
  186. Example: `cscli parsers upgrade crowdsecurity/caddy-logs crowdsecurity/sshd-logs`,
  187. DisableAutoGenTag: true,
  188. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  189. return compInstalledItems(cwhub.PARSERS, args, toComplete)
  190. },
  191. RunE: runParsersUpgrade,
  192. }
  193. flags := cmdParsersUpgrade.Flags()
  194. flags.BoolP("all", "a", false, "Upgrade all the parsers")
  195. flags.Bool("force", false, "Force upgrade: overwrite tainted and outdated files")
  196. return cmdParsersUpgrade
  197. }
  198. func runParsersInspect(cmd *cobra.Command, args []string) error {
  199. flags := cmd.Flags()
  200. url, err := flags.GetString("url")
  201. if err != nil {
  202. return err
  203. }
  204. if url != "" {
  205. csConfig.Cscli.PrometheusUrl = url
  206. }
  207. noMetrics, err := flags.GetBool("no-metrics")
  208. if err != nil {
  209. return err
  210. }
  211. for _, name := range args {
  212. if err = InspectItem(name, cwhub.PARSERS, noMetrics); err != nil {
  213. return err
  214. }
  215. }
  216. return nil
  217. }
  218. func NewParsersInspectCmd() *cobra.Command {
  219. cmdParsersInspect := &cobra.Command{
  220. Use: "inspect <parser>",
  221. Short: "Inspect a parser",
  222. Long: `Inspect a parser`,
  223. Example: `cscli parsers inspect crowdsecurity/httpd-logs crowdsecurity/sshd-logs`,
  224. Args: cobra.MinimumNArgs(1),
  225. DisableAutoGenTag: true,
  226. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  227. return compInstalledItems(cwhub.PARSERS, args, toComplete)
  228. },
  229. RunE: runParsersInspect,
  230. }
  231. flags := cmdParsersInspect.Flags()
  232. flags.StringP("url", "u", "", "Prometheus url")
  233. flags.Bool("no-metrics", false, "Don't show metrics (when cscli.output=human)")
  234. return cmdParsersInspect
  235. }
  236. func runParsersList(cmd *cobra.Command, args []string) error {
  237. flags := cmd.Flags()
  238. all, err := flags.GetBool("all")
  239. if err != nil {
  240. return err
  241. }
  242. if err = ListItems(color.Output, []string{cwhub.PARSERS}, args, false, true, all); err != nil {
  243. return err
  244. }
  245. return nil
  246. }
  247. func NewParsersListCmd() *cobra.Command {
  248. cmdParsersList := &cobra.Command{
  249. Use: "list [parser... | -a]",
  250. Short: "List parsers",
  251. Long: `List of installed/available/specified parsers`,
  252. Example: `cscli parsers list
  253. cscli parsers list -a
  254. cscli parsers list crowdsecurity/caddy-logs crowdsecurity/sshd-logs`,
  255. DisableAutoGenTag: true,
  256. RunE: runParsersList,
  257. }
  258. flags := cmdParsersList.Flags()
  259. flags.BoolP("all", "a", false, "List disabled items as well")
  260. return cmdParsersList
  261. }