commit b7e3440: [Feature] JSON endpoint for querying maps

Andrew Lewis nerf at judo.za.org
Wed Nov 17 14:42:05 UTC 2021


Author: Andrew Lewis
Date: 2021-11-17 15:57:01 +0200
URL: https://github.com/rspamd/rspamd/commit/b7e34400244638aa01a3520ba965250a15e86904 (refs/pull/3982/head)

[Feature] JSON endpoint for querying maps

---
 rules/controller/maps.lua | 66 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 65 insertions(+), 1 deletion(-)

diff --git a/rules/controller/maps.lua b/rules/controller/maps.lua
index 71bf4beef..9079cb39a 100644
--- a/rules/controller/maps.lua
+++ b/rules/controller/maps.lua
@@ -18,6 +18,8 @@ limitations under the License.
 local maps_cache
 local maps_aliases
 local lua_util = require "lua_util"
+local ts = require("tableshape").types
+local ucl = require "ucl"
 
 local function maybe_fill_maps_cache()
   if not maps_cache then
@@ -140,11 +142,73 @@ local function handle_list_maps(_, conn, _)
   }
 end
 
+local query_json_schema = ts.shape{
+  maps = ts.array_of(ts.string):is_optional(),
+  report_misses = ts.boolean:is_optional(),
+  values = ts.array_of(ts.string),
+}
+
+local function handle_query_json(task, conn)
+  maybe_fill_maps_cache()
+
+  local parser = ucl.parser()
+  local ok, err = parser:parse_text(task:get_rawbody())
+  if not ok then
+    conn:send_error(400, err)
+    return
+  end
+  local obj = parser:get_object()
+
+  ok, err = query_json_schema:transform(obj)
+  if not ok then
+    conn:send_error(400, err)
+    return
+  end
+
+  local maps_to_check = {}
+  local report_misses = obj.report_misses
+  local results = {}
+
+  if obj.maps then
+    for _,mn in ipairs(obj.maps) do
+      if maps_cache[mn] then
+        maps_to_check[mn] = maps_cache[mn]
+      else
+        local alias = maps_aliases[mn]
+
+        if alias then
+          maps_to_check[alias] = maps_cache[alias]
+        else
+          conn:send_error(400, 'no such map: ' .. mn)
+          return
+        end
+      end
+    end
+  else
+    maps_to_check = maps_cache
+  end
+
+  for _,key in ipairs(obj.values) do
+    for uri,m in pairs(maps_to_check) do
+      check_specific_map(key, uri, m, results, report_misses)
+    end
+  end
+  conn:send_ucl{
+    success = (#results > 0),
+    results = results
+  }
+end
+
 return {
   query = {
     handler = handle_query_map,
     enable = false,
   },
+  query_json = {
+    handler = handle_query_json,
+    enable = false,
+    need_task = true,
+  },
   query_specific = {
     handler = handle_query_specific_map,
     enable = false,
@@ -153,4 +217,4 @@ return {
     handler = handle_list_maps,
     enable = false,
   },
-}
\ No newline at end of file
+}


More information about the Commits mailing list