diff options
-rw-r--r-- | p11p-daemon/.gitignore | 15 | ||||
-rw-r--r-- | p11p-daemon/LICENSE | 31 | ||||
-rw-r--r-- | p11p-daemon/Makefile | 27 | ||||
-rw-r--r-- | p11p-daemon/config/sys.config | 9 | ||||
-rw-r--r-- | p11p-daemon/config/vm.args | 6 | ||||
-rw-r--r-- | p11p-daemon/rebar.config | 27 | ||||
-rw-r--r-- | p11p-daemon/src/p11p.app.src | 17 | ||||
-rw-r--r-- | p11p-daemon/src/p11p_app.erl | 17 | ||||
-rw-r--r-- | p11p-daemon/src/p11p_config.erl | 110 | ||||
-rw-r--r-- | p11p-daemon/src/p11p_defs.hrl | 11 | ||||
-rw-r--r-- | p11p-daemon/src/p11p_sup.erl | 24 |
11 files changed, 294 insertions, 0 deletions
diff --git a/p11p-daemon/.gitignore b/p11p-daemon/.gitignore new file mode 100644 index 0000000..ced0c5e --- /dev/null +++ b/p11p-daemon/.gitignore @@ -0,0 +1,15 @@ +.rebar3 +_* +.eunit +*.o +*.beam +*.plt +*.swp +*.swo +.erlang.cookie +ebin +log +erl_crash.dump +.rebar +logs +_build diff --git a/p11p-daemon/LICENSE b/p11p-daemon/LICENSE new file mode 100644 index 0000000..75ba921 --- /dev/null +++ b/p11p-daemon/LICENSE @@ -0,0 +1,31 @@ +Copyright (c) 2019, Sunet. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with distribution. + + * Neither the names of the copyright holders nor the names of the + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR +TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE +USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH +DAMAGE. diff --git a/p11p-daemon/Makefile b/p11p-daemon/Makefile new file mode 100644 index 0000000..328d4c4 --- /dev/null +++ b/p11p-daemon/Makefile @@ -0,0 +1,27 @@ +PROJECT = p11p +REBAR = rebar3 # export PATH=$PATH:~/.cache/rebar3/bin + +all: compile + +compile: + @$(REBAR) compile +rel: + @$(REBAR) release +clean: + @$(REBAR) clean +clean-all: + @$(REBAR) clean -a +dialyzer: + @$(REBAR) dialyzer +check: + @$(REBAR) do eunit -v, ct -v, proper -v +tests: + @$(REBAR) ct +start: rel + ./_build/default/rel/$(PROJECT)/bin/$(PROJECT) start +stop: rel + ./_build/default/rel/$(PROJECT)/bin/$(PROJECT) stop +shell: + @$(REBAR) shell + +.PHONY: compile rel clean dialyzer check tests shell diff --git a/p11p-daemon/config/sys.config b/p11p-daemon/config/sys.config new file mode 100644 index 0000000..7af56a2 --- /dev/null +++ b/p11p-daemon/config/sys.config @@ -0,0 +1,9 @@ +[ + {p11p, [ + {global, [ + {loglevel, 3}]}, + ]}, + {lager, + [{handlers, + [{lager_console_backend, debug}]}]} +]. diff --git a/p11p-daemon/config/vm.args b/p11p-daemon/config/vm.args new file mode 100644 index 0000000..b953e63 --- /dev/null +++ b/p11p-daemon/config/vm.args @@ -0,0 +1,6 @@ +-sname p11p + +-setcookie p11p_cookie + ++K true ++A30 diff --git a/p11p-daemon/rebar.config b/p11p-daemon/rebar.config new file mode 100644 index 0000000..800cce9 --- /dev/null +++ b/p11p-daemon/rebar.config @@ -0,0 +1,27 @@ +{minimum_otp_vsn, "19.0"}. + +{erl_opts, [ + debug_info, + {parse_transform, lager_transform} +]}. + +{deps, [ + %% Debian packages: apt install erlang-lager erlang-goldrush + %%{lager, {git, "https://github.com/erlang-lager/lager", {tag, "3.7.0"}}}, + %%{goldrush, fixme} +]}. + +{relx, [ + {release, {p11p, "0.0-dev"}, [p11p, sasl]}, + {sys_config, "config/sys.config"}, + {vm_args, "config/vm.args"}, + {dev_mode, true}, + {include_erts, false}, + {extended_start_script, true} +]}. + +{profiles, [ + {prod, [{relx, [{dev_mode, false}, + {include_erts, true}]}] + }] +}. diff --git a/p11p-daemon/src/p11p.app.src b/p11p-daemon/src/p11p.app.src new file mode 100644 index 0000000..d81c1f4 --- /dev/null +++ b/p11p-daemon/src/p11p.app.src @@ -0,0 +1,17 @@ +{application, p11p, + [{description, "PKCS #11 proxy"}, + {vsn, "0.0-dev"}, + {registered, []}, + {mod, { p11p_app, []}}, % args end up in p11p_app:start/2 + {applications, + [kernel, + stdlib, + lager + ]}, + {env,[]}, + {modules, []}, + + {maintainers, []}, + {licenses, []}, + {links, []} + ]}. diff --git a/p11p-daemon/src/p11p_app.erl b/p11p-daemon/src/p11p_app.erl new file mode 100644 index 0000000..ad48080 --- /dev/null +++ b/p11p-daemon/src/p11p_app.erl @@ -0,0 +1,17 @@ +-module(p11p_app). +-behaviour(application). + +-export([start/2, stop/1]). + +-include("p11p_defs.hrl"). + +start(_Type, _Args) -> % Args from application spec (key 'p11p'?). + lager:notice("p11p version ~s starting", [p11p:version()]), + {ok, Sup} = p11p_sup:start_link(), + {ok, Sup}. + +-spec stop([]) -> ok. +stop(_State) -> + ok. + +%% Private. diff --git a/p11p-daemon/src/p11p_config.erl b/p11p-daemon/src/p11p_config.erl new file mode 100644 index 0000000..aa222e3 --- /dev/null +++ b/p11p-daemon/src/p11p_config.erl @@ -0,0 +1,110 @@ +-module(p11p_config). +-behaviour(gen_server). + +%% API +-export([start_link/0]). +-export([tokens/0]). +-export([modules_for_token/1]). + +%% Genserver callbacks. +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, + terminate/2, code_change/3]). + +%% Records and types. +-include("p11p_defs.hrl"). + +%% Genserver state. +-record(state, { tokens :: [token()] }). + +%%%%%%%%%%%%%%%%%%%% +%% API. +start_link() -> + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). + +config() -> + gen_server:call(?MODULE, config). + +-spec tokens() -> [token()]. +tokens() -> + gen_server:call(?MODULE, tokens). + +-spec modules_for_token(token()) -> [module()]. +modules_for_token(Token) -> + gen_server:call(?MODULE, {modules_for_token, Token}). + + +%%%%%%%%%%%%%%%%%%%% +%% Genserver callbacks. +init(_Args) -> + State = init_state(), + {ok, State}. + +handle_call(config, _From, State) -> + {reply, State, State}; +handle_call(tokens, _From, #state{tokens = Tokens} = State) -> + {reply, Tokens, State}; +handle_call({modules_for_token, Token}, _From, #state{tokens = Tokens} = State) -> + Reply = #p11module{name = "FIXME"}, + {reply, Reply, State}; +handle_call(Request, _From, State) -> + lager:warning("Unhandled call: ~p", [Request]), + {reply, unhandled, State}. + +handle_cast(Message, State) -> + lager:warning("Unhandled cast: ~p", [Message]), + {noreply, State}. + +handle_info(Info, State) -> + lager:warning("Unhandled info: ~p", [Info]), + {noreply, State}. + +terminate(_Reason, _State) -> + ok. + +code_change(_OldVersion, State, _Extra) -> + {ok, State}. + +%%%%%%%%%%%%%%%%%%%% +%% Private. + +init_state() -> + #state { + tokens = init_tokens(application:get_env(p11p, group, [])) + }. + +init_tokens(Tokens) -> + [new_token(T) || T <- Tokens]. + +-spec new_token({string(), [tuple()]}) -> token(). +new_token({Name, Settings}) -> + #token{ + name = Name, + modules = [new_module(M) || M <- proplists:get_value(modules, Settings, [])] + }. + +new_module(Name) -> + #p11module{ + name = Name + }. + +%%%%%%%%%%%%%% +%% Unit tests. + +-include_lib("eunit/include/eunit.hrl"). + +tokens_test_() -> + {setup, + fun() -> init_tokens( + [ + {"vtoken0", + [{modules, ["token_0_0", "token_0_1"]}]}, + {"vtoken1", + [{modules, ["token_1_0"]}]} + ]) end, + fun(_) -> ok end, + fun(Conf) -> + [?_assertEqual( + [ + {token,"vtoken0",[{module,"token_0_0"},{module,"token_0_1"}]}, + {token,"vtoken1",[{module,"token_1_0"}]}], + Conf)] end}. diff --git a/p11p-daemon/src/p11p_defs.hrl b/p11p-daemon/src/p11p_defs.hrl new file mode 100644 index 0000000..bd390a7 --- /dev/null +++ b/p11p-daemon/src/p11p_defs.hrl @@ -0,0 +1,11 @@ +-record(p11module, { + name :: string() + }). + +-record(token, { + name :: string(), + modules = [] :: [p11module()] + }). + +-type token() :: #token{}. +-type p11module() :: #p11module{}. diff --git a/p11p-daemon/src/p11p_sup.erl b/p11p-daemon/src/p11p_sup.erl new file mode 100644 index 0000000..b8bffe2 --- /dev/null +++ b/p11p-daemon/src/p11p_sup.erl @@ -0,0 +1,24 @@ +-module(p11p_sup). +-behaviour(supervisor). + +%% API. +-export([start_link/0]). + +%% Supervisor callbacks. +-export([init/1]). + +%% From supervisor. +-type start_link_err() :: {already_started, pid()} | shutdown | term(). +-type start_link_ret() :: {ok, pid()} | ignore | {error, start_link_err()}. + +-spec start_link() -> start_link_ret(). +start_link() -> + supervisor:start_link({local, ?MODULE}, ?MODULE, []). + +-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}). + +%% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules} +init([]) -> + {ok, {{one_for_all, 10, 10}, [ + ?CHILD(p11p_config, worker) + ]}}. |