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)
This commit is contained in:
Aurelien DARRAGON 2023-04-21 17:32:46 +02:00 committed by Christopher Faulet
parent 0bd53b2152
commit 5bed48fec8
4 changed files with 37 additions and 0 deletions

View 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

View 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, ...)

View 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");

View File

@ -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;