From: Andrew Lewis Date: Wed, 17 Nov 2021 13:57:01 +0000 (+0200) Subject: [Feature] JSON endpoint for querying maps X-Git-Tag: 3.2~255^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F3982%2Fhead;p=thirdparty%2Frspamd.git [Feature] JSON endpoint for querying maps --- diff --git a/rules/controller/maps.lua b/rules/controller/maps.lua index 71bf4beef4..9079cb39ac 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 +}