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

fix(ldap): fixed that the connection to the LDAP connector could be disconnected after a period of time #12610

Merged
merged 2 commits into from Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 32 additions & 14 deletions apps/emqx_ldap/src/emqx_ldap.erl
Expand Up @@ -39,11 +39,12 @@

-export([namespace/0, roots/0, fields/1, desc/1]).

-export([do_get_status/1]).
-export([do_get_status/1, get_status_with_poolname/1]).

-define(LDAP_HOST_OPTIONS, #{
default_port => 389
}).
-define(REDACT_VAL, "******").

-type params_tokens() :: #{atom() => list()}.
-type state() ::
Expand Down Expand Up @@ -154,18 +155,19 @@ on_start(
false ->
Config2
end,

Options = [
{pool_size, PoolSize},
{auto_reconnect, ?AUTO_RECONNECT_INTERVAL},
{options, Config3}
],

case emqx_resource_pool:start(InstId, ?MODULE, Options) of
case emqx_resource_pool:start(InstId, ?MODULE, [{log_tag, "eldap_info"} | Options]) of
ok ->
emqx_ldap_bind_worker:on_start(
InstId,
Config,
Options,
[{log_tag, "eldap_bind_info"} | Options],
prepare_template(Config, #{pool_name => InstId})
);
{error, Reason} ->
Expand Down Expand Up @@ -193,7 +195,15 @@ on_query(InstId, {query, Data, Attrs, Timeout}, State) ->
on_query(InstId, {bind, _DN, _Data} = Req, State) ->
emqx_ldap_bind_worker:on_query(InstId, Req, State).

on_get_status(_InstId, #{pool_name := PoolName} = _State) ->
on_get_status(InstId, #{pool_name := PoolName} = State) ->
case get_status_with_poolname(PoolName) of
connected ->
emqx_ldap_bind_worker:on_get_status(InstId, State);
disconnected ->
disconnected
end.

get_status_with_poolname(PoolName) ->
case emqx_resource_pool:health_check_workers(PoolName, fun ?MODULE:do_get_status/1) of
true ->
connected;
Expand All @@ -209,7 +219,7 @@ do_get_status(Conn) ->
%% if the server is down, the result is {error, ldap_closed}
%% otherwise is {error, invalidDNSyntax/timeout}
{error, ldap_closed} =/=
eldap:search(Conn, [{base, "checkalive"}, {filter, eldap:'approxMatch'("", "")}]).
eldap:search(Conn, [{base, "cn=checkalive"}, {filter, eldap:'approxMatch'("", "")}]).

%% ===================================================================

Expand All @@ -222,7 +232,8 @@ connect(Options) ->
} =
Conf = proplists:get_value(options, Options),
OpenOpts = maps:to_list(maps:with([port, sslopts], Conf)),
case eldap:open([Host], [{log, fun log/3}, {timeout, RequestTimeout} | OpenOpts]) of
LogTag = proplists:get_value(log_tag, Options),
case eldap:open([Host], [{log, mk_log_func(LogTag)}, {timeout, RequestTimeout} | OpenOpts]) of
{ok, Handle} = Ret ->
%% TODO: teach `eldap` to accept 0-arity closures as passwords.
case eldap:simple_bind(Handle, Username, emqx_secret:unwrap(Password)) of
Expand Down Expand Up @@ -313,14 +324,21 @@ do_ldap_query(
end.

%% Note: the value of the `_Level` here always is 2
log(_Level, Format, Args) ->
?SLOG(
info,
#{
msg => "eldap_info",
log => io_lib:format(Format, Args)
}
).
mk_log_func(LogTag) ->
fun(_Level, Format, Args) ->
?SLOG(
info,
#{
msg => LogTag,
log => io_lib:format(Format, [redact_ldap_log(Arg) || Arg <- Args])
}
)
end.

redact_ldap_log({'BindRequest', Version, Name, {simple, _}}) ->
{'BindRequest', Version, Name, {simple, ?REDACT_VAL}};
redact_ldap_log(Arg) ->
Arg.

prepare_template(Config, State) ->
maps:fold(fun prepare_template/3, State, Config).
Expand Down
8 changes: 7 additions & 1 deletion apps/emqx_ldap/src/emqx_ldap_bind_worker.erl
Expand Up @@ -24,7 +24,8 @@
-export([
on_start/4,
on_stop/2,
on_query/3
on_query/3,
on_get_status/2
]).

%% ecpool connect & reconnect
Expand Down Expand Up @@ -103,6 +104,11 @@ on_query(
{error, {unrecoverable_error, Reason}}
end.

on_get_status(_InstId, #{bind_pool_name := PoolName}) ->
emqx_ldap:get_status_with_poolname(PoolName);
on_get_status(_InstId, _) ->
connected.

%% ===================================================================

connect(Conf) ->
Expand Down
1 change: 1 addition & 0 deletions changes/ee/fix-12610.en.md
@@ -0,0 +1 @@
Fixed that the connection to the LDAP connector may be disconnected after a period of time.