Skip to content

Commit c7b12e9

Browse files
authored
refact csplugin: move ProfileAlert to models (#3671)
this is a step to make csplugin support optional. next is tomb management, then add the build tag
1 parent 948e0a0 commit c7b12e9

File tree

9 files changed

+36
-40
lines changed

9 files changed

+36
-40
lines changed

cmd/crowdsec-cli/clinotifications/notifications.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func (cli *cliNotifications) newTestCmd() *cobra.Command {
345345
return fmt.Errorf("failed to parse alert override: %w", err)
346346
}
347347

348-
pluginBroker.PluginChannel <- csplugin.ProfileAlert{
348+
pluginBroker.PluginChannel <- models.ProfileAlert{
349349
ProfileID: uint(0),
350350
Alert: alert,
351351
}
@@ -452,7 +452,7 @@ cscli notifications reinject <alert_id> -a '{"remediation": true,"scenario":"not
452452
loop:
453453
for {
454454
select {
455-
case pluginBroker.PluginChannel <- csplugin.ProfileAlert{
455+
case pluginBroker.PluginChannel <- models.ProfileAlert{
456456
ProfileID: uint(id),
457457
Alert: alert,
458458
}:

pkg/apiserver/alerts_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/stretchr/testify/require"
1818

1919
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
20-
"github.com/crowdsecurity/crowdsec/pkg/csplugin"
2120
"github.com/crowdsecurity/crowdsec/pkg/database"
2221
"github.com/crowdsecurity/crowdsec/pkg/models"
2322
)
@@ -203,15 +202,15 @@ func TestCreateAllowlistedAlert(t *testing.T) {
203202
func TestCreateAlertChannels(t *testing.T) {
204203
ctx := t.Context()
205204
apiServer, config := NewAPIServer(t, ctx)
206-
apiServer.controller.PluginChannel = make(chan csplugin.ProfileAlert)
205+
apiServer.controller.PluginChannel = make(chan models.ProfileAlert)
207206
err := apiServer.InitController()
208207
require.NoError(t, err)
209208

210209
loginResp := LoginToTestAPI(t, ctx, apiServer.router, config)
211210
lapi := LAPI{router: apiServer.router, loginResp: loginResp}
212211

213212
var (
214-
pd csplugin.ProfileAlert
213+
pd models.ProfileAlert
215214
wg sync.WaitGroup
216215
)
217216

pkg/apiserver/controllers/controller.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
v1 "github.com/crowdsecurity/crowdsec/pkg/apiserver/controllers/v1"
1313
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
14-
"github.com/crowdsecurity/crowdsec/pkg/csplugin"
1514
"github.com/crowdsecurity/crowdsec/pkg/database"
1615
"github.com/crowdsecurity/crowdsec/pkg/models"
1716
)
@@ -22,7 +21,7 @@ type Controller struct {
2221
Profiles []*csconfig.ProfileCfg
2322
AlertsAddChan chan []*models.Alert
2423
DecisionDeleteChan chan []*models.Decision
25-
PluginChannel chan csplugin.ProfileAlert
24+
PluginChannel chan models.ProfileAlert
2625
Log *log.Logger
2726
ConsoleConfig *csconfig.ConsoleConfig
2827
TrustedIPs []net.IPNet

pkg/apiserver/controllers/v1/alerts.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/google/uuid"
1515
log "github.com/sirupsen/logrus"
1616

17-
"github.com/crowdsecurity/crowdsec/pkg/csplugin"
1817
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
1918
"github.com/crowdsecurity/crowdsec/pkg/models"
2019
"github.com/crowdsecurity/crowdsec/pkg/types"
@@ -112,7 +111,7 @@ func (c *Controller) sendAlertToPluginChannel(alert *models.Alert, profileID uin
112111
RETRY:
113112
for try := range 3 {
114113
select {
115-
case c.PluginChannel <- csplugin.ProfileAlert{ProfileID: profileID, Alert: alert}:
114+
case c.PluginChannel <- models.ProfileAlert{ProfileID: profileID, Alert: alert}:
116115
log.Debugf("alert sent to Plugin channel")
117116

118117
break RETRY

pkg/apiserver/controllers/v1/controller.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
middlewares "github.com/crowdsecurity/crowdsec/pkg/apiserver/middlewares/v1"
88
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
9-
"github.com/crowdsecurity/crowdsec/pkg/csplugin"
109
"github.com/crowdsecurity/crowdsec/pkg/csprofiles"
1110
"github.com/crowdsecurity/crowdsec/pkg/database"
1211
"github.com/crowdsecurity/crowdsec/pkg/models"
@@ -21,7 +20,7 @@ type Controller struct {
2120
AlertsAddChan chan []*models.Alert
2221
DecisionDeleteChan chan []*models.Decision
2322

24-
PluginChannel chan csplugin.ProfileAlert
23+
PluginChannel chan models.ProfileAlert
2524
ConsoleConfig csconfig.ConsoleConfig
2625
TrustedIPs []net.IPNet
2726
AutoRegisterCfg *csconfig.LocalAPIAutoRegisterCfg
@@ -34,7 +33,7 @@ type ControllerV1Config struct {
3433
AlertsAddChan chan []*models.Alert
3534
DecisionDeleteChan chan []*models.Decision
3635

37-
PluginChannel chan csplugin.ProfileAlert
36+
PluginChannel chan models.ProfileAlert
3837
ConsoleConfig csconfig.ConsoleConfig
3938
TrustedIPs []net.IPNet
4039
AutoRegisterCfg *csconfig.LocalAPIAutoRegisterCfg

pkg/csplugin/broker.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const (
4141
// It receives all the events from the main process and stacks them up
4242
// It is as well notified by the watcher when it needs to deliver events to plugins (based on time or count threshold)
4343
type PluginBroker struct {
44-
PluginChannel chan ProfileAlert
44+
PluginChannel chan models.ProfileAlert
4545
alertsByPluginName map[string][]*models.Alert
4646
profileConfigs []*csconfig.ProfileCfg
4747
pluginConfigByName map[string]PluginConfig
@@ -68,13 +68,8 @@ type PluginConfig struct {
6868
Config map[string]any `yaml:",inline"` // to keep the plugin-specific config
6969
}
7070

71-
type ProfileAlert struct {
72-
ProfileID uint
73-
Alert *models.Alert
74-
}
75-
7671
func (pb *PluginBroker) Init(ctx context.Context, pluginCfg *csconfig.PluginCfg, profileConfigs []*csconfig.ProfileCfg, configPaths *csconfig.ConfigurationPaths) error {
77-
pb.PluginChannel = make(chan ProfileAlert)
72+
pb.PluginChannel = make(chan models.ProfileAlert)
7873
pb.notificationConfigsByPluginType = make(map[string][][]byte)
7974
pb.notificationPluginByName = make(map[string]protobufs.NotifierServer)
8075
pb.pluginMap = make(map[string]plugin.Plugin)
@@ -165,7 +160,7 @@ func (pb *PluginBroker) Run(pluginTomb *tomb.Tomb) {
165160
}
166161
}
167162

168-
func (pb *PluginBroker) addProfileAlert(profileAlert ProfileAlert) {
163+
func (pb *PluginBroker) addProfileAlert(profileAlert models.ProfileAlert) {
169164
for _, pluginName := range pb.profileConfigs[profileAlert.ProfileID].Notifications {
170165
if _, ok := pb.pluginConfigByName[pluginName]; !ok {
171166
log.Errorf("plugin %s is not configured properly.", pluginName)

pkg/csplugin/broker_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (s *PluginSuite) TestBrokerNoThreshold() {
158158
go pb.Run(&tomb)
159159

160160
// send one item, it should be processed right now
161-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
161+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
162162

163163
time.Sleep(200 * time.Millisecond)
164164

@@ -175,7 +175,7 @@ func (s *PluginSuite) TestBrokerNoThreshold() {
175175

176176
// and another one
177177
log.Printf("second send")
178-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
178+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
179179

180180
time.Sleep(200 * time.Millisecond)
181181

@@ -210,9 +210,9 @@ func (s *PluginSuite) TestBrokerRunGroupAndTimeThreshold_TimeFirst() {
210210
go pb.Run(&tomb)
211211

212212
// send data
213-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
214-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
215-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
213+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
214+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
215+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
216216

217217
time.Sleep(500 * time.Millisecond)
218218
// because of group threshold, we shouldn't have data yet
@@ -247,15 +247,15 @@ func (s *PluginSuite) TestBrokerRunGroupAndTimeThreshold_CountFirst() {
247247
go pb.Run(&tomb)
248248

249249
// send data
250-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
251-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
252-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
250+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
251+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
252+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
253253

254254
time.Sleep(100 * time.Millisecond)
255255

256256
// because of group threshold, we shouldn't have data yet
257257
assert.NoFileExists(t, s.outFile)
258-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
258+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
259259

260260
time.Sleep(100 * time.Millisecond)
261261

@@ -288,17 +288,17 @@ func (s *PluginSuite) TestBrokerRunGroupThreshold() {
288288
go pb.Run(&tomb)
289289

290290
// send data
291-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
292-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
293-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
291+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
292+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
293+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
294294

295295
time.Sleep(time.Second)
296296

297297
// because of group threshold, we shouldn't have data yet
298298
assert.NoFileExists(t, s.outFile)
299-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
300-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
301-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
299+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
300+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
301+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
302302

303303
time.Sleep(time.Second)
304304

@@ -342,7 +342,7 @@ func (s *PluginSuite) TestBrokerRunTimeThreshold() {
342342
go pb.Run(&tomb)
343343

344344
// send data
345-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
345+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
346346

347347
time.Sleep(200 * time.Millisecond)
348348

@@ -376,8 +376,8 @@ func (s *PluginSuite) TestBrokerRunSimple() {
376376

377377
defer os.Remove(s.outFile)
378378

379-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
380-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
379+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
380+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
381381
// make it wait a bit, CI can be slow
382382
time.Sleep(time.Second)
383383

pkg/csplugin/broker_win_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ func (s *PluginSuite) TestBrokerRun() {
7979
assert.NoFileExists(t, s.outFile)
8080
defer os.Remove(s.outFile)
8181

82-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
83-
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
82+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
83+
pb.PluginChannel <- models.ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
8484
time.Sleep(time.Second * 4)
8585

8686
assert.FileExists(t, s.outFile)

pkg/models/helpers.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,8 @@ func (a *Alert) FormatAsStrings(machineID string, logger *log.Logger) []string {
164164

165165
return retStr
166166
}
167+
168+
type ProfileAlert struct {
169+
ProfileID uint
170+
Alert *Alert
171+
}

0 commit comments

Comments
 (0)