Skip to content

Commit 7b3b044

Browse files
authored
refact: remove unused code in crowdsec-cli, apiserver, acquisition, database (#4304)
* deadcode: remove unused ip helpers in pkg/database * deadcode: remove unused require.DB from cmd/crowdsec-cli/core * deadcode: remove unused LogAppsecEvent() from pkg/acquisition/modules/appsec * deadcode: remove unused createAlertsForDecisions() from pkg/apiserver
1 parent 7a63567 commit 7b3b044

File tree

5 files changed

+0
-226
lines changed

5 files changed

+0
-226
lines changed

cmd/crowdsec-cli/core/require/require.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,6 @@ func DBClient(ctx context.Context, dbcfg *csconfig.DatabaseCfg) (*database.Clien
8989
return db, nil
9090
}
9191

92-
func DB(c *csconfig.Config) error {
93-
if err := c.LoadDBConfig(true); err != nil {
94-
return fmt.Errorf("this command requires direct database access (must be run on the local API machine): %w", err)
95-
}
96-
97-
return nil
98-
}
99-
10092
func HubDownloader(ctx context.Context, c *csconfig.Config) (*cwhub.Downloader, error) {
10193
// set branch in config, and log if necessary
10294
branch, err := HubBranch(ctx, c)

pkg/acquisition/modules/appsec/utils.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -314,33 +314,6 @@ func EventFromRequest(r *appsec.ParsedRequest, labels map[string]string, txUuid
314314
return evt, nil
315315
}
316316

317-
func LogAppsecEvent(evt *pipeline.Event, logger *log.Entry) {
318-
req := evt.Parsed["target_uri"]
319-
if len(req) > 12 {
320-
req = req[:10] + ".."
321-
}
322-
323-
if evt.Meta["appsec_interrupted"] == "true" {
324-
logger.WithFields(log.Fields{
325-
"module": ModuleName,
326-
"source": evt.Parsed["source_ip"],
327-
"target_uri": req,
328-
}).Infof("%s blocked on %s (%d rules) [%v]", evt.Parsed["source_ip"], req, len(evt.Appsec.MatchedRules), evt.Appsec.GetRuleIDs())
329-
} else if evt.Parsed["outofband_interrupted"] == "true" {
330-
logger.WithFields(log.Fields{
331-
"module": ModuleName,
332-
"source": evt.Parsed["source_ip"],
333-
"target_uri": req,
334-
}).Infof("%s out-of-band blocking rules on %s (%d rules) [%v]", evt.Parsed["source_ip"], req, len(evt.Appsec.MatchedRules), evt.Appsec.GetRuleIDs())
335-
} else {
336-
logger.WithFields(log.Fields{
337-
"module": ModuleName,
338-
"source": evt.Parsed["source_ip"],
339-
"target_uri": req,
340-
}).Debugf("%s triggered non-blocking rules on %s (%d rules) [%v]", evt.Parsed["source_ip"], req, len(evt.Appsec.MatchedRules), evt.Appsec.GetRuleIDs())
341-
}
342-
}
343-
344317
type ruleData struct {
345318
ID int
346319
Name string

pkg/apiserver/apic.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -471,48 +471,6 @@ func (a *apic) HandleDeletedDecisionsV3(ctx context.Context, deletedDecisions []
471471
return nbDeleted, nil
472472
}
473473

474-
func createAlertsForDecisions(decisions []*models.Decision) []*models.Alert {
475-
newAlerts := make([]*models.Alert, 0)
476-
477-
for _, decision := range decisions {
478-
found := false
479-
480-
for _, sub := range newAlerts {
481-
if sub.Source.Scope == nil {
482-
log.Warningf("nil scope in %+v", sub)
483-
continue
484-
}
485-
486-
if *decision.Origin == types.CAPIOrigin {
487-
if *sub.Source.Scope == types.CAPIOrigin {
488-
found = true
489-
break
490-
}
491-
} else if *decision.Origin == types.ListOrigin {
492-
if *sub.Source.Scope == *decision.Origin {
493-
if sub.Scenario == nil {
494-
log.Warningf("nil scenario in %+v", sub)
495-
}
496-
497-
if *sub.Scenario == *decision.Scenario {
498-
found = true
499-
break
500-
}
501-
}
502-
} else {
503-
log.Warningf("unknown origin %s : %+v", *decision.Origin, decision)
504-
}
505-
}
506-
507-
if !found {
508-
log.Debugf("Create entry for origin:%s scenario:%s", *decision.Origin, *decision.Scenario)
509-
newAlerts = append(newAlerts, createAlertForDecision(decision))
510-
}
511-
}
512-
513-
return newAlerts
514-
}
515-
516474
func createAlertForDecision(decision *models.Decision) *models.Alert {
517475
var (
518476
scenario string

pkg/apiserver/apic_test.go

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -362,87 +362,6 @@ func TestAPICGetMetrics(t *testing.T) {
362362
}
363363
}
364364

365-
func TestCreateAlertsForDecision(t *testing.T) {
366-
httpBfDecisionList := &models.Decision{
367-
Origin: ptr.Of(types.ListOrigin),
368-
Scenario: ptr.Of("crowdsecurity/http-bf"),
369-
}
370-
371-
sshBfDecisionList := &models.Decision{
372-
Origin: ptr.Of(types.ListOrigin),
373-
Scenario: ptr.Of("crowdsecurity/ssh-bf"),
374-
}
375-
376-
httpBfDecisionCommunity := &models.Decision{
377-
Origin: ptr.Of(types.CAPIOrigin),
378-
Scenario: ptr.Of("crowdsecurity/http-bf"),
379-
}
380-
381-
sshBfDecisionCommunity := &models.Decision{
382-
Origin: ptr.Of(types.CAPIOrigin),
383-
Scenario: ptr.Of("crowdsecurity/ssh-bf"),
384-
}
385-
386-
type args struct {
387-
decisions []*models.Decision
388-
}
389-
390-
tests := []struct {
391-
name string
392-
args args
393-
want []*models.Alert
394-
}{
395-
{
396-
name: "2 decisions CAPI List Decisions should create 2 alerts",
397-
args: args{
398-
decisions: []*models.Decision{
399-
httpBfDecisionList,
400-
sshBfDecisionList,
401-
},
402-
},
403-
want: []*models.Alert{
404-
createAlertForDecision(httpBfDecisionList),
405-
createAlertForDecision(sshBfDecisionList),
406-
},
407-
},
408-
{
409-
name: "2 decisions CAPI List same scenario decisions should create 1 alert",
410-
args: args{
411-
decisions: []*models.Decision{
412-
httpBfDecisionList,
413-
httpBfDecisionList,
414-
},
415-
},
416-
want: []*models.Alert{
417-
createAlertForDecision(httpBfDecisionList),
418-
},
419-
},
420-
{
421-
name: "5 decisions from community list should create 1 alert",
422-
args: args{
423-
decisions: []*models.Decision{
424-
httpBfDecisionCommunity,
425-
httpBfDecisionCommunity,
426-
sshBfDecisionCommunity,
427-
sshBfDecisionCommunity,
428-
sshBfDecisionCommunity,
429-
},
430-
},
431-
want: []*models.Alert{
432-
createAlertForDecision(sshBfDecisionCommunity),
433-
},
434-
},
435-
}
436-
437-
for _, tc := range tests {
438-
t.Run(tc.name, func(t *testing.T) {
439-
if got := createAlertsForDecisions(tc.args.decisions); !reflect.DeepEqual(got, tc.want) {
440-
t.Errorf("createAlertsForDecisions() = %v, want %v", got, tc.want)
441-
}
442-
})
443-
}
444-
}
445-
446365
func TestFillAlertsWithDecisions(t *testing.T) {
447366
httpBfDecisionCommunity := &models.Decision{
448367
Origin: ptr.Of(types.CAPIOrigin),

pkg/database/utils.go

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)