From c04808c938e2a3deb0e9b43e3ae0af915d1d561d Mon Sep 17 00:00:00 2001 From: Simon Hauser Date: Fri, 4 Aug 2023 16:12:01 +0200 Subject: [PATCH] fix: redis schema parsing --- lualib/lua_redis.lua | 60 +++++++++++++++++-------------- lualib/lua_util.lua | 13 +++++++ src/plugins/lua/bimi.lua | 6 ++-- src/plugins/lua/history_redis.lua | 4 +-- src/plugins/lua/reputation.lua | 4 +-- 5 files changed, 54 insertions(+), 33 deletions(-) diff --git a/lualib/lua_redis.lua b/lualib/lua_redis.lua index 2a43a915c..47da29899 100644 --- a/lualib/lua_redis.lua +++ b/lualib/lua_redis.lua @@ -39,25 +39,39 @@ local common_schema = ts.shape { sentinel_password = ts.string:is_optional(), } -local config_schema = -- Allow separate read/write servers to allow usage in the `extra_fields` -ts.shape({ +local read_schema = lutil.table_merge({ read_servers = ts.string + ts.array_of(ts.string), -}, { extra_fields = common_schema }) + - ts.shape({ - write_servers = ts.string + ts.array_of(ts.string), - }, { extra_fields = common_schema }) + - ts.shape({ - read_servers = ts.string + ts.array_of(ts.string), - write_servers = ts.string + ts.array_of(ts.string), - }, { extra_fields = common_schema }) + - ts.shape({ - servers = ts.string + ts.array_of(ts.string), - }, { extra_fields = common_schema }) + - ts.shape({ - server = ts.string + ts.array_of(ts.string), - }, { extra_fields = common_schema }) - -exports.config_schema = config_schema +}, common_schema) + +local write_schema = lutil.table_merge({ + write_servers = ts.string + ts.array_of(ts.string), +}, common_schema) + +local rw_schema = lutil.table_merge({ + read_servers = ts.string + ts.array_of(ts.string), + write_servers = ts.string + ts.array_of(ts.string), +}, common_schema) + +local servers_schema = lutil.table_merge({ + servers = ts.string + ts.array_of(ts.string), +}, common_schema) + +local server_schema = lutil.table_merge({ + server = ts.string + ts.array_of(ts.string), +}, common_schema) + +local generate_schema = function(external) + return ts.one_of { + ts.shape(external), + ts.shape(lutil.table_merge(read_schema, external)), + ts.shape(lutil.table_merge(write_schema, external)), + ts.shape(lutil.table_merge(rw_schema, external)), + ts.shape(lutil.table_merge(servers_schema, external)), + ts.shape(lutil.table_merge(server_schema, external)), + } +end + +exports.generate_schema = generate_schema local function redis_query_sentinel(ev_base, params, initialised) local function flatten_redis_table(tbl) @@ -1122,20 +1136,14 @@ local function script_set_loaded(script) end local function prepare_redis_call(script) - local function merge_tables(t1, t2) - for k, v in pairs(t2) do - t1[k] = v - end - end - local servers = {} local options = {} if script.redis_params.read_servers then - merge_tables(servers, script.redis_params.read_servers:all_upstreams()) + lutil.table_merge(servers, script.redis_params.read_servers:all_upstreams()) end if script.redis_params.write_servers then - merge_tables(servers, script.redis_params.write_servers:all_upstreams()) + lutil.table_merge(servers, script.redis_params.write_servers:all_upstreams()) end -- Call load script on each server, set loaded flag diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index c2bdf40a9..289e2ed3a 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -618,6 +618,19 @@ end exports.table_cmp = table_cmp +--[[[ +-- @function lua_util.table_merge(t1, t2) +-- Merge two tables +--]] +local function table_merge(t1, t2) + for k, v in pairs(t2) do + t1[k] = v + end + return t1 +end + +exports.table_merge = table_merge + --[[[ -- @function lua_util.table_cmp(task, name, value, stop_chars) -- Performs header folding diff --git a/src/plugins/lua/bimi.lua b/src/plugins/lua/bimi.lua index bcf5a823d..e90a4e3b9 100644 --- a/src/plugins/lua/bimi.lua +++ b/src/plugins/lua/bimi.lua @@ -34,7 +34,7 @@ local settings = { } local redis_params -local settings_schema = ts.shape({ +local settings_schema = lua_redis.generate_schema({ helper_url = ts.string, helper_timeout = ts.number + ts.string / lua_util.parse_time_interval, helper_sync = ts.boolean, @@ -42,7 +42,7 @@ local settings_schema = ts.shape({ redis_min_expiry = ts.number + ts.string / lua_util.parse_time_interval, redis_prefix = ts.string, enabled = ts.boolean:is_optional(), -}, {extra_fields = lua_redis.config_schema}) +}) local function check_dmarc_policy(task) local dmarc_sym = task:get_symbol('DMARC_POLICY_ALLOW') @@ -378,4 +378,4 @@ if redis_params then rspamd_config:register_dependency('BIMI_CHECK', 'DMARC_CHECK') else lua_util.disable_module(N, "redis") -end \ No newline at end of file +end diff --git a/src/plugins/lua/history_redis.lua b/src/plugins/lua/history_redis.lua index d4ee71cd1..cfedfeac3 100644 --- a/src/plugins/lua/history_redis.lua +++ b/src/plugins/lua/history_redis.lua @@ -65,7 +65,7 @@ local settings = { subject_privacy_length = 16, -- cut the length of the hash } -local settings_schema = ts.shape({ +local settings_schema = lua_redis.generate_schema({ key_prefix = ts.string, expire = (ts.number + ts.string / lua_util.parse_time_interval):is_optional(), nrows = ts.number, @@ -74,7 +74,7 @@ local settings_schema = ts.shape({ subject_privacy_alg = ts.string:is_optional(), subject_privacy_prefix = ts.string:is_optional(), subject_privacy_length = ts.number:is_optional(), -}, {extra_fields = lua_redis.config_schema}) +}) local function process_addr(addr) if addr then diff --git a/src/plugins/lua/reputation.lua b/src/plugins/lua/reputation.lua index 19e7085ef..e0530a42e 100644 --- a/src/plugins/lua/reputation.lua +++ b/src/plugins/lua/reputation.lua @@ -1064,7 +1064,7 @@ end --]] local backends = { redis = { - schema = ts.shape({ + schema = lua_redis.generate_schema({ prefix = ts.string, expiry = ts.number + ts.string / lua_util.parse_time_interval, buckets = ts.array_of(ts.shape{ @@ -1072,7 +1072,7 @@ local backends = { name = ts.string, mult = ts.number + ts.string / tonumber }), - }, {extra_fields = lua_redis.config_schema}), + }), config = { expiry = default_expiry, prefix = default_prefix, -- 2.39.5