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

remove PAPI feature flag #2601

Merged
merged 6 commits into from
Dec 8, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions cmd/crowdsec-cli/capi.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
"github.com/crowdsecurity/crowdsec/pkg/fflag"
"github.com/crowdsecurity/crowdsec/pkg/models"
"github.com/crowdsecurity/crowdsec/pkg/types"

Expand Down Expand Up @@ -98,9 +97,6 @@ func NewCapiRegisterCmd() *cobra.Command {
Password: password.String(),
URL: types.CAPIBaseURL,
}
if fflag.PapiClient.IsEnabled() {
apiCfg.PapiURL = types.PAPIBaseURL
}
apiConfigDump, err := yaml.Marshal(apiCfg)
if err != nil {
return fmt.Errorf("unable to marshal api credentials: %w", err)
Expand Down
12 changes: 4 additions & 8 deletions cmd/crowdsec-cli/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
"github.com/crowdsecurity/crowdsec/pkg/fflag"
"github.com/crowdsecurity/crowdsec/pkg/types"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
Expand Down Expand Up @@ -189,11 +188,11 @@ Disable given information push to the central API.`,
case "json":
c := csConfig.API.Server.ConsoleConfig
out := map[string](*bool){
csconfig.SEND_MANUAL_SCENARIOS: c.ShareManualDecisions,
csconfig.SEND_CUSTOM_SCENARIOS: c.ShareCustomScenarios,
csconfig.SEND_MANUAL_SCENARIOS: c.ShareManualDecisions,
csconfig.SEND_CUSTOM_SCENARIOS: c.ShareCustomScenarios,
csconfig.SEND_TAINTED_SCENARIOS: c.ShareTaintedScenarios,
csconfig.SEND_CONTEXT: c.ShareContext,
csconfig.CONSOLE_MANAGEMENT: c.ConsoleManagement,
csconfig.SEND_CONTEXT: c.ShareContext,
csconfig.CONSOLE_MANAGEMENT: c.ConsoleManagement,
}
data, err := json.MarshalIndent(out, "", " ")
if err != nil {
Expand Down Expand Up @@ -252,9 +251,6 @@ func SetConsoleOpts(args []string, wanted bool) error {
for _, arg := range args {
switch arg {
case csconfig.CONSOLE_MANAGEMENT:
if !fflag.PapiClient.IsEnabled() {
continue
}
/*for each flag check if it's already set before setting it*/
if csConfig.API.Server.ConsoleConfig.ConsoleManagement != nil {
if *csConfig.API.Server.ConsoleConfig.ConsoleManagement == wanted {
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
rootCmd.AddCommand(NewHubTestCmd())
rootCmd.AddCommand(NewCLINotifications().NewCommand())
rootCmd.AddCommand(NewCLISupport().NewCommand())

rootCmd.AddCommand(NewCLIPapi().NewCommand())
rootCmd.AddCommand(NewCollectionCLI().NewCommand())
rootCmd.AddCommand(NewParserCLI().NewCommand())
rootCmd.AddCommand(NewScenarioCLI().NewCommand())
Expand Down
63 changes: 29 additions & 34 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/csplugin"
"github.com/crowdsecurity/crowdsec/pkg/database"
"github.com/crowdsecurity/crowdsec/pkg/fflag"
"github.com/crowdsecurity/crowdsec/pkg/types"
)

Expand Down Expand Up @@ -220,17 +219,15 @@
log.Infof("CAPI manager configured successfully")
isMachineEnrolled = isEnrolled(apiClient.apiClient)
controller.AlertsAddChan = apiClient.AlertsAddChan
if fflag.PapiClient.IsEnabled() {
if isMachineEnrolled {
log.Infof("Machine is enrolled in the console, Loading PAPI Client")
papiClient, err = NewPAPI(apiClient, dbClient, config.ConsoleConfig, *config.PapiLogLevel)
if err != nil {
return nil, err
}
controller.DecisionDeleteChan = papiClient.Channels.DeleteDecisionChannel
} else {
log.Errorf("Machine is not enrolled in the console, can't synchronize with the console")
if isMachineEnrolled {
log.Infof("Machine is enrolled in the console, Loading PAPI Client")
papiClient, err = NewPAPI(apiClient, dbClient, config.ConsoleConfig, *config.PapiLogLevel)
if err != nil {
return nil, err

Check warning on line 226 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L223-L226

Added lines #L223 - L226 were not covered by tests
}
controller.DecisionDeleteChan = papiClient.Channels.DeleteDecisionChannel

Check warning on line 228 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L228

Added line #L228 was not covered by tests
} else {
log.Errorf("Machine is not enrolled in the console, can't synchronize with the console")
}
} else {
apiClient = nil
Expand Down Expand Up @@ -357,31 +354,29 @@

//csConfig.API.Server.ConsoleConfig.ShareCustomScenarios
if s.isEnrolled {
if fflag.PapiClient.IsEnabled() {
if s.consoleConfig.ConsoleManagement != nil && *s.consoleConfig.ConsoleManagement {
if s.papi.URL != "" {
log.Infof("Starting PAPI decision receiver")
s.papi.pullTomb.Go(func() error {
if err := s.papi.Pull(); err != nil {
log.Errorf("papi pull: %s", err)
return err
}
return nil
})

s.papi.syncTomb.Go(func() error {
if err := s.papi.SyncDecisions(); err != nil {
log.Errorf("capi decisions sync: %s", err)
return err
}
return nil
})
} else {
log.Warnf("papi_url is not set in online_api_credentials.yaml, can't synchronize with the console. Run cscli console enable console_management to add it.")
}
if s.consoleConfig.ConsoleManagement != nil && *s.consoleConfig.ConsoleManagement {
if s.papi.URL != "" {
log.Infof("Starting PAPI decision receiver")
s.papi.pullTomb.Go(func() error {
if err := s.papi.Pull(); err != nil {
log.Errorf("papi pull: %s", err)
return err
}
return nil

Check warning on line 365 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L357-L365

Added lines #L357 - L365 were not covered by tests
})

s.papi.syncTomb.Go(func() error {
if err := s.papi.SyncDecisions(); err != nil {
log.Errorf("capi decisions sync: %s", err)
return err
}
return nil

Check warning on line 373 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L368-L373

Added lines #L368 - L373 were not covered by tests
})
} else {
log.Warningf("Machine is not allowed to synchronize decisions, you can enable it with `cscli console enable console_management`")
log.Warnf("papi_url is not set in online_api_credentials.yaml, can't synchronize with the console. Run cscli console enable console_management to add it.")

Check warning on line 376 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L376

Added line #L376 was not covered by tests
}
} else {
log.Warningf("Machine is not allowed to synchronize decisions, you can enable it with `cscli console enable console_management`")

Check warning on line 379 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L378-L379

Added lines #L378 - L379 were not covered by tests
}
}

Expand Down
6 changes: 1 addition & 5 deletions pkg/csconfig/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"gopkg.in/yaml.v2"

"github.com/crowdsecurity/go-cs-lib/ptr"

"github.com/crowdsecurity/crowdsec/pkg/fflag"
)

const (
Expand Down Expand Up @@ -66,9 +64,7 @@ func (c *LocalApiServerCfg) LoadConsoleConfig() error {
c.ConsoleConfig.ShareManualDecisions = ptr.Of(false)
}

if !fflag.PapiClient.IsEnabled() {
c.ConsoleConfig.ConsoleManagement = ptr.Of(false)
} else if c.ConsoleConfig.ConsoleManagement == nil {
if c.ConsoleConfig.ConsoleManagement == nil {
log.Debugf("no console_management found, setting to false")
c.ConsoleConfig.ConsoleManagement = ptr.Of(false)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/fflag/crowdsec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var Crowdsec = FeatureRegister{EnvPrefix: "CROWDSEC_FEATURE_"}
var CscliSetup = &Feature{Name: "cscli_setup", Description: "Enable cscli setup command (service detection)"}
var DisableHttpRetryBackoff = &Feature{Name: "disable_http_retry_backoff", Description: "Disable http retry backoff"}
var ChunkedDecisionsStream = &Feature{Name: "chunked_decisions_stream", Description: "Enable chunked decisions stream"}
var PapiClient = &Feature{Name: "papi_client", Description: "Enable Polling API client"}
var PapiClient = &Feature{Name: "papi_client", Description: "Enable Polling API client", State: RetiredState}
var Re2GrokSupport = &Feature{Name: "re2_grok_support", Description: "Enable RE2 support for GROK patterns"}
var Re2RegexpInfileSupport = &Feature{Name: "re2_regexp_in_file_support", Description: "Enable RE2 support for RegexpInFile expr helper"}

Expand Down
36 changes: 21 additions & 15 deletions pkg/longpollclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,26 +193,32 @@
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var pollResp pollResponse
err = decoder.Decode(&pollResp)
if err != nil {
if err == io.EOF {
c.logger.Debugf("server closed connection")
return nil, nil
evts := []Event{}
for {
var pollResp pollResponse
err = decoder.Decode(&pollResp)
if err != nil {
if err == io.EOF {
c.logger.Debugf("server closed connection")
break

Check warning on line 203 in pkg/longpollclient/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/longpollclient/client.go#L196-L203

Added lines #L196 - L203 were not covered by tests
}
log.Errorf("error decoding poll response: %v", err)
break

Check warning on line 206 in pkg/longpollclient/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/longpollclient/client.go#L205-L206

Added lines #L205 - L206 were not covered by tests
}
return nil, fmt.Errorf("error decoding poll response: %v", err)
}

c.logger.Tracef("got response: %+v", pollResp)
c.logger.Tracef("got response: %+v", pollResp)

Check warning on line 209 in pkg/longpollclient/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/longpollclient/client.go#L209

Added line #L209 was not covered by tests

if len(pollResp.ErrorMessage) > 0 {
if pollResp.ErrorMessage == timeoutMessage {
c.logger.Debugf("got timeout message")
return nil, nil
if len(pollResp.ErrorMessage) > 0 {
if pollResp.ErrorMessage == timeoutMessage {
c.logger.Debugf("got timeout message")
break

Check warning on line 214 in pkg/longpollclient/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/longpollclient/client.go#L211-L214

Added lines #L211 - L214 were not covered by tests
}
log.Errorf("longpoll API error message: %s", pollResp.ErrorMessage)
break

Check warning on line 217 in pkg/longpollclient/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/longpollclient/client.go#L216-L217

Added lines #L216 - L217 were not covered by tests
}
return nil, fmt.Errorf("longpoll API error message: %s", pollResp.ErrorMessage)
evts = append(evts, pollResp.Events...)

Check warning on line 219 in pkg/longpollclient/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/longpollclient/client.go#L219

Added line #L219 was not covered by tests
}
return pollResp.Events, nil
return evts, nil

Check warning on line 221 in pkg/longpollclient/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/longpollclient/client.go#L221

Added line #L221 was not covered by tests
}

func NewLongPollClient(config LongPollClientConfig) (*LongPollClient, error) {
Expand Down
6 changes: 2 additions & 4 deletions test/bats/09_console.bats
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ setup() {
assert_stderr --partial "manual already set to true"
assert_stderr --partial "tainted already set to true"
assert_stderr --partial "context already set to true"
assert_stderr --partial "All features have been enabled successfully"
CROWDSEC_FEATURE_PAPI_CLIENT=true rune -0 cscli console enable --all --debug
assert_stderr --partial "console_management set to true"
assert_stderr --partial "All features have been enabled successfully"
rune -1 cscli console enable tralala
assert_stderr --partial "unknown flag tralala"
}
Expand All @@ -94,9 +93,8 @@ setup() {
assert_stderr --partial "manual already set to false"
assert_stderr --partial "tainted already set to false"
assert_stderr --partial "context already set to false"
assert_stderr --partial "All features have been disabled"
CROWDSEC_FEATURE_PAPI_CLIENT=true rune -0 cscli console disable --all --debug
assert_stderr --partial "console_management already set to false"
assert_stderr --partial "All features have been disabled"
rune -1 cscli console disable tralala
assert_stderr --partial "unknown flag tralala"
}
Loading