Skip to content

Commit 756ce6b

Browse files
enhance: Add supported decisions type (#113)
* enhance: Add supported decisions type and update deps/goversion * enhance: Add ban as the default * enhance: Remove all * enhance: Fix CI * enhance: More CI --------- Co-authored-by: marco <marco@crowdsec.net>
1 parent 0df4501 commit 756ce6b

7 files changed

Lines changed: 49 additions & 6 deletions

File tree

.golangci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ linters:
130130
- name: function-length
131131
arguments:
132132
# lower this after refactoring
133-
- 43
134-
- 123
133+
- 44
134+
- 126
135135
- name: import-shadowing
136136
disabled: true
137137
- name: line-length-limit

cmd/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ func Execute() error {
8585
return err
8686
}
8787

88+
// propagate supported decision types to the registry for runtime filtering
89+
registry.GlobalDecisionRegistry.SupportedDecisionTypes = config.CrowdsecConfig.SupportedDecisionsTypes
90+
8891
if debugMode != nil && *debugMode {
8992
log.SetLevel(log.DebugLevel)
9093
}

config/crowdsec-blocklist-mirror.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ crowdsec_config:
77
exclude_scenarios_containing: []
88
only_include_decisions_from: []
99
insecure_skip_verify: false
10+
supported_decisions_types:
11+
- ban
1012

1113
blocklists:
1214
- format: plain_text # Supported formats are either "plain_text" or "mikrotik"

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ require (
99
github.com/felixge/httpsnoop v1.0.4
1010
github.com/prometheus/client_golang v1.23.2
1111
github.com/sirupsen/logrus v1.9.3
12-
golang.org/x/exp v0.0.0-20251009144603-d2f985daa21b
1312
golang.org/x/sync v0.17.0
1413
gopkg.in/natefinch/lumberjack.v2 v2.2.1
1514
gopkg.in/yaml.v3 v3.0.1

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
120120
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
121121
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
122122
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
123-
golang.org/x/exp v0.0.0-20251009144603-d2f985daa21b h1:18qgiDvlvH7kk8Ioa8Ov+K6xCi0GMvmGfGW0sgd/SYA=
124-
golang.org/x/exp v0.0.0-20251009144603-d2f985daa21b/go.mod h1:j/pmGrbnkbPtQfxEe5D0VQhZC6qKbfKifgD0oM7sR70=
125123
golang.org/x/net v0.44.0 h1:evd8IRDyfNBMBTTY5XRF1vaZlD+EmWx6x8PkhR04H/I=
126124
golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY=
127125
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=

pkg/cfg/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"fmt"
66
"io"
77
"os"
8+
"slices"
89
"strings"
910

1011
"github.com/sirupsen/logrus"
11-
"golang.org/x/exp/slices"
1212
"gopkg.in/yaml.v3"
1313

1414
"github.com/crowdsecurity/go-cs-lib/csstring"
@@ -31,6 +31,7 @@ type CrowdsecConfig struct {
3131
ExcludeScenariosContaining []string `yaml:"exclude_scenarios_containing"`
3232
OnlyIncludeDecisionsFrom []string `yaml:"only_include_decisions_from"`
3333
Scopes []string `yaml:"scopes,omitempty"`
34+
SupportedDecisionsTypes []string `yaml:"supported_decisions_types"`
3435
}
3536

3637
type BlockListConfig struct {

pkg/registry/registry.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package registry
22

33
import (
44
"net/url"
5+
"slices"
56
"sort"
67
"strings"
78

@@ -21,6 +22,7 @@ type Key int
2122
type DecisionRegistry struct {
2223
ActiveDecisionsByValue map[string]*models.Decision
2324
Key Key
25+
SupportedDecisionTypes []string
2426
}
2527

2628
func (dr *DecisionRegistry) AddDecisions(decisions []*models.Decision) {
@@ -33,10 +35,48 @@ func (dr *DecisionRegistry) AddDecisions(decisions []*models.Decision) {
3335
}
3436
}
3537

38+
func (dr *DecisionRegistry) GetSupportedDecisionTypesWithFilter(filter url.Values) []string {
39+
// determine allowed types: per-request override or registry default
40+
allowedTypes := make([]string, 0)
41+
if filter.Has("supported_decisions_types") {
42+
for _, v := range filter["supported_decisions_types"] {
43+
for _, t := range strings.Split(v, ",") {
44+
tt := strings.TrimSpace(strings.ToLower(t))
45+
if tt == "" {
46+
continue
47+
}
48+
allowedTypes = append(allowedTypes, tt)
49+
}
50+
}
51+
} else {
52+
for _, t := range dr.SupportedDecisionTypes {
53+
tt := strings.TrimSpace(strings.ToLower(t))
54+
if tt == "" {
55+
continue
56+
}
57+
allowedTypes = append(allowedTypes, tt)
58+
}
59+
}
60+
61+
return allowedTypes
62+
}
63+
3664
func (dr *DecisionRegistry) GetActiveDecisions(filter url.Values) []*models.Decision {
3765
ret := make([]*models.Decision, 0, len(dr.ActiveDecisionsByValue))
3866

67+
allowedTypes := dr.GetSupportedDecisionTypesWithFilter(filter)
68+
3969
for _, v := range dr.ActiveDecisionsByValue {
70+
// filter by type if allowedTypes is non-empty
71+
if len(allowedTypes) > 0 {
72+
dType := ""
73+
if v.Type != nil {
74+
dType = strings.ToLower(*v.Type)
75+
}
76+
if !slices.Contains(allowedTypes, dType) {
77+
continue
78+
}
79+
}
4080
if filter.Has("ipv6only") && strings.Contains(*v.Value, ".") {
4181
continue
4282
}

0 commit comments

Comments
 (0)