Remove shell script options (alert is not provided as stdin)

This commit is contained in:
Alfredo Cardigliano 2021-03-24 10:49:45 +01:00
parent 129749211e
commit aaf44b3edd
2 changed files with 8 additions and 59 deletions

View file

@ -23,7 +23,7 @@ local shell = {
template_name = "shell_endpoint.template"
},
recipient_params = {
{ param_name = "shell_options" },
{},
},
recipient_template = {
plugin_key = endpoint_key,
@ -48,7 +48,6 @@ end
local function recipient2sendMessageSettings(recipient)
local settings = {
path = recipient.endpoint_conf.shell_script,
options = recipient.recipient_params.shell_options,
}
return settings
@ -66,51 +65,6 @@ end
-- ##############################################
function runScriptWithArgs(cmd, myalert)
local use_args = false
alert = myalert -- Not sure why we need a non-local variable
if(do_debug) then
-- tprint(myalert)
tprint("[Before] "..cmd)
end
-- Check if the user configured arguments to be mapped to alert fields
-- Search all alert.* strings
for word in string.gmatch(cmd, 'alert.[^,%s]+') do
local arg_val = ""
use_args = true
local field_name = string.gsub(word, 'alert%.', '')
if field_name then
local field_value = myalert[field_name]
if field_value then
arg_val = field_value
end
end
cmd = cmd:gsub(word, '"'..arg_val..'"')
end
-- Mask output
local full_cmd = cmd.." > /dev/null"
if use_args then
-- Running script with expanded args
os.execute(full_cmd)
else
-- Running script with the alert (json) as input (stdin)
sys_utils.execShellCmd(full_cmd, json.encode(myalert))
end
if(do_debug) then tprint("[After] "..cmd) end
return(cmd)
end
-- ##############################################
function shell.runScript(alerts, settings)
local where = { "/usr/share/ntopng/scripts/shell/", dirs.installdir.."/scripts/shell/" }
local fullpath = nil
@ -134,9 +88,13 @@ function shell.runScript(alerts, settings)
for key, alert in ipairs(alerts) do
-- Executing the script
local exec_script = fullpath .. " " .. settings.options
local exec_script = fullpath
exec_script = runScriptWithArgs(exec_script, alert)
-- Mask output
local cmd = exec_script .. " > /dev/null"
-- Running script with the alert (json) as input (stdin)
sys_utils.execShellCmd(cmd, json.encode(alert))
-- Storing an alert-notice in regard of the shell script execution
-- for security reasons