summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Nordberg <linus@sunet.se>2020-01-06 16:32:21 +0100
committerLinus Nordberg <linus@sunet.se>2020-01-07 23:09:09 +0100
commitae6fc8623e35eae56aad02d9a1f922199a59ad36 (patch)
tree9518e11114d1f004ea037ab5a6d2af6b899d59e9
parent00d3b28a452ff821ed689b22d77ad6719f6addb9 (diff)
Set up environment for remotes according to configuration
-rw-r--r--p11p-daemon/config/sys.config3
-rw-r--r--p11p-daemon/src/p11p_config.erl32
-rw-r--r--p11p-daemon/src/p11p_remote.erl17
-rw-r--r--p11p-daemon/src/p11p_remote_manager.erl7
4 files changed, 40 insertions, 19 deletions
diff --git a/p11p-daemon/config/sys.config b/p11p-daemon/config/sys.config
index 77c9b6f..487bee0 100644
--- a/p11p-daemon/config/sys.config
+++ b/p11p-daemon/config/sys.config
@@ -14,7 +14,8 @@
]},
{"vtoken1",
[{modules,
- [{"softhsm2", "/usr/lib/softhsm/libsofthsm2.so"}]}]}
+ [{"softhsm2", "/usr/lib/softhsm/libsofthsm2.so", [{"SOME_ENV", "hi env!"}]}]}
+ ]}
]}
]},
{lager,
diff --git a/p11p-daemon/src/p11p_config.erl b/p11p-daemon/src/p11p_config.erl
index 187fa8b..1fc83e9 100644
--- a/p11p-daemon/src/p11p_config.erl
+++ b/p11p-daemon/src/p11p_config.erl
@@ -9,7 +9,7 @@
%%-export([config/0]).
-export([nameof/1]).
-export([tokens/0]).
--export([remotebin_path/0, modules_for_token/1, module_path/1, token_mode/1]).
+-export([remotebin_path/0, modules_for_token/1, module_path/1, module_env/1, token_mode/1]).
-export_type([token_mode_t/0]).
%% Genserver callbacks.
@@ -19,7 +19,8 @@
%% Records and types.
-record(p11module, {
name :: string(),
- path :: string()
+ path :: string(),
+ env :: [{string(), string()}] %FIXME: maches [] too?
}).
-type p11module() :: #p11module{}.
@@ -65,6 +66,10 @@ modules_for_token(TokName) ->
module_path(Module) ->
Module#p11module.path.
+-spec module_env(p11module()) -> [].
+module_env(Module) ->
+ Module#p11module.env.
+
nameof(#token{name = Name}) ->
Name;
nameof(#p11module{name = Name}) ->
@@ -128,7 +133,7 @@ conf_tokens([H = {Name, _}|T], Acc) ->
-spec new_token({string(), [tuple()]}) -> token().
new_token({Name, Settings}) ->
Modules = conf_modules(proplists:get_value(modules, Settings)),
- Mode = mode(proplists:get_value(mode, Settings, {failover, [timeout]}),
+ Mode = mode(proplists:get_value(mode, Settings, {failover, [timeout]}), %FIXME: s/[timeout]/[10]/g or some other sane default?
maps:size(Modules)),
#token{
name = Name,
@@ -141,12 +146,15 @@ conf_modules(L) ->
conf_modules([], Acc) ->
Acc;
conf_modules([{Name, Path}|T], Acc) ->
- conf_modules(T, Acc#{Name => new_module(Name, Path)}).
+ conf_modules(T, Acc#{Name => new_module(Name, Path, [])});
+conf_modules([{Name, Path, Env}|T], Acc) ->
+ conf_modules(T, Acc#{Name => new_module(Name, Path, Env)}).
-new_module(Name, Path) ->
+new_module(Name, Path, Env) ->
#p11module{
name = Name,
- path = Path
+ path = Path,
+ env = Env
}.
-spec mode(p11p_config:token_mode_t(), non_neg_integer()) -> p11p_config:token_mode_t().
@@ -171,7 +179,9 @@ tokens_init_test_() ->
{"bogusmod0_1", "/path/to/bogusmod0_1"}
]}]},
{"vtoken1",
- [{modules, [{"bogusmod1_0", "/path/to/bogusmod1_0"}]}]}
+ [{modules, [{"bogusmod1_0", "/path/to/bogusmod1_0"},
+ {"bogusmod1_1", "/path/to/bogusmod1_1", [{"MYENV", "myenv"}]}
+ ]}]}
]) end,
fun(_) -> ok end,
fun(Conf) ->
@@ -180,14 +190,16 @@ tokens_init_test_() ->
{token,"vtoken0",
{balance,[3,1]},
#{"bogusmod0_0" =>
- {p11module,"bogusmod0_0", "/path/to/bogusmod0_0"},
+ {p11module,"bogusmod0_0", "/path/to/bogusmod0_0", []},
"bogusmod0_1" =>
- {p11module,"bogusmod0_1", "/path/to/bogusmod0_1"}}},
+ {p11module,"bogusmod0_1", "/path/to/bogusmod0_1", []}}},
"vtoken1" =>
{token,"vtoken1",
{failover,[timeout]},
#{"bogusmod1_0" =>
- {p11module,"bogusmod1_0", "/path/to/bogusmod1_0"}}}
+ {p11module,"bogusmod1_0", "/path/to/bogusmod1_0", []},
+ "bogusmod1_1" =>
+ {p11module,"bogusmod1_1", "/path/to/bogusmod1_1", [{"MYENV", "myenv"}]}}}
},
Conf)] end}.
%% modules_for_token_test_() ->
diff --git a/p11p-daemon/src/p11p_remote.erl b/p11p-daemon/src/p11p_remote.erl
index 1247f12..6a050e6 100644
--- a/p11p-daemon/src/p11p_remote.erl
+++ b/p11p-daemon/src/p11p_remote.erl
@@ -16,7 +16,7 @@
-behaviour(gen_server).
%% API.
--export([start_link/3]).
+-export([start_link/4]).
-export([request/2, add_to_outbuf/2, stop/2]).
-include("p11p_rpc.hrl").
@@ -36,10 +36,10 @@
}).
%% API.
--spec start_link(atom(), string(), string()) -> {ok, pid()} | {error, term()}.
-start_link(ServName, TokName, ModPath) ->
+-spec start_link(atom(), string(), string(), list()) -> {ok, pid()} | {error, term()}.
+start_link(ServName, TokName, ModPath, ModEnv) ->
lager:info("~p: p11p_remote starting for ~s", [ServName, ModPath]),
- gen_server:start_link({local, ServName}, ?MODULE, [TokName, ModPath], []).
+ gen_server:start_link({local, ServName}, ?MODULE, [TokName, ModPath, ModEnv], []).
-spec request(pid(), p11rpc_msg()) -> ok.
request(Remote, Request) ->
@@ -58,11 +58,16 @@ stop(Pid, Reason) ->
gen_server:cast(Pid, {stop, Reason}).
%% Genserver callbacks.
-init([TokName, ModPath]) ->
+init([TokName, ModPath, ModEnv]) ->
RemoteBinPath = p11p_config:remotebin_path(),
Port = open_port({spawn_executable, RemoteBinPath},
- [stream, exit_status, {args, [ModPath, "-v"]}]),
+ [stream,
+ exit_status,
+ {env, ModEnv},
+ {args, [ModPath, "-v"]} % FIXME: Remove -v
+ ]),
lager:debug("~p: ~s: new remote port: ~p", [self(), RemoteBinPath, Port]),
+ lager:debug("~p: ~s: module: ~s, env: ~p", [self(), RemoteBinPath, ModPath, ModEnv]),
{ok, #state{port = Port, token = TokName}}.
handle_call({add_to_outbuf, Data}, _From, State) ->
diff --git a/p11p-daemon/src/p11p_remote_manager.erl b/p11p-daemon/src/p11p_remote_manager.erl
index 10b9d22..eabb67f 100644
--- a/p11p-daemon/src/p11p_remote_manager.erl
+++ b/p11p-daemon/src/p11p_remote_manager.erl
@@ -39,6 +39,7 @@
tokname :: string(),
servid :: atom(),
modpath :: string(),
+ modenv :: [],
balance :: integer(),
pid :: pid() | undefined
}).
@@ -87,10 +88,10 @@ handle_call({remote_for_token, TokName}, _From, #state{tokens = Tokens} = State)
-1 ->
{Token#token.remotes, -1}
end,
- #remote{tokname = TokName, servid = ServId, modpath = ModPath, pid = Pid} = Remote = hd(Remotes),
+ #remote{tokname = TokName, servid = ServId, modpath = ModPath, modenv = ModEnv, pid = Pid} = Remote = hd(Remotes),
case Pid of
undefined ->
- {ok, NewPid} = p11p_remote:start_link(ServId, TokName, ModPath),
+ {ok, NewPid} = p11p_remote:start_link(ServId, TokName, ModPath, ModEnv),
NewRemote = Remote#remote{pid = NewPid},
NewToken = Token#token{remotes = [NewRemote | tl(Remotes)],
balance_count = NewBC},
@@ -177,10 +178,12 @@ remotes(TokName, [H|T], ConfMode, Acc) ->
ModName = p11p_config:nameof(H),
ServName = "p11p_remote:" ++ TokName ++ ":" ++ ModName,
ModPath = p11p_config:module_path(H),
+ ModEnv = p11p_config:module_env(H),
remotes(TokName, T, ConfMode, [#remote{
tokname = TokName,
servid = list_to_atom(ServName),
modpath = ModPath,
+ modenv = ModEnv,
balance = balance(ConfMode, length(T) + 1)
}
| Acc]).