2020-11-30 09:37:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-05-17 09:20:53 +00:00
|
|
|
"errors"
|
2020-11-30 09:37:17 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-10-13 10:28:24 +00:00
|
|
|
"github.com/fatih/color"
|
2020-11-30 09:37:17 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
2022-10-07 09:05:35 +00:00
|
|
|
|
2023-10-04 08:42:47 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
2022-10-07 09:05:35 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
2020-11-30 09:37:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewHubCmd() *cobra.Command {
|
|
|
|
var cmdHub = &cobra.Command{
|
|
|
|
Use: "hub [action]",
|
|
|
|
Short: "Manage Hub",
|
|
|
|
Long: `
|
|
|
|
Hub management
|
|
|
|
|
|
|
|
List/update parsers/scenarios/postoverflows/collections from [Crowdsec Hub](https://hub.crowdsec.net).
|
2022-05-24 13:46:48 +00:00
|
|
|
The Hub is managed by cscli, to get the latest hub files from [Crowdsec Hub](https://hub.crowdsec.net), you need to update.
|
2020-11-30 09:37:17 +00:00
|
|
|
`,
|
|
|
|
Example: `
|
|
|
|
cscli hub list # List all installed configurations
|
|
|
|
cscli hub update # Download list of available configurations from the hub
|
|
|
|
`,
|
2021-08-31 13:03:47 +00:00
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
DisableAutoGenTag: true,
|
2020-11-30 09:37:17 +00:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if csConfig.Cscli == nil {
|
|
|
|
return fmt.Errorf("you must configure cli before interacting with hub")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cmdHub.PersistentFlags().StringVarP(&cwhub.HubBranch, "branch", "b", "", "Use given branch from hub")
|
|
|
|
|
2023-01-19 12:29:36 +00:00
|
|
|
cmdHub.AddCommand(NewHubListCmd())
|
|
|
|
cmdHub.AddCommand(NewHubUpdateCmd())
|
|
|
|
cmdHub.AddCommand(NewHubUpgradeCmd())
|
|
|
|
|
|
|
|
return cmdHub
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHubListCmd() *cobra.Command {
|
2020-11-30 09:37:17 +00:00
|
|
|
var cmdHubList = &cobra.Command{
|
2021-08-31 13:03:47 +00:00
|
|
|
Use: "list [-a]",
|
|
|
|
Short: "List installed configs",
|
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
DisableAutoGenTag: true,
|
2023-10-04 08:42:47 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if err := require.Hub(csConfig); err != nil {
|
|
|
|
return err
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
2023-10-04 08:42:47 +00:00
|
|
|
|
|
|
|
// use LocalSync to get warnings about tainted / outdated items
|
2021-03-29 08:33:23 +00:00
|
|
|
_, warn := cwhub.LocalSync(csConfig.Hub)
|
|
|
|
for _, v := range warn {
|
|
|
|
log.Info(v)
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
cwhub.DisplaySummary()
|
2022-10-13 10:28:24 +00:00
|
|
|
ListItems(color.Output, []string{
|
2022-04-19 10:07:35 +00:00
|
|
|
cwhub.COLLECTIONS, cwhub.PARSERS, cwhub.SCENARIOS, cwhub.PARSERS_OVFLW,
|
|
|
|
}, args, true, false, all)
|
2023-10-04 08:42:47 +00:00
|
|
|
|
|
|
|
return nil
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
|
|
|
}
|
2022-01-03 16:09:07 +00:00
|
|
|
cmdHubList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List disabled items as well")
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2023-01-19 12:29:36 +00:00
|
|
|
return cmdHubList
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHubUpdateCmd() *cobra.Command {
|
2020-11-30 09:37:17 +00:00
|
|
|
var cmdHubUpdate = &cobra.Command{
|
|
|
|
Use: "update",
|
|
|
|
Short: "Fetch available configs from hub",
|
|
|
|
Long: `
|
|
|
|
Fetches the [.index.json](https://github.com/crowdsecurity/hub/blob/master/.index.json) file from hub, containing the list of available configs.
|
|
|
|
`,
|
2021-08-31 13:03:47 +00:00
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
DisableAutoGenTag: true,
|
2020-11-30 09:37:17 +00:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if csConfig.Cscli == nil {
|
|
|
|
return fmt.Errorf("you must configure cli before interacting with hub")
|
|
|
|
}
|
|
|
|
|
2022-05-24 13:46:48 +00:00
|
|
|
if err := cwhub.SetHubBranch(); err != nil {
|
2020-11-30 09:37:17 +00:00
|
|
|
return fmt.Errorf("error while setting hub branch: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2023-10-04 08:42:47 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2021-03-24 17:16:17 +00:00
|
|
|
if err := csConfig.LoadHub(); err != nil {
|
2023-10-04 08:42:47 +00:00
|
|
|
return err
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
if err := cwhub.UpdateHubIdx(csConfig.Hub); err != nil {
|
2023-10-04 08:42:47 +00:00
|
|
|
if !errors.Is(err, cwhub.ErrIndexNotFound) {
|
|
|
|
return fmt.Errorf("failed to get Hub index : %w", err)
|
|
|
|
}
|
|
|
|
log.Warnf("Could not find index file for branch '%s', using 'master'", cwhub.HubBranch)
|
|
|
|
cwhub.HubBranch = "master"
|
|
|
|
if err := cwhub.UpdateHubIdx(csConfig.Hub); err != nil {
|
|
|
|
return fmt.Errorf("failed to get Hub index after retry: %w", err)
|
2023-05-17 09:20:53 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2021-03-29 08:33:23 +00:00
|
|
|
//use LocalSync to get warnings about tainted / outdated items
|
|
|
|
_, warn := cwhub.LocalSync(csConfig.Hub)
|
|
|
|
for _, v := range warn {
|
|
|
|
log.Info(v)
|
|
|
|
}
|
2023-10-04 08:42:47 +00:00
|
|
|
|
|
|
|
return nil
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-01-19 12:29:36 +00:00
|
|
|
return cmdHubUpdate
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHubUpgradeCmd() *cobra.Command {
|
2020-12-14 10:53:30 +00:00
|
|
|
var cmdHubUpgrade = &cobra.Command{
|
|
|
|
Use: "upgrade",
|
|
|
|
Short: "Upgrade all configs installed from hub",
|
|
|
|
Long: `
|
|
|
|
Upgrade all configs installed from Crowdsec Hub. Run 'sudo cscli hub update' if you want the latest versions available.
|
|
|
|
`,
|
2021-08-31 13:03:47 +00:00
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
DisableAutoGenTag: true,
|
2020-12-14 10:53:30 +00:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if csConfig.Cscli == nil {
|
|
|
|
return fmt.Errorf("you must configure cli before interacting with hub")
|
|
|
|
}
|
|
|
|
|
2022-05-24 13:46:48 +00:00
|
|
|
if err := cwhub.SetHubBranch(); err != nil {
|
2020-12-14 10:53:30 +00:00
|
|
|
return fmt.Errorf("error while setting hub branch: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2023-10-04 08:42:47 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if err := require.Hub(csConfig); err != nil {
|
|
|
|
return err
|
2020-12-14 10:53:30 +00:00
|
|
|
}
|
2021-03-29 08:33:23 +00:00
|
|
|
|
2020-12-14 10:53:30 +00:00
|
|
|
log.Infof("Upgrading collections")
|
2022-04-19 10:07:35 +00:00
|
|
|
cwhub.UpgradeConfig(csConfig, cwhub.COLLECTIONS, "", forceAction)
|
2020-12-14 10:53:30 +00:00
|
|
|
log.Infof("Upgrading parsers")
|
2022-04-19 10:07:35 +00:00
|
|
|
cwhub.UpgradeConfig(csConfig, cwhub.PARSERS, "", forceAction)
|
2020-12-14 10:53:30 +00:00
|
|
|
log.Infof("Upgrading scenarios")
|
2022-04-19 10:07:35 +00:00
|
|
|
cwhub.UpgradeConfig(csConfig, cwhub.SCENARIOS, "", forceAction)
|
2020-12-14 10:53:30 +00:00
|
|
|
log.Infof("Upgrading postoverflows")
|
2022-04-19 10:07:35 +00:00
|
|
|
cwhub.UpgradeConfig(csConfig, cwhub.PARSERS_OVFLW, "", forceAction)
|
2023-10-04 08:42:47 +00:00
|
|
|
|
|
|
|
return nil
|
2020-12-14 10:53:30 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
cmdHubUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files")
|
2023-01-19 12:29:36 +00:00
|
|
|
|
|
|
|
return cmdHubUpgrade
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|