Skip to content

Commit

Permalink
Make keys a map
Browse files Browse the repository at this point in the history
  • Loading branch information
gustawlippa committed Mar 21, 2022
1 parent b56b254 commit d6b2f35
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 26 deletions.
4 changes: 2 additions & 2 deletions big_tests/tests/oauth_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,13 @@ to_lower(B) when is_binary(B) ->
list_to_binary(string:to_lower(binary_to_list(B))).

required_modules() ->
KeyOpts = #{keys => [{token_secret, ram},
KeyOpts = #{keys => #{token_secret => ram,
%% This is a hack for tests! As the name implies,
%% a pre-shared key should be read from a file stored
%% on disk. This way it can be shared with trusted 3rd
%% parties who can use it to sign tokens for users
%% to authenticate with and MongooseIM to verify.
{provision_pre_shared, ram}]},
provision_pre_shared => ram}},
KeyStoreOpts = config_parser_helper:mod_config(mod_keystore, KeyOpts),
[{mod_last, stopped},
{mod_keystore, KeyStoreOpts},
Expand Down
18 changes: 5 additions & 13 deletions src/mod_keystore.erl
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ config_spec() ->
#section{
items = #{<<"ram_key_size">> => #option{type = integer,
validate = non_negative},
<<"keys">> => #list{items = keys_spec()}
<<"keys">> => #list{items = keys_spec(),
format_items = map}
},
defaults = #{<<"ram_key_size">> => ?DEFAULT_RAM_KEY_SIZE,
<<"keys">> => []},
format_items = map,
process = fun validate_key_ids/1
<<"keys">> => #{}},
format_items = map
}.

keys_spec() ->
Expand Down Expand Up @@ -163,7 +163,7 @@ does_table_exist(NameOrTID) ->
ets:info(NameOrTID, name) /= undefined.

init_keys(HostType, Opts = #{keys := Keys}) ->
[ init_key(K, HostType, Opts) || K <- Keys ].
maps:map(fun(KeyName, KeyType) -> init_key({KeyName, KeyType}, HostType, Opts) end, Keys).

-spec init_key({key_name(), key_type()}, mongooseim:host_type(), gen_mod:module_opts()) -> ok.
init_key({KeyName, {file, Path}}, HostType, _Opts) ->
Expand All @@ -184,13 +184,5 @@ ets_get_key(KeyID) ->
ets_store_key(KeyID, RawKey) ->
ets:insert(keystore, {KeyID, RawKey}).

validate_key_ids(Opts = #{keys := KeySpecs}) ->
KeyIDs = [ KeyID || {KeyID, _} <- KeySpecs ],
SortedAndUniqueKeyIDs = lists:usort(KeyIDs),
case KeyIDs -- SortedAndUniqueKeyIDs of
[] -> Opts;
[_|_] -> error(non_unique_key_ids, KeySpecs)
end.

config_metrics(Host) ->
mongoose_module_metrics:opts_for_module(Host, ?MODULE, [backend]).
8 changes: 4 additions & 4 deletions test/common/config_parser_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,9 @@ all_modules() ->
mod_adhoc => #{iqdisc => one_queue, report_commands_node => true},
mod_mam_rdbms_arch_async => default_config([modules, mod_mam_meta, async_writer]),
mod_keystore =>
mod_config(mod_keystore, #{keys => [{access_secret, ram},
{access_psk, {file, "priv/access_psk"}},
{provision_psk, {file, "priv/provision_psk"}}],
mod_config(mod_keystore, #{keys => #{access_secret => ram,
access_psk => {file, "priv/access_psk"},
provision_psk => {file, "priv/provision_psk"}},
ram_key_size => 1000}),
mod_global_distrib =>
mod_config(mod_global_distrib,
Expand Down Expand Up @@ -874,7 +874,7 @@ default_mod_config(mod_inbox) ->
reset_markers => [<<"displayed">>],
iqdisc => no_queue};
default_mod_config(mod_keystore) ->
#{ram_key_size => 2048, keys => []};
#{ram_key_size => 2048, keys => #{}};
default_mod_config(mod_last) ->
#{iqdisc => one_queue, backend => mnesia};
default_mod_config(mod_mam) ->
Expand Down
7 changes: 3 additions & 4 deletions test/config_parser_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2115,9 +2115,9 @@ mod_keystore_keys(_Config) ->
P = [modules, mod_keystore, keys],
RequiredOpts = #{<<"name">> => <<"access_secret">>,
<<"type">> => <<"ram">>},
?cfgh(P, [{access_secret, ram}],
?cfgh(P ++ [access_secret], ram,
T([RequiredOpts])),
?cfgh(P, [{access_secret, {file, "priv/access_psk"}}],
?cfgh(P ++ [access_secret], {file, "priv/access_psk"},
T([RequiredOpts#{<<"type">> => <<"file">>,
<<"path">> => <<"priv/access_psk">>}])),
[?errh(T([maps:remove(Key, RequiredOpts)])) || Key <- maps:keys(RequiredOpts)],
Expand All @@ -2126,8 +2126,7 @@ mod_keystore_keys(_Config) ->
?errh(T([RequiredOpts#{<<"type">> => <<"file">>}])),
?errh(T([RequiredOpts#{<<"type">> => <<"file">>,
<<"path">> => <<"does/not/exists">>}])),
?errh([#{reason := non_unique_key_ids}],
T([#{<<"name">> => <<"same_name_twice">>,
?errh(T([#{<<"name">> => <<"same_name_twice">>,
<<"type">> => <<"ram">>},
#{<<"name">> => <<"same_name_twice">>,
<<"type">> => <<"file">>,
Expand Down
6 changes: 3 additions & 3 deletions test/keystore_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ key_at(Path, Data) ->
{ok, Path}.

key_from_file(KeyFile) ->
mod_config(mod_keystore, #{keys => [{key_from_file, {file, KeyFile}}]}).
mod_config(mod_keystore, #{keys => #{key_from_file => {file, KeyFile}}}).

ram_key() ->
mod_config(mod_keystore, #{keys => [{ram_key, ram}]}).
mod_config(mod_keystore, #{keys => #{ram_key => ram}}).

sized_ram_key(Size) ->
mod_config(mod_keystore, #{keys => [{ram_key, ram}],
mod_config(mod_keystore, #{keys => #{ram_key => ram},
ram_key_size => Size}).

mock_mongoose_metrics() ->
Expand Down

0 comments on commit d6b2f35

Please sign in to comment.