2020-11-30 09:37:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-12-01 08:36:38 +00:00
|
|
|
"encoding/json"
|
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"
|
2023-12-01 08:36:38 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
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
|
|
|
)
|
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
type cliHub struct{}
|
|
|
|
|
|
|
|
func NewCLIHub() *cliHub {
|
|
|
|
return &cliHub{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli cliHub) NewCommand() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2020-11-30 09:37:17 +00:00
|
|
|
Use: "hub [action]",
|
2023-11-24 14:57:32 +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-11-24 14:57:32 +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
|
|
|
}
|
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
cmd.AddCommand(cli.NewListCmd())
|
|
|
|
cmd.AddCommand(cli.NewUpdateCmd())
|
|
|
|
cmd.AddCommand(cli.NewUpgradeCmd())
|
|
|
|
cmd.AddCommand(cli.NewTypesCmd())
|
2023-01-19 12:29:36 +00:00
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
return cmd
|
2023-01-19 12:29:36 +00:00
|
|
|
}
|
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
func (cli cliHub) list(cmd *cobra.Command, args []string) error {
|
2023-11-24 14:57:32 +00:00
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
all, err := flags.GetBool("all")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
hub, err := require.Hub(csConfig, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range hub.Warnings {
|
|
|
|
log.Info(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, line := range hub.ItemStats() {
|
|
|
|
log.Info(line)
|
|
|
|
}
|
|
|
|
|
|
|
|
items := make(map[string][]*cwhub.Item)
|
|
|
|
|
|
|
|
for _, itemType := range cwhub.ItemTypes {
|
|
|
|
items[itemType], err = selectItems(hub, itemType, nil, !all)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-05 12:38:52 +00:00
|
|
|
err = listItems(color.Output, cwhub.ItemTypes, items, true)
|
2023-11-24 14:57:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
func (cli cliHub) NewListCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2021-08-31 13:03:47 +00:00
|
|
|
Use: "list [-a]",
|
2023-11-24 14:57:32 +00:00
|
|
|
Short: "List all installed configurations",
|
2021-08-31 13:03:47 +00:00
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
DisableAutoGenTag: true,
|
2023-12-07 13:36:35 +00:00
|
|
|
RunE: cli.list,
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
2023-10-04 08:42:47 +00:00
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
flags := cmd.Flags()
|
2023-11-24 14:57:32 +00:00
|
|
|
flags.BoolP("all", "a", false, "List disabled items as well")
|
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
return cmd
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
2023-10-04 08:42:47 +00:00
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
func (cli cliHub) update(cmd *cobra.Command, args []string) error {
|
2023-11-24 14:57:32 +00:00
|
|
|
local := csConfig.Hub
|
|
|
|
remote := require.RemoteHub(csConfig)
|
|
|
|
|
|
|
|
// don't use require.Hub because if there is no index file, it would fail
|
|
|
|
hub, err := cwhub.NewHub(local, remote, true)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to update hub: %w", err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
for _, v := range hub.Warnings {
|
|
|
|
log.Info(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-01-19 12:29:36 +00:00
|
|
|
}
|
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
func (cli cliHub) NewUpdateCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2020-11-30 09:37:17 +00:00
|
|
|
Use: "update",
|
2023-11-24 14:57:32 +00:00
|
|
|
Short: "Download the latest index (catalog of available configurations)",
|
2020-11-30 09:37:17 +00:00
|
|
|
Long: `
|
2023-11-24 14:57:32 +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,
|
2023-12-07 13:36:35 +00:00
|
|
|
RunE: cli.update,
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
return cmd
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
func (cli cliHub) upgrade(cmd *cobra.Command, args []string) error {
|
2023-11-24 14:57:32 +00:00
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
force, err := flags.GetBool("force")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
hub, err := require.Hub(csConfig, require.RemoteHub(csConfig))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-06 11:59:51 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
for _, itemType := range cwhub.ItemTypes {
|
|
|
|
items, err := hub.GetInstalledItems(itemType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
updated := 0
|
|
|
|
|
|
|
|
log.Infof("Upgrading %s", itemType)
|
|
|
|
for _, item := range items {
|
|
|
|
didUpdate, err := item.Upgrade(force)
|
|
|
|
if err != nil {
|
2023-10-04 08:42:47 +00:00
|
|
|
return err
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
if didUpdate {
|
|
|
|
updated++
|
2021-03-29 08:33:23 +00:00
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
log.Infof("Upgraded %d %s", updated, itemType)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
return nil
|
2023-01-19 12:29:36 +00:00
|
|
|
}
|
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
func (cli cliHub) NewUpgradeCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2020-12-14 10:53:30 +00:00
|
|
|
Use: "upgrade",
|
2023-11-24 14:57:32 +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,
|
2023-12-07 13:36:35 +00:00
|
|
|
RunE: cli.upgrade,
|
2020-12-14 10:53:30 +00:00
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
flags := cmd.Flags()
|
2023-11-24 14:57:32 +00:00
|
|
|
flags.Bool("force", false, "Force upgrade: overwrite tainted and outdated files")
|
2023-01-19 12:29:36 +00:00
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
return cmd
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2023-12-01 08:36:38 +00:00
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
func (cli cliHub) types(cmd *cobra.Command, args []string) error {
|
2023-12-01 08:36:38 +00:00
|
|
|
switch csConfig.Cscli.Output {
|
|
|
|
case "human":
|
|
|
|
s, err := yaml.Marshal(cwhub.ItemTypes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Print(string(s))
|
|
|
|
case "json":
|
|
|
|
jsonStr, err := json.Marshal(cwhub.ItemTypes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(string(jsonStr))
|
|
|
|
case "raw":
|
|
|
|
for _, itemType := range cwhub.ItemTypes {
|
|
|
|
fmt.Println(itemType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
func (cli cliHub) NewTypesCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2023-12-01 08:36:38 +00:00
|
|
|
Use: "types",
|
|
|
|
Short: "List supported item types",
|
|
|
|
Long: `
|
|
|
|
List the types of supported hub items.
|
|
|
|
`,
|
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
DisableAutoGenTag: true,
|
2023-12-07 13:36:35 +00:00
|
|
|
RunE: cli.types,
|
2023-12-01 08:36:38 +00:00
|
|
|
}
|
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
return cmd
|
2023-12-01 08:36:38 +00:00
|
|
|
}
|