2023-03-03 12:46:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-02-01 16:27:00 +00:00
|
|
|
"fmt"
|
2023-03-03 12:46:28 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"gopkg.in/tomb.v2"
|
2023-06-01 14:31:56 +00:00
|
|
|
|
2023-07-28 14:35:08 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/ptr"
|
2023-06-01 14:31:56 +00:00
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/apiserver"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
2023-07-27 15:02:20 +00:00
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
2023-03-03 12:46:28 +00:00
|
|
|
)
|
|
|
|
|
2024-02-01 16:27:00 +00:00
|
|
|
type cliPapi struct {
|
|
|
|
cfg configGetter
|
|
|
|
}
|
2023-12-07 13:36:35 +00:00
|
|
|
|
2024-02-01 16:27:00 +00:00
|
|
|
func NewCLIPapi(getconfig configGetter) *cliPapi {
|
|
|
|
return &cliPapi{
|
|
|
|
cfg: getconfig,
|
|
|
|
}
|
2023-12-07 13:36:35 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 16:27:00 +00:00
|
|
|
func (cli *cliPapi) NewCommand() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2023-03-03 12:46:28 +00:00
|
|
|
Use: "papi [action]",
|
|
|
|
Short: "Manage interaction with Polling API (PAPI)",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
DisableAutoGenTag: true,
|
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
2024-02-01 16:27:00 +00:00
|
|
|
cfg := cli.cfg()
|
|
|
|
if err := require.LAPI(cfg); err != nil {
|
2023-07-27 15:02:20 +00:00
|
|
|
return err
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|
2024-02-01 16:27:00 +00:00
|
|
|
if err := require.CAPI(cfg); err != nil {
|
2023-07-27 15:02:20 +00:00
|
|
|
return err
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|
2024-02-01 16:27:00 +00:00
|
|
|
if err := require.PAPI(cfg); err != nil {
|
2023-07-27 15:02:20 +00:00
|
|
|
return err
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
cmd.AddCommand(cli.NewStatusCmd())
|
|
|
|
cmd.AddCommand(cli.NewSyncCmd())
|
2023-03-03 12:46:28 +00:00
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
return cmd
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 16:27:00 +00:00
|
|
|
func (cli *cliPapi) NewStatusCmd() *cobra.Command {
|
2023-12-07 13:36:35 +00:00
|
|
|
cmd := &cobra.Command{
|
2023-03-03 12:46:28 +00:00
|
|
|
Use: "status",
|
|
|
|
Short: "Get status of the Polling API",
|
|
|
|
Args: cobra.MinimumNArgs(0),
|
|
|
|
DisableAutoGenTag: true,
|
2024-02-01 16:27:00 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-03-03 12:46:28 +00:00
|
|
|
var err error
|
2024-02-01 16:27:00 +00:00
|
|
|
cfg := cli.cfg()
|
|
|
|
dbClient, err = database.NewClient(cfg.DbConfig)
|
2023-03-03 12:46:28 +00:00
|
|
|
if err != nil {
|
2024-02-01 16:27:00 +00:00
|
|
|
return fmt.Errorf("unable to initialize database client: %s", err)
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 16:27:00 +00:00
|
|
|
apic, err := apiserver.NewAPIC(cfg.API.Server.OnlineClient, dbClient, cfg.API.Server.ConsoleConfig, cfg.API.Server.CapiWhitelists)
|
2023-03-03 12:46:28 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2024-02-01 16:27:00 +00:00
|
|
|
return fmt.Errorf("unable to initialize API client: %s", err)
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 16:27:00 +00:00
|
|
|
papi, err := apiserver.NewPAPI(apic, dbClient, cfg.API.Server.ConsoleConfig, log.GetLevel())
|
2023-03-03 12:46:28 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2024-02-01 16:27:00 +00:00
|
|
|
return fmt.Errorf("unable to initialize PAPI client: %s", err)
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
perms, err := papi.GetPermissions()
|
|
|
|
|
|
|
|
if err != nil {
|
2024-02-01 16:27:00 +00:00
|
|
|
return fmt.Errorf("unable to get PAPI permissions: %s", err)
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|
|
|
|
var lastTimestampStr *string
|
|
|
|
lastTimestampStr, err = dbClient.GetConfigItem(apiserver.PapiPullKey)
|
|
|
|
if err != nil {
|
2023-06-01 14:31:56 +00:00
|
|
|
lastTimestampStr = ptr.Of("never")
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|
|
|
|
log.Infof("You can successfully interact with Polling API (PAPI)")
|
|
|
|
log.Infof("Console plan: %s", perms.Plan)
|
|
|
|
log.Infof("Last order received: %s", *lastTimestampStr)
|
|
|
|
|
|
|
|
log.Infof("PAPI subscriptions:")
|
|
|
|
for _, sub := range perms.Categories {
|
|
|
|
log.Infof(" - %s", sub)
|
|
|
|
}
|
2024-02-01 16:27:00 +00:00
|
|
|
|
|
|
|
return nil
|
2023-03-03 12:46:28 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
return cmd
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 16:27:00 +00:00
|
|
|
func (cli *cliPapi) NewSyncCmd() *cobra.Command {
|
2023-12-07 13:36:35 +00:00
|
|
|
cmd := &cobra.Command{
|
2023-03-03 12:46:28 +00:00
|
|
|
Use: "sync",
|
|
|
|
Short: "Sync with the Polling API, pulling all non-expired orders for the instance",
|
|
|
|
Args: cobra.MinimumNArgs(0),
|
|
|
|
DisableAutoGenTag: true,
|
2024-02-01 16:27:00 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-03-03 12:46:28 +00:00
|
|
|
var err error
|
2024-02-01 16:27:00 +00:00
|
|
|
cfg := cli.cfg()
|
2023-03-03 12:46:28 +00:00
|
|
|
t := tomb.Tomb{}
|
2024-02-01 16:27:00 +00:00
|
|
|
|
|
|
|
dbClient, err = database.NewClient(cfg.DbConfig)
|
2023-03-03 12:46:28 +00:00
|
|
|
if err != nil {
|
2024-02-01 16:27:00 +00:00
|
|
|
return fmt.Errorf("unable to initialize database client: %s", err)
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 16:27:00 +00:00
|
|
|
apic, err := apiserver.NewAPIC(cfg.API.Server.OnlineClient, dbClient, cfg.API.Server.ConsoleConfig, cfg.API.Server.CapiWhitelists)
|
2023-03-03 12:46:28 +00:00
|
|
|
if err != nil {
|
2024-02-01 16:27:00 +00:00
|
|
|
return fmt.Errorf("unable to initialize API client: %s", err)
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Go(apic.Push)
|
|
|
|
|
2024-02-01 16:27:00 +00:00
|
|
|
papi, err := apiserver.NewPAPI(apic, dbClient, cfg.API.Server.ConsoleConfig, log.GetLevel())
|
2023-03-03 12:46:28 +00:00
|
|
|
if err != nil {
|
2024-02-01 16:27:00 +00:00
|
|
|
return fmt.Errorf("unable to initialize PAPI client: %s", err)
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|
2024-02-01 16:27:00 +00:00
|
|
|
|
2023-03-03 12:46:28 +00:00
|
|
|
t.Go(papi.SyncDecisions)
|
|
|
|
|
2023-03-21 13:06:19 +00:00
|
|
|
err = papi.PullOnce(time.Time{}, true)
|
2023-03-03 12:46:28 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2024-02-01 16:27:00 +00:00
|
|
|
return fmt.Errorf("unable to sync decisions: %s", err)
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Sending acknowledgements to CAPI")
|
|
|
|
|
|
|
|
apic.Shutdown()
|
|
|
|
papi.Shutdown()
|
|
|
|
t.Wait()
|
|
|
|
time.Sleep(5 * time.Second) //FIXME: the push done by apic.Push is run inside a sub goroutine, sleep to make sure it's done
|
|
|
|
|
2024-02-01 16:27:00 +00:00
|
|
|
return nil
|
2023-03-03 12:46:28 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
return cmd
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|