aboutsummaryrefslogtreecommitdiffstats
path: root/rules/controller
diff options
context:
space:
mode:
authorAndrew Lewis <nerf@judo.za.org>2021-11-17 15:57:01 +0200
committerAndrew Lewis <nerf@judo.za.org>2021-11-17 15:57:01 +0200
commitb7e34400244638aa01a3520ba965250a15e86904 (patch)
tree3cb05eaf012e160dce179b06c604ba8c593e18e0 /rules/controller
parentda4034ce506fd5bb93687791c5a4af3c9218bba3 (diff)
downloadrspamd-b7e34400244638aa01a3520ba965250a15e86904.tar.gz
rspamd-b7e34400244638aa01a3520ba965250a15e86904.zip
[Feature] JSON endpoint for querying maps
Diffstat (limited to 'rules/controller')
-rw-r--r--rules/controller/maps.lua66
1 files changed, 65 insertions, 1 deletions
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
+}