]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: mailers: warn if mailers are configured but not actually used
authorAurelien DARRAGON <adarragon@haproxy.com>
Fri, 27 Jun 2025 14:28:48 +0000 (16:28 +0200)
committerAurelien DARRAGON <adarragon@haproxy.com>
Fri, 27 Jun 2025 14:41:18 +0000 (16:41 +0200)
Now that native mailers configuration is only usable with Lua mailers,
Willy noticed that we lack a way to warn the user if mailers were
previously configured on an older version but Lua mailers were not loaded,
which could trick the user into thinking mailers keep working when
transitionning to 3.2 while it is not.

In this patch we add the 'core.use_native_mailers_config()' Lua function
which should be called in Lua script body before making use of
'Proxy:get_mailers()' function to retrieve legacy mailers configuration
from haproxy main config. This way haproxy effectively knows that the
native mailers config is actually being used from Lua (which indicates
user correctly migrated from native mailers to Lua mailers), else if
mailers are configured but not used from Lua then haproxy warns the user
about the fact that they will be ignored unless they are used from Lua.
(e.g.: using the provided 'examples/lua/mailers.lua' to ease transition)

doc/lua-api/index.rst
examples/lua/mailers.lua
include/haproxy/mailers.h
src/hlua.c
src/mailers.c

index 7f96274522b7e15b9b559bf81232b603ae303c1e..a4c32a41b1a5328157f8325bfc7f307f0908b6c8 100644 (file)
@@ -1089,6 +1089,14 @@ Core class
     perform the heavy job in a dedicated task and allow remaining events to be
     processed more quickly.
 
+.. js:function:: core.use_native_mailers_config()
+
+  **context**: body
+
+  Inform haproxy that the script will make use of the native "mailers"
+  config section (although legacy). In other words, inform haproxy that
+  :js:func:`Proxy.get_mailers()` will be used later in the program.
+
 .. _proxy_class:
 
 Proxy class
@@ -1216,8 +1224,14 @@ Proxy class
 
   **LEGACY**
 
-  Returns a table containing mailers config for the current proxy or nil
-  if mailers are not available for the proxy.
+  Returns a table containing legacy mailers config (from haproxy configuration
+  file) for the current proxy or nil if mailers are not available for the proxy.
+
+  .. warning::
+    When relying on :js:func:`Proxy.get_mailers()` to retrieve mailers
+    configuration, :js:func:`core.use_native_mailers_config()` must be called
+    first from body or init context to inform haproxy that Lua makes use of the
+    legacy mailers config.
 
   :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
    proxy.
index b1201fffd58308c5e46362a681c793781cf6a9e0..6f1620cade67bbabe673296a885bcdca65c2600a 100644 (file)
@@ -364,6 +364,10 @@ local function srv_event_add(event, data)
        mailers_track_server_events(data.reference)
 end
 
+-- tell haproxy that we do use the legacy native "mailers" config section
+-- which allows us to retrieve mailers configuration using Proxy:get_mailers()
+core.use_native_mailers_config()
+
 -- event subscriptions are purposely performed in an init function to prevent
 -- email alerts from being generated too early (when process is starting up)
 core.register_init(function()
index 509224ac6fa591f4e7cd2c530f2ffe43efcbc901..ea2c2d8aff5ff10666cf120a7a723e37eca4c7cd 100644 (file)
@@ -31,6 +31,7 @@
 #include <haproxy/proxy-t.h>
 #include <haproxy/server-t.h>
 
+extern int mailers_used_from_lua;
 extern struct mailers *mailers;
 
 int init_email_alert(struct mailers *mailers, struct proxy *p, char **err);
index 25df956907063ed4a349d9a667d9ac8fbed65374..d4734e0af22783451294dc3d2911b3a560e475da 100644 (file)
@@ -86,6 +86,8 @@ static uint hlua_log_opts = HLUA_LOG_LOGGERS_ON | HLUA_LOG_STDERR_AUTO;
 
 /* set to 1 once some lua was loaded already */
 static uint8_t hlua_loaded = 0;
+/* set to 1 during body evaluation (very early lua file loading), then 0 */
+static uint8_t hlua_body = 1;
 
 #define HLUA_BOOL_SAMPLE_CONVERSION_UNK     0x0
 #define HLUA_BOOL_SAMPLE_CONVERSION_NORMAL  0x1
@@ -2384,6 +2386,20 @@ __LJMP static int hlua_core_get_var(lua_State *L)
        return MAY_LJMP(hlua_smp2lua(L, &smp));
 }
 
+/* This function informs haproxy that native mailers config section is
+ * actually being used from Lua.
+ *
+ * It may not be used outside of body context.
+ */
+__LJMP static int hlua_use_native_mailers_config(lua_State *L)
+{
+       if (!hlua_body)
+               WILL_LJMP(luaL_error(L, "use_native_mailers_config: "
+               "not available outside of body context"));
+       mailers_used_from_lua = 1;
+       return 0;
+}
+
 /* A class is a lot of memory that contain data. This data can be a table,
  * an integer or user data. This data is associated with a metatable. This
  * metatable have an original version registered in the global context with
@@ -13811,6 +13827,8 @@ int hlua_post_init()
        struct hlua_function *fcn;
        struct hlua_reg_filter *reg_flt;
 
+       hlua_body = 0;
+
 #if defined(USE_OPENSSL)
        /* Initialize SSL server. */
        if (socket_ssl->xprt->prepare_srv) {
@@ -14108,6 +14126,7 @@ lua_State *hlua_init_state(int thread_num)
        hlua_class_function(L, "Warning", hlua_log_warning);
        hlua_class_function(L, "Alert", hlua_log_alert);
        hlua_class_function(L, "done", hlua_done);
+       hlua_class_function(L, "use_native_mailers_config", hlua_use_native_mailers_config);
        hlua_fcn_reg_core_fcn(L);
 
        lua_setglobal(L, "core");
index 7d3fe38391fd7758825c82dcef8d8efefbe59621..451b1e2ee9ac23db7a1e5ceba6d24c3b07dc53ce 100644 (file)
@@ -23,6 +23,7 @@
 #include <haproxy/time.h>
 #include <haproxy/tools.h>
 
+int mailers_used_from_lua = 0;
 
 struct mailers *mailers = NULL;
 
@@ -48,3 +49,19 @@ void free_email_alert(struct proxy *p)
        ha_free(&p->email_alert.to);
        ha_free(&p->email_alert.myhostname);
 }
+
+static int mailers_post_check(void)
+{
+       struct mailers *cur;
+
+       for (cur = mailers; cur != NULL; cur = cur->next) {
+               if (cur->users && !mailers_used_from_lua) {
+                       ha_warning("mailers '%s' is referenced on at least one proxy but Lua "
+                                  "mailers are not configured so the setting will be ignored. "
+                                  "Use 'examples/lua/mailers.lua' file for basic mailers support.\n", cur->id);
+                       return ERR_WARN;
+               }
+       }
+       return ERR_NONE;
+}
+REGISTER_POST_CHECK(mailers_post_check);