aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@rspamd.com>2024-11-11 18:06:06 +0600
committerGitHub <noreply@github.com>2024-11-11 18:06:06 +0600
commit77755e91c0e0876230821410e856b5203c6f7592 (patch)
treecf17c380fbce1ac0030775c2a0772eeb584ff489
parent6b056e4e6054e486fcda3aa5c5a289906af0bd4b (diff)
parentae3eb8efcdb13c4e73a92f19816adca553ac56ea (diff)
downloadrspamd-77755e91c0e0876230821410e856b5203c6f7592.tar.gz
rspamd-77755e91c0e0876230821410e856b5203c6f7592.zip
Merge pull request #5214 from rspamd/vstakhov-conf-reorg
[Conf] Add more ways to extend Rspamd configuration
-rw-r--r--CMakeLists.txt10
-rw-r--r--conf/local.d/module.conf.example20
-rw-r--r--conf/lua.local.d/module.lua.example62
-rw-r--r--conf/modules.conf3
-rw-r--r--conf/modules.local.d/module.conf.example21
-rw-r--r--conf/override.d/module.conf.example18
-rw-r--r--rules/rspamd.lua4
7 files changed, 135 insertions, 3 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6daced05a..0d07e940f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -50,6 +50,7 @@ OPTION(ENABLE_LUAJIT "Link with libluajit [default: ON]" ON)
OPTION(ENABLE_URL_INCLUDE "Enable urls in ucl includes (requires libcurl or libfetch) [default: OFF]" OFF)
OPTION(NO_SHARED "Build internal libs static [default: ON]" ON)
OPTION(INSTALL_WEBUI "Install web interface [default: ON]" ON)
+OPTION(INSTALL_EXAMPLES "Install examples of the configuration and Lua [default: ON]" ON)
OPTION(WANT_SYSTEMD_UNITS "Install systemd unit files on Linux [default: OFF]" OFF)
OPTION(ENABLE_SNOWBALL "Enable snowball stemmer [default: ON]" ON)
OPTION(ENABLE_CLANG_PLUGIN "Enable clang static analysing plugin [default: OFF]" OFF)
@@ -714,9 +715,14 @@ INSTALL(CODE "FILE(MAKE_DIRECTORY \$ENV{DESTDIR}${RULESDIR})")
LIST(LENGTH CONFFILES CONFLIST_COUNT)
MATH(EXPR CONFLIST_MAX ${CONFLIST_COUNT}-1)
+SET(GLOB_PATTERNS "${CMAKE_SOURCE_DIR}/conf/*.conf;${CMAKE_SOURCE_DIR}/conf/*.inc")
+IF(INSTALL_EXAMPLES MATCHES "ON")
+ LIST(APPEND GLOB_PATTERNS "${CMAKE_SOURCE_DIR}/conf/*.lua.example")
+ LIST(APPEND GLOB_PATTERNS "${CMAKE_SOURCE_DIR}/conf/*.conf.example")
+ENDIF()
+
FILE(GLOB_RECURSE CONF_FILES RELATIVE "${CMAKE_SOURCE_DIR}/conf" CONFIGURE_DEPENDS
- "${CMAKE_SOURCE_DIR}/conf/*.conf"
- "${CMAKE_SOURCE_DIR}/conf/*.inc")
+ ${GLOB_PATTERNS})
FOREACH (CONF_FILE ${CONF_FILES})
GET_FILENAME_COMPONENT(_rp ${CONF_FILE} PATH)
INSTALL(CODE "FILE(MAKE_DIRECTORY \$ENV{DESTDIR}${CONFDIR}/${_rp})")
diff --git a/conf/local.d/module.conf.example b/conf/local.d/module.conf.example
new file mode 100644
index 000000000..ba3d0b7bb
--- /dev/null
+++ b/conf/local.d/module.conf.example
@@ -0,0 +1,20 @@
+# Define local parameters that you need to merge with the main configuration from
+# either modules.d (for the modules shipped with Rspamd) or modules.local.d (for
+# the modules that you have written).
+#
+# !!! Ensure NOT to enclose your configuration in the outer block with the module
+
+# DON'T DO THIS:
+# module_name { <--- Don't do this
+# param = "other_value";
+# }
+
+# Just define your parameters here:
+
+param = "other_value";
+
+# Merge with the default complex param
+complex_param {
+ key = "new_value";
+}
+
diff --git a/conf/lua.local.d/module.lua.example b/conf/lua.local.d/module.lua.example
new file mode 100644
index 000000000..336db6c49
--- /dev/null
+++ b/conf/lua.local.d/module.lua.example
@@ -0,0 +1,62 @@
+-- This directory is used to define user specific rules and plugins for Rspamd in Lua
+-- Each *.lua file is executed and added to the Rspamd
+
+-- Example of regexp rule:
+
+local reconf = config['regexp'] -- Create alias for regexp configs
+
+local re1 = 'From=/foo@/H' -- Mind local here
+local re2 = '/blah/P'
+
+reconf['SYMBOL'] = {
+ re = string.format('(%s) && !(%s)', re1, re2), -- use string.format to create expression
+ score = 1.2,
+ description = 'some description',
+
+ condition = function(task)
+ -- run this rule only if some condition is satisfied
+ return true
+ end,
+}
+
+-- Example of a simple lua rule:
+rspamd_config.SYMBOL = {
+ callback = function(task)
+ return true
+ end,
+ score = 1.2,
+ description = 'some description',
+
+ condition = function(task)
+ -- run this rule only if some condition is satisfied
+ return true
+ end,
+}
+
+-- Example of a plugin with configuration:
+local redis_params
+local lua_redis = require "lua_redis"
+
+local function symbol_cb(task)
+ local function redis_set_cb(err)
+ if err ~= nil then
+ rspamd_logger.errx(task, 'redis_set_cb received error: %1', err)
+ end
+ end
+ -- Create hash of message-id and store to redis
+ local key = make_key(task)
+ local ret = lua_redis.redis_make_request(task,
+ redis_params, -- connect params
+ key, -- hash key
+ true, -- is write
+ redis_set_cb, --callback
+ 'SETEX', -- command
+ { key, tostring(settings['expire']), "1" } -- arguments
+ )
+end
+
+-- Load redis server for module named 'module'
+redis_params = lua_redis.parse_redis_server('module')
+if redis_params then
+ -- Register symbol
+end
diff --git a/conf/modules.conf b/conf/modules.conf
index b37da06d5..51f1f420e 100644
--- a/conf/modules.conf
+++ b/conf/modules.conf
@@ -14,4 +14,5 @@
#
# See https://rspamd.com/doc/tutorials/writing_rules.html for details
-.include(glob=true) "${CONFDIR}/modules.d/*.conf" \ No newline at end of file
+.include(glob=true) "${CONFDIR}/modules.d/*.conf"
+.include(try=true, glob=true) "${CONFDIR}/modules.local.d/*.conf" \ No newline at end of file
diff --git a/conf/modules.local.d/module.conf.example b/conf/modules.local.d/module.conf.example
new file mode 100644
index 000000000..9e6ba20e5
--- /dev/null
+++ b/conf/modules.local.d/module.conf.example
@@ -0,0 +1,21 @@
+# This directory is scanned for *.conf files to define configuration for the
+# modules that are not shipped by Rspamd by default.
+# So you can add your own configuration for the modules you have written.
+# Please bear in mind, that each file *MUST* have the outer block with
+# the actual module name, e.g.:
+
+module_name {
+ param = "value";
+ complex_param = {
+ key = "value";
+ other_key = "other value";
+ flag = false;
+ };
+ limit = 10;
+ duration = 10d;
+
+ # If you want to redefine something alternatively
+ .include(try=true,priority=5) "${DBDIR}/dynamic/module_name.conf"
+ .include(try=true,priority=1,duplicate=merge) "$LOCAL_CONFDIR/local.d/module_name.conf"
+ .include(try=true,priority=10) "$LOCAL_CONFDIR/override.d/module_name.conf"
+} \ No newline at end of file
diff --git a/conf/override.d/module.conf.example b/conf/override.d/module.conf.example
new file mode 100644
index 000000000..2028a94b9
--- /dev/null
+++ b/conf/override.d/module.conf.example
@@ -0,0 +1,18 @@
+# Define local parameters that you need to override the main configuration from
+# either modules.d (for the modules shipped with Rspamd) or modules.local.d (for
+# the modules that you have written).
+#
+# !!! Ensure NOT to enclose your configuration in the outer block with the module
+
+# DON'T DO THIS:
+# module_name { <--- Don't do this
+# complex_param = null;
+# }
+
+# Just define your parameters here:
+
+# Redefine the whole complex param instead of merging like in `local.d`
+# All other keys in `complex_param` will be removed
+complex_param {
+ key = "new_value";
+} \ No newline at end of file
diff --git a/rules/rspamd.lua b/rules/rspamd.lua
index dcc872d15..e41a946cb 100644
--- a/rules/rspamd.lua
+++ b/rules/rspamd.lua
@@ -51,6 +51,10 @@ else
end
end
+for _, local_lua in ipairs(rspamd_util.glob(local_conf .. '/lua.local.d/*.lua') or {}) do
+ dofile(local_lua)
+end
+
if rspamd_util.file_exists(local_conf .. '/local.d/rspamd.lua') then
dofile(local_conf .. '/local.d/rspamd.lua')
end