commit 0e3bc7a: [Fix] Fix lua schema enrichment logic for Redis params

Vsevolod Stakhov vsevolod at rspamd.com
Mon Aug 7 13:56:04 UTC 2023


Author: Vsevolod Stakhov
Date: 2023-08-07 14:52:12 +0100
URL: https://github.com/rspamd/rspamd/commit/0e3bc7a86a600b88c57207a0a4442cbe9840d081 (HEAD -> master)

[Fix] Fix lua schema enrichment logic for Redis params

---
 lualib/lua_redis.lua              | 42 +++++++++++++++++++--------------------
 src/plugins/lua/bimi.lua          |  2 +-
 src/plugins/lua/history_redis.lua |  2 +-
 src/plugins/lua/reputation.lua    | 11 +++++-----
 4 files changed, 29 insertions(+), 28 deletions(-)

diff --git a/lualib/lua_redis.lua b/lualib/lua_redis.lua
index 47da29899..6d2a9e4f1 100644
--- a/lualib/lua_redis.lua
+++ b/lualib/lua_redis.lua
@@ -24,19 +24,19 @@ local exports = {}
 local E = {}
 local N = "lua_redis"
 
-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(),
-  sentinels = (ts.string + ts.array_of(ts.string)):is_optional(),
-  sentinel_watch_time = (ts.number + ts.string / lutil.parse_time_interval):is_optional(),
-  sentinel_masters_pattern = ts.string:is_optional(),
-  sentinel_master_maxerrors = (ts.number + ts.string / tonumber):is_optional(),
-  sentinel_password = ts.string:is_optional(),
+local common_schema = {
+  timeout = (ts.number + ts.string / lutil.parse_time_interval):is_optional():describe("Connection timeout"),
+  db = ts.string:is_optional():describe("Database number"),
+  database = ts.string:is_optional():describe("Database number"),
+  dbname = ts.string:is_optional():describe("Database number"),
+  prefix = ts.string:is_optional():describe("Key prefix"),
+  password = ts.string:is_optional():describe("Password"),
+  expand_keys = ts.boolean:is_optional():describe("Expand keys"),
+  sentinels = (ts.string + ts.array_of(ts.string)):is_optional():describe("Sentinel servers"),
+  sentinel_watch_time = (ts.number + ts.string / lutil.parse_time_interval):is_optional():describe("Sentinel watch time"),
+  sentinel_masters_pattern = ts.string:is_optional():describe("Sentinel masters pattern"),
+  sentinel_master_maxerrors = (ts.number + ts.string / tonumber):is_optional():describe("Sentinel master max errors"),
+  sentinel_password = ts.string:is_optional():describe("Sentinel password"),
 }
 
 local read_schema = lutil.table_merge({
@@ -60,18 +60,18 @@ local server_schema = lutil.table_merge({
   server = ts.string + ts.array_of(ts.string),
 }, common_schema)
 
-local generate_schema = function(external)
+local enrich_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)),
+    ts.shape(external), -- no specific redis parameters
+    ts.shape(lutil.table_merge(read_schema, external)), -- read_servers specified
+    ts.shape(lutil.table_merge(write_schema, external)), -- write_servers specified
+    ts.shape(lutil.table_merge(rw_schema, external)), -- both read and write servers defined
+    ts.shape(lutil.table_merge(servers_schema, external)), -- just servers for both ops
+    ts.shape(lutil.table_merge(server_schema, external)), -- legacy `server` attribute
   }
 end
 
-exports.generate_schema = generate_schema
+exports.enrich_schema = enrich_schema
 
 local function redis_query_sentinel(ev_base, params, initialised)
   local function flatten_redis_table(tbl)
diff --git a/src/plugins/lua/bimi.lua b/src/plugins/lua/bimi.lua
index 5dbb83623..914484543 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 = lua_redis.generate_schema({
+local settings_schema = lua_redis.enrich_schema({
   helper_url = ts.string,
   helper_timeout = ts.number + ts.string / lua_util.parse_time_interval,
   helper_sync = ts.boolean,
diff --git a/src/plugins/lua/history_redis.lua b/src/plugins/lua/history_redis.lua
index 9cbe7caf7..d0aa5aec7 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 = lua_redis.generate_schema({
+local settings_schema = lua_redis.enrich_schema({
   key_prefix = ts.string,
   expire = (ts.number + ts.string / lua_util.parse_time_interval):is_optional(),
   nrows = ts.number,
diff --git a/src/plugins/lua/reputation.lua b/src/plugins/lua/reputation.lua
index fc7001eb1..a3af26c91 100644
--- a/src/plugins/lua/reputation.lua
+++ b/src/plugins/lua/reputation.lua
@@ -1089,14 +1089,14 @@ end
 --]]
 local backends = {
   redis = {
-    schema = lua_redis.generate_schema({
-      prefix = ts.string,
-      expiry = ts.number + ts.string / lua_util.parse_time_interval,
+    schema = lua_redis.enrich_schema({
+      prefix = ts.string:is_optional(),
+      expiry = (ts.number + ts.string / lua_util.parse_time_interval):is_optional(),
       buckets = ts.array_of(ts.shape {
         time = ts.number + ts.string / lua_util.parse_time_interval,
         name = ts.string,
         mult = ts.number + ts.string / tonumber
-      }),
+      })          :is_optional(),
     }),
     config = {
       expiry = default_expiry,
@@ -1263,7 +1263,7 @@ local function parse_rule(name, tbl)
       rspamd_logger.errx(rspamd_config, "cannot parse whitelist map config for %s: (%s)",
           sel_type,
           rule.config.whitelist)
-      return
+      return false
     end
   end
 
@@ -1362,6 +1362,7 @@ local function parse_rule(name, tbl)
     }
   end
 
+  return true
 end
 
 redis_params = lua_redis.parse_redis_server('reputation')


More information about the Commits mailing list