123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package main
- import (
- "fmt"
- "github.com/crowdsecurity/crowdsec/pkg/cwhub"
- log "github.com/sirupsen/logrus"
- "github.com/spf13/cobra"
- )
- func NewPostOverflowsCmd() *cobra.Command {
- var cmdPostOverflows = &cobra.Command{
- Use: "postoverflows [action] [config]",
- Short: "Install/Remove/Upgrade/Inspect postoverflow(s) from hub",
- Example: `cscli postoverflows install crowdsecurity/cdn-whitelist
- cscli postoverflows inspect crowdsecurity/cdn-whitelist
- cscli postoverflows upgrade crowdsecurity/cdn-whitelist
- cscli postoverflows list
- cscli postoverflows remove crowdsecurity/cdn-whitelist`,
- Args: cobra.MinimumNArgs(1),
- Aliases: []string{"postoverflow"},
- DisableAutoGenTag: true,
- PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
- if err := csConfig.LoadHub(); err != nil {
- log.Fatal(err)
- }
- if csConfig.Hub == nil {
- return fmt.Errorf("you must configure cli before interacting with hub")
- }
- if err := cwhub.SetHubBranch(); err != nil {
- return fmt.Errorf("error while setting hub branch: %s", err)
- }
- if err := cwhub.GetHubIdx(csConfig.Hub); err != nil {
- log.Info("Run 'sudo cscli hub update' to get the hub index")
- log.Fatalf("Failed to get Hub index : %v", err)
- }
- return nil
- },
- PersistentPostRun: func(cmd *cobra.Command, args []string) {
- if cmd.Name() == "inspect" || cmd.Name() == "list" {
- return
- }
- log.Infof(ReloadMessage())
- },
- }
- var ignoreError bool
- var cmdPostOverflowsInstall = &cobra.Command{
- Use: "install [config]",
- Short: "Install given postoverflow(s)",
- Long: `Fetch and install given postoverflow(s) from hub`,
- Example: `cscli postoverflows install crowdsec/xxx crowdsec/xyz`,
- Args: cobra.MinimumNArgs(1),
- DisableAutoGenTag: true,
- ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
- return compAllItems(cwhub.PARSERS_OVFLW, args, toComplete)
- },
- Run: func(cmd *cobra.Command, args []string) {
- for _, name := range args {
- if err := cwhub.InstallItem(csConfig, name, cwhub.PARSERS_OVFLW, forceAction, downloadOnly); err != nil {
- if ignoreError {
- log.Errorf("Error while installing '%s': %s", name, err)
- } else {
- log.Fatalf("Error while installing '%s': %s", name, err)
- }
- }
- }
- },
- }
- cmdPostOverflowsInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable")
- cmdPostOverflowsInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files")
- cmdPostOverflowsInstall.PersistentFlags().BoolVar(&ignoreError, "ignore", false, "Ignore errors when installing multiple postoverflows")
- cmdPostOverflows.AddCommand(cmdPostOverflowsInstall)
- var cmdPostOverflowsRemove = &cobra.Command{
- Use: "remove [config]",
- Short: "Remove given postoverflow(s)",
- Long: `remove given postoverflow(s)`,
- Example: `cscli postoverflows remove crowdsec/xxx crowdsec/xyz`,
- DisableAutoGenTag: true,
- Aliases: []string{"delete"},
- ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
- return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
- },
- Run: func(cmd *cobra.Command, args []string) {
- if all {
- cwhub.RemoveMany(csConfig, cwhub.PARSERS_OVFLW, "", all, purge, forceAction)
- return
- }
- if len(args) == 0 {
- log.Fatalf("Specify at least one postoverflow to remove or '--all' flag.")
- }
- for _, name := range args {
- cwhub.RemoveMany(csConfig, cwhub.PARSERS_OVFLW, name, all, purge, forceAction)
- }
- },
- }
- cmdPostOverflowsRemove.PersistentFlags().BoolVar(&purge, "purge", false, "Delete source file too")
- cmdPostOverflowsRemove.PersistentFlags().BoolVar(&forceAction, "force", false, "Force remove : Remove tainted and outdated files")
- cmdPostOverflowsRemove.PersistentFlags().BoolVar(&all, "all", false, "Delete all the postoverflows")
- cmdPostOverflows.AddCommand(cmdPostOverflowsRemove)
- var cmdPostOverflowsUpgrade = &cobra.Command{
- Use: "upgrade [config]",
- Short: "Upgrade given postoverflow(s)",
- Long: `Fetch and Upgrade given postoverflow(s) from hub`,
- Example: `cscli postoverflows upgrade crowdsec/xxx crowdsec/xyz`,
- DisableAutoGenTag: true,
- ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
- return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
- },
- Run: func(cmd *cobra.Command, args []string) {
- if all {
- cwhub.UpgradeConfig(csConfig, cwhub.PARSERS_OVFLW, "", forceAction)
- } else {
- if len(args) == 0 {
- log.Fatalf("no target postoverflow to upgrade")
- }
- for _, name := range args {
- cwhub.UpgradeConfig(csConfig, cwhub.PARSERS_OVFLW, name, forceAction)
- }
- }
- },
- }
- cmdPostOverflowsUpgrade.PersistentFlags().BoolVarP(&all, "all", "a", false, "Upgrade all the postoverflows")
- cmdPostOverflowsUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files")
- cmdPostOverflows.AddCommand(cmdPostOverflowsUpgrade)
- var cmdPostOverflowsInspect = &cobra.Command{
- Use: "inspect [config]",
- Short: "Inspect given postoverflow",
- Long: `Inspect given postoverflow`,
- Example: `cscli postoverflows inspect crowdsec/xxx crowdsec/xyz`,
- DisableAutoGenTag: true,
- ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
- return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
- },
- Args: cobra.MinimumNArgs(1),
- Run: func(cmd *cobra.Command, args []string) {
- InspectItem(args[0], cwhub.PARSERS_OVFLW)
- },
- }
- cmdPostOverflows.AddCommand(cmdPostOverflowsInspect)
- var cmdPostOverflowsList = &cobra.Command{
- Use: "list [config]",
- Short: "List all postoverflows or given one",
- Long: `List all postoverflows or given one`,
- Example: `cscli postoverflows list
- cscli postoverflows list crowdsecurity/xxx`,
- DisableAutoGenTag: true,
- Run: func(cmd *cobra.Command, args []string) {
- ListItems([]string{cwhub.PARSERS_OVFLW}, args, false, true, all)
- },
- }
- cmdPostOverflowsList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List disabled items as well")
- cmdPostOverflows.AddCommand(cmdPostOverflowsList)
- return cmdPostOverflows
- }
|