itemcommands.go 15 KB

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