Skip to content
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

Add ability to enabled/disable bouncer from config. #27

Merged
merged 4 commits into from
Mar 8, 2022
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
1 change: 1 addition & 0 deletions config_example.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ENABLED=true
API_URL=${CROWDSEC_LAPI_URL}
API_KEY=${API_KEY}
CACHE_EXPIRATION=1
Expand Down
8 changes: 8 additions & 0 deletions lib/crowdsec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ function csmod.init(configFile, userAgent)
runtime.cache = ngx.shared.crowdsec_cache
runtime.fallback = runtime.conf["FALLBACK_REMEDIATION"]

if runtime.conf["ENABLED"] == "false" then
return "Disabled", nil
end

if runtime.conf["REDIRECT_LOCATION"] == "/" then
ngx.log(ngx.ERR, "redirect location is set to '/' this will lead into infinite redirection")
Expand Down Expand Up @@ -382,6 +385,11 @@ end


function csmod.Allow(ip)

if runtime.conf["ENABLED"] == "false" then
return "Disabled", nil
end

if utils.table_len(runtime.conf["EXCLUDE_LOCATION"]) > 0 then
for k, v in pairs(runtime.conf["EXCLUDE_LOCATION"]) do
if ngx.var.uri == v then
Expand Down
11 changes: 10 additions & 1 deletion lib/plugins/crowdsec/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ function config.loadConfig(file)
return nil, "File ".. file .." doesn't exist"
end
local conf = {}
local valid_params = {'API_URL', 'API_KEY', 'BOUNCING_ON_TYPE', 'MODE', 'SECRET_KEY', 'SITE_KEY', 'BAN_TEMPLATE_PATH' ,'CAPTCHA_TEMPLATE_PATH', 'REDIRECT_LOCATION', 'RET_CODE', 'EXCLUDE_LOCATION', 'FALLBACK_REMEDIATION'}
local valid_params = {'ENABLED','API_URL', 'API_KEY', 'BOUNCING_ON_TYPE', 'MODE', 'SECRET_KEY', 'SITE_KEY', 'BAN_TEMPLATE_PATH' ,'CAPTCHA_TEMPLATE_PATH', 'REDIRECT_LOCATION', 'RET_CODE', 'EXCLUDE_LOCATION', 'FALLBACK_REMEDIATION'}
local valid_int_params = {'CACHE_EXPIRATION', 'CACHE_SIZE', 'REQUEST_TIMEOUT', 'UPDATE_FREQUENCY', 'CAPTCHA_EXPIRATION'}
local valid_bouncing_on_type_values = {'ban', 'captcha', 'all'}
local valid_truefalse_values = {'false', 'true'}
local default_values = {
['ENABLED'] = "true",
['REQUEST_TIMEOUT'] = 0.2,
['BOUNCING_ON_TYPE'] = "ban",
['MODE'] = "stream",
Expand All @@ -57,6 +59,13 @@ function config.loadConfig(file)
local s = split(line, "=")
for k, v in pairs(s) do
if has_value(valid_params, v) then
if v == "ENABLED" then
local value = s[2]
if not has_value(valid_truefalse_values, s[2]) then
ngx.log(ngx.ERR, "unsupported value '" .. s[2] .. "' for variable '" .. v .. "'. Using default value 'true' instead")
break
end
end
if v == "BOUNCING_ON_TYPE" then
local value = s[2]
if not has_value(valid_bouncing_on_type_values, s[2]) then
Expand Down