itemcommands.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. List only enabled parsers unless "-a" or names are specified.`,
  60. },
  61. },
  62. "postoverflows": {
  63. name: "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: "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: "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. removed++
  281. }
  282. }
  283. log.Infof("Removed %d %s", removed, it.name)
  284. if removed > 0 {
  285. log.Infof(ReloadMessage())
  286. }
  287. return nil
  288. }
  289. if len(args) == 0 {
  290. return fmt.Errorf("specify at least one %s to remove or '--all'", it.singular)
  291. }
  292. removed := 0
  293. for _, itemName := range args {
  294. item := hub.GetItem(it.name, itemName)
  295. if item == nil {
  296. return fmt.Errorf("can't find '%s' in %s", itemName, it.name)
  297. }
  298. parents := istalledParentNames(item)
  299. if !force && len(parents) > 0 {
  300. log.Warningf("%s belongs to collections: %s", item.Name, parents)
  301. log.Warningf("Run 'sudo cscli %s remove %s --force' if you want to force remove this %s", item.Type, item.Name, it.singular)
  302. continue
  303. }
  304. didRemove, err := item.Remove(purge, force)
  305. if err != nil {
  306. return err
  307. }
  308. if didRemove {
  309. log.Infof("Removed %s", item.Name)
  310. removed++
  311. }
  312. }
  313. if removed > 0 {
  314. log.Infof(ReloadMessage())
  315. }
  316. return nil
  317. }
  318. return run
  319. }
  320. func NewItemsRemoveCmd(typeName string) *cobra.Command {
  321. it := hubItemTypes[typeName]
  322. cmd := &cobra.Command{
  323. Use: coalesce.String(it.removeHelp.use, "remove [item]..."),
  324. Short: coalesce.String(it.removeHelp.short, fmt.Sprintf("Remove given %s", it.oneOrMore)),
  325. Long: coalesce.String(it.removeHelp.long, fmt.Sprintf("Remove one or more %s", it.name)),
  326. Example: it.removeHelp.example,
  327. Aliases: []string{"delete"},
  328. DisableAutoGenTag: true,
  329. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  330. return compInstalledItems(it.name, args, toComplete)
  331. },
  332. RunE: itemsRemoveRunner(it),
  333. }
  334. flags := cmd.Flags()
  335. flags.Bool("purge", false, "Delete source file too")
  336. flags.Bool("force", false, "Force remove: remove tainted and outdated files")
  337. flags.Bool("all", false, fmt.Sprintf("Remove all the %s", it.name))
  338. return cmd
  339. }
  340. func itemsUpgradeRunner(it hubItemType) func(cmd *cobra.Command, args []string) error {
  341. run := func(cmd *cobra.Command, args []string) error {
  342. flags := cmd.Flags()
  343. force, err := flags.GetBool("force")
  344. if err != nil {
  345. return err
  346. }
  347. all, err := flags.GetBool("all")
  348. if err != nil {
  349. return err
  350. }
  351. hub, err := require.Hub(csConfig, require.RemoteHub(csConfig))
  352. if err != nil {
  353. return err
  354. }
  355. if all {
  356. items, err := hub.GetInstalledItems(it.name)
  357. if err != nil {
  358. return err
  359. }
  360. updated := 0
  361. for _, item := range items {
  362. didUpdate, err := item.Upgrade(force)
  363. if err != nil {
  364. return err
  365. }
  366. if didUpdate {
  367. updated++
  368. }
  369. }
  370. log.Infof("Updated %d %s", updated, it.name)
  371. if updated > 0 {
  372. log.Infof(ReloadMessage())
  373. }
  374. return nil
  375. }
  376. if len(args) == 0 {
  377. return fmt.Errorf("specify at least one %s to upgrade or '--all'", it.singular)
  378. }
  379. updated := 0
  380. for _, itemName := range args {
  381. item := hub.GetItem(it.name, itemName)
  382. if item == nil {
  383. return fmt.Errorf("can't find '%s' in %s", itemName, it.name)
  384. }
  385. didUpdate, err := item.Upgrade(force)
  386. if err != nil {
  387. return err
  388. }
  389. if didUpdate {
  390. log.Infof("Updated %s", item.Name)
  391. updated++
  392. }
  393. }
  394. if updated > 0 {
  395. log.Infof(ReloadMessage())
  396. }
  397. return nil
  398. }
  399. return run
  400. }
  401. func NewItemsUpgradeCmd(typeName string) *cobra.Command {
  402. it := hubItemTypes[typeName]
  403. cmd := &cobra.Command{
  404. Use: coalesce.String(it.upgradeHelp.use, "upgrade [item]..."),
  405. Short: coalesce.String(it.upgradeHelp.short, fmt.Sprintf("Upgrade given %s", it.oneOrMore)),
  406. Long: coalesce.String(it.upgradeHelp.long, fmt.Sprintf("Fetch and upgrade one or more %s from the hub", it.name)),
  407. Example: it.upgradeHelp.example,
  408. DisableAutoGenTag: true,
  409. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  410. return compInstalledItems(it.name, args, toComplete)
  411. },
  412. RunE: itemsUpgradeRunner(it),
  413. }
  414. flags := cmd.Flags()
  415. flags.BoolP("all", "a", false, fmt.Sprintf("Upgrade all the %s", it.name))
  416. flags.Bool("force", false, "Force upgrade: overwrite tainted and outdated files")
  417. return cmd
  418. }
  419. func itemsInspectRunner(it hubItemType) func(cmd *cobra.Command, args []string) error {
  420. run := func(cmd *cobra.Command, args []string) error {
  421. flags := cmd.Flags()
  422. url, err := flags.GetString("url")
  423. if err != nil {
  424. return err
  425. }
  426. if url != "" {
  427. csConfig.Cscli.PrometheusUrl = url
  428. }
  429. noMetrics, err := flags.GetBool("no-metrics")
  430. if err != nil {
  431. return err
  432. }
  433. hub, err := require.Hub(csConfig, nil)
  434. if err != nil {
  435. return err
  436. }
  437. for _, name := range args {
  438. item := hub.GetItem(it.name, name)
  439. if item == nil {
  440. return fmt.Errorf("can't find '%s' in %s", name, it.name)
  441. }
  442. if err = InspectItem(item, !noMetrics); err != nil {
  443. return err
  444. }
  445. }
  446. return nil
  447. }
  448. return run
  449. }
  450. func NewItemsInspectCmd(typeName string) *cobra.Command {
  451. it := hubItemTypes[typeName]
  452. cmd := &cobra.Command{
  453. Use: coalesce.String(it.inspectHelp.use, "inspect [item]..."),
  454. Short: coalesce.String(it.inspectHelp.short, fmt.Sprintf("Inspect given %s", it.oneOrMore)),
  455. Long: coalesce.String(it.inspectHelp.long, fmt.Sprintf("Inspect the state of one or more %s", it.name)),
  456. Example: it.inspectHelp.example,
  457. Args: cobra.MinimumNArgs(1),
  458. DisableAutoGenTag: true,
  459. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  460. return compInstalledItems(it.name, args, toComplete)
  461. },
  462. RunE: itemsInspectRunner(it),
  463. }
  464. flags := cmd.Flags()
  465. flags.StringP("url", "u", "", "Prometheus url")
  466. flags.Bool("no-metrics", false, "Don't show metrics (when cscli.output=human)")
  467. return cmd
  468. }
  469. func itemsListRunner(it hubItemType) func(cmd *cobra.Command, args []string) error {
  470. run := func(cmd *cobra.Command, args []string) error {
  471. flags := cmd.Flags()
  472. all, err := flags.GetBool("all")
  473. if err != nil {
  474. return err
  475. }
  476. hub, err := require.Hub(csConfig, nil)
  477. if err != nil {
  478. return err
  479. }
  480. items := make(map[string][]*cwhub.Item)
  481. items[it.name], err = selectItems(hub, it.name, args, !all)
  482. if err != nil {
  483. return err
  484. }
  485. if err = listItems(color.Output, []string{it.name}, items); err != nil {
  486. return err
  487. }
  488. return nil
  489. }
  490. return run
  491. }
  492. func NewItemsListCmd(typeName string) *cobra.Command {
  493. it := hubItemTypes[typeName]
  494. cmd := &cobra.Command{
  495. Use: coalesce.String(it.listHelp.use, "list [item... | -a]"),
  496. Short: coalesce.String(it.listHelp.short, fmt.Sprintf("List %s", it.oneOrMore)),
  497. Long: coalesce.String(it.listHelp.long, fmt.Sprintf("List of installed/available/specified %s", it.name)),
  498. Example: it.listHelp.example,
  499. DisableAutoGenTag: true,
  500. RunE: itemsListRunner(it),
  501. }
  502. flags := cmd.Flags()
  503. flags.BoolP("all", "a", false, "List disabled items as well")
  504. return cmd
  505. }