itemcli.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 cliHelp 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 itemCLI struct {
  20. name string // plural, as used in the hub index
  21. singular string
  22. oneOrMore string // parenthetical pluralizaion: "parser(s)"
  23. help cliHelp
  24. installHelp cliHelp
  25. removeHelp cliHelp
  26. upgradeHelp cliHelp
  27. inspectHelp cliHelp
  28. inspectDetail func(item *cwhub.Item) error
  29. listHelp cliHelp
  30. }
  31. func (it itemCLI) NewCommand() *cobra.Command {
  32. cmd := &cobra.Command{
  33. Use: coalesce.String(it.help.use, fmt.Sprintf("%s <action> [item]...", it.name)),
  34. Short: coalesce.String(it.help.short, fmt.Sprintf("Manage hub %s", it.name)),
  35. Long: it.help.long,
  36. Example: it.help.example,
  37. Args: cobra.MinimumNArgs(1),
  38. Aliases: []string{it.singular},
  39. DisableAutoGenTag: true,
  40. }
  41. cmd.AddCommand(it.NewInstallCmd())
  42. cmd.AddCommand(it.NewRemoveCmd())
  43. cmd.AddCommand(it.NewUpgradeCmd())
  44. cmd.AddCommand(it.NewInspectCmd())
  45. cmd.AddCommand(it.NewListCmd())
  46. return cmd
  47. }
  48. func (it itemCLI) Install(cmd *cobra.Command, args []string) error {
  49. flags := cmd.Flags()
  50. downloadOnly, err := flags.GetBool("download-only")
  51. if err != nil {
  52. return err
  53. }
  54. force, err := flags.GetBool("force")
  55. if err != nil {
  56. return err
  57. }
  58. ignoreError, err := flags.GetBool("ignore")
  59. if err != nil {
  60. return err
  61. }
  62. hub, err := require.Hub(csConfig, require.RemoteHub(csConfig))
  63. if err != nil {
  64. return err
  65. }
  66. for _, name := range args {
  67. item := hub.GetItem(it.name, name)
  68. if item == nil {
  69. msg := suggestNearestMessage(hub, it.name, name)
  70. if !ignoreError {
  71. return fmt.Errorf(msg)
  72. }
  73. log.Errorf(msg)
  74. continue
  75. }
  76. if err := item.Install(force, downloadOnly); err != nil {
  77. if !ignoreError {
  78. return fmt.Errorf("error while installing '%s': %w", item.Name, err)
  79. }
  80. log.Errorf("Error while installing '%s': %s", item.Name, err)
  81. }
  82. }
  83. log.Infof(ReloadMessage())
  84. return nil
  85. }
  86. func (it itemCLI) NewInstallCmd() *cobra.Command {
  87. cmd := &cobra.Command{
  88. Use: coalesce.String(it.installHelp.use, "install [item]..."),
  89. Short: coalesce.String(it.installHelp.short, fmt.Sprintf("Install given %s", it.oneOrMore)),
  90. Long: coalesce.String(it.installHelp.long, fmt.Sprintf("Fetch and install one or more %s from the hub", it.name)),
  91. Example: it.installHelp.example,
  92. Args: cobra.MinimumNArgs(1),
  93. DisableAutoGenTag: true,
  94. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  95. return compAllItems(it.name, args, toComplete)
  96. },
  97. RunE: it.Install,
  98. }
  99. flags := cmd.Flags()
  100. flags.BoolP("download-only", "d", false, "Only download packages, don't enable")
  101. flags.Bool("force", false, "Force install: overwrite tainted and outdated files")
  102. flags.Bool("ignore", false, fmt.Sprintf("Ignore errors when installing multiple %s", it.name))
  103. return cmd
  104. }
  105. // return the names of the installed parents of an item, used to check if we can remove it
  106. func istalledParentNames(item *cwhub.Item) []string {
  107. ret := make([]string, 0)
  108. for _, parent := range item.Ancestors() {
  109. if parent.State.Installed {
  110. ret = append(ret, parent.Name)
  111. }
  112. }
  113. return ret
  114. }
  115. func (it itemCLI) Remove(cmd *cobra.Command, args []string) error {
  116. flags := cmd.Flags()
  117. purge, err := flags.GetBool("purge")
  118. if err != nil {
  119. return err
  120. }
  121. force, err := flags.GetBool("force")
  122. if err != nil {
  123. return err
  124. }
  125. all, err := flags.GetBool("all")
  126. if err != nil {
  127. return err
  128. }
  129. hub, err := require.Hub(csConfig, nil)
  130. if err != nil {
  131. return err
  132. }
  133. if all {
  134. getter := hub.GetInstalledItems
  135. if purge {
  136. getter = hub.GetAllItems
  137. }
  138. items, err := getter(it.name)
  139. if err != nil {
  140. return err
  141. }
  142. removed := 0
  143. for _, item := range items {
  144. didRemove, err := item.Remove(purge, force)
  145. if err != nil {
  146. return err
  147. }
  148. if didRemove {
  149. log.Infof("Removed %s", item.Name)
  150. removed++
  151. }
  152. }
  153. log.Infof("Removed %d %s", removed, it.name)
  154. if removed > 0 {
  155. log.Infof(ReloadMessage())
  156. }
  157. return nil
  158. }
  159. if len(args) == 0 {
  160. return fmt.Errorf("specify at least one %s to remove or '--all'", it.singular)
  161. }
  162. removed := 0
  163. for _, itemName := range args {
  164. item := hub.GetItem(it.name, itemName)
  165. if item == nil {
  166. return fmt.Errorf("can't find '%s' in %s", itemName, it.name)
  167. }
  168. parents := istalledParentNames(item)
  169. if !force && len(parents) > 0 {
  170. log.Warningf("%s belongs to collections: %s", item.Name, parents)
  171. log.Warningf("Run 'sudo cscli %s remove %s --force' if you want to force remove this %s", item.Type, item.Name, it.singular)
  172. continue
  173. }
  174. didRemove, err := item.Remove(purge, force)
  175. if err != nil {
  176. return err
  177. }
  178. if didRemove {
  179. log.Infof("Removed %s", item.Name)
  180. removed++
  181. }
  182. }
  183. log.Infof("Removed %d %s", removed, it.name)
  184. if removed > 0 {
  185. log.Infof(ReloadMessage())
  186. }
  187. return nil
  188. }
  189. func (it itemCLI) NewRemoveCmd() *cobra.Command {
  190. cmd := &cobra.Command{
  191. Use: coalesce.String(it.removeHelp.use, "remove [item]..."),
  192. Short: coalesce.String(it.removeHelp.short, fmt.Sprintf("Remove given %s", it.oneOrMore)),
  193. Long: coalesce.String(it.removeHelp.long, fmt.Sprintf("Remove one or more %s", it.name)),
  194. Example: it.removeHelp.example,
  195. Aliases: []string{"delete"},
  196. DisableAutoGenTag: true,
  197. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  198. return compInstalledItems(it.name, args, toComplete)
  199. },
  200. RunE: it.Remove,
  201. }
  202. flags := cmd.Flags()
  203. flags.Bool("purge", false, "Delete source file too")
  204. flags.Bool("force", false, "Force remove: remove tainted and outdated files")
  205. flags.Bool("all", false, fmt.Sprintf("Remove all the %s", it.name))
  206. return cmd
  207. }
  208. func (it itemCLI) Upgrade(cmd *cobra.Command, args []string) error {
  209. flags := cmd.Flags()
  210. force, err := flags.GetBool("force")
  211. if err != nil {
  212. return err
  213. }
  214. all, err := flags.GetBool("all")
  215. if err != nil {
  216. return err
  217. }
  218. hub, err := require.Hub(csConfig, require.RemoteHub(csConfig))
  219. if err != nil {
  220. return err
  221. }
  222. if all {
  223. items, err := hub.GetInstalledItems(it.name)
  224. if err != nil {
  225. return err
  226. }
  227. updated := 0
  228. for _, item := range items {
  229. didUpdate, err := item.Upgrade(force)
  230. if err != nil {
  231. return err
  232. }
  233. if didUpdate {
  234. updated++
  235. }
  236. }
  237. log.Infof("Updated %d %s", updated, it.name)
  238. if updated > 0 {
  239. log.Infof(ReloadMessage())
  240. }
  241. return nil
  242. }
  243. if len(args) == 0 {
  244. return fmt.Errorf("specify at least one %s to upgrade or '--all'", it.singular)
  245. }
  246. updated := 0
  247. for _, itemName := range args {
  248. item := hub.GetItem(it.name, itemName)
  249. if item == nil {
  250. return fmt.Errorf("can't find '%s' in %s", itemName, it.name)
  251. }
  252. didUpdate, err := item.Upgrade(force)
  253. if err != nil {
  254. return err
  255. }
  256. if didUpdate {
  257. log.Infof("Updated %s", item.Name)
  258. updated++
  259. }
  260. }
  261. if updated > 0 {
  262. log.Infof(ReloadMessage())
  263. }
  264. return nil
  265. }
  266. func (it itemCLI) NewUpgradeCmd() *cobra.Command {
  267. cmd := &cobra.Command{
  268. Use: coalesce.String(it.upgradeHelp.use, "upgrade [item]..."),
  269. Short: coalesce.String(it.upgradeHelp.short, fmt.Sprintf("Upgrade given %s", it.oneOrMore)),
  270. Long: coalesce.String(it.upgradeHelp.long, fmt.Sprintf("Fetch and upgrade one or more %s from the hub", it.name)),
  271. Example: it.upgradeHelp.example,
  272. DisableAutoGenTag: true,
  273. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  274. return compInstalledItems(it.name, args, toComplete)
  275. },
  276. RunE: it.Upgrade,
  277. }
  278. flags := cmd.Flags()
  279. flags.BoolP("all", "a", false, fmt.Sprintf("Upgrade all the %s", it.name))
  280. flags.Bool("force", false, "Force upgrade: overwrite tainted and outdated files")
  281. return cmd
  282. }
  283. func (it itemCLI) Inspect(cmd *cobra.Command, args []string) error {
  284. flags := cmd.Flags()
  285. url, err := flags.GetString("url")
  286. if err != nil {
  287. return err
  288. }
  289. if url != "" {
  290. csConfig.Cscli.PrometheusUrl = url
  291. }
  292. noMetrics, err := flags.GetBool("no-metrics")
  293. if err != nil {
  294. return err
  295. }
  296. hub, err := require.Hub(csConfig, nil)
  297. if err != nil {
  298. return err
  299. }
  300. for _, name := range args {
  301. item := hub.GetItem(it.name, name)
  302. if item == nil {
  303. return fmt.Errorf("can't find '%s' in %s", name, it.name)
  304. }
  305. if err = InspectItem(item, !noMetrics); err != nil {
  306. return err
  307. }
  308. if it.inspectDetail != nil {
  309. if err = it.inspectDetail(item); err != nil {
  310. return err
  311. }
  312. }
  313. }
  314. return nil
  315. }
  316. func (it itemCLI) NewInspectCmd() *cobra.Command {
  317. cmd := &cobra.Command{
  318. Use: coalesce.String(it.inspectHelp.use, "inspect [item]..."),
  319. Short: coalesce.String(it.inspectHelp.short, fmt.Sprintf("Inspect given %s", it.oneOrMore)),
  320. Long: coalesce.String(it.inspectHelp.long, fmt.Sprintf("Inspect the state of one or more %s", it.name)),
  321. Example: it.inspectHelp.example,
  322. Args: cobra.MinimumNArgs(1),
  323. DisableAutoGenTag: true,
  324. ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  325. return compInstalledItems(it.name, args, toComplete)
  326. },
  327. RunE: it.Inspect,
  328. }
  329. flags := cmd.Flags()
  330. flags.StringP("url", "u", "", "Prometheus url")
  331. flags.Bool("no-metrics", false, "Don't show metrics (when cscli.output=human)")
  332. return cmd
  333. }
  334. func (it itemCLI) List(cmd *cobra.Command, args []string) error {
  335. flags := cmd.Flags()
  336. all, err := flags.GetBool("all")
  337. if err != nil {
  338. return err
  339. }
  340. hub, err := require.Hub(csConfig, nil)
  341. if err != nil {
  342. return err
  343. }
  344. items := make(map[string][]*cwhub.Item)
  345. items[it.name], err = selectItems(hub, it.name, args, !all)
  346. if err != nil {
  347. return err
  348. }
  349. if err = listItems(color.Output, []string{it.name}, items, false); err != nil {
  350. return err
  351. }
  352. return nil
  353. }
  354. func (it itemCLI) NewListCmd() *cobra.Command {
  355. cmd := &cobra.Command{
  356. Use: coalesce.String(it.listHelp.use, "list [item... | -a]"),
  357. Short: coalesce.String(it.listHelp.short, fmt.Sprintf("List %s", it.oneOrMore)),
  358. Long: coalesce.String(it.listHelp.long, fmt.Sprintf("List of installed/available/specified %s", it.name)),
  359. Example: it.listHelp.example,
  360. DisableAutoGenTag: true,
  361. RunE: it.List,
  362. }
  363. flags := cmd.Flags()
  364. flags.BoolP("all", "a", false, "List disabled items as well")
  365. return cmd
  366. }