25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-26 16:26:24 +01:00

Try to limit serial access when checking api permissions

This commit is contained in:
Paweł Chmielowski 2021-06-24 15:15:13 +02:00
parent 774de2bdc5
commit 795addca7d

View File

@ -46,6 +46,7 @@
code_change/3]). code_change/3]).
-define(SERVER, ?MODULE). -define(SERVER, ?MODULE).
-define(CACHE_TAB, access_permissions_cache).
-record(state, -record(state,
{definitions = none :: none | [definition()]}). {definitions = none :: none | [definition()]}).
@ -71,35 +72,10 @@
%%%=================================================================== %%%===================================================================
-spec can_access(atom(), caller_info()) -> allow | deny. -spec can_access(atom(), caller_info()) -> allow | deny.
can_access(Cmd, CallerInfo) -> can_access(Cmd, CallerInfo) ->
gen_server:call(?MODULE, {can_access, Cmd, CallerInfo}). Defs0 = show_current_definitions(),
-spec invalidate() -> ok.
invalidate() ->
gen_server:cast(?MODULE, invalidate).
-spec show_current_definitions() -> [definition()].
show_current_definitions() ->
gen_server:call(?MODULE, show_current_definitions).
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
-spec init([]) -> {ok, state()}.
init([]) ->
ejabberd_hooks:add(config_reloaded, ?MODULE, invalidate, 90),
{ok, #state{}}.
-spec handle_call({can_access, atom(), caller_info()} |
show_current_definitions | term(),
term(), state()) -> {reply, term(), state()}.
handle_call({can_access, Cmd, CallerInfo}, _From, State) ->
CallerModule = maps:get(caller_module, CallerInfo, none), CallerModule = maps:get(caller_module, CallerInfo, none),
Host = maps:get(caller_host, CallerInfo, global), Host = maps:get(caller_host, CallerInfo, global),
Tag = maps:get(tag, CallerInfo, none), Tag = maps:get(tag, CallerInfo, none),
{State2, Defs0} = get_definitions(State),
Defs = maps:get(extra_permissions, CallerInfo, []) ++ Defs0, Defs = maps:get(extra_permissions, CallerInfo, []) ++ Defs0,
Res = lists:foldl( Res = lists:foldl(
fun({Name, _} = Def, none) -> fun({Name, _} = Def, none) ->
@ -114,14 +90,40 @@ handle_call({can_access, Cmd, CallerInfo}, _From, State) ->
(_, Val) -> (_, Val) ->
Val Val
end, none, Defs), end, none, Defs),
Res2 = case Res of case Res of
allow -> allow; allow -> allow;
_ -> _ ->
?DEBUG("Command '~p' execution denied " ?DEBUG("Command '~p' execution denied "
"(CallerInfo=~p)", [Cmd, CallerInfo]), "(CallerInfo=~p)", [Cmd, CallerInfo]),
deny deny
end, end.
{reply, Res2, State2};
-spec invalidate() -> ok.
invalidate() ->
gen_server:cast(?MODULE, invalidate),
ets_cache:delete(?CACHE_TAB, definitions).
-spec show_current_definitions() -> [definition()].
show_current_definitions() ->
ets_cache:lookup(?CACHE_TAB, definitions,
fun() ->
{cache, gen_server:call(?MODULE, show_current_definitions)}
end).
start_link() ->
ets_cache:new(?CACHE_TAB, [{max_size, 2}]),
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
-spec init([]) -> {ok, state()}.
init([]) ->
ejabberd_hooks:add(config_reloaded, ?MODULE, invalidate, 90),
ets_cache:new(access_permissions),
{ok, #state{}}.
-spec handle_call(show_current_definitions | term(),
term(), state()) -> {reply, term(), state()}.
handle_call(show_current_definitions, _From, State) -> handle_call(show_current_definitions, _From, State) ->
{State2, Defs} = get_definitions(State), {State2, Defs} = get_definitions(State),
{reply, Defs, State2}; {reply, Defs, State2};