Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async imem server calls #279

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/imem_sec.erl
Original file line number Diff line number Diff line change
Expand Up @@ -919,15 +919,21 @@ have_module_permission(SKey, Module, Operation) ->

%% ------- local private security extension for sql and tables (do not export!!) ------------

seco_authorized(SKey) ->
seco_authorized(SKey) ->
case imem_meta:read(ddSeCo@, SKey) of
[#ddSeCo{pid=Pid, authState=authorized} = SeCo] when Pid == self() ->
[#ddSeCo{pid=Pid, authState=authorized} = SeCo] when Pid == self() ->
SeCo;
[#ddSeCo{pid=Pid}] ->
?SecurityViolation({"Not logged in", {SKey,Pid}});
[] ->
[#ddSeCo{pid=Pid, authState=authorized} = SeCo] ->
{links, Links} = erlang:process_info(self(), links),
case lists:member(Pid, Links) of
true ->
SeCo;
false ->
?SecurityViolation({"Not logged in", SKey})
end;
[] ->
?SecurityException({"Not logged in", SKey})
end.
end.

% have_table_ownership(SKey, {Schema,Table,_Alias}) ->
% have_table_ownership(SKey, {Schema,Table});
Expand Down
22 changes: 18 additions & 4 deletions src/imem_seco.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
, has_role/2
, has_permission/3
, has_permission/2
, get_skey_pid/1
]).

-export([ have_role/2
Expand Down Expand Up @@ -296,6 +297,9 @@ if_has_child_permission([RootRoleId|OtherRoles], Permission) ->
false -> if_has_child_permission(OtherRoles, Permission)
end.

get_skey_pid(SKey) ->
#ddSeCo{pid=Pid} = seco_authorized(SKey),
{ok, Pid}.

%% --Implementation (exported helper functions) ----------------------------------------

Expand Down Expand Up @@ -354,11 +358,21 @@ seco_register(#ddSeCo{skey=SKey, pid=Pid}=SeCo, AuthState) when Pid == self() ->
seco_unregister(#ddSeCo{skey=SKey, pid=Pid}) when Pid == self() ->
catch if_delete(SKey, ddSeCo@, SKey).

is_pid_linked(SKey, #ddSeCo{pid=Pid} = SeCo) ->
{links, Links} = erlang:process_info(self(), links),
case lists:member(Pid, Links) of
true ->
SeCo;
false ->
?SecurityViolation({"Not logged in", SKey})
end.

seco_existing(SKey) ->
case if_read(ddSeCo@, SKey) of
[#ddSeCo{pid=Pid} = SeCo] when Pid == self() ->
SeCo;
[#ddSeCo{} = SeCo] ->
is_pid_linked(SKey, SeCo);
[] ->
?SecurityException({"Not logged in", SKey})
end.
Expand All @@ -379,11 +393,11 @@ seco_authorized(SKey) ->
case if_read(ddSeCo@, SKey) of
[#ddSeCo{pid=Pid, authState=authorized} = SeCo] when Pid == self() ->
SeCo;
[#ddSeCo{}] ->
?SecurityViolation({"Not logged in", SKey});
[] ->
[#ddSeCo{authState=authorized} = SeCo] ->
is_pid_linked(SKey, SeCo);
[] ->
?SecurityException({"Not logged in", SKey})
end.
end.

seco_update(#ddSeCo{skey=SKey,pid=Pid}=SeCo, #ddSeCo{skey=SKey,pid=Pid}=SeCoNew) when Pid == self() ->
case if_read(ddSeCo@, SKey) of
Expand Down
25 changes: 19 additions & 6 deletions src/imem_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,27 @@ mfa({Ref, Mod, which_applications, Args}, Transport) when Mod =:= imem_sec;
mfa({_Ref, imem_sec, echo, [_, Term]}, Transport) ->
send_resp({server_echo, Term}, Transport),
ok;
mfa({Ref, imem_sec = Mod, Fun, Args}, Transport) when Fun =:= login;
Fun =:= auth_start;
Fun =:= schema;
Fun =:= logout;
Fun =:= auth_add_cred ->
mfa(Ref, Mod, Fun, Args, Transport);
mfa({Ref, Mod, Fun, Args}, Transport) ->
spawn_link(fun() ->
mfa(Ref, Mod, Fun, Args, Transport)
end),
ok. % 'ok' returned for erlimem compatibility

mfa(Ref, Mod, Fun, Args, Transport) ->
NewArgs = args(Ref,Fun,Args,Transport),
ApplyRes = try
?TLog("~p MFA -> R ~n ~p:~p(~p)~n", [Transport,Mod,Fun,NewArgs]),
apply(Mod,Fun,NewArgs)
catch
_Class:Reason -> {error, {Reason, erlang:get_stacktrace()}}
end,
ApplyRes =
try
?TLog("~p MFA -> R ~n ~p:~p(~p)~n", [Transport,Mod,Fun,NewArgs]),
apply(Mod,Fun,NewArgs)
catch
_Class:Reason -> {error, {Reason, erlang:get_stacktrace()}}
end,
?TLog("~p MFA -> R ~n ~p:~p(~p) -> ~p~n", [Transport,Mod,Fun,NewArgs,ApplyRes]),
?TLog("~p MF -> R ~n ~p:~p -> ~p~n", [Transport,Mod,Fun,ApplyRes]),
send_resp(ApplyRes, Transport),
Expand Down
5 changes: 3 additions & 2 deletions src/imem_statement.erl
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@
%% gen_server -----------------------------------------------------

create_stmt(Statement, SKey, IsSec) ->
{ok, SKeyPid} = imem_seco:get_skey_pid(SKey),
case IsSec of
false ->
gen_server:start(?MODULE, [Statement,self()], [{spawn_opt, [{fullsweep_after, 0}]}]);
gen_server:start(?MODULE, [Statement,SKeyPid], [{spawn_opt, [{fullsweep_after, 0}]}]);
true ->
{ok, Pid} = gen_server:start(?MODULE, [Statement,self()], []),
{ok, Pid} = gen_server:start(?MODULE, [Statement,SKeyPid], []),
NewSKey = imem_sec:clone_seco(SKey, Pid),
ok = gen_server:call(Pid, {set_seco, NewSKey}),
{ok, Pid}
Expand Down