Skip to content

Commit e4dcdd2

Browse files
authored
fix include_capi filter (#2478)
1 parent ac01faf commit e4dcdd2

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

pkg/apiserver/apic.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ const (
4343
metricsIntervalDelta = time.Minute * 15
4444
)
4545

46-
var SCOPE_CAPI_ALIAS_ALIAS string = "crowdsecurity/community-blocklist" //we don't use "CAPI" directly, to make it less confusing for the user
47-
4846
type apic struct {
4947
// when changing the intervals in tests, always set *First too
5048
// or they can be negative
@@ -776,14 +774,14 @@ func (a *apic) UpdateBlocklists(links *modelscapi.GetDecisionsStreamResponseLink
776774
for _, blocklist := range links.Blocklists {
777775
if err := a.updateBlocklist(defaultClient, blocklist, add_counters); err != nil {
778776
return err
779-
}
777+
}
780778
}
781779
return nil
782780
}
783781

784782
func setAlertScenario(alert *models.Alert, add_counters map[string]map[string]int, delete_counters map[string]map[string]int) {
785783
if *alert.Source.Scope == types.CAPIOrigin {
786-
*alert.Source.Scope = SCOPE_CAPI_ALIAS_ALIAS
784+
*alert.Source.Scope = types.CommunityBlocklistPullSourceScope
787785
alert.Scenario = ptr.Of(fmt.Sprintf("update : +%d/-%d IPs", add_counters[types.CAPIOrigin]["all"], delete_counters[types.CAPIOrigin]["all"]))
788786
} else if *alert.Source.Scope == types.ListOrigin {
789787
*alert.Source.Scope = fmt.Sprintf("%s:%s", types.ListOrigin, *alert.Scenario)

pkg/apiserver/apic_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ func TestAPICWhitelists(t *testing.T) {
689689
alertScenario[alert.SourceScope]++
690690
}
691691
assert.Equal(t, 3, len(alertScenario))
692-
assert.Equal(t, 1, alertScenario[SCOPE_CAPI_ALIAS_ALIAS])
692+
assert.Equal(t, 1, alertScenario[types.CommunityBlocklistPullSourceScope])
693693
assert.Equal(t, 1, alertScenario["lists:blocklist1"])
694694
assert.Equal(t, 1, alertScenario["lists:blocklist2"])
695695

@@ -818,7 +818,7 @@ func TestAPICPullTop(t *testing.T) {
818818
alertScenario[alert.SourceScope]++
819819
}
820820
assert.Equal(t, 3, len(alertScenario))
821-
assert.Equal(t, 1, alertScenario[SCOPE_CAPI_ALIAS_ALIAS])
821+
assert.Equal(t, 1, alertScenario[types.CommunityBlocklistPullSourceScope])
822822
assert.Equal(t, 1, alertScenario["lists:blocklist1"])
823823
assert.Equal(t, 1, alertScenario["lists:blocklist2"])
824824

pkg/database/alerts.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -859,8 +859,24 @@ func AlertPredicatesFromFilter(filter map[string][]string) ([]predicate.Alert, e
859859
predicates = append(predicates, alert.HasDecisionsWith(decision.OriginEQ(value[0])))
860860
case "include_capi": //allows to exclude one or more specific origins
861861
if value[0] == "false" {
862-
predicates = append(predicates, alert.Not(alert.HasDecisionsWith(decision.OriginEQ(types.CAPIOrigin))))
863-
predicates = append(predicates, alert.Not(alert.HasDecisionsWith(decision.OriginEQ(types.ListOrigin))))
862+
predicates = append(predicates, alert.And(
863+
//do not show alerts with active decisions having origin CAPI or lists
864+
alert.And(
865+
alert.Not(alert.HasDecisionsWith(decision.OriginEQ(types.CAPIOrigin))),
866+
alert.Not(alert.HasDecisionsWith(decision.OriginEQ(types.ListOrigin))),
867+
),
868+
alert.Not(
869+
alert.And(
870+
//do not show neither alerts with no decisions if the Source Scope is lists: or CAPI
871+
alert.Not(alert.HasDecisions()),
872+
alert.Or(
873+
alert.SourceScopeHasPrefix(types.ListOrigin+":"),
874+
alert.SourceScopeEQ(types.CommunityBlocklistPullSourceScope),
875+
),
876+
),
877+
),
878+
),
879+
)
864880

865881
} else if value[0] != "true" {
866882
log.Errorf("Invalid bool '%s' for include_capi", value[0])

pkg/types/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const ConsoleOrigin = "console"
1717
const CscliImportOrigin = "cscli-import"
1818
const ListOrigin = "lists"
1919
const CAPIOrigin = "CAPI"
20+
const CommunityBlocklistPullSourceScope = "crowdsecurity/community-blocklist"
2021

2122
const DecisionTypeBan = "ban"
2223

0 commit comments

Comments
 (0)