Skip to content

fix the cache management for live mode. #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/crowdsec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ function csmod.allowIp(ip)
local key_type = key_parts[1]
if key_type == "normal" then
local decision_string, flag_id = runtime.cache:get("decision_cache/" .. key)
ngx.log(ngx.DEBUG, "[CACHE] Looking for '" .. key .. "' in cache")
local t = utils.split_on_delimiter(decision_string,"/")
if t == nil then
return true, nil, "Failed to split decision string"
Expand Down Expand Up @@ -382,9 +383,10 @@ function csmod.allowIp(ip)
item = key_type.."_"..table.concat(netmask, ":").."_"..iputils.ipv6_band(ip_network_address, netmask)
end
local decision_string, flag_id = runtime.cache:get("decision_cache/" .. item)
ngx.log(ngx.DEBUG, "[CACHE] Looking for '" .. key .. "' in cache")
if decision_string ~= nil then -- we have it in cache
if decision_string == "none" then
ngx.log(ngx.DEBUG, "'" .. key .. "' is in cache with value'" .. decision_string .. "'")
ngx.log(ngx.DEBUG, "[CACHE]'" .. key .. "' is in cache with value'" .. decision_string .. "'")
return true, nil, nil
end
ngx.log(ngx.DEBUG, "'" .. key .. "' is in cache with value'" .. decision_string .. "'")
Expand Down Expand Up @@ -413,7 +415,7 @@ function csmod.allowIp(ip)
ip,
runtime.conf["API_URL"],
runtime.conf["REQUEST_TIMEOUT"],
runtime.conf["CAPTCHA_EXPIRATION"],
runtime.conf["CACHE_EXPIRATION"],
REMEDIATION_API_KEY_HEADER,
runtime.conf['API_KEY'],
runtime.userAgent,
Expand Down
8 changes: 4 additions & 4 deletions lib/plugins/crowdsec/live.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ function live:live_query(ip, api_url, timeout, cache_expiration, api_key_header,
if body == "null" then -- no result from API, no decision for this IP
-- set ip in cache and DON'T block it
local key,_ = utils.item_to_string(ip, "ip")
local succ, err, forcible = live.cache:set(key, "none", cache_expiration, 1)
local succ, err, forcible = live.cache:set("decision_cache/" .. key, "none", cache_expiration, 1)
--
ngx.log(ngx.DEBUG, "Adding '" .. key .. "' in cache for '" .. cache_expiration .. "' seconds") --debug
ngx.log(ngx.DEBUG, "[CACHE] Adding '" .. key .. "' in cache for '" .. cache_expiration .. "' seconds") --debug
if not succ then
ngx.log(ngx.ERR, "failed to add ip '" .. ip .. "' in cache: ".. err)
end
Expand All @@ -66,8 +66,8 @@ function live:live_query(ip, api_url, timeout, cache_expiration, api_key_header,
end
local cache_value = decision.type .. "/" .. decision.origin
local key,_ = utils.item_to_string(decision.value, decision.scope)
local succ, err, forcible = live.cache:set(key, cache_value, cache_expiration, 0)
ngx.log(ngx.DEBUG, "Adding '" .. key .. "' in cache for '" .. cache_expiration .. "' seconds with decision type'" .. decision.type .. "'with origin'" .. decision.origin ) --debug
local succ, err, forcible = live.cache:set("decision_cache/" .. key, cache_value, cache_expiration, 0)
ngx.log(ngx.DEBUG, "[CACHE] Adding '" .. key .. "' in cache for '" .. cache_expiration .. "' seconds with decision type'" .. decision.type .. "'with origin'" .. decision.origin ) --debug
if not succ then
ngx.log(ngx.ERR, "failed to add ".. decision.value .." : "..err)
end
Expand Down
8 changes: 3 additions & 5 deletions t/10_live_ban_and_cache.t
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,17 @@ qr/DEBUG CACHE:[^ ]*/
DEBUG CACHE:metrics_first_run:false
DEBUG CACHE:metrics_processed/ip_type=ipv4&:1
DEBUG CACHE:metrics_all:processed/ip_type=ipv4&,
DEBUG CACHE:ipv4_4294967295_16843010:none
DEBUG CACHE:decision_cache/ipv4_4294967295_16843010:none
DEBUG CACHE:captcha_ok:false
DEBUG CACHE:ipv4_4294967295_16843010:none
DEBUG CACHE:metrics_first_run:false
DEBUG CACHE:metrics_processed/ip_type=ipv4&:2
DEBUG CACHE:ipv4_4294967295_16843009:ban/CAPI
DEBUG CACHE:decision_cache/ipv4_4294967295_16843009:ban/CAPI
DEBUG CACHE:metrics_dropped/ip_type=ipv4&origin=CAPI&:1
DEBUG CACHE:metrics_all:processed/ip_type=ipv4&,dropped/ip_type=ipv4&origin=CAPI&,
DEBUG CACHE:captcha_ok:false
DEBUG CACHE:ipv4_4294967295_16843010:none
DEBUG CACHE:metrics_first_run:false
DEBUG CACHE:metrics_processed/ip_type=ipv4&:3
DEBUG CACHE:ipv4_4294967295_16843009:ban/CAPI
DEBUG CACHE:decision_cache/ipv4_4294967295_16843009:ban/CAPI
DEBUG CACHE:metrics_dropped/ip_type=ipv4&origin=CAPI&:2
DEBUG CACHE:metrics_all:processed/ip_type=ipv4&,dropped/ip_type=ipv4&origin=CAPI&,
DEBUG CACHE:captcha_ok:false