Skip to content

Commit

Permalink
Use a map for config in mongoose_ldap_worker and remove eldap_utils:g…
Browse files Browse the repository at this point in the history
…et_mod_opt/4

This was the last place that was using it. It's better to use the new config
with defaults, or maps directly.
  • Loading branch information
gustawlippa committed Mar 2, 2022
1 parent c3085e3 commit 0c2b3ea
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 66 deletions.
8 changes: 4 additions & 4 deletions src/config/mongoose_config_spec.erl
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,8 @@ outgoing_pool_connection(<<"ldap">>) ->
#section{
items = #{<<"port">> => #option{type = integer,
validate = port},
<<"rootdn">> => #option{type = string},
<<"password">> => #option{type = string},
<<"rootdn">> => #option{type = binary},
<<"password">> => #option{type = binary},
<<"encrypt">> => #option{type = atom,
validate = {enum, [none, tls]}},
<<"servers">> => #list{items = #option{type = string}},
Expand All @@ -633,8 +633,8 @@ outgoing_pool_connection(<<"ldap">>) ->
},
format_items = map,
include = always,
defaults = #{<<"rootdn">> => "",
<<"password">> => "",
defaults = #{<<"rootdn">> => <<"">>,
<<"password">> => <<"">>,
<<"encrypt">> => none,
<<"servers">> => ["localhost"],
<<"connect_interval">> => 10000}
Expand Down
29 changes: 0 additions & 29 deletions src/eldap_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
make_filter/3,
get_state/2,
case_insensitive_match/2,
get_mod_opt/4,
deref_aliases/1,
process_user_filter/2,
get_search_filter/1,
Expand Down Expand Up @@ -217,34 +216,6 @@ uids_domain_subst(Host, UIDs) ->
end,
UIDs).


-spec get_mod_opt(atom(), gen_mod:module_opts(), fun(), any()) -> any().
get_mod_opt(Key, Opts, F, Default) ->
case gen_mod:get_opt(Key, Opts, Default) of
Default ->
Default;
Val ->
prepare_opt_val(Key, Val, F, Default)
end.

-type check_fun() :: fun((any()) -> any()) | {module(), atom()}.
-spec prepare_opt_val(any(), any(), check_fun(), any()) -> any().
prepare_opt_val(Opt, Val, F, Default) ->
Res = case F of
{Mod, Fun} ->
catch Mod:Fun(Val);
_ ->
catch F(Val)
end,
case Res of
{'EXIT', _} ->
?LOG_ERROR(#{what => configuration_error, option => Opt,
value => Val, default => Default}),
Default;
_ ->
Res
end.

deref_aliases(never) -> neverDerefAliases;
deref_aliases(searching) -> derefInSearching;
deref_aliases(finding) -> derefFindingBaseObj;
Expand Down
41 changes: 10 additions & 31 deletions src/mongoose_ldap_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

%% gen_server callbacks

-spec init(list()) -> {ok, state()}.
-spec init(gen_mod:module_opts()) -> {ok, state()}.
init(Options) ->
State = initial_state(Options),
self() ! connect,
Expand Down Expand Up @@ -58,33 +58,15 @@ code_change(_OldVsn, State, _Extra) ->

%% internal functions

initial_state(Opts) ->
Servers = eldap_utils:get_mod_opt(servers, Opts,
fun(L) ->
lists:map(fun(H) when is_list(H) -> H end, L)
end, ["localhost"]),
Encrypt = eldap_utils:get_mod_opt(encrypt, Opts,
fun(tls) -> tls;
(none) -> none
end, none),
TLSOptions = eldap_utils:get_mod_opt(tls_options, Opts,
fun(L) when is_list(L) -> L end, []),
Port = eldap_utils:get_mod_opt(port, Opts,
fun(I) when is_integer(I), I>0 -> I end,
case Encrypt of
tls -> ?LDAPS_PORT;
starttls -> ?LDAP_PORT;
_ -> ?LDAP_PORT
end),
RootDN = eldap_utils:get_mod_opt(rootdn, Opts,
fun iolist_to_binary/1,
<<"">>),
Password = eldap_utils:get_mod_opt(password, Opts,
fun iolist_to_binary/1,
<<"">>),
ConnectInterval = eldap_utils:get_mod_opt(connect_interval, Opts,
fun(I) when is_integer(I), I>0 -> I end,
default_connect_interval()),
initial_state(Opts = #{servers := Servers, encrypt := Encrypt, rootdn := RootDN, password := Password,
connect_interval := ConnectInterval}) ->
TLSOptions = maps:get(tls_options, Opts, []),
DefaultPort = case Encrypt of
tls -> ?LDAPS_PORT;
starttls -> ?LDAP_PORT;
_ -> ?LDAP_PORT
end,
Port = maps:get(port, Opts, DefaultPort),
#{handle => none,
servers => Servers,
encrypt => Encrypt,
Expand Down Expand Up @@ -149,6 +131,3 @@ retry_call_eldap(Request, State) ->

do_call_eldap(_Request, #{handle := none}) -> {error, not_connected};
do_call_eldap({F, Args}, #{handle := Handle}) -> apply(eldap, F, [Handle | Args]).

default_connect_interval() ->
10000.
3 changes: 1 addition & 2 deletions src/wpool/mongoose_wpool_ldap.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
init() ->
ok.

start(HostType, Tag, WpoolOpts, ConnOptsIn) ->
ConnOpts = maps:to_list(ConnOptsIn),
start(HostType, Tag, WpoolOpts, ConnOpts) ->
WorkerSpec = {mongoose_ldap_worker, ConnOpts},
ProcName = mongoose_wpool:make_pool_name(ldap, HostType, Tag),
mongoose_wpool:start_sup_pool(ldap, ProcName, [{worker, WorkerSpec} | WpoolOpts]).
Expand Down

0 comments on commit 0c2b3ea

Please sign in to comment.