2023-03-03 12:46:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
type cliPapi struct {}
|
|
|
|
|
|
|
|
func NewCLIPapi() *cliPapi {
|
|
|
|
return &cliPapi{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli cliPapi) NewCommand() *cobra.Command {
|
|
|
|
var 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 {
|
2023-07-27 15:02:20 +00:00
|
|
|
if err := require.LAPI(csConfig); err != nil {
|
|
|
|
return err
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|
2023-07-27 15:02:20 +00:00
|
|
|
if err := require.CAPI(csConfig); err != nil {
|
|
|
|
return err
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|
2023-07-27 15:02:20 +00:00
|
|
|
if err := require.PAPI(csConfig); err != nil {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
func (cli cliPapi) NewStatusCmd() *cobra.Command {
|
|
|
|
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,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
var err error
|
|
|
|
dbClient, err = database.NewClient(csConfig.DbConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to initialize database client : %s", err)
|
|
|
|
}
|
|
|
|
|
2023-03-21 10:50:10 +00:00
|
|
|
apic, err := apiserver.NewAPIC(csConfig.API.Server.OnlineClient, dbClient, csConfig.API.Server.ConsoleConfig, csConfig.API.Server.CapiWhitelists)
|
2023-03-03 12:46:28 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to initialize API client : %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
papi, err := apiserver.NewPAPI(apic, dbClient, csConfig.API.Server.ConsoleConfig, log.GetLevel())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to initialize PAPI client : %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
perms, err := papi.GetPermissions()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to get PAPI permissions: %s", err)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
return cmd
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
func (cli cliPapi) NewSyncCmd() *cobra.Command {
|
|
|
|
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,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
var err error
|
|
|
|
t := tomb.Tomb{}
|
|
|
|
dbClient, err = database.NewClient(csConfig.DbConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to initialize database client : %s", err)
|
|
|
|
}
|
|
|
|
|
2023-03-21 10:50:10 +00:00
|
|
|
apic, err := apiserver.NewAPIC(csConfig.API.Server.OnlineClient, dbClient, csConfig.API.Server.ConsoleConfig, csConfig.API.Server.CapiWhitelists)
|
2023-03-03 12:46:28 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to initialize API client : %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Go(apic.Push)
|
|
|
|
|
|
|
|
papi, err := apiserver.NewPAPI(apic, dbClient, csConfig.API.Server.ConsoleConfig, log.GetLevel())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to initialize PAPI client : %s", err)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
log.Fatalf("unable to sync decisions: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-12-07 13:36:35 +00:00
|
|
|
return cmd
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|