diff --git a/scripts/locales/en.lua b/scripts/locales/en.lua index f2e9883a0c..90f44a1444 100644 --- a/scripts/locales/en.lua +++ b/scripts/locales/en.lua @@ -480,6 +480,10 @@ local lang = { ["conf_backup"] = "Backup Configuration", ["conf_restore"] = "Restore Configuration", }, + ["configsets"] = { + ["error_exists"] = "The configuration name \"%{name}\" is already in use", + ["unknown_id"] = "No configuration found with id %{confid}", + }, ["contacts_page"] = { ["client_address"] = "Client Address", ["client_contacts_initiator"] = "Client Contacts (Initiator)", diff --git a/scripts/lua/edit_scripts_configsets.lua b/scripts/lua/edit_scripts_configsets.lua new file mode 100644 index 0000000000..0d089eb7e2 --- /dev/null +++ b/scripts/lua/edit_scripts_configsets.lua @@ -0,0 +1,86 @@ +-- +-- (C) 2019 - ntop.org +-- + +local dirs = ntop.getDirs() +package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path + +require "lua_utils" +local json = require("dkjson") +local user_scripts = require("user_scripts") + +local action = _GET["action"] + +sendHTTPContentTypeHeader('application/json') + +if(action == nil) then + traceError(TRACE_ERROR, TRACE_CONSOLE, "Missing 'action' parameter") + return +end + +-- ################################################ + +local result = {} + +if(action == "add") then + local name = _GET["confset_name"] + + if(name == nil) then + traceError(TRACE_ERROR, TRACE_CONSOLE, "Missing 'confset_name' parameter") + return + end + + local confset, err = user_scripts.newConfigset(name) + + if(confset == nil) then + result.error = err + end +else + local confid = _GET["confset_id"] + + if(confid == nil) then + traceError(TRACE_ERROR, TRACE_CONSOLE, "Missing 'confset_id' parameter") + return + end + + if(action == "delete") then + local success, err = user_scripts.deleteConfigset(confid) + + if not success then + result.error = err + end + elseif(action == "rename") then + local new_name = _GET["confset_name"] + + if(new_name == nil) then + traceError(TRACE_ERROR, TRACE_CONSOLE, "Missing 'newname' parameter") + return + end + + local success, err = user_scripts.renameConfigset(confid, new_name) + + if not success then + result.error = err + end + elseif(action == "clone") then + local new_name = _GET["confset_name"] + + if(new_name == nil) then + traceError(TRACE_ERROR, TRACE_CONSOLE, "Missing 'confset_name' parameter") + return + end + + local success, err = user_scripts.cloneConfigset(confid, new_name) + + if not success then + result.error = err + end + else + traceError(TRACE_ERROR, TRACE_CONSOLE, "Unknown action '".. action .. "'") + return + end +end + +-- ################################################ + +print(json.encode(result)) diff --git a/scripts/lua/get_scripts_configsets.lua b/scripts/lua/get_scripts_configsets.lua new file mode 100644 index 0000000000..cbd2158921 --- /dev/null +++ b/scripts/lua/get_scripts_configsets.lua @@ -0,0 +1,16 @@ +-- +-- (C) 2019 - ntop.org +-- + +local dirs = ntop.getDirs() +package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path + +require "lua_utils" +local json = require("dkjson") +local user_scripts = require("user_scripts") + +local rv = user_scripts.getConfigsets() + +sendHTTPContentTypeHeader('application/json') + +print(json.encode(rv)) diff --git a/scripts/lua/modules/http_lint.lua b/scripts/lua/modules/http_lint.lua index c5a1c3b5b0..a4d50d6dc6 100644 --- a/scripts/lua/modules/http_lint.lua +++ b/scripts/lua/modules/http_lint.lua @@ -1115,6 +1115,10 @@ local known_parameters = { -- NAVIGATION ["page"] = validateSingleWord, -- Currently active subpage tab ["tab"] = validateSingleWord, -- Currently active tab, handled by javascript + +-- CONFIGSETS + ["confset_id"] = validateNumber, + ["confset_name"] = validateUnquoted, -- OTHER ["_"] = validateEmptyOr(validateNumber), -- jQuery nonce in ajax requests used to prevent browser caching diff --git a/scripts/lua/modules/user_scripts.lua b/scripts/lua/modules/user_scripts.lua index dbe946dd60..6fcb153f6e 100644 --- a/scripts/lua/modules/user_scripts.lua +++ b/scripts/lua/modules/user_scripts.lua @@ -30,6 +30,7 @@ user_scripts.field_units = { local CALLBACKS_DIR = plugins_utils.PLUGINS_RUNTIME_PATH .. "/callbacks" local NON_TRAFFIC_ELEMENT_CONF_KEY = "all" local NON_TRAFFIC_ELEMENT_ENTITY = "no_entity" +local CONFIGSETS_KEY = "ntopng.prefs.user_scripts.configsets" -- Hook points for flow/periodic modules -- NOTE: keep in sync with the Documentation @@ -956,4 +957,135 @@ end -- ############################################## +local function findConfigSet(configsets, name) + for id, configset in pairs(configsets) do + if(configset.name == name) then + return(configset) + end + end + + return(nil) +end + +-- ############################################## + +local function getNewConfigSetId(configsets) + local max_id = -1 + + for i in pairs(configsets) do + max_id = math.max(max_id, tonumber(i)) + end + + return(max_id+1) +end + +-- ############################################## + +local function saveConfigsets(configsets) + local rv = json.encode(configsets) + ntop.setPref(CONFIGSETS_KEY, rv) +end + +-- ############################################## + +function user_scripts.getConfigsets() + local configsets = ntop.getPref(CONFIGSETS_KEY) or "" + + configsets = json.decode(configsets) or {} + + return(configsets) +end + +-- ############################################## + +-- @brief Creates a new configset. +-- @params name the unique config set name +-- @returns a tuple . If configset is nil, errmsg +-- will contain a localized error describing the problem. +function user_scripts.newConfigset(name) + local configsets = user_scripts.getConfigsets() + local existing = findConfigSet(configsets, name) + + if existing then + return nil, i18n("configsets.error_exists", {name=name}) + end + + local confid = getNewConfigSetId(configsets) + + local newconfset = { + id = confid, + name = name, + config = {}, + } + + configsets[confid] = newconfset + saveConfigsets(configsets) + + return newconfset +end + +-- ############################################## + +function user_scripts.deleteConfigset(confid) + local configsets = user_scripts.getConfigsets() + + if(configsets[confid] == nil) then + return false, i18n("configsets.unknown_id", {confid=confid}) + end + + configsets[confid] = nil + saveConfigsets(configsets) + + return true +end + +-- ############################################## + +function user_scripts.renameConfigset(confid, new_name) + local configsets = user_scripts.getConfigsets() + + if(configsets[confid] == nil) then + return false, i18n("configsets.unknown_id", {confid=confid}) + end + + local existing = findConfigSet(configsets, new_name) + + if existing then + return false, i18n("configsets.error_exists", {name=new_name}) + end + + configsets[confid].name = new_name + saveConfigsets(configsets) + + return true +end + +-- ############################################## + +function user_scripts.cloneConfigset(confid, new_name) + local configsets = user_scripts.getConfigsets() + + if(configsets[confid] == nil) then + return false, i18n("configsets.unknown_id", {confid=confid}) + end + + local existing = findConfigSet(configsets, new_name) + + if existing then + return false, i18n("configsets.error_exists", {name=new_name}) + end + + local new_confid = getNewConfigSetId(configsets) + + configsets[new_confid] = table.clone(configsets[confid]) + configsets[new_confid].id = new_confid + configsets[new_confid].name = new_name + + saveConfigsets(configsets) + + return true +end + +-- ############################################## + return(user_scripts)