From f0ae0420370fb3ff74e322f87f6adcd498c09a90 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 11 Jun 2019 17:54:25 +0100 Subject: [PATCH] [Project] Settings: Add lua_settings library --- lualib/lua_settings.lua | 73 ++++++++++++++++++++++++++++++++++++ src/plugins/lua/settings.lua | 22 +++++------ 2 files changed, 83 insertions(+), 12 deletions(-) create mode 100644 lualib/lua_settings.lua diff --git a/lualib/lua_settings.lua b/lualib/lua_settings.lua new file mode 100644 index 000000000..7f1679184 --- /dev/null +++ b/lualib/lua_settings.lua @@ -0,0 +1,73 @@ +--[[ +Copyright (c) 2019, Vsevolod Stakhov + +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. +]]-- + +--[[[ +-- @module lua_settings +-- This module contains internal helpers for the settings infrastructure in Rspamd +-- More details at https://rspamd.com/doc/configuration/settings.html +--]] + +local exports = {} +local known_ids = {} + +-- Returns numeric representation of the settings id +local function numeric_settings_id(str) + local cr = require "rspamd_cryptobox_hash" + local util = require "rspamd_util" + local ret = util.unpack("I4", + cr.create_specific('xxh64'):update(str):bin()) + + return ret +end + +local function register_settings_id(str, settings) + local numeric_id = numeric_settings_id(str) + + if known_ids[numeric_id] then + -- Might be either rewrite or a collision + if known_ids[numeric_id].name ~= str then + local logger = require "rspamd_logger" + + logger.errx(rspamd_config, 'settings ID clash! id %s maps to %s and conflicts with %s', + numeric_id, known_ids[numeric_id].name, str) + + return nil + end + else + known_ids[numeric_id] = { + name = str, + settings = settings + } + 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] +end + +exports.settings_by_id = settings_by_id + +return exports \ No newline at end of file diff --git a/src/plugins/lua/settings.lua b/src/plugins/lua/settings.lua index 48dca67b9..da9a56553 100644 --- a/src/plugins/lua/settings.lua +++ b/src/plugins/lua/settings.lua @@ -29,6 +29,7 @@ local rspamd_util = require "rspamd_util" local rspamd_ip = require "rspamd_ip" local rspamd_regexp = require "rspamd_regexp" local lua_selectors = require "lua_selectors" +local lua_settings = require "lua_settings" local ucl = require "ucl" local fun = require "fun" @@ -36,7 +37,6 @@ local redis_params local settings = {} local N = "settings" -local settings_ids = {} local settings_initialized = false local max_pri = 0 @@ -143,10 +143,10 @@ local function check_query_settings(task) end if settings_id and settings_initialized then - -- settings_id is rspamd text, so need to convert it to string for lua - local elt = settings_ids[settings_id] + local elt = lua_settings.settings_by_id(settings_id) if elt then + elt = elt.settings if elt['whitelist'] then elt['apply'] = {whitelist = true} end @@ -746,14 +746,6 @@ local function process_settings_table(tbl) if not elt.id then elt.id = name end - if elt['id'] then - local cr = require "rspamd_cryptobox_hash" - out.id = rspamd_util.unpack("I4", - cr.create_specific('xxh64'):update(elt.id):bin()) - lua_util.debugm(N, rspamd_config, 'added settings id to "%s": %s -> %s', - name, elt.id, out.id) - settings_ids[out.id] = out - end if elt['apply'] then -- Just insert all metric results to the action key @@ -765,6 +757,12 @@ 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) + end + return out end @@ -781,7 +779,7 @@ local function process_settings_table(tbl) -- clear all settings max_pri = 0 local nrules = 0 - settings_ids = {} + lua_settings.reset_ids() for k in pairs(settings) do settings[k]={} end -- fill new settings by priority fun.for_each(function(k, v) -- 2.39.5