]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: mailers/hlua: disable email sending from lua
authorAurelien DARRAGON <adarragon@haproxy.com>
Fri, 21 Apr 2023 15:32:46 +0000 (17:32 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 5 May 2023 14:28:32 +0000 (16:28 +0200)
Exposing a new hlua function, available from body or init contexts, that
forcefully disables the sending of email alerts even if the mailers are
defined in haproxy configuration.

This will help for sending email directly from lua.
(prevent legacy email sending from intefering with lua)

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

index d790c6b6d990357c83907f027c89041b4788cd77..92c2904c68d2751e1eb10918af7dc28fe0b351ab 100644 (file)
@@ -1010,6 +1010,17 @@ Core class
     perform the heavy job in a dedicated task and allow remaining events to be
     processed more quickly.
 
+.. js:function:: core.disable_legacy_mailers()
+
+  **LEGACY**
+
+  **context**: body, init
+
+  Disable the sending of email alerts through the legacy email sending
+  function when mailers are used in the configuration.
+
+  Use this when sending email alerts directly from lua.
+
 .. _proxy_class:
 
 Proxy class
index 43db167b6f11818417e9e7fcfc2ce792d09193db..89aa1b0920ede73dbbcc7814d4dbdbf8032da650 100644 (file)
@@ -32,6 +32,7 @@
 #include <haproxy/server-t.h>
 
 extern struct mailers *mailers;
+extern int send_email_disabled;
 
 int init_email_alert(struct mailers *mailers, struct proxy *p, char **err);
 void send_email_alert(struct server *s, int priority, const char *format, ...)
index f8dc9bb023a80187ec550c97ce41a039f8adbfa7..d05e9cd6068b8af5955d4c969b51fa2d02215a4b 100644 (file)
@@ -67,6 +67,7 @@
 #include <haproxy/xref.h>
 #include <haproxy/event_hdl.h>
 #include <haproxy/check.h>
+#include <haproxy/mailers.h>
 
 /* Lua uses longjmp to perform yield or throwing errors. This
  * macro is used only for identifying the function that can
@@ -1930,6 +1931,21 @@ static int hlua_set_map(lua_State *L)
        return 0;
 }
 
+/* This function disables the sending of email through the
+ * legacy email sending function which is implemented using
+ * checks.
+ *
+ * It may not be used during runtime.
+ */
+__LJMP static int hlua_disable_legacy_mailers(lua_State *L)
+{
+       if (hlua_gethlua(L))
+               WILL_LJMP(luaL_error(L, "disable_legacy_mailers: "
+                                       "not available outside of init or body context"));
+       send_email_disabled = 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
@@ -13141,6 +13157,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, "disable_legacy_mailers", hlua_disable_legacy_mailers);
        hlua_fcn_reg_core_fcn(L);
 
        lua_setglobal(L, "core");
index 601e8b927bbecbe0784b6384e06e755e5248826a..c09e73c3507746832e6fcf957216a269e027245d 100644 (file)
 
 struct mailers *mailers = NULL;
 
+/* Set to 1 to disable email sending through checks even if the
+ * mailers are configured to do so. (e.g.: disable from lua)
+ */
+int send_email_disabled = 0;
+
 DECLARE_STATIC_POOL(pool_head_email_alert,   "email_alert",   sizeof(struct email_alert));
 
 /****************************** Email alerts ******************************/
@@ -305,6 +310,9 @@ void send_email_alert(struct server *s, int level, const char *format, ...)
        int len;
        struct proxy *p = s->proxy;
 
+       if (send_email_disabled)
+               return;
+
        if (!p->email_alert.mailers.m || level > p->email_alert.level || format == NULL)
                return;