2020-11-30 09:37:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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]",
|
2023-10-16 11:12:53 +00:00
|
|
|
Short: "Manage hub index",
|
|
|
|
Long: `Hub management
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
List/update parsers/scenarios/postoverflows/collections from [Crowdsec Hub](https://hub.crowdsec.net).
|
2023-10-16 11:12:53 +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.`,
|
|
|
|
Example: `cscli hub list
|
|
|
|
cscli hub update
|
|
|
|
cscli hub upgrade`,
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-01-19 12:29:36 +00:00
|
|
|
cmdHub.AddCommand(NewHubListCmd())
|
|
|
|
cmdHub.AddCommand(NewHubUpdateCmd())
|
|
|
|
cmdHub.AddCommand(NewHubUpgradeCmd())
|
|
|
|
|
|
|
|
return cmdHub
|
|
|
|
}
|
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
func runHubList(cmd *cobra.Command, args []string) error {
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
all, err := flags.GetBool("all")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-30 16:23:50 +00:00
|
|
|
hub, err := require.Hub(csConfig, nil)
|
2023-10-19 10:04:29 +00:00
|
|
|
if err != nil {
|
2023-10-12 12:53:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// use LocalSync to get warnings about tainted / outdated items
|
2023-10-19 10:04:29 +00:00
|
|
|
warn, _ := hub.LocalSync()
|
2023-10-12 12:53:17 +00:00
|
|
|
for _, v := range warn {
|
|
|
|
log.Info(v)
|
|
|
|
}
|
|
|
|
|
2023-11-09 10:34:14 +00:00
|
|
|
for _, line := range hub.ItemStats() {
|
2023-10-20 12:32:35 +00:00
|
|
|
log.Info(line)
|
|
|
|
}
|
2023-10-12 12:53:17 +00:00
|
|
|
|
2023-10-31 11:47:39 +00:00
|
|
|
err = ListItems(hub, color.Output, cwhub.ItemTypes, nil, true, false, all)
|
2023-10-16 12:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-12 12:53:17 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-19 12:29:36 +00:00
|
|
|
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]",
|
2023-10-16 11:12:53 +00:00
|
|
|
Short: "List all installed configurations",
|
2021-08-31 13:03:47 +00:00
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
DisableAutoGenTag: true,
|
2023-10-27 08:25:29 +00:00
|
|
|
RunE: runHubList,
|
2023-10-12 12:53:17 +00:00
|
|
|
}
|
2023-10-04 08:42:47 +00:00
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
flags := cmdHubList.Flags()
|
|
|
|
flags.BoolP("all", "a", false, "List disabled items as well")
|
2023-10-04 08:42:47 +00:00
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
return cmdHubList
|
|
|
|
}
|
|
|
|
|
|
|
|
func runHubUpdate(cmd *cobra.Command, args []string) error {
|
2023-10-30 16:23:50 +00:00
|
|
|
local := csConfig.Hub
|
|
|
|
remote := require.RemoteHub(csConfig)
|
2023-10-27 08:25:29 +00:00
|
|
|
|
2023-10-30 16:23:50 +00:00
|
|
|
// don't use require.Hub because if there is no index file, it would fail
|
2023-10-31 11:47:39 +00:00
|
|
|
hub, err := cwhub.NewHub(local, remote, true)
|
2023-10-19 10:04:29 +00:00
|
|
|
if err != nil {
|
2023-11-08 12:21:59 +00:00
|
|
|
return fmt.Errorf("failed to update hub: %w", err)
|
2023-10-12 12:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// use LocalSync to get warnings about tainted / outdated items
|
2023-10-19 10:04:29 +00:00
|
|
|
warn, _ := hub.LocalSync()
|
2023-10-12 12:53:17 +00:00
|
|
|
for _, v := range warn {
|
|
|
|
log.Info(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-01-19 12:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHubUpdateCmd() *cobra.Command {
|
2020-11-30 09:37:17 +00:00
|
|
|
var cmdHubUpdate = &cobra.Command{
|
|
|
|
Use: "update",
|
2023-10-16 11:12:53 +00:00
|
|
|
Short: "Download the latest index (catalog of available configurations)",
|
2020-11-30 09:37:17 +00:00
|
|
|
Long: `
|
2023-10-30 16:23:50 +00:00
|
|
|
Fetches the .index.json file from the hub, containing the list of available configs.
|
2020-11-30 09:37:17 +00:00
|
|
|
`,
|
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
|
|
|
|
},
|
2023-10-12 12:53:17 +00:00
|
|
|
RunE: runHubUpdate,
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 12:29:36 +00:00
|
|
|
return cmdHubUpdate
|
|
|
|
}
|
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
func runHubUpgrade(cmd *cobra.Command, args []string) error {
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
force, err := flags.GetBool("force")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-30 16:23:50 +00:00
|
|
|
hub, err := require.Hub(csConfig, require.RemoteHub(csConfig))
|
2023-10-19 10:04:29 +00:00
|
|
|
if err != nil {
|
2023-10-12 12:53:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-30 16:23:50 +00:00
|
|
|
for _, itemType := range cwhub.ItemTypes {
|
|
|
|
items, err := hub.GetInstalledItems(itemType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-16 11:12:53 +00:00
|
|
|
|
2023-10-30 16:23:50 +00:00
|
|
|
updated := 0
|
|
|
|
log.Infof("Upgrading %s", itemType)
|
|
|
|
for _, item := range items {
|
|
|
|
didUpdate, err := hub.UpgradeItem(itemType, item.Name, force)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if didUpdate {
|
|
|
|
updated++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Infof("Upgraded %d %s", updated, itemType)
|
2023-10-16 11:12:53 +00:00
|
|
|
}
|
2023-10-12 12:53:17 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-19 12:29:36 +00:00
|
|
|
func NewHubUpgradeCmd() *cobra.Command {
|
2020-12-14 10:53:30 +00:00
|
|
|
var cmdHubUpgrade = &cobra.Command{
|
|
|
|
Use: "upgrade",
|
2023-10-16 11:12:53 +00:00
|
|
|
Short: "Upgrade all configurations to their latest version",
|
2020-12-14 10:53:30 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2023-10-12 12:53:17 +00:00
|
|
|
RunE: runHubUpgrade,
|
2020-12-14 10:53:30 +00:00
|
|
|
}
|
2023-10-12 12:53:17 +00:00
|
|
|
|
|
|
|
flags := cmdHubUpgrade.Flags()
|
2023-10-16 11:12:53 +00:00
|
|
|
flags.Bool("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
|
|
|
}
|