Skip to content

Commit 8a49fcf

Browse files
authored
refact cmd/crowdsec: move plugin initialization to apiserver (#3668)
1 parent 49cc40c commit 8a49fcf

File tree

2 files changed

+38
-30
lines changed

2 files changed

+38
-30
lines changed

cmd/crowdsec/api.go

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package main
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
7-
"runtime"
86
"time"
97

108
log "github.com/sirupsen/logrus"
@@ -25,24 +23,9 @@ func initAPIServer(ctx context.Context, cConfig *csconfig.Config) (*apiserver.AP
2523
return nil, fmt.Errorf("unable to run local API: %w", err)
2624
}
2725

28-
if hasPlugins(cConfig.API.Server.Profiles) {
29-
log.Info("initiating plugin broker")
30-
// On windows, the plugins are always run as medium-integrity processes, so we don't care about plugin_config
31-
if cConfig.PluginConfig == nil && runtime.GOOS != "windows" {
32-
return nil, errors.New("plugins are enabled, but the plugin_config section is missing in the configuration")
33-
}
34-
35-
if cConfig.ConfigPaths.PluginDir == "" {
36-
return nil, errors.New("plugins are enabled, but config_paths.plugin_dir is not defined")
37-
}
38-
39-
err = pluginBroker.Init(ctx, cConfig.PluginConfig, cConfig.API.Server.Profiles, cConfig.ConfigPaths)
40-
if err != nil {
41-
return nil, fmt.Errorf("plugin broker: %w", err)
42-
}
43-
44-
log.Info("initiated plugin broker")
45-
apiServer.AttachPluginBroker(&pluginBroker)
26+
err = apiServer.InitPlugins(ctx, cConfig, &pluginBroker)
27+
if err != nil {
28+
return nil, err
4629
}
4730

4831
err = apiServer.InitController()
@@ -81,13 +64,3 @@ func serveAPIServer(apiServer *apiserver.APIServer) {
8164
})
8265
<-apiReady
8366
}
84-
85-
func hasPlugins(profiles []*csconfig.ProfileCfg) bool {
86-
for _, profile := range profiles {
87-
if len(profile.Notifications) != 0 {
88-
return true
89-
}
90-
}
91-
92-
return false
93-
}

pkg/apiserver/apiserver.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"os"
1212
"path/filepath"
13+
"runtime"
1314
"strings"
1415
"time"
1516

@@ -547,6 +548,40 @@ func (s *APIServer) AttachPluginBroker(broker *csplugin.PluginBroker) {
547548
s.controller.PluginChannel = broker.PluginChannel
548549
}
549550

551+
func hasPlugins(profiles []*csconfig.ProfileCfg) bool {
552+
for _, profile := range profiles {
553+
if len(profile.Notifications) != 0 {
554+
return true
555+
}
556+
}
557+
558+
return false
559+
}
560+
561+
func (s *APIServer) InitPlugins(ctx context.Context, cConfig *csconfig.Config, pluginBroker *csplugin.PluginBroker) error {
562+
if hasPlugins(cConfig.API.Server.Profiles) {
563+
log.Info("initiating plugin broker")
564+
// On windows, the plugins are always run as medium-integrity processes, so we don't care about plugin_config
565+
if cConfig.PluginConfig == nil && runtime.GOOS != "windows" {
566+
return errors.New("plugins are enabled, but the plugin_config section is missing in the configuration")
567+
}
568+
569+
if cConfig.ConfigPaths.PluginDir == "" {
570+
return errors.New("plugins are enabled, but config_paths.plugin_dir is not defined")
571+
}
572+
573+
err := pluginBroker.Init(ctx, cConfig.PluginConfig, cConfig.API.Server.Profiles, cConfig.ConfigPaths)
574+
if err != nil {
575+
return fmt.Errorf("plugin broker: %w", err)
576+
}
577+
578+
log.Info("initiated plugin broker")
579+
s.AttachPluginBroker(pluginBroker)
580+
}
581+
582+
return nil
583+
}
584+
550585
func (s *APIServer) InitController() error {
551586
err := s.controller.Init()
552587
if err != nil {

0 commit comments

Comments
 (0)