From 5bed48fec8aa99ee6e1610ad99a3e57f7a8346f2 Mon Sep 17 00:00:00 2001 From: Aurelien DARRAGON Date: Fri, 21 Apr 2023 17:32:46 +0200 Subject: [PATCH] MINOR: mailers/hlua: disable email sending from lua 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 | 11 +++++++++++ include/haproxy/mailers.h | 1 + src/hlua.c | 17 +++++++++++++++++ src/mailers.c | 8 ++++++++ 4 files changed, 37 insertions(+) diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst index d790c6b6d..92c2904c6 100644 --- a/doc/lua-api/index.rst +++ b/doc/lua-api/index.rst @@ -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 diff --git a/include/haproxy/mailers.h b/include/haproxy/mailers.h index 43db167b6..89aa1b092 100644 --- a/include/haproxy/mailers.h +++ b/include/haproxy/mailers.h @@ -32,6 +32,7 @@ #include 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, ...) diff --git a/src/hlua.c b/src/hlua.c index f8dc9bb02..d05e9cd60 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -67,6 +67,7 @@ #include #include #include +#include /* 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"); diff --git a/src/mailers.c b/src/mailers.c index 601e8b927..c09e73c35 100644 --- a/src/mailers.c +++ b/src/mailers.c @@ -31,6 +31,11 @@ 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;