2020-11-30 09:37:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewHubCmd() *cobra.Command {
|
|
|
|
/* ---- HUB 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")
|
|
|
|
|
|
|
|
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,
|
2020-11-30 09:37:17 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-03-29 08:33:23 +00:00
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
if err := csConfig.LoadHub(); err != nil {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Fatal(err)
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
if err := cwhub.GetHubIdx(csConfig.Hub); err != nil {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Info("Run 'sudo cscli hub update' to get the hub index")
|
2020-11-30 09:37:17 +00:00
|
|
|
log.Fatalf("Failed to get Hub index : %v", err)
|
|
|
|
}
|
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)
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
cwhub.DisplaySummary()
|
2022-08-18 09:54:01 +00:00
|
|
|
items := ListItems([]string{
|
2022-04-19 10:07:35 +00:00
|
|
|
cwhub.COLLECTIONS, cwhub.PARSERS, cwhub.SCENARIOS, cwhub.PARSERS_OVFLW,
|
|
|
|
}, args, true, false, all)
|
2022-08-18 09:54:01 +00:00
|
|
|
fmt.Printf("%s\n", items)
|
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
|
|
|
cmdHub.AddCommand(cmdHubList)
|
|
|
|
|
|
|
|
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
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-03-24 17:16:17 +00:00
|
|
|
if err := csConfig.LoadHub(); err != nil {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Fatal(err)
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
if err := cwhub.UpdateHubIdx(csConfig.Hub); err != nil {
|
2020-11-30 09:37:17 +00:00
|
|
|
log.Fatalf("Failed to get Hub index : %v", err)
|
|
|
|
}
|
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)
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
cmdHub.AddCommand(cmdHubUpdate)
|
|
|
|
|
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
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-03-24 17:16:17 +00:00
|
|
|
if err := csConfig.LoadHub(); err != nil {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Fatal(err)
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
if err := cwhub.GetHubIdx(csConfig.Hub); err != nil {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Info("Run 'sudo cscli hub update' to get the hub index")
|
2020-12-14 10:53:30 +00:00
|
|
|
log.Fatalf("Failed to get Hub index : %v", err)
|
|
|
|
}
|
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)
|
2020-12-14 10:53:30 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
cmdHubUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files")
|
|
|
|
cmdHub.AddCommand(cmdHubUpgrade)
|
2020-11-30 09:37:17 +00:00
|
|
|
return cmdHub
|
|
|
|
}
|