itemcommands.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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/go-cs-lib/coalesce"
  8. "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
  9. "github.com/crowdsecurity/crowdsec/pkg/cwhub"
  10. )
  11. type cmdHelp struct {
  12. // Example is required, the others have a default value
  13. // generated from the item type
  14. use string
  15. short string
  16. long string
  17. example string
  18. }
  19. type hubItemType struct {
  20. name string // plural, as used in the hub index
  21. singular string
  22. oneOrMore string // parenthetical pluralizaion: "parser(s)"
  23. help cmdHelp
  24. installHelp cmdHelp
  25. removeHelp cmdHelp
  26. upgradeHelp cmdHelp
  27. inspectHelp cmdHelp
  28. listHelp cmdHelp
  29. }
  30. var hubItemTypes = map[string]hubItemType{
  31. "parsers": {
  32. name: "parsers",
  33. singular: "parser",
  34. oneOrMore: "parser(s)",
  35. help: cmdHelp{
  36. example: `cscli parsers list -a
  37. cscli parsers install crowdsecurity/caddy-logs crowdsecurity/sshd-logs
  38. cscli parsers inspect crowdsecurity/caddy-logs crowdsecurity/sshd-logs
  39. cscli parsers upgrade crowdsecurity/caddy-logs crowdsecurity/sshd-logs
  40. cscli parsers remove crowdsecurity/caddy-logs crowdsecurity/sshd-logs
  41. `,
  42. },
  43. installHelp: cmdHelp{
  44. example: `cscli parsers install crowdsecurity/caddy-logs crowdsecurity/sshd-logs`,
  45. },
  46. removeHelp: cmdHelp{
  47. example: `cscli parsers remove crowdsecurity/caddy-logs crowdsecurity/sshd-logs`,
  48. },
  49. upgradeHelp: cmdHelp{
  50. example: `cscli parsers upgrade crowdsecurity/caddy-logs crowdsecurity/sshd-logs`,
  51. },
  52. inspectHelp: cmdHelp{
  53. example: `cscli parsers inspect crowdsecurity/httpd-logs crowdsecurity/sshd-logs`,
  54. },
  55. listHelp: cmdHelp{
  56. example: `cscli parsers list
  57. cscli parsers list -a
  58. cscli parsers list crowdsecurity/caddy-logs crowdsecurity/sshd-logs`,
  59. },
  60. },
  61. "postoverflows": {
  62. name: "postoverflows",
  63. singular: "postoverflow",
  64. oneOrMore: "postoverflow(s)",
  65. help: cmdHelp{
  66. example: `cscli postoverflows list -a
  67. cscli postoverflows install crowdsecurity/cdn-whitelist crowdsecurity/rdns
  68. cscli postoverflows inspect crowdsecurity/cdn-whitelist crowdsecurity/rdns
  69. cscli postoverflows upgrade crowdsecurity/cdn-whitelist crowdsecurity/rdns
  70. cscli postoverflows remove crowdsecurity/cdn-whitelist crowdsecurity/rdns
  71. `,
  72. },
  73. installHelp: cmdHelp{
  74. example: `cscli postoverflows install crowdsecurity/cdn-whitelist crowdsecurity/rdns`,
  75. },
  76. removeHelp: cmdHelp{
  77. example: `cscli postoverflows remove crowdsecurity/cdn-whitelist crowdsecurity/rdns`,
  78. },
  79. upgradeHelp: cmdHelp{
  80. example: `cscli postoverflows upgrade crowdsecurity/cdn-whitelist crowdsecurity/rdns`,
  81. },
  82. inspectHelp: cmdHelp{
  83. example: `cscli postoverflows inspect crowdsecurity/cdn-whitelist crowdsecurity/rdns`,
  84. },
  85. listHelp: cmdHelp{
  86. example: `cscli postoverflows list
  87. cscli postoverflows list -a
  88. cscli postoverflows list crowdsecurity/cdn-whitelist crowdsecurity/rdns`,
  89. },
  90. },
  91. "scenarios": {
  92. name: "scenarios",
  93. singular: "scenario",
  94. oneOrMore: "scenario(s)",
  95. help: cmdHelp{
  96. example: `cscli scenarios list -a
  97. cscli scenarios install crowdsecurity/ssh-bf crowdsecurity/http-probing
  98. cscli scenarios inspect crowdsecurity/ssh-bf crowdsecurity/http-probing
  99. cscli scenarios upgrade crowdsecurity/ssh-bf crowdsecurity/http-probing
  100. cscli scenarios remove crowdsecurity/ssh-bf crowdsecurity/http-probing
  101. `,
  102. },
  103. installHelp: cmdHelp{
  104. example: `cscli scenarios install crowdsecurity/ssh-bf crowdsecurity/http-probing`,
  105. },
  106. removeHelp: cmdHelp{
  107. example: `cscli scenarios remove crowdsecurity/ssh-bf crowdsecurity/http-probing`,
  108. },
  109. upgradeHelp: cmdHelp{
  110. example: `cscli scenarios upgrade crowdsecurity/ssh-bf crowdsecurity/http-probing`,
  111. },
  112. inspectHelp: cmdHelp{
  113. example: `cscli scenarios inspect crowdsecurity/ssh-bf crowdsecurity/http-probing`,
  114. },
  115. listHelp: cmdHelp{
  116. example: `cscli scenarios list
  117. cscli scenarios list -a
  118. cscli scenarios list crowdsecurity/ssh-bf crowdsecurity/http-probing`,
  119. },
  120. },
  121. "collections": {
  122. name: "collections",
  123. singular: "collection",
  124. oneOrMore: "collection(s)",
  125. help: cmdHelp{
  126. example: `cscli collections list -a
  127. cscli collections install crowdsecurity/http-cve crowdsecurity/iptables
  128. cscli collections inspect crowdsecurity/http-cve crowdsecurity/iptables
  129. cscli collections upgrade crowdsecurity/http-cve crowdsecurity/iptables
  130. cscli collections remove crowdsecurity/http-cve crowdsecurity/iptables
  131. `,
  132. },
  133. installHelp: cmdHelp{
  134. example: `cscli collections install crowdsecurity/http-cve crowdsecurity/iptables`,
  135. },
  136. removeHelp: cmdHelp{
  137. example: `cscli collections remove crowdsecurity/http-cve crowdsecurity/iptables`,
  138. },
  139. upgradeHelp: cmdHelp{
  140. example: `cscli collections upgrade crowdsecurity/http-cve crowdsecurity/iptables`,
  141. },
  142. inspectHelp: cmdHelp{
  143. example: `cscli collections inspect crowdsecurity/http-cve crowdsecurity/iptables`,
  144. },
  145. listHelp: cmdHelp{
  146. example: `cscli collections list
  147. cscli collections list -a
  148. cscli collections list crowdsecurity/http-cve crowdsecurity/iptables`,
  149. },
  150. },
  151. }
  152. func NewItemsCmd(typeName string) *cobra.Command {
  153. it := hubItemTypes[typeName]
  154. cmd := &cobra.Command{
  155. Use: coalesce.String(it.help.use, fmt.Sprintf("%s <action> [item]...", it.name)),
  156. Short: coalesce.String(it.help.short, fmt.Sprintf("Manage hub %s", it.name)),
  157. Long: it.help.long,
  158. Example: it.help.example,
  159. Args: cobra.MinimumNArgs(1),
  160. Aliases: []string{it.singular},
  161. DisableAutoGenTag: true,
  162. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  163. if _, err := require.Hub(csConfig); err != nil {
  164. return err
  165. }
  166. return nil
  167. },
  168. PersistentPostRun: func(cmd *cobra.Command, args []string) {
  169. if cmd.Name() == "inspect" || cmd.Name() == "list" {
  170. return
  171. }
  172. log.Infof(ReloadMessage())
  173. },
  174. }
  175. cmd.AddCommand(NewItemsInstallCmd(typeName))
  176. cmd.AddCommand(NewItemsRemoveCmd(typeName))
  177. cmd.AddCommand(NewItemsUpgradeCmd(typeName))
  178. cmd.AddCommand(NewItemsInspectCmd(typeName))
  179. cmd.AddCommand(NewItemsListCmd(typeName))
  180. return cmd
  181. }
  182. func itemsInstallRunner(it hubItemType) func(cmd *cobra.Command, args []string) error {
  183. run := func(cmd *cobra.Command, args []string) error {
  184. flags := cmd.Flags()
  185. downloadOnly, err := flags.GetBool("download-only")
  186. if err != nil {
  187. return err
  188. }
  189. force, err := flags.GetBool("force")
  190. if err != nil {
  191. return err
  192. }
  193. ignoreError, err := flags.GetBool("ignore")
  194. if err != nil {
  195. return err
  196. }
  197. hub, err := cwhub.GetHub()
  198. if err != nil {
  199. return err
  200. }
  201. for _, name := range args {
  202. t := hub.GetItem(it.name, name)
  203. if t == nil {
  204. nearestItem, score := GetDistance(it.name, name)
  205. Suggest(it.name, name, nearestItem.Name, score, ignoreError)
  206. continue
  207. }
  208. if err := hub.InstallItem(name, it.name, force, downloadOnly); err != nil {
  209. if !ignoreError {
  210. return fmt.Errorf("error while installing '%s': %w", name, err)
  211. }
  212. log.Errorf("Error while installing '%s': %s", name, err)
  213. }
  214. }
  215. return nil
  216. }
  217. return run
  218. }
  219. func NewItemsInstallCmd(typeName string) *cobra.Command {
  220. it := hubItemTypes[typeName]
  221. cmd := &cobra.Command{
  222. Use: coalesce.String(it.installHelp.use, "install [item]..."),
  223. Short: coalesce.String(it.installHelp.short, fmt.Sprintf("Install given %s", it.oneOrMore)),
  224. Long: coalesce.String(it.installHelp.long, fmt.Sprintf("Fetch and install one or more %s from the hub", it.name)),
  225. Example: it.installHelp.example,
  226. Args: cobra.MinimumNArgs(1),
  227. DisableAutoGenTag: true,
  228. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  229. return compAllItems(typeName, args, toComplete)
  230. },
  231. RunE: itemsInstallRunner(it),
  232. }
  233. flags := cmd.Flags()
  234. flags.BoolP("download-only", "d", false, "Only download packages, don't enable")
  235. flags.Bool("force", false, "Force install: overwrite tainted and outdated files")
  236. flags.Bool("ignore", false, fmt.Sprintf("Ignore errors when installing multiple %s", it.name))
  237. return cmd
  238. }
  239. func itemsRemoveRunner(it hubItemType) func(cmd *cobra.Command, args []string) error {
  240. run := func(cmd *cobra.Command, args []string) error {
  241. flags := cmd.Flags()
  242. purge, err := flags.GetBool("purge")
  243. if err != nil {
  244. return err
  245. }
  246. force, err := flags.GetBool("force")
  247. if err != nil {
  248. return err
  249. }
  250. all, err := flags.GetBool("all")
  251. if err != nil {
  252. return err
  253. }
  254. hub, err := cwhub.GetHub()
  255. if err != nil {
  256. return err
  257. }
  258. if all {
  259. err := hub.RemoveMany(it.name, "", all, purge, force)
  260. if err != nil {
  261. return err
  262. }
  263. return nil
  264. }
  265. if len(args) == 0 {
  266. return fmt.Errorf("specify at least one %s to remove or '--all'", it.singular)
  267. }
  268. for _, name := range args {
  269. if !force {
  270. item := hub.GetItem(it.name, name)
  271. if item == nil {
  272. // XXX: this should be in GetItem?
  273. return fmt.Errorf("can't find '%s' in %s", name, it.name)
  274. }
  275. if len(item.BelongsToCollections) > 0 {
  276. log.Warningf("%s belongs to collections: %s", name, item.BelongsToCollections)
  277. log.Warningf("Run 'sudo cscli %s remove %s --force' if you want to force remove this %s", it.name, name, it.singular)
  278. continue
  279. }
  280. }
  281. err := hub.RemoveMany(it.name, name, all, purge, force)
  282. if err != nil {
  283. return err
  284. }
  285. }
  286. return nil
  287. }
  288. return run
  289. }
  290. func NewItemsRemoveCmd(typeName string) *cobra.Command {
  291. it := hubItemTypes[typeName]
  292. cmd := &cobra.Command{
  293. Use: coalesce.String(it.removeHelp.use, "remove [item]..."),
  294. Short: coalesce.String(it.removeHelp.short, fmt.Sprintf("Remove given %s", it.oneOrMore)),
  295. Long: coalesce.String(it.removeHelp.long, fmt.Sprintf("Remove one or more %s", it.name)),
  296. Example: it.removeHelp.example,
  297. Aliases: []string{"delete"},
  298. DisableAutoGenTag: true,
  299. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  300. return compInstalledItems(it.name, args, toComplete)
  301. },
  302. RunE: itemsRemoveRunner(it),
  303. }
  304. flags := cmd.Flags()
  305. flags.Bool("purge", false, "Delete source file too")
  306. flags.Bool("force", false, "Force remove: remove tainted and outdated files")
  307. flags.Bool("all", false, fmt.Sprintf("Remove all the %s", it.name))
  308. return cmd
  309. }
  310. func itemsUpgradeRunner(it hubItemType) func(cmd *cobra.Command, args []string) error {
  311. run := func(cmd *cobra.Command, args []string) error {
  312. flags := cmd.Flags()
  313. force, err := flags.GetBool("force")
  314. if err != nil {
  315. return err
  316. }
  317. all, err := flags.GetBool("all")
  318. if err != nil {
  319. return err
  320. }
  321. hub, err := cwhub.GetHub()
  322. if err != nil {
  323. return err
  324. }
  325. if all {
  326. if err := hub.UpgradeConfig(it.name, "", force); err != nil {
  327. return err
  328. }
  329. return nil
  330. }
  331. if len(args) == 0 {
  332. return fmt.Errorf("specify at least one %s to upgrade or '--all'", it.singular)
  333. }
  334. for _, name := range args {
  335. if err := hub.UpgradeConfig(it.name, name, force); err != nil {
  336. return err
  337. }
  338. }
  339. return nil
  340. }
  341. return run
  342. }
  343. func NewItemsUpgradeCmd(typeName string) *cobra.Command {
  344. it := hubItemTypes[typeName]
  345. cmd := &cobra.Command{
  346. Use: coalesce.String(it.upgradeHelp.use, "upgrade [item]..."),
  347. Short: coalesce.String(it.upgradeHelp.short, fmt.Sprintf("Upgrade given %s", it.oneOrMore)),
  348. Long: coalesce.String(it.upgradeHelp.long, fmt.Sprintf("Fetch and upgrade one or more %s from the hub", it.name)),
  349. Example: it.upgradeHelp.example,
  350. DisableAutoGenTag: true,
  351. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  352. return compInstalledItems(it.name, args, toComplete)
  353. },
  354. RunE: itemsUpgradeRunner(it),
  355. }
  356. flags := cmd.Flags()
  357. flags.BoolP("all", "a", false, fmt.Sprintf("Upgrade all the %s", it.name))
  358. flags.Bool("force", false, "Force upgrade: overwrite tainted and outdated files")
  359. return cmd
  360. }
  361. func itemsInspectRunner(it hubItemType) func(cmd *cobra.Command, args []string) error {
  362. run := func(cmd *cobra.Command, args []string) error {
  363. flags := cmd.Flags()
  364. url, err := flags.GetString("url")
  365. if err != nil {
  366. return err
  367. }
  368. if url != "" {
  369. csConfig.Cscli.PrometheusUrl = url
  370. }
  371. noMetrics, err := flags.GetBool("no-metrics")
  372. if err != nil {
  373. return err
  374. }
  375. for _, name := range args {
  376. if err = InspectItem(name, it.name, noMetrics); err != nil {
  377. return err
  378. }
  379. }
  380. return nil
  381. }
  382. return run
  383. }
  384. func NewItemsInspectCmd(typeName string) *cobra.Command {
  385. it := hubItemTypes[typeName]
  386. cmd := &cobra.Command{
  387. Use: coalesce.String(it.inspectHelp.use, "inspect [item]..."),
  388. Short: coalesce.String(it.inspectHelp.short, fmt.Sprintf("Inspect given %s", it.oneOrMore)),
  389. Long: coalesce.String(it.inspectHelp.long, fmt.Sprintf("Inspect the state of one or more %s", it.name)),
  390. Example: it.inspectHelp.example,
  391. Args: cobra.MinimumNArgs(1),
  392. DisableAutoGenTag: true,
  393. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  394. return compInstalledItems(it.name, args, toComplete)
  395. },
  396. RunE: itemsInspectRunner(it),
  397. }
  398. flags := cmd.Flags()
  399. flags.StringP("url", "u", "", "Prometheus url")
  400. flags.Bool("no-metrics", false, "Don't show metrics (when cscli.output=human)")
  401. return cmd
  402. }
  403. func itemsListRunner(it hubItemType) func(cmd *cobra.Command, args []string) error {
  404. run := func(cmd *cobra.Command, args []string) error {
  405. flags := cmd.Flags()
  406. all, err := flags.GetBool("all")
  407. if err != nil {
  408. return err
  409. }
  410. if err = ListItems(color.Output, []string{it.name}, args, false, true, all); err != nil {
  411. return err
  412. }
  413. return nil
  414. }
  415. return run
  416. }
  417. func NewItemsListCmd(typeName string) *cobra.Command {
  418. it := hubItemTypes[typeName]
  419. cmd := &cobra.Command{
  420. Use: coalesce.String(it.listHelp.use, "list [item... | -a]"),
  421. Short: coalesce.String(it.listHelp.short, fmt.Sprintf("List %s", it.oneOrMore)),
  422. Long: coalesce.String(it.listHelp.long, fmt.Sprintf("List of installed/available/specified %s", it.name)),
  423. Example: it.listHelp.example,
  424. DisableAutoGenTag: true,
  425. RunE: itemsListRunner(it),
  426. }
  427. flags := cmd.Flags()
  428. flags.BoolP("all", "a", false, "List disabled items as well")
  429. return cmd
  430. }