refact "cscli papi" (#2802)
This commit is contained in:
parent
825c08aa9d
commit
45c669fb65
2 changed files with 41 additions and 34 deletions
|
@ -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(NewCLINotifications().NewCommand())
|
||||
cmd.AddCommand(NewCLISupport().NewCommand())
|
||||
cmd.AddCommand(NewCLIPapi().NewCommand())
|
||||
cmd.AddCommand(NewCLIPapi(getconfig).NewCommand())
|
||||
cmd.AddCommand(NewCLICollection().NewCommand())
|
||||
cmd.AddCommand(NewCLIParser().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())
|
||||
}
|
||||
|
||||
if fflag.PapiClient.IsEnabled() {
|
||||
cmd.AddCommand(NewCLIPapi().NewCommand())
|
||||
}
|
||||
|
||||
if err := cmd.Execute(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
@ -15,26 +16,31 @@ import (
|
|||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||
)
|
||||
|
||||
type cliPapi struct {}
|
||||
|
||||
func NewCLIPapi() *cliPapi {
|
||||
return &cliPapi{}
|
||||
type cliPapi struct {
|
||||
cfg configGetter
|
||||
}
|
||||
|
||||
func (cli cliPapi) NewCommand() *cobra.Command {
|
||||
var cmd = &cobra.Command{
|
||||
func NewCLIPapi(getconfig configGetter) *cliPapi {
|
||||
return &cliPapi{
|
||||
cfg: getconfig,
|
||||
}
|
||||
}
|
||||
|
||||
func (cli *cliPapi) NewCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "papi [action]",
|
||||
Short: "Manage interaction with Polling API (PAPI)",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
DisableAutoGenTag: true,
|
||||
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
|
||||
}
|
||||
if err := require.CAPI(csConfig); err != nil {
|
||||
if err := require.CAPI(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := require.PAPI(csConfig); err != nil {
|
||||
if err := require.PAPI(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
@ -47,35 +53,36 @@ func (cli cliPapi) NewCommand() *cobra.Command {
|
|||
return cmd
|
||||
}
|
||||
|
||||
func (cli cliPapi) NewStatusCmd() *cobra.Command {
|
||||
func (cli *cliPapi) NewStatusCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "status",
|
||||
Short: "Get status of the Polling API",
|
||||
Args: cobra.MinimumNArgs(0),
|
||||
DisableAutoGenTag: true,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
var err error
|
||||
dbClient, err = database.NewClient(csConfig.DbConfig)
|
||||
cfg := cli.cfg()
|
||||
dbClient, err = database.NewClient(cfg.DbConfig)
|
||||
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 {
|
||||
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 {
|
||||
log.Fatalf("unable to initialize PAPI client : %s", err)
|
||||
return fmt.Errorf("unable to initialize PAPI client: %s", err)
|
||||
}
|
||||
|
||||
perms, err := papi.GetPermissions()
|
||||
|
||||
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
|
||||
lastTimestampStr, err = dbClient.GetConfigItem(apiserver.PapiPullKey)
|
||||
|
@ -90,45 +97,48 @@ func (cli cliPapi) NewStatusCmd() *cobra.Command {
|
|||
for _, sub := range perms.Categories {
|
||||
log.Infof(" - %s", sub)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (cli cliPapi) NewSyncCmd() *cobra.Command {
|
||||
func (cli *cliPapi) NewSyncCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
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) {
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
var err error
|
||||
cfg := cli.cfg()
|
||||
t := tomb.Tomb{}
|
||||
dbClient, err = database.NewClient(csConfig.DbConfig)
|
||||
|
||||
dbClient, err = database.NewClient(cfg.DbConfig)
|
||||
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 {
|
||||
log.Fatalf("unable to initialize API client : %s", err)
|
||||
return fmt.Errorf("unable to initialize API client: %s", err)
|
||||
}
|
||||
|
||||
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 {
|
||||
log.Fatalf("unable to initialize PAPI client : %s", err)
|
||||
return fmt.Errorf("unable to initialize PAPI client: %s", err)
|
||||
}
|
||||
|
||||
t.Go(papi.SyncDecisions)
|
||||
|
||||
err = papi.PullOnce(time.Time{}, true)
|
||||
|
||||
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")
|
||||
|
@ -138,6 +148,7 @@ func (cli cliPapi) NewSyncCmd() *cobra.Command {
|
|||
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
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue