Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact "cscli papi" #2802

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions cmd/crowdsec-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ var mergedConfig string
// flagBranch overrides the value in csConfig.Cscli.HubBranch
var flagBranch = ""

type configGetter func() *csconfig.Config

func initConfig() {
var err error

Expand Down Expand Up @@ -206,7 +208,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())
Expand All @@ -219,10 +221,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)
}
Expand Down
67 changes: 39 additions & 28 deletions cmd/crowdsec-cli/papi.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"time"

log "github.com/sirupsen/logrus"
Expand All @@ -15,26 +16,31 @@
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
)

type cliPapi struct {}
type cliPapi struct {
cfg configGetter
}

func NewCLIPapi() *cliPapi {
return &cliPapi{}
func NewCLIPapi(getconfig configGetter) *cliPapi {
return &cliPapi{
cfg: getconfig,
}
}

func (cli cliPapi) NewCommand() *cobra.Command {
var cmd = &cobra.Command{
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 {

Check warning on line 37 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L36-L37

Added lines #L36 - L37 were not covered by tests
return err
}
if err := require.CAPI(csConfig); err != nil {
if err := require.CAPI(cfg); err != nil {

Check warning on line 40 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L40

Added line #L40 was not covered by tests
return err
}
if err := require.PAPI(csConfig); err != nil {
if err := require.PAPI(cfg); err != nil {

Check warning on line 43 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L43

Added line #L43 was not covered by tests
return err
}
return nil
Expand All @@ -47,35 +53,36 @@
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)

Check warning on line 65 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L64-L65

Added lines #L64 - L65 were not covered by tests
if err != nil {
log.Fatalf("unable to initialize database client : %s", err)
return fmt.Errorf("unable to initialize database client: %s", err)

Check warning on line 67 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L67

Added line #L67 was not covered by tests
}

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)

Check warning on line 70 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L70

Added line #L70 was not covered by tests

if err != nil {
log.Fatalf("unable to initialize API client : %s", err)
return fmt.Errorf("unable to initialize API client: %s", err)

Check warning on line 73 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L73

Added line #L73 was not covered by tests
}

papi, err := apiserver.NewPAPI(apic, dbClient, csConfig.API.Server.ConsoleConfig, log.GetLevel())
papi, err := apiserver.NewPAPI(apic, dbClient, cfg.API.Server.ConsoleConfig, log.GetLevel())

Check warning on line 76 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L76

Added line #L76 was not covered by tests

if err != nil {
log.Fatalf("unable to initialize PAPI client : %s", err)
return fmt.Errorf("unable to initialize PAPI client: %s", err)

Check warning on line 79 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L79

Added line #L79 was not covered by tests
}

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)

Check warning on line 85 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L85

Added line #L85 was not covered by tests
}
var lastTimestampStr *string
lastTimestampStr, err = dbClient.GetConfigItem(apiserver.PapiPullKey)
Expand All @@ -90,45 +97,48 @@
for _, sub := range perms.Categories {
log.Infof(" - %s", sub)
}

return nil

Check warning on line 101 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L101

Added line #L101 was not covered by tests
},
}

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()

Check warning on line 116 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L116

Added line #L116 was not covered by tests
t := tomb.Tomb{}
dbClient, err = database.NewClient(csConfig.DbConfig)

dbClient, err = database.NewClient(cfg.DbConfig)

Check warning on line 119 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L118-L119

Added lines #L118 - L119 were not covered by tests
if err != nil {
log.Fatalf("unable to initialize database client : %s", err)
return fmt.Errorf("unable to initialize database client: %s", err)

Check warning on line 121 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L121

Added line #L121 was not covered by tests
}

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)

Check warning on line 124 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L124

Added line #L124 was not covered by tests
if err != nil {
log.Fatalf("unable to initialize API client : %s", err)
return fmt.Errorf("unable to initialize API client: %s", err)

Check warning on line 126 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L126

Added line #L126 was not covered by tests
}

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())

Check warning on line 131 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L131

Added line #L131 was not covered by tests
if err != nil {
log.Fatalf("unable to initialize PAPI client : %s", err)
return fmt.Errorf("unable to initialize PAPI client: %s", err)

Check warning on line 133 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L133

Added line #L133 was not covered by tests
}

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)

Check warning on line 141 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L141

Added line #L141 was not covered by tests
}

log.Infof("Sending acknowledgements to CAPI")
Expand All @@ -138,6 +148,7 @@
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

Check warning on line 151 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L151

Added line #L151 was not covered by tests
},
}

Expand Down