postoverflows.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/crowdsecurity/crowdsec/pkg/cwhub"
  5. log "github.com/sirupsen/logrus"
  6. "github.com/spf13/cobra"
  7. )
  8. func NewPostOverflowsCmd() *cobra.Command {
  9. var cmdPostOverflows = &cobra.Command{
  10. Use: "postoverflows [action] [config]",
  11. Short: "Install/Remove/Upgrade/Inspect postoverflow(s) from hub",
  12. Example: `cscli postoverflows install crowdsecurity/cdn-whitelist
  13. cscli postoverflows inspect crowdsecurity/cdn-whitelist
  14. cscli postoverflows upgrade crowdsecurity/cdn-whitelist
  15. cscli postoverflows list
  16. cscli postoverflows remove crowdsecurity/cdn-whitelist`,
  17. Args: cobra.MinimumNArgs(1),
  18. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  19. if csConfig.Cscli == nil {
  20. return fmt.Errorf("you must configure cli before interacting with hub")
  21. }
  22. if err := setHubBranch(); err != nil {
  23. return fmt.Errorf("error while setting hub branch: %s", err)
  24. }
  25. return nil
  26. },
  27. PersistentPostRun: func(cmd *cobra.Command, args []string) {
  28. if cmd.Name() == "inspect" || cmd.Name() == "list" {
  29. return
  30. }
  31. log.Infof("Run 'systemctl reload crowdsec' for the new configuration to be effective.")
  32. },
  33. }
  34. var cmdPostOverflowsInstall = &cobra.Command{
  35. Use: "install [config]",
  36. Short: "Install given postoverflow(s)",
  37. Long: `Fetch and install given postoverflow(s) from hub`,
  38. Example: `cscli postoverflows install crowdsec/xxx crowdsec/xyz`,
  39. Args: cobra.MinimumNArgs(1),
  40. Run: func(cmd *cobra.Command, args []string) {
  41. if err := cwhub.GetHubIdx(csConfig.Cscli); err != nil {
  42. log.Fatalf("Failed to get Hub index : %v", err)
  43. log.Infoln("Run 'sudo cscli hub update' to get the hub index")
  44. }
  45. for _, name := range args {
  46. InstallItem(name, cwhub.PARSERS_OVFLW, forceInstall)
  47. }
  48. },
  49. }
  50. cmdPostOverflowsInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable")
  51. cmdPostOverflowsInstall.PersistentFlags().BoolVar(&forceInstall, "force", false, "Force install : Overwrite tainted and outdated files")
  52. cmdPostOverflows.AddCommand(cmdPostOverflowsInstall)
  53. var cmdPostOverflowsRemove = &cobra.Command{
  54. Use: "remove [config]",
  55. Short: "Remove given postoverflow(s)",
  56. Long: `remove given postoverflow(s)`,
  57. Example: `cscli postoverflows remove crowdsec/xxx crowdsec/xyz`,
  58. Args: cobra.MinimumNArgs(1),
  59. Run: func(cmd *cobra.Command, args []string) {
  60. if err := cwhub.GetHubIdx(csConfig.Cscli); err != nil {
  61. log.Fatalf("Failed to get Hub index : %v", err)
  62. log.Infoln("Run 'sudo cscli hub update' to get the hub index")
  63. }
  64. if removeAll {
  65. RemoveMany(cwhub.PARSERS_OVFLW, "")
  66. } else {
  67. for _, name := range args {
  68. RemoveMany(cwhub.PARSERS_OVFLW, name)
  69. }
  70. }
  71. },
  72. }
  73. cmdPostOverflowsRemove.PersistentFlags().BoolVar(&purgeRemove, "purge", false, "Delete source file in ~/.cscli/hub/ too")
  74. cmdPostOverflowsRemove.PersistentFlags().BoolVar(&removeAll, "all", false, "Delete all the files in selected scope")
  75. cmdPostOverflows.AddCommand(cmdPostOverflowsRemove)
  76. var cmdPostOverflowsUpgrade = &cobra.Command{
  77. Use: "upgrade [config]",
  78. Short: "Upgrade given postoverflow(s)",
  79. Long: `Fetch and Upgrade given postoverflow(s) from hub`,
  80. Example: `cscli postoverflows upgrade crowdsec/xxx crowdsec/xyz`,
  81. Args: cobra.MinimumNArgs(1),
  82. Run: func(cmd *cobra.Command, args []string) {
  83. if err := cwhub.GetHubIdx(csConfig.Cscli); err != nil {
  84. log.Fatalf("Failed to get Hub index : %v", err)
  85. log.Infoln("Run 'sudo cscli hub update' to get the hub index")
  86. }
  87. if upgradeAll {
  88. UpgradeConfig(cwhub.PARSERS_OVFLW, "", forceUpgrade)
  89. } else {
  90. for _, name := range args {
  91. UpgradeConfig(cwhub.PARSERS_OVFLW, name, forceUpgrade)
  92. }
  93. }
  94. },
  95. }
  96. cmdPostOverflowsUpgrade.PersistentFlags().BoolVarP(&upgradeAll, "download-only", "d", false, "Only download packages, don't enable")
  97. cmdPostOverflowsUpgrade.PersistentFlags().BoolVar(&forceUpgrade, "force", false, "Force install : Overwrite tainted and outdated files")
  98. cmdPostOverflows.AddCommand(cmdPostOverflowsUpgrade)
  99. var cmdPostOverflowsInspect = &cobra.Command{
  100. Use: "inspect [config]",
  101. Short: "Inspect given postoverflow",
  102. Long: `Inspect given postoverflow`,
  103. Example: `cscli postoverflows inspect crowdsec/xxx crowdsec/xyz`,
  104. Args: cobra.MinimumNArgs(1),
  105. Run: func(cmd *cobra.Command, args []string) {
  106. if err := cwhub.GetHubIdx(csConfig.Cscli); err != nil {
  107. log.Fatalf("Failed to get Hub index : %v", err)
  108. log.Infoln("Run 'sudo cscli hub update' to get the hub index")
  109. }
  110. InspectItem(args[0], cwhub.PARSERS_OVFLW)
  111. },
  112. }
  113. cmdPostOverflows.AddCommand(cmdPostOverflowsInspect)
  114. var cmdPostOverflowsList = &cobra.Command{
  115. Use: "list [config]",
  116. Short: "List all postoverflows or given one",
  117. Long: `List all postoverflows or given one`,
  118. Example: `cscli postoverflows list
  119. cscli postoverflows list crowdsecurity/xxx`,
  120. Run: func(cmd *cobra.Command, args []string) {
  121. if err := cwhub.GetHubIdx(csConfig.Cscli); err != nil {
  122. log.Fatalf("Failed to get Hub index : %v", err)
  123. log.Infoln("Run 'sudo cscli hub update' to get the hub index")
  124. }
  125. ListItem(cwhub.PARSERS_OVFLW, args)
  126. },
  127. }
  128. cmdPostOverflowsList.PersistentFlags().BoolVarP(&listAll, "all", "a", false, "List as well disabled items")
  129. cmdPostOverflows.AddCommand(cmdPostOverflowsList)
  130. return cmdPostOverflows
  131. }