summaryrefslogtreecommitdiffstats
path: root/lualib
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2018-09-20 13:08:31 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2018-09-20 13:42:13 +0100
commitba2f3148eb56e45a12b64696994abd2ffc319c7c (patch)
treefc4c4efd917428d7457107fe1899f10478de00e3 /lualib
parentd4e7f410032b2fac361cf28bd3a3e3060da2354e (diff)
downloadrspamd-ba2f3148eb56e45a12b64696994abd2ffc319c7c.tar.gz
rspamd-ba2f3148eb56e45a12b64696994abd2ffc319c7c.zip
[Feature] Add schema validation for Redis settings
Diffstat (limited to 'lualib')
-rw-r--r--lualib/lua_redis.lua56
1 files changed, 50 insertions, 6 deletions
diff --git a/lualib/lua_redis.lua b/lualib/lua_redis.lua
index 19a3349f5..314cbbef1 100644
--- a/lualib/lua_redis.lua
+++ b/lualib/lua_redis.lua
@@ -17,11 +17,36 @@ limitations under the License.
local logger = require "rspamd_logger"
local lutil = require "lua_util"
local rspamd_util = require "rspamd_util"
+local ts = require("tableshape").types
local exports = {}
local E = {}
+local common_schema = ts.shape {
+ timeout = (ts.number + ts.string / lutil.parse_time_interval):is_optional(),
+ db = ts.string:is_optional(),
+ database = ts.string:is_optional(),
+ dbname = ts.string:is_optional(),
+ prefix = ts.string:is_optional(),
+ password = ts.string:is_optional(),
+ expand_keys = ts.boolean:is_optional(),
+}
+
+local config_schema =
+ ts.shape({
+ read_servers = ts.string + ts.array_of(ts.string),
+ write_servers = ts.string + ts.array_of(ts.string),
+ }, {extra_opts = common_schema}) +
+ ts.shape({
+ servers = ts.string + ts.array_of(ts.string),
+ }, {extra_opts = common_schema}) +
+ ts.shape({
+ server = ts.string + ts.array_of(ts.string),
+ }, {extra_opts = common_schema})
+
+exports.config_schema = config_schema
+
--[[[
-- @module lua_redis
-- This module contains helper functions for working with Redis
@@ -143,6 +168,8 @@ local function rspamd_parse_redis_server(module_name, module_opts, no_fallback)
opts = module_opts
end
+ local schema_error
+
if opts then
local ret
@@ -157,11 +184,18 @@ local function rspamd_parse_redis_server(module_name, module_opts, no_fallback)
ret = try_load_redis_servers(opts, rspamd_config, result)
if ret then
- return result
+ ret,schema_error = config_schema:transform(ret)
+
+ if ret then return ret end
end
end
- if no_fallback then return nil end
+ if no_fallback then
+ if schema_error then
+ logger.errx(rspamd_config, "invalid Redis definition: %s", schema_error)
+ end
+ return nil
+ end
-- Try global options
opts = rspamd_config:get_all_opt('redis')
@@ -171,8 +205,11 @@ local function rspamd_parse_redis_server(module_name, module_opts, no_fallback)
if opts[module_name] then
ret = try_load_redis_servers(opts[module_name], rspamd_config, result)
+
if ret then
- return result
+ ret,schema_error = config_schema:transform(ret)
+
+ if ret then return ret end
end
else
ret = try_load_redis_servers(opts, rspamd_config, result)
@@ -190,15 +227,22 @@ local function rspamd_parse_redis_server(module_name, module_opts, no_fallback)
end
if ret then
- logger.infox(rspamd_config, "using default redis server for module %s",
- module_name)
+ ret,schema_error = config_schema:transform(ret)
+
+ if ret then return ret end
end
end
end
if result.read_servers then
- return result
+ result,schema_error = config_schema:transform(ret)
+
+ if result then return result end
else
+ if schema_error then
+ logger.errx(rspamd_config, "invalid Redis definition: %s", schema_error)
+ end
+
return nil
end
end