Skip to content

Commit

Permalink
Fix: Refactor monitoring filter to streamline service checks.
Browse files Browse the repository at this point in the history
Introduced new helper functions `get_service` and `get_dovecot_session` to improve code readability and modularity. Updated logic to include explicit service validation using HTTP headers. Removed redundant protocol checks for a cleaner and more maintainable implementation.

Signed-off-by: Christian Roessner <[email protected]>
  • Loading branch information
Christian Roessner committed Dec 11, 2024
1 parent e7afe2a commit 817b90d
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions server/lua-plugins.d/filters/monitoring.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,44 @@
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <https://www.gnu.org/licenses/>.

local nauthilus_util = require("nauthilus_util")

dynamic_loader("nauthilus_http_request")
local nauthilus_http_request = require("nauthilus_http_request")

dynamic_loader("nauthilus_backend")
local nauthilus_backend = require("nauthilus_backend")

local N = "director"

local WANTED_PROTOCOLS = {
imap = true,
imapa = true,
pop3 = true,
pop3s = true,
lmtp = true,
lmtps = true,
sieve = true,
}
local function get_service()
local header = nauthilus_http_request.get_http_request_header("X-Nauthilus-Service")
if nauthilus_util.table_length(header) == 1 then
return header[1]
end

return nil
end

local function get_dovecot_session()
local header = nauthilus_http_request.get_http_request_header("X-Dovecot-Session")
if nauthilus_util.table_length(header) == 1 then
return header[1]
end

return nil
end

function nauthilus_call_filter(request)
if not request.authenticated then
return nauthilus_builtin.FILTER_REJECT, nauthilus_builtin.FILTER_RESULT_OK
end

if not WANTED_PROTOCOLS[request.protocol] then
local service = get_service()
if service ~= "Dovecot" then
return nauthilus_builtin.FILTER_ACCEPT, nauthilus_builtin.FILTER_RESULT_OK
end

local nauthilus_util = require("nauthilus_util")

dynamic_loader("nauthilus_http_request")
local nauthilus_http_request = require("nauthilus_http_request")

local function get_dovecot_session()
local header = nauthilus_http_request.get_http_request_header("X-Dovecot-Session")
if nauthilus_util.table_length(header) == 1 then
return header[1]
end

return nil
end

dynamic_loader("nauthilus_redis")
local nauthilus_redis = require("nauthilus_redis")
local redis_key = "ntc:DS:" .. request.account
Expand Down

0 comments on commit 817b90d

Please sign in to comment.