commit 5a35f2e: [Project] Rework settings registration

Vsevolod Stakhov vsevolod at highsecure.ru
Fri Jun 14 19:00:14 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-06-14 17:44:46 +0100
URL: https://github.com/rspamd/rspamd/commit/5a35f2e53577a45deb541318c2502de1e1392537

[Project] Rework settings registration

---
 lualib/lua_settings.lua      | 63 ++++++++++++++++++++++++++++++++++++++++----
 src/plugins/lua/settings.lua | 51 +++++++++++++++++++++--------------
 2 files changed, 89 insertions(+), 25 deletions(-)

diff --git a/lualib/lua_settings.lua b/lualib/lua_settings.lua
index 7f1679184..6723e4171 100644
--- a/lualib/lua_settings.lua
+++ b/lualib/lua_settings.lua
@@ -22,6 +22,59 @@ limitations under the License.
 
 local exports = {}
 local known_ids = {}
+local on_load_added = false
+
+local function register_settings_cb()
+  for _,set in pairs(known_ids) do
+    local s = set.settings
+    local enabled_symbols = {}
+    local disabled_symbols = {}
+
+    -- Enabled map
+    if s.symbols_enabled then
+      for _,sym in ipairs(s.symbols_enabled) do
+        enabled_symbols[sym] = true
+      end
+    end
+    if s.groups_enabled then
+      for _,gr in ipairs(s.groups_enabled) do
+        local syms = rspamd_config:get_group_symbols()
+
+        if syms then
+          for _,sym in ipairs(syms) do
+            enabled_symbols[sym] = true
+          end
+        end
+      end
+    end
+
+    -- Disabled map
+    if s.symbols_disabled then
+      for _,sym in ipairs(s.symbols_disabled) do
+        disabled_symbols[sym] = true
+      end
+    end
+    if s.groups_disabled then
+      for _,gr in ipairs(s.groups_disabled) do
+        local syms = rspamd_config:get_group_symbols()
+
+        if syms then
+          for _,sym in ipairs(syms) do
+            disabled_symbols[sym] = true
+          end
+        end
+      end
+    end
+
+    rspamd_config:register_settings_id(set.name, enabled_symbols, disabled_symbols)
+
+    -- Remove to avoid clash
+    s.symbols_disabled = nil
+    s.symbols_enabled = nil
+    s.groups_enabled = nil
+    s.groups_disabled = nil
+  end
+end
 
 -- Returns numeric representation of the settings id
 local function numeric_settings_id(str)
@@ -53,16 +106,16 @@ local function register_settings_id(str, settings)
     }
   end
 
+  if not on_load_added then
+    rspamd_config:add_on_load(register_settings_cb)
+    on_load_added = true
+  end
+
   return numeric_id
 end
 
 exports.register_settings_id = register_settings_id
 
-local function reset_ids()
-  known_ids = {}
-end
-
-exports.reset_ids = reset_ids
 
 local function settings_by_id(id)
   return known_ids[id]
diff --git a/src/plugins/lua/settings.lua b/src/plugins/lua/settings.lua
index 838bb9953..630095006 100644
--- a/src/plugins/lua/settings.lua
+++ b/src/plugins/lua/settings.lua
@@ -41,10 +41,14 @@ local max_pri = 0
 
 local selectors_cache = {} -- Used to speed up selectors in settings
 
-local function apply_settings(task, to_apply)
+local function apply_settings(task, to_apply, id)
   task:set_settings(to_apply)
   task:cache_set('settings', to_apply)
 
+  if id then
+    task:set_settings_id(id)
+  end
+
   if to_apply['add_headers'] or to_apply['remove_headers'] then
     local rep = {
       add_headers = to_apply['add_headers'] or {},
@@ -100,7 +104,7 @@ local function check_query_settings(task)
     local res,err = parser:parse_string(tostring(query_set))
     if res then
       local settings_obj = parser:get_object()
-      apply_settings(task, settings_obj)
+      apply_settings(task, settings_obj, nil)
 
       return true
     else
@@ -135,7 +139,7 @@ local function check_query_settings(task)
 
       if not settings_id then
         rspamd_logger.infox(task, 'apply maxscore = %s', nset.actions)
-        apply_settings(task, nset)
+        apply_settings(task, nset, nil)
         return true
       end
     end
@@ -154,7 +158,7 @@ local function check_query_settings(task)
         if nset then
           elt.apply = lua_util.override_defaults(nset, elt.apply)
         end
-        apply_settings(task, elt['apply'])
+        apply_settings(task, elt['apply'], settings_id)
         rspamd_logger.infox(task, "applying settings id %s", settings_id)
         return true
       end
@@ -162,14 +166,14 @@ local function check_query_settings(task)
       rspamd_logger.warnx(task, 'no settings id "%s" has been found', settings_id)
       if nset then
         rspamd_logger.infox(task, 'apply maxscore = %s', nset.actions)
-        apply_settings(task, nset)
+        apply_settings(task, nset, nil)
         return true
       end
     end
   else
     if nset then
       rspamd_logger.infox(task, 'apply maxscore = %s', nset.actions)
-      apply_settings(task, nset)
+      apply_settings(task, nset, nil)
       return true
     end
   end
@@ -448,7 +452,7 @@ local function check_settings(task)
           rspamd_logger.infox(task, "<%s> apply settings according to rule %s (%s matched)",
             task:get_message_id(), s.name, table.concat(matched, ','))
           if s.rule['apply'] then
-            apply_settings(task, s.rule['apply'])
+            apply_settings(task, s.rule.apply, s.rule.id)
             applied = true
           end
           if s.rule['symbols'] then
@@ -465,7 +469,7 @@ local function check_settings(task)
 end
 
 -- Process settings based on their priority
-local function process_settings_table(tbl)
+local function process_settings_table(tbl, allow_ids)
   local get_priority = function(elt)
     local pri_tonum = function(p)
       if p then
@@ -742,9 +746,7 @@ local function process_settings_table(tbl)
           name, elt.symbols)
       out['symbols'] = elt['symbols']
     end
-    if not elt.id then
-      elt.id = name
-    end
+
 
     if elt['apply'] then
       -- Just insert all metric results to the action key
@@ -756,10 +758,20 @@ local function process_settings_table(tbl)
       return nil
     end
 
-    if elt['id'] then
-      out.id = lua_settings.register_settings_id(elt.id, out)
-      lua_util.debugm(N, rspamd_config, 'added settings id to "%s": %s -> %s',
-          name, elt.id, out.id)
+    if allow_ids then
+      if not elt.id then
+        elt.id = name
+      end
+
+      if elt['id'] then
+        out.id = lua_settings.register_settings_id(elt.id, out)
+        lua_util.debugm(N, rspamd_config, 'added settings id to "%s": %s -> %s',
+            name, elt.id, out.id)
+      end
+    else
+      if elt['id'] then
+        rspamd_logger.errx(rspamd_config, 'cannot set static IDs from dynamic settings, please read the docs')
+      end
     end
 
     return out
@@ -778,7 +790,6 @@ local function process_settings_table(tbl)
   -- clear all settings
   max_pri = 0
   local nrules = 0
-  lua_settings.reset_ids()
   for k in pairs(settings) do settings[k]={} end
   -- fill new settings by priority
   fun.for_each(function(k, v)
@@ -813,9 +824,9 @@ local function process_settings_map(string)
   else
     local obj = parser:get_object()
     if obj['settings'] then
-      process_settings_table(obj['settings'])
+      process_settings_table(obj['settings'], false)
     else
-      process_settings_table(obj)
+      process_settings_table(obj, false)
     end
   end
 
@@ -839,7 +850,7 @@ local function gen_redis_callback(handler, id)
               local obj = parser:get_object()
               rspamd_logger.infox(task, "<%1> apply settings according to redis rule %2",
                 task:get_message_id(), id)
-              apply_settings(task, obj)
+              apply_settings(task, obj, nil)
               break
             end
           end
@@ -921,7 +932,7 @@ if set_section and set_section[1] and type(set_section[1]) == "string" then
     rspamd_logger.errx(rspamd_config, 'cannot load settings from %1', set_section)
   end
 elseif set_section and type(set_section) == "table" then
-  process_settings_table(set_section)
+  process_settings_table(set_section, true)
 end
 
 rspamd_config:register_symbol({


More information about the Commits mailing list