File

tools/modtrace.lua @ 12642:9061f9621330

Switch to a new role-based authorization framework, removing is_admin() We began moving away from simple "is this user an admin?" permission checks before 0.12, with the introduction of mod_authz_internal and the ability to dynamically change the roles of individual users. The approach in 0.12 still had various limitations however, and apart from the introduction of roles other than "admin" and the ability to pull that info from storage, not much actually changed. This new framework shakes things up a lot, though aims to maintain the same functionality and behaviour on the surface for a default Prosody configuration. That is, if you don't take advantage of any of the new features, you shouldn't notice any change. The biggest change visible to developers is that usermanager.is_admin() (and the auth provider is_admin() method) have been removed. Gone. Completely. Permission checks should now be performed using a new module API method: module:may(action_name, context) This method accepts an action name, followed by either a JID (string) or (preferably) a table containing 'origin'/'session' and 'stanza' fields (e.g. the standard object passed to most events). It will return true if the action should be permitted, or false/nil otherwise. Modules should no longer perform permission checks based on the role name. E.g. a lot of code previously checked if the user's role was prosody:admin before permitting some action. Since many roles might now exist with similar permissions, and the permissions of prosody:admin may be redefined dynamically, it is no longer suitable to use this method for permission checks. Use module:may(). If you start an action name with ':' (recommended) then the current module's name will automatically be used as a prefix. To define a new permission, use the new module API: module:default_permission(role_name, action_name) module:default_permissions(role_name, { action_name[, action_name...] }) This grants the specified role permission to execute the named action(s) by default. This may be overridden via other mechanisms external to your module. The built-in roles that developers should use are: - prosody:user (normal user) - prosody:admin (host admin) - prosody:operator (global admin) The new prosody:operator role is intended for server-wide actions (such as shutting down Prosody). Finally, all usage of is_admin() in modules has been fixed by this commit. Some of these changes were trickier than others, but no change is expected to break existing deployments. EXCEPT: mod_auth_ldap no longer supports the ldap_admin_filter option. It's very possible nobody is using this, but if someone is then we can later update it to pull roles from LDAP somehow.
author Matthew Wild <mwild1@gmail.com>
date Wed, 15 Jun 2022 12:15:01 +0100
parent 12590:5eaf77114fdb
line wrap: on
line source

-- Trace module calls and method calls on created objects
--
-- Very rough and for debugging purposes only. It makes many
-- assumptions and there are many ways it could fail.
--
-- Example use:
--
--   local dbuffer = require "tools.modtrace".trace("util.dbuffer");
--

local t_pack = table.pack;
local serialize = require "util.serialization".serialize;
local unpack = table.unpack;
local set = require "util.set";

local serialize_cfg = {
	preset = "oneline";
	freeze = true;
	fatal = false;
	fallback = function (v) return "<"..tostring(v)..">" end;
};

local function stringify_value(v)
	if type(v) == "string" and #v > 20 then
		return ("<string(%d)>"):format(#v);
	elseif type(v) == "function" then
		return tostring(v);
	end
	return serialize(v, serialize_cfg);
end

local function stringify_params(...)
	local n = select("#", ...);
	local r = {};
	for i = 1, n do
		table.insert(r, stringify_value((select(i, ...))));
	end
	return table.concat(r, ", ");
end

local function stringify_result(ret)
	local r = {};
	for i = 1, ret.n do
		table.insert(r, stringify_value(ret[i]));
	end
	return table.concat(r, ", ");
end

local function stringify_call(method_name, ...)
	return ("%s(%s)"):format(method_name, stringify_params(...));
end

local function wrap_method(original_obj, original_method, method_name)
	method_name = ("<%s>:%s"):format(getmetatable(original_obj).__name or "object", method_name);
	return function (new_obj_self, ...)
		local opts = new_obj_self._modtrace_opts;
		local f = opts.output or io.stderr;
		f:write(stringify_call(method_name, ...));
		local ret = t_pack(original_method(original_obj, ...));
		if ret.n > 0 then
			f:write(" = ", stringify_result(ret), "\n");
		else
			f:write("\n");
		end
		return unpack(ret, 1, ret.n);
	end;
end

local function wrap_function(original_function, function_name, opts)
	local f = opts.output or io.stderr;
	return function (...)
		f:write(stringify_call(function_name, ...));
		local ret = t_pack(original_function(...));
		if ret.n > 0 then
			f:write(" = ", stringify_result(ret), "\n");
		else
			f:write("\n");
		end
		return unpack(ret, 1, ret.n);
	end;
end

local function wrap_metamethod(name, method)
	if name == "__index" then
		return function (new_obj, k)
			local original_method;
			if type(method) == "table" then
				original_method = new_obj._modtrace_original_obj[k];
			else
				original_method = method(new_obj._modtrace_original_obj, k);
			end
			if original_method == nil then
				return nil;
			end
			return wrap_method(new_obj._modtrace_original_obj, original_method, k);
		end;
	end
	return function (new_obj, ...)
		return method(new_obj._modtrace_original_obj, ...);
	end;
end

local function wrap_mt(original_mt)
	local new_mt = {};
	for k, v in pairs(original_mt) do
		new_mt[k] = wrap_metamethod(k, v);
	end
	return new_mt;
end

local function wrap_obj(original_obj, opts)
	local new_mt = wrap_mt(getmetatable(original_obj));
	return setmetatable({_modtrace_original_obj = original_obj, _modtrace_opts = opts}, new_mt);
end

local function wrap_new(original_new, function_name, opts)
	local f = opts.output or io.stderr;
	return function (...)
		f:write(stringify_call(function_name, ...));
		local ret = t_pack(original_new(...));
		local obj = ret[1];

		if ret.n == 1 and type(ret[1]) == "table" then
			f:write(" = <", getmetatable(ret[1]).__name or "object", ">", "\n");
		elseif ret.n > 0 then
			f:write(" = ", stringify_result(ret), "\n");
		else
			f:write("\n");
		end

		if obj then
			ret[1] = wrap_obj(obj, opts);
		end
		return unpack(ret, 1, ret.n);
	end;
end

local function trace(module, opts)
	if type(module) == "string" then
		module = require(module);
	end
	opts = opts or {};
	local new_methods = set.new(opts.new_methods or {"new"});
	local fake_module = setmetatable({}, {
		__index = function (_, k)
			if new_methods:contains(k) then
				return wrap_new(module[k], k, opts);
			else
				return wrap_function(module[k], k, opts);
			end
		end;
	});
	return fake_module;
end

return {
	wrap = trace;
	trace = trace;
}