itemcommands.go 16 KB

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