Skip to content

exprhelpers: optional scenario argument for GetDecisionsCount, GetActiveDecisionsCount and GetActiveDecisionsTimeLeft - #4791

Open
tommy-ng0 wants to merge 2 commits into
crowdsecurity:masterfrom
tommy-ng0:feat/expr-active-decisions-scenario
Open

tommy-ng0 wants to merge 2 commits into
crowdsecurity:masterfrom
tommy-ng0:feat/expr-active-decisions-scenario

Conversation

@tommy-ng0

@tommy-ng0 tommy-ng0 commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

What and why

#4756 gave GetDecisionsSinceCount an optional scenario argument so a profile can escalate ban length per scenario. I should have done the other three decision helpers in the same PR: this brings GetDecisionsCount, GetActiveDecisionsCount and GetActiveDecisionsTimeLeft to parity, with the same optional trailing scenario and the same exact match.

@blotus please let me know if you have any questions/ reviews :)

How it was tested

TEST_LOCAL_ONLY=1 go test -tags=netgo,osusergo,expr_debug,nomsgpack ./pkg/exprhelpers/ ./pkg/database/
ok  github.com/crowdsecurity/crowdsec/pkg/exprhelpers
ok  github.com/crowdsecurity/crowdsec/pkg/database

Three new table tests in the shape of TestGetDecisionsSinceCountWithScenario: scenario omitted, the same scenario (expired decision counted by GetDecisionsCount, not by the active helpers; own time left rather than the IP's longest), another scenario, an unknown scenario, an unknown IP, and one argument too many failing to compile. With the scenario filters removed, eight sub-tests fail.

Live run: crowdsec built from this branch, in the v1.8.2 image, crowdsecurity/sshd installed, with the postoverflow and profile above. A manual 2h ban on the IP carrying scenario crowdsecurity/ssh-slow-bf, then six failed logins:

  • ssh-bf was not skipped (the IP had 2h left, but none from ssh-bf) and got a 1h ban: GetDecisionsCount with the scenario saw 0, where the IP-wide count would have given 4h.
  • A second burst after the blackhole was skipped: Ban for 203.0.113.10 whitelisted, reason [this scenario already banned the source with >30m left]. The ssh-slow-bf overflow from the same lines was skipped too, by the manual ban carrying its scenario.
  • A fresh IP got the usual alert and 1h.

golangci-lint was not run locally.

Checklist

  • One concern only.
  • No break to LAPI/CAPI payloads, database schema, config keys, or cscli -o json|raw output.
  • A human has reviewed this diff line by line.
  • A human has tested this change, not only an agent.

AI assistance used: mostly

…nd GetActiveDecisionsTimeLeft

GetDecisionsSinceCount can filter by scenario since crowdsecurity#4756, but the two
active-decision helpers still look at every decision on the IP, so a
postoverflow can't ask "does this scenario's own ban still have time
left". Give both the same optional trailing scenario argument, with the
same exact match, and let GetActiveDecisionsTimeLeftByValue take the
scenario pointer like CountDecisionsByValue does. Without it they behave
as before.
The last of the four decision helpers without it. With a scenario it
answers "has this scenario banned the IP before" without picking a
window, the first rung of a per-scenario ban ladder. The query already
took the filter; this only passes it through and registers the
two-argument signature.
@github-actions

Copy link
Copy Markdown

@tommy-ng0: There are no 'kind' label on this PR. You need a 'kind' label to generate the release automatically.

  • /kind feature
  • /kind enhancement
  • /kind refactoring
  • /kind fix
  • /kind chore
  • /kind dependencies
Details

I am a bot created to help the crowdsecurity developers manage community feedback and contributions. You can check out my manifest file to understand my behavior and what I can do. If you want to use this for your project, you can check out the BirthdayResearch/oss-governance-bot repository.

@github-actions

Copy link
Copy Markdown

@tommy-ng0: There are no area labels on this PR. You can add as many areas as you see fit.

  • /area agent
  • /area local-api
  • /area cscli
  • /area appsec
  • /area security
  • /area configuration
Details

I am a bot created to help the crowdsecurity developers manage community feedback and contributions. You can check out my manifest file to understand my behavior and what I can do. If you want to use this for your project, you can check out the BirthdayResearch/oss-governance-bot repository.

@tommy-ng0

Copy link
Copy Markdown
Contributor Author

/kind enhancement
/area local-api
/area agent

@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 71.82%. Comparing base (f4b4d9d) to head (edb98d0).

Additional details and impacted files
@@             Coverage Diff             @@
##           master    #4791       +/-   ##
===========================================
+ Coverage   45.28%   71.82%   +26.54%     
===========================================
  Files         518      545       +27     
  Lines       35942    37165     +1223     
===========================================
+ Hits        16275    26694    +10419     
+ Misses      19656    10470     -9186     
+ Partials       11        1       -10     
Flag Coverage Δ
bats 45.27% <0.00%> (-0.02%) ⬇️
unit-linux 49.79% <100.00%> (?)
unit-windows 37.79% <80.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant