refact "cscli papi" (#2802)
This commit is contained in:
parent
825c08aa9d
commit
45c669fb65
|
@ -210,7 +210,7 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
|
||||||
cmd.AddCommand(NewCLIHubTest().NewCommand())
|
cmd.AddCommand(NewCLIHubTest().NewCommand())
|
||||||
cmd.AddCommand(NewCLINotifications().NewCommand())
|
cmd.AddCommand(NewCLINotifications().NewCommand())
|
||||||
cmd.AddCommand(NewCLISupport().NewCommand())
|
cmd.AddCommand(NewCLISupport().NewCommand())
|
||||||
cmd.AddCommand(NewCLIPapi().NewCommand())
|
cmd.AddCommand(NewCLIPapi(getconfig).NewCommand())
|
||||||
cmd.AddCommand(NewCLICollection().NewCommand())
|
cmd.AddCommand(NewCLICollection().NewCommand())
|
||||||
cmd.AddCommand(NewCLIParser().NewCommand())
|
cmd.AddCommand(NewCLIParser().NewCommand())
|
||||||
cmd.AddCommand(NewCLIScenario().NewCommand())
|
cmd.AddCommand(NewCLIScenario().NewCommand())
|
||||||
|
@ -223,10 +223,6 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
|
||||||
cmd.AddCommand(NewSetupCmd())
|
cmd.AddCommand(NewSetupCmd())
|
||||||
}
|
}
|
||||||
|
|
||||||
if fflag.PapiClient.IsEnabled() {
|
|
||||||
cmd.AddCommand(NewCLIPapi().NewCommand())
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := cmd.Execute(); err != nil {
|
if err := cmd.Execute(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
@ -15,26 +16,31 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
type cliPapi struct {}
|
type cliPapi struct {
|
||||||
|
cfg configGetter
|
||||||
func NewCLIPapi() *cliPapi {
|
|
||||||
return &cliPapi{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli cliPapi) NewCommand() *cobra.Command {
|
func NewCLIPapi(getconfig configGetter) *cliPapi {
|
||||||
var cmd = &cobra.Command{
|
return &cliPapi{
|
||||||
|
cfg: getconfig,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cli *cliPapi) NewCommand() *cobra.Command {
|
||||||
|
cmd := &cobra.Command{
|
||||||
Use: "papi [action]",
|
Use: "papi [action]",
|
||||||
Short: "Manage interaction with Polling API (PAPI)",
|
Short: "Manage interaction with Polling API (PAPI)",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if err := require.LAPI(csConfig); err != nil {
|
cfg := cli.cfg()
|
||||||
|
if err := require.LAPI(cfg); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := require.CAPI(csConfig); err != nil {
|
if err := require.CAPI(cfg); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := require.PAPI(csConfig); err != nil {
|
if err := require.PAPI(cfg); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -47,35 +53,36 @@ func (cli cliPapi) NewCommand() *cobra.Command {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli cliPapi) NewStatusCmd() *cobra.Command {
|
func (cli *cliPapi) NewStatusCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "status",
|
Use: "status",
|
||||||
Short: "Get status of the Polling API",
|
Short: "Get status of the Polling API",
|
||||||
Args: cobra.MinimumNArgs(0),
|
Args: cobra.MinimumNArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
var err error
|
var err error
|
||||||
dbClient, err = database.NewClient(csConfig.DbConfig)
|
cfg := cli.cfg()
|
||||||
|
dbClient, err = database.NewClient(cfg.DbConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to initialize database client : %s", err)
|
return fmt.Errorf("unable to initialize database client: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
apic, err := apiserver.NewAPIC(csConfig.API.Server.OnlineClient, dbClient, csConfig.API.Server.ConsoleConfig, csConfig.API.Server.CapiWhitelists)
|
apic, err := apiserver.NewAPIC(cfg.API.Server.OnlineClient, dbClient, cfg.API.Server.ConsoleConfig, cfg.API.Server.CapiWhitelists)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to initialize API client : %s", err)
|
return fmt.Errorf("unable to initialize API client: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
papi, err := apiserver.NewPAPI(apic, dbClient, csConfig.API.Server.ConsoleConfig, log.GetLevel())
|
papi, err := apiserver.NewPAPI(apic, dbClient, cfg.API.Server.ConsoleConfig, log.GetLevel())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to initialize PAPI client : %s", err)
|
return fmt.Errorf("unable to initialize PAPI client: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
perms, err := papi.GetPermissions()
|
perms, err := papi.GetPermissions()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to get PAPI permissions: %s", err)
|
return fmt.Errorf("unable to get PAPI permissions: %s", err)
|
||||||
}
|
}
|
||||||
var lastTimestampStr *string
|
var lastTimestampStr *string
|
||||||
lastTimestampStr, err = dbClient.GetConfigItem(apiserver.PapiPullKey)
|
lastTimestampStr, err = dbClient.GetConfigItem(apiserver.PapiPullKey)
|
||||||
|
@ -90,45 +97,48 @@ func (cli cliPapi) NewStatusCmd() *cobra.Command {
|
||||||
for _, sub := range perms.Categories {
|
for _, sub := range perms.Categories {
|
||||||
log.Infof(" - %s", sub)
|
log.Infof(" - %s", sub)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli cliPapi) NewSyncCmd() *cobra.Command {
|
func (cli *cliPapi) NewSyncCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "sync",
|
Use: "sync",
|
||||||
Short: "Sync with the Polling API, pulling all non-expired orders for the instance",
|
Short: "Sync with the Polling API, pulling all non-expired orders for the instance",
|
||||||
Args: cobra.MinimumNArgs(0),
|
Args: cobra.MinimumNArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
var err error
|
var err error
|
||||||
|
cfg := cli.cfg()
|
||||||
t := tomb.Tomb{}
|
t := tomb.Tomb{}
|
||||||
dbClient, err = database.NewClient(csConfig.DbConfig)
|
|
||||||
|
dbClient, err = database.NewClient(cfg.DbConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to initialize database client : %s", err)
|
return fmt.Errorf("unable to initialize database client: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
apic, err := apiserver.NewAPIC(csConfig.API.Server.OnlineClient, dbClient, csConfig.API.Server.ConsoleConfig, csConfig.API.Server.CapiWhitelists)
|
apic, err := apiserver.NewAPIC(cfg.API.Server.OnlineClient, dbClient, cfg.API.Server.ConsoleConfig, cfg.API.Server.CapiWhitelists)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to initialize API client : %s", err)
|
return fmt.Errorf("unable to initialize API client: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Go(apic.Push)
|
t.Go(apic.Push)
|
||||||
|
|
||||||
papi, err := apiserver.NewPAPI(apic, dbClient, csConfig.API.Server.ConsoleConfig, log.GetLevel())
|
papi, err := apiserver.NewPAPI(apic, dbClient, cfg.API.Server.ConsoleConfig, log.GetLevel())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to initialize PAPI client : %s", err)
|
return fmt.Errorf("unable to initialize PAPI client: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Go(papi.SyncDecisions)
|
t.Go(papi.SyncDecisions)
|
||||||
|
|
||||||
err = papi.PullOnce(time.Time{}, true)
|
err = papi.PullOnce(time.Time{}, true)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to sync decisions: %s", err)
|
return fmt.Errorf("unable to sync decisions: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Sending acknowledgements to CAPI")
|
log.Infof("Sending acknowledgements to CAPI")
|
||||||
|
@ -138,6 +148,7 @@ func (cli cliPapi) NewSyncCmd() *cobra.Command {
|
||||||
t.Wait()
|
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
|
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
|
||||||
|
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue