Skip to content

Commit

Permalink
do not try to load PAPI is url is not set (#2099)
Browse files Browse the repository at this point in the history
  • Loading branch information
blotus authored Mar 6, 2023
1 parent e27a0a0 commit 16a3be4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 30 deletions.
10 changes: 5 additions & 5 deletions cmd/crowdsec-cli/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ Disable given information push to the central API.`,
{csconfig.SEND_CUSTOM_SCENARIOS, fmt.Sprintf("%t", *csConfig.API.Server.ConsoleConfig.ShareCustomScenarios)},
{csconfig.SEND_TAINTED_SCENARIOS, fmt.Sprintf("%t", *csConfig.API.Server.ConsoleConfig.ShareTaintedScenarios)},
{csconfig.SEND_CONTEXT, fmt.Sprintf("%t", *csConfig.API.Server.ConsoleConfig.ShareContext)},
{csconfig.CONSOLE_MANAGEMENT, fmt.Sprintf("%t", *csConfig.API.Server.ConsoleConfig.ReceiveDecisions)},
{csconfig.CONSOLE_MANAGEMENT, fmt.Sprintf("%t", *csConfig.API.Server.ConsoleConfig.ConsoleManagement)},
}
for _, row := range rows {
err = csvwriter.Write(row)
Expand All @@ -240,16 +240,16 @@ func SetConsoleOpts(args []string, wanted bool) {
continue
}
/*for each flag check if it's already set before setting it*/
if csConfig.API.Server.ConsoleConfig.ReceiveDecisions != nil {
if *csConfig.API.Server.ConsoleConfig.ReceiveDecisions == wanted {
if csConfig.API.Server.ConsoleConfig.ConsoleManagement != nil {
if *csConfig.API.Server.ConsoleConfig.ConsoleManagement == wanted {
log.Debugf("%s already set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
} else {
log.Infof("%s set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
*csConfig.API.Server.ConsoleConfig.ReceiveDecisions = wanted
*csConfig.API.Server.ConsoleConfig.ConsoleManagement = wanted
}
} else {
log.Infof("%s set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
csConfig.API.Server.ConsoleConfig.ReceiveDecisions = types.BoolPtr(wanted)
csConfig.API.Server.ConsoleConfig.ConsoleManagement = types.BoolPtr(wanted)
}
if csConfig.API.Server.OnlineClient.Credentials != nil {
changed := false
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/console_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func cmdConsoleStatusTable(out io.Writer, csConfig csconfig.Config) {
t.AddRow(option, activated, "Send context with alerts to the console")
case csconfig.CONSOLE_MANAGEMENT:
activated := string(emoji.CrossMark)
if *csConfig.API.Server.ConsoleConfig.ReceiveDecisions {
if *csConfig.API.Server.ConsoleConfig.ConsoleManagement {
activated = string(emoji.CheckMarkButton)
}
t.AddRow(option, activated, "Receive decisions from console")
Expand Down
38 changes: 21 additions & 17 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,23 +356,27 @@ func (s *APIServer) Run(apiReady chan bool) error {
//csConfig.API.Server.ConsoleConfig.ShareCustomScenarios
if s.isEnrolled {
if fflag.PapiClient.IsEnabled() {
if s.consoleConfig.ReceiveDecisions != nil && *s.consoleConfig.ReceiveDecisions {
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
})
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.")
}
} else {
log.Warningf("Machine is not allowed to synchronize decisions, you can enable it with `cscli console enable console_management`")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/apiserver/papi.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func (p *Papi) SyncDecisions() error {
go p.SendDeletedDecisions(&cacheCopy)
}
case deletedDecisions := <-p.Channels.DeleteDecisionChannel:
if (p.consoleConfig.ShareManualDecisions != nil && *p.consoleConfig.ShareManualDecisions) || (p.consoleConfig.ReceiveDecisions != nil && *p.consoleConfig.ReceiveDecisions) {
if (p.consoleConfig.ShareManualDecisions != nil && *p.consoleConfig.ShareManualDecisions) || (p.consoleConfig.ConsoleManagement != nil && *p.consoleConfig.ConsoleManagement) {
var tmpDecisions []models.DecisionsDeleteRequestItem
p.Logger.Debugf("%d decisions deletion to add in cache", len(deletedDecisions))
for _, decision := range deletedDecisions {
Expand Down
2 changes: 1 addition & 1 deletion pkg/csconfig/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func TestLoadAPIServer(t *testing.T) {
ShareTaintedScenarios: types.BoolPtr(true),
ShareCustomScenarios: types.BoolPtr(true),
ShareContext: types.BoolPtr(false),
ReceiveDecisions: types.BoolPtr(false),
ConsoleManagement: types.BoolPtr(false),
},
LogDir: LogDirFullPath,
LogMedia: "stdout",
Expand Down
10 changes: 5 additions & 5 deletions pkg/csconfig/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type ConsoleConfig struct {
ShareManualDecisions *bool `yaml:"share_manual_decisions"`
ShareTaintedScenarios *bool `yaml:"share_tainted"`
ShareCustomScenarios *bool `yaml:"share_custom"`
ReceiveDecisions *bool `yaml:"console_management"`
ConsoleManagement *bool `yaml:"console_management"`
ShareContext *bool `yaml:"share_context"`
}

Expand All @@ -38,7 +38,7 @@ func (c *LocalApiServerCfg) LoadConsoleConfig() error {
c.ConsoleConfig.ShareCustomScenarios = types.BoolPtr(true)
c.ConsoleConfig.ShareTaintedScenarios = types.BoolPtr(true)
c.ConsoleConfig.ShareManualDecisions = types.BoolPtr(false)
c.ConsoleConfig.ReceiveDecisions = types.BoolPtr(false)
c.ConsoleConfig.ConsoleManagement = types.BoolPtr(false)
c.ConsoleConfig.ShareContext = types.BoolPtr(false)
return nil
}
Expand Down Expand Up @@ -66,10 +66,10 @@ func (c *LocalApiServerCfg) LoadConsoleConfig() error {
}

if !fflag.PapiClient.IsEnabled() {
c.ConsoleConfig.ReceiveDecisions = types.BoolPtr(false)
} else if c.ConsoleConfig.ReceiveDecisions == nil {
c.ConsoleConfig.ConsoleManagement = types.BoolPtr(false)
} else if c.ConsoleConfig.ConsoleManagement == nil {
log.Debugf("no console_management found, setting to false")
c.ConsoleConfig.ReceiveDecisions = types.BoolPtr(false)
c.ConsoleConfig.ConsoleManagement = types.BoolPtr(false)
}

if c.ConsoleConfig.ShareContext == nil {
Expand Down

0 comments on commit 16a3be4

Please sign in to comment.