Skip to content

Commit 113a6da

Browse files
authored
appsec: inject dependencies, avoid globals (#4148)
1 parent 13a5352 commit 113a6da

19 files changed

+124
-112
lines changed

cmd/crowdsec/crowdsec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func initCrowdsec(ctx context.Context, cConfig *csconfig.Config, hub *cwhub.Hub,
6161
}
6262
}
6363

64-
datasources, err := LoadAcquisition(ctx, cConfig)
64+
datasources, err := LoadAcquisition(ctx, cConfig, hub)
6565
if err != nil {
6666
return nil, nil, fmt.Errorf("while loading acquisition config: %w", err)
6767
}

cmd/crowdsec/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,18 @@ func LoadBuckets(cConfig *csconfig.Config, hub *cwhub.Hub) error {
7575
return nil
7676
}
7777

78-
func LoadAcquisition(ctx context.Context, cConfig *csconfig.Config) ([]acquisition.DataSource, error) {
78+
func LoadAcquisition(ctx context.Context, cConfig *csconfig.Config, hub *cwhub.Hub) ([]acquisition.DataSource, error) {
7979
if flags.SingleFileType != "" && flags.OneShotDSN != "" {
8080
flags.Labels = additionalLabels
8181
flags.Labels["type"] = flags.SingleFileType
8282

83-
ds, err := acquisition.LoadAcquisitionFromDSN(ctx, flags.OneShotDSN, flags.Labels, flags.Transform)
83+
ds, err := acquisition.LoadAcquisitionFromDSN(ctx, flags.OneShotDSN, flags.Labels, flags.Transform, hub)
8484
if err != nil {
8585
return nil, err
8686
}
8787
dataSources = append(dataSources, ds)
8888
} else {
89-
dss, err := acquisition.LoadAcquisitionFromFiles(ctx, cConfig.Crowdsec, cConfig.Prometheus)
89+
dss, err := acquisition.LoadAcquisitionFromFiles(ctx, cConfig.Crowdsec, cConfig.Prometheus, hub)
9090
if err != nil {
9191
return nil, err
9292
}

pkg/acquisition/acquisition.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
2929
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
30+
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
3031
"github.com/crowdsecurity/crowdsec/pkg/cwversion/component"
3132
"github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
3233
"github.com/crowdsecurity/crowdsec/pkg/logging"
@@ -115,6 +116,14 @@ type DSNConfigurer interface {
115116
ConfigureByDSN(ctx context.Context, dsn string, labels map[string]string, logger *log.Entry, uniqueID string) error
116117
}
117118

119+
type LAPIClientAware interface {
120+
SetClientConfig(config *csconfig.LocalApiClientCfg)
121+
}
122+
123+
type HubAware interface {
124+
SetHub(hub *cwhub.Hub)
125+
}
126+
118127
var (
119128
// We register the datasources at init time so we can tell if they are unsupported, or excluded from the build
120129
AcquisitionSources = map[string]func() DataSource{}
@@ -157,7 +166,7 @@ func registerDataSource(dataSourceType string, dsGetter func() DataSource) {
157166
// if the configuration is not valid it returns an error.
158167
// If the datasource can't be run (eg. journalctl not available), it still returns an error which
159168
// can be checked for the appropriate action.
160-
func DataSourceConfigure(ctx context.Context, commonConfig configuration.DataSourceCommonCfg, yamlConfig []byte, metricsLevel metrics.AcquisitionMetricsLevel) (DataSource, error) {
169+
func DataSourceConfigure(ctx context.Context, commonConfig configuration.DataSourceCommonCfg, yamlConfig []byte, metricsLevel metrics.AcquisitionMetricsLevel, hub *cwhub.Hub) (DataSource, error) {
161170
dataSrc, err := GetDataSourceIface(commonConfig.Source)
162171
if err != nil {
163172
return nil, err
@@ -177,6 +186,15 @@ func DataSourceConfigure(ctx context.Context, commonConfig configuration.DataSou
177186

178187
subLogger.Info("Configuring datasource")
179188

189+
if hubAware, ok := dataSrc.(HubAware); ok {
190+
hubAware.SetHub(hub)
191+
}
192+
193+
if lapiClientAware, ok := dataSrc.(LAPIClientAware); ok {
194+
cConfig := csconfig.GetConfig()
195+
lapiClientAware.SetClientConfig(cConfig.API.Client)
196+
}
197+
180198
/* configure the actual datasource */
181199
if err := dataSrc.Configure(ctx, yamlConfig, subLogger, metricsLevel); err != nil {
182200
return nil, err
@@ -185,7 +203,7 @@ func DataSourceConfigure(ctx context.Context, commonConfig configuration.DataSou
185203
return dataSrc, nil
186204
}
187205

188-
func LoadAcquisitionFromDSN(ctx context.Context, dsn string, labels map[string]string, transformExpr string) (DataSource, error) {
206+
func LoadAcquisitionFromDSN(ctx context.Context, dsn string, labels map[string]string, transformExpr string, hub *cwhub.Hub) (DataSource, error) {
189207
frags := strings.Split(dsn, ":")
190208
if len(frags) == 1 {
191209
return nil, fmt.Errorf("%s is not a valid dsn (no protocol)", dsn)
@@ -207,6 +225,15 @@ func LoadAcquisitionFromDSN(ctx context.Context, dsn string, labels map[string]s
207225
transformRuntimes[uniqueID] = vm
208226
}
209227

228+
if hubAware, ok := dataSrc.(HubAware); ok {
229+
hubAware.SetHub(hub)
230+
}
231+
232+
if lapiClientAware, ok := dataSrc.(LAPIClientAware); ok {
233+
cConfig := csconfig.GetConfig()
234+
lapiClientAware.SetClientConfig(cConfig.API.Client)
235+
}
236+
210237
dsnConf, ok := dataSrc.(DSNConfigurer)
211238
if !ok {
212239
return nil, fmt.Errorf("%s datasource does not support command-line acquisition", frags[0])
@@ -272,7 +299,7 @@ func detectType(r io.Reader) (string, error) {
272299
}
273300

274301
// sourcesFromFile reads and parses one acquisition file into DataSources.
275-
func sourcesFromFile(ctx context.Context, acquisFile string, metricsLevel metrics.AcquisitionMetricsLevel) ([]DataSource, error) {
302+
func sourcesFromFile(ctx context.Context, acquisFile string, metricsLevel metrics.AcquisitionMetricsLevel, hub *cwhub.Hub) ([]DataSource, error) {
276303
var sources []DataSource
277304

278305
log.Infof("loading acquisition file : %s", acquisFile)
@@ -356,7 +383,7 @@ func sourcesFromFile(ctx context.Context, acquisFile string, metricsLevel metric
356383
uniqueID := uuid.NewString()
357384
sub.UniqueId = uniqueID
358385

359-
src, err := DataSourceConfigure(ctx, sub, yamlDoc, metricsLevel)
386+
src, err := DataSourceConfigure(ctx, sub, yamlDoc, metricsLevel, hub)
360387
if err != nil {
361388
var dserr *DataSourceUnavailableError
362389
if errors.As(err, &dserr) {
@@ -383,13 +410,13 @@ func sourcesFromFile(ctx context.Context, acquisFile string, metricsLevel metric
383410
}
384411

385412
// LoadAcquisitionFromFiles unmarshals the configuration item and checks its availability
386-
func LoadAcquisitionFromFiles(ctx context.Context, config *csconfig.CrowdsecServiceCfg, prom *csconfig.PrometheusCfg) ([]DataSource, error) {
413+
func LoadAcquisitionFromFiles(ctx context.Context, config *csconfig.CrowdsecServiceCfg, prom *csconfig.PrometheusCfg, hub *cwhub.Hub) ([]DataSource, error) {
387414
var allSources []DataSource
388415

389416
metricsLevel := GetMetricsLevelFromPromCfg(prom)
390417

391418
for _, acquisFile := range config.AcquisitionFiles {
392-
sources, err := sourcesFromFile(ctx, acquisFile, metricsLevel)
419+
sources, err := sourcesFromFile(ctx, acquisFile, metricsLevel, hub)
393420
if err != nil {
394421
return nil, err
395422
}

pkg/acquisition/acquisition_test.go

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

1919
"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
2020
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
21+
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
2122
"github.com/crowdsecurity/crowdsec/pkg/metrics"
2223
"github.com/crowdsecurity/crowdsec/pkg/pipeline"
2324
)
@@ -181,7 +182,8 @@ filename: foo.log
181182
common := configuration.DataSourceCommonCfg{}
182183
err := yaml.Unmarshal([]byte(tc.String), &common)
183184
require.NoError(t, err)
184-
ds, err := DataSourceConfigure(ctx, common, []byte(tc.String), metrics.AcquisitionMetricsLevelNone)
185+
hub := cwhub.Hub{}
186+
ds, err := DataSourceConfigure(ctx, common, []byte(tc.String), metrics.AcquisitionMetricsLevelNone, &hub)
185187
cstest.RequireErrorContains(t, err, tc.ExpectedError)
186188

187189
if tc.ExpectedError != "" {
@@ -292,7 +294,8 @@ func TestLoadAcquisitionFromFiles(t *testing.T) {
292294
}
293295
for _, tc := range tests {
294296
t.Run(tc.TestName, func(t *testing.T) {
295-
dss, err := LoadAcquisitionFromFiles(ctx, &tc.Config, nil)
297+
hub := cwhub.Hub{}
298+
dss, err := LoadAcquisitionFromFiles(ctx, &tc.Config, nil, &hub)
296299
cstest.RequireErrorContains(t, err, tc.ExpectedError)
297300

298301
if tc.ExpectedError != "" {
@@ -552,7 +555,8 @@ func TestConfigureByDSN(t *testing.T) {
552555

553556
for _, tc := range tests {
554557
t.Run(tc.dsn, func(t *testing.T) {
555-
source, err := LoadAcquisitionFromDSN(ctx, tc.dsn, map[string]string{"type": "test_label"}, "")
558+
hub := cwhub.Hub{}
559+
source, err := LoadAcquisitionFromDSN(ctx, tc.dsn, map[string]string{"type": "test_label"}, "", &hub)
556560
cstest.RequireErrorContains(t, err, tc.ExpectedError)
557561

558562
if tc.ExpectedError != "" {

pkg/acquisition/appsec.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ var (
1111
_ DataSource = (*appsecacquisition.Source)(nil)
1212
_ Tailer = (*appsecacquisition.Source)(nil)
1313
_ MetricsProvider = (*appsecacquisition.Source)(nil)
14+
_ HubAware = (*appsecacquisition.Source)(nil)
15+
_ LAPIClientAware = (*appsecacquisition.Source)(nil)
1416
)
1517

1618
//nolint:gochecknoinits

pkg/acquisition/modules/appsec/appsec_hooks_test.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,8 @@ func TestAppsecOnMatchHooks(t *testing.T) {
407407
},
408408
},
409409
}
410-
for _, test := range tests {
411-
t.Run(test.name, func(t *testing.T) {
412-
loadAppSecEngine(test, t)
413-
})
414-
}
410+
411+
runTests(t, tests)
415412
}
416413

417414
func TestAppsecPreEvalHooks(t *testing.T) {
@@ -824,11 +821,7 @@ func TestAppsecPreEvalHooks(t *testing.T) {
824821
},
825822
}
826823

827-
for _, test := range tests {
828-
t.Run(test.name, func(t *testing.T) {
829-
loadAppSecEngine(test, t)
830-
})
831-
}
824+
runTests(t, tests)
832825
}
833826

834827
func TestAppsecRemediationConfigHooks(t *testing.T) {
@@ -917,11 +910,7 @@ func TestAppsecRemediationConfigHooks(t *testing.T) {
917910
},
918911
}
919912

920-
for _, test := range tests {
921-
t.Run(test.name, func(t *testing.T) {
922-
loadAppSecEngine(test, t)
923-
})
924-
}
913+
runTests(t, tests)
925914
}
926915

927916
func TestOnMatchRemediationHooks(t *testing.T) {
@@ -1090,9 +1079,6 @@ func TestOnMatchRemediationHooks(t *testing.T) {
10901079
},
10911080
},
10921081
}
1093-
for _, test := range tests {
1094-
t.Run(test.name, func(t *testing.T) {
1095-
loadAppSecEngine(test, t)
1096-
})
1097-
}
1082+
1083+
runTests(t, tests)
10981084
}

pkg/acquisition/modules/appsec/appsec_lnx_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ func TestAppsecRuleTransformsOthers(t *testing.T) {
6969
},
7070
},
7171
}
72-
for _, test := range tests {
73-
t.Run(test.name, func(t *testing.T) {
74-
loadAppSecEngine(test, t)
75-
})
76-
}
72+
73+
runTests(t, tests)
7774
}

pkg/acquisition/modules/appsec/appsec_remediation_test.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,8 @@ func TestAppsecDefaultPassRemediation(t *testing.T) {
149149
},
150150
},
151151
}
152-
for _, test := range tests {
153-
t.Run(test.name, func(t *testing.T) {
154-
loadAppSecEngine(test, t)
155-
})
156-
}
152+
153+
runTests(t, tests)
157154
}
158155

159156
func TestAppsecDefaultRemediation(t *testing.T) {
@@ -322,9 +319,5 @@ func TestAppsecDefaultRemediation(t *testing.T) {
322319
},
323320
}
324321

325-
for _, test := range tests {
326-
t.Run(test.name, func(t *testing.T) {
327-
loadAppSecEngine(test, t)
328-
})
329-
}
322+
runTests(t, tests)
330323
}

pkg/acquisition/modules/appsec/appsec_rules_test.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,7 @@ toto
418418
},
419419
}
420420

421-
for _, test := range tests {
422-
t.Run(test.name, func(t *testing.T) {
423-
loadAppSecEngine(test, t)
424-
})
425-
}
421+
runTests(t, tests)
426422
}
427423

428424
func TestAppsecRuleTransforms(t *testing.T) {
@@ -633,11 +629,8 @@ func TestAppsecRuleTransforms(t *testing.T) {
633629
},
634630
},
635631
}
636-
for _, test := range tests {
637-
t.Run(test.name, func(t *testing.T) {
638-
loadAppSecEngine(test, t)
639-
})
640-
}
632+
633+
runTests(t, tests)
641634
}
642635

643636
func TestAppsecRuleZones(t *testing.T) {
@@ -947,9 +940,6 @@ func TestAppsecRuleZones(t *testing.T) {
947940
},
948941
},
949942
}
950-
for _, test := range tests {
951-
t.Run(test.name, func(t *testing.T) {
952-
loadAppSecEngine(test, t)
953-
})
954-
}
943+
944+
runTests(t, tests)
955945
}

pkg/acquisition/modules/appsec/appsec_runner_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,8 @@ func TestAppsecConflictRuleLoad(t *testing.T) {
6666
},
6767
},
6868
}
69-
for _, test := range tests {
70-
t.Run(test.name, func(t *testing.T) {
71-
loadAppSecEngine(test, t)
72-
})
73-
}
69+
70+
runTests(t, tests)
7471
}
7572

7673
func TestAppsecRuleLoad(t *testing.T) {
@@ -200,9 +197,6 @@ func TestAppsecRuleLoad(t *testing.T) {
200197
},
201198
},
202199
}
203-
for _, test := range tests {
204-
t.Run(test.name, func(t *testing.T) {
205-
loadAppSecEngine(test, t)
206-
})
207-
}
200+
201+
runTests(t, tests)
208202
}

0 commit comments

Comments
 (0)