commit f860d55: [Feature] Preliminary maps addon for controller

Vsevolod Stakhov vsevolod at highsecure.ru
Wed Jul 1 11:28:07 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-07-01 11:53:29 +0100
URL: https://github.com/rspamd/rspamd/commit/f860d55ac4b9b678b0a1419e88bf014d9bca72f5

[Feature] Preliminary maps addon for controller

---
 rules/controller/init.lua |  3 +-
 rules/controller/maps.lua | 72 +++++++++++++++++++++++++++++++++++++++++++++++
 src/lua/lua_map.c         |  8 ++----
 3 files changed, 76 insertions(+), 7 deletions(-)

diff --git a/rules/controller/init.lua b/rules/controller/init.lua
index 5057c08f5..e5204da63 100644
--- a/rules/controller/init.lua
+++ b/rules/controller/init.lua
@@ -25,7 +25,8 @@ local rspamd_logger = require "rspamd_logger"
 -- Define default controller paths, could be overridden in local.d/controller.lua
 
 local controller_plugin_paths = {
-  selectors = dofile(local_rules .. "/controller/selectors.lua")
+  selectors = dofile(local_rules .. "/controller/selectors.lua"),
+  maps = dofile(local_rules .. "/controller/maps.lua")
 }
 
 if rspamd_util.file_exists(local_conf .. '/controller.lua') then
diff --git a/rules/controller/maps.lua b/rules/controller/maps.lua
new file mode 100644
index 000000000..42d761172
--- /dev/null
+++ b/rules/controller/maps.lua
@@ -0,0 +1,72 @@
+--[[
+Copyright (c) 2020, Vsevolod Stakhov <vsevolod at highsecure.ru>
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+]]--
+
+-- Controller maps plugin
+local maps_cache
+local maps_aliases
+local lua_util = require "lua_util"
+
+local function maybe_fill_maps_cache()
+  if not maps_cache then
+    maps_cache = {}
+    maps_aliases = {}
+    local maps = rspamd_config:get_maps()
+    for _,m in ipairs(maps) do
+      -- We get the first url here and that's it
+      local url = m:get_uri()
+      if url ~= 'static' then
+        if not maps_cache[url] then
+          local alias = url:match('/([^/]+)$')
+          maps_cache[url] = m
+          if not maps_aliases[alias] then
+            maps_aliases[alias] = url
+          end
+        else
+          -- Do not override, as we don't care about duplicate maps that come from different
+          -- sources.
+          -- In theory, that should be cached but there are some exceptions even so far...
+        end
+      end
+    end
+  end
+end
+
+local function handle_query_map(_, conn, req_params)
+  maybe_fill_maps_cache()
+  if req_params.value and req_params.value ~= '' then
+
+    conn:send_ucl({success = false and true})
+  else
+    conn:send_error(404, 'missing value')
+  end
+end
+
+local function handle_list_maps(_, conn, _)
+  maybe_fill_maps_cache()
+  conn:send_ucl({maps = lua_util.keys(maps_cache),
+                 aliases = maps_aliases})
+end
+
+return {
+  query = {
+    handler = handle_query_map,
+    enable = false,
+  },
+  list = {
+    handler = handle_list_maps,
+    enable = false,
+  },
+}
\ No newline at end of file
diff --git a/src/lua/lua_map.c b/src/lua/lua_map.c
index 25e7bdc92..72bc00fca 100644
--- a/src/lua/lua_map.c
+++ b/src/lua/lua_map.c
@@ -843,8 +843,6 @@ lua_map_get_key (lua_State * L)
 
 				if (!rspamd_parse_inet_address_ip (addr_str, len, addr->addr)) {
 					addr = NULL;
-					msg_warn ("invalid ip address: %*s, when checking map: %s",
-							(gint)len, addr_str, map->map->name);
 				}
 			}
 			else if (lua_type (L, 2) == LUA_TUSERDATA) {
@@ -1222,15 +1220,13 @@ lua_map_get_uri (lua_State *L)
 {
 	LUA_TRACE_POINT;
 	struct rspamd_lua_map *map = lua_check_map (L, 1);
-	const gchar *ret = "undefined";
 	struct rspamd_map_backend *bk;
-		guint i;
+	guint i;
 
 	if (map != NULL) {
 		for (i = 0; i < map->map->backends->len; i ++) {
 			bk = g_ptr_array_index (map->map->backends, i);
-			ret = bk->uri;
-			lua_pushstring (L, ret);
+			lua_pushstring (L, bk->uri);
 		}
 	}
 	else {


More information about the Commits mailing list