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

Instrument/hook handlers: auth - register #4301

Merged
merged 10 commits into from
Jun 17, 2024
1 change: 1 addition & 0 deletions big_tests/dynamic_domains.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{host_type, <<"test type">>},
{secondary_domain, <<"domain.example.org">>},
{secondary_host_type, <<"test type">>},
{anonymous_host_type, <<"anonymous">>},
{dynamic_domains, [{<<"test type">>, [<<"domain.example.com">>, <<"domain.example.org">>]},
{<<"anonymous">>, [<<"anonymous.example.com">>]}]},
{muc_service, <<"groupchats.domain.example.com">>},
Expand Down
1 change: 1 addition & 0 deletions big_tests/test.config
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
{secondary_domain, <<"localhost.bis">>},
{reloaded_domain, <<"sogndal">>},
{secondary_host_type, <<"localhost.bis">>},
{anonymous_host_type, <<"anonymous.localhost">>},
{muc_service, <<"muc.localhost">>},
{muc_service_pattern, <<"muc.@HOST@">>},
{muc_light_service, <<"muclight.localhost">>},
Expand Down
27 changes: 21 additions & 6 deletions big_tests/tests/accounts_SUITE.erl
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
-module(accounts_SUITE).
-compile([export_all, nowarn_export_all]).

-include_lib("escalus/include/escalus.hrl").
-include_lib("escalus/include/escalus_xmlns.hrl").

-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").

-include_lib("exml/include/exml.hrl").
-include_lib("jid/include/jid.hrl").

-import(distributed_helper, [mim/0,
require_rpc_nodes/1,
Expand Down Expand Up @@ -37,6 +34,7 @@ all() ->

groups() ->
[{register, [parallel], [register,
unregister,
already_registered,
registration_conflict,
check_unregistered]},
Expand Down Expand Up @@ -68,13 +66,15 @@ change_password_tests() ->
%%--------------------------------------------------------------------

init_per_suite(Config1) ->
instrument_helper:start(instrument_helper:declared_events(ejabberd_auth)),
ok = dynamic_modules:ensure_modules(host_type(), required_modules()),
Config2 = [{mod_register_options, mod_register_options()} | Config1],
escalus:init_per_suite([{escalus_user_db, xmpp} | Config2]).

end_per_suite(Config) ->
escalus_fresh:clean(),
escalus:end_per_suite(Config).
escalus:end_per_suite(Config),
instrument_helper:stop().

required_modules() ->
[{mod_register, mod_register_options()}].
Expand Down Expand Up @@ -178,7 +178,15 @@ end_per_testcase(CaseName, Config) ->

register(Config) ->
[{Name1, _UserSpec1}, {Name2, _UserSpec2}] = escalus_users:get_users([alice, bob]),
escalus_fresh:create_users(Config, escalus:get_users([Name1, Name2])).
Config1 = escalus_fresh:create_users(Config, escalus:get_users([Name1, Name2])),
assert_event(auth_register_user, escalus_users:get_jid(Config1, Name1)),
assert_event(auth_register_user, escalus_users:get_jid(Config1, Name2)).

unregister(Config) ->
UserSpec = escalus_fresh:freshen_spec(Config, alice),
escalus_users:create_user(Config, {alice, UserSpec}),
escalus_users:delete_user(Config, {alice, UserSpec}),
assert_event(auth_unregister_user, escalus_users:get_jid(Config, UserSpec)).

already_registered(Config) ->
escalus_fresh:story(Config, [{alice, 1}], fun(Alice) ->
Expand Down Expand Up @@ -444,3 +452,10 @@ enable_watcher(Config, Watcher) ->

disable_watcher(Config) ->
restore_mod_register_options(Config).

%% Instrumentation events

assert_event(EventName, BinJid) ->
#jid{luser = LUser, lserver = LServer} = jid:from_binary(BinJid),
instrument_helper:assert(EventName, #{host_type => host_type()},
fun(M) -> M =:= #{count => 1, user => LUser, server => LServer} end).
24 changes: 21 additions & 3 deletions big_tests/tests/anonymous_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ suite() ->
%% Init & teardown
%%--------------------------------------------------------------------
init_per_suite(Config) ->
instrument_helper:start(declared_events()),
escalus:init_per_suite(Config).

end_per_suite(Config) ->
escalus:end_per_suite(Config).
escalus:end_per_suite(Config),
instrument_helper:stop().

init_per_testcase(CaseName, Config0) ->
NewUsers = escalus_ct:get_config(escalus_anon_users),
Expand All @@ -67,23 +69,27 @@ end_per_testcase(CaseName, Config) ->
connection_is_registered_with_sasl_anon(Config) ->
escalus:story(Config, [{jon, 1}], fun(Jon) ->
JID = jid:from_binary(escalus_client:short_jid(Jon)),
assert_event(auth_anonymous_register_user, JID),
OrigName = escalus_users:get_username(Config, jon),
?assertNotEqual(OrigName, JID#jid.luser),
F = fun() -> rpc(mim(), ejabberd_auth, does_user_exist, [JID]) end,
true = F(),
escalus_connection:kill(Jon),
mongoose_helper:wait_until(F, false)
mongoose_helper:wait_until(F, false),
assert_event(auth_anonymous_unregister_user, JID)
end).

connection_is_registered_with_login(Config) ->
escalus:story(Config, [{anna, 1}], fun(Anna) ->
JID = jid:from_binary(escalus_client:short_jid(Anna)),
assert_event(auth_anonymous_register_user, JID),
OrigName = escalus_users:get_username(Config, anna),
?assertEqual(OrigName, JID#jid.luser),
F = fun() -> rpc(mim(), ejabberd_auth, does_user_exist, [JID]) end,
true = F(),
escalus_connection:kill(Anna),
mongoose_helper:wait_until(F, false)
mongoose_helper:wait_until(F, false),
assert_event(auth_anonymous_register_user, JID)
end).

messages_story(Config) ->
Expand All @@ -94,3 +100,15 @@ messages_story(Config) ->
%% Below's dirty, but there is no other easy way...
escalus_assert:is_chat_message(<<"Hi!">>, Stanza)
end).

%% Helpers

declared_events() ->
instrument_helper:declared_events(ejabberd_auth_anonymous, [host_type()]).

host_type() ->
domain_helper:anonymous_host_type().

assert_event(EventName, #jid{luser = LUser, lserver = LServer}) ->
instrument_helper:assert(EventName, #{host_type => host_type()},
fun(M) -> M =:= #{count => 1, user => LUser, server => LServer} end).
6 changes: 6 additions & 0 deletions big_tests/tests/domain_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ secondary_host_type() ->
secondary_host_type(NodeKey) ->
get_or_fail({hosts, NodeKey, secondary_host_type}).

anonymous_host_type() ->
anonymous_host_type(mim).

anonymous_host_type(NodeKey) ->
get_or_fail({hosts, NodeKey, anonymous_host_type}).

make_metrics_prefix(HostType) ->
metrics_helper:make_host_type_name(HostType).

Expand Down
117 changes: 0 additions & 117 deletions big_tests/tests/metrics_register_SUITE.erl

This file was deleted.

26 changes: 20 additions & 6 deletions src/auth/ejabberd_auth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@
-export([remove_domain/3]).
-export([on_does_user_exist/3]).

%% Exported for tests
-export([instrumentation/1]).

-ignore_xref([
auth_methods/1, auth_modules/1, check_password/4, get_vh_registered_users/2,
get_vh_registered_users_number/2, start/1, stop/1]).
get_vh_registered_users_number/2, start/1, stop/1, instrumentation/1]).

-include("mongoose.hrl").
-include("jlib.hrl").
Expand Down Expand Up @@ -97,20 +100,20 @@
start() ->
lists:foreach(fun start/1, ?ALL_HOST_TYPES).

-spec start(HostType :: mongooseim:host_type()) -> 'ok'.
-spec start(mongooseim:host_type()) -> ok.
start(HostType) ->
ensure_metrics(HostType),
mongoose_instrument:set_up(instrumentation(HostType)),
F = fun(Mod) -> mongoose_gen_auth:start(Mod, HostType) end,
call_auth_modules_for_host_type(HostType, F, #{op => map}),
gen_hook:add_handlers(hooks(HostType)),
ok.
gen_hook:add_handlers(hooks(HostType)).

-spec stop(HostType :: mongooseim:host_type()) -> 'ok'.
-spec stop(mongooseim:host_type()) -> ok.
stop(HostType) ->
gen_hook:delete_handlers(hooks(HostType)),
F = fun(Mod) -> mongoose_gen_auth:stop(Mod, HostType) end,
call_auth_modules_for_host_type(HostType, F, #{op => map}),
ok.
mongoose_instrument:tear_down(instrumentation(HostType)).

Check warning on line 116 in src/auth/ejabberd_auth.erl

View check run for this annotation

Codecov / codecov/patch

src/auth/ejabberd_auth.erl#L116

Added line #L116 was not covered by tests

-spec hooks(mongooseim:host_type()) -> gen_hook:hook_list().
hooks(HostType) ->
Expand Down Expand Up @@ -229,6 +232,8 @@
F = fun(HostType, Mod) ->
case mongoose_gen_auth:try_register(Mod, HostType, LUser, LServer, Password) of
ok ->
mongoose_instrument:execute(auth_register_user, #{host_type => HostType},
#{count => 1, user => LUser, server => LServer}),
mongoose_hooks:register_user(HostType, LServer, LUser),
{stop, ok};
{error, _Error} ->
Expand Down Expand Up @@ -381,6 +386,8 @@
true ->
case call_auth_modules_for_domain(LServer, F, #{default => {error, not_allowed}}) of
{ok, HostType} ->
mongoose_instrument:execute(auth_unregister_user, #{host_type => HostType},
#{count => 1, user => LUser, server => LServer}),
Acc = mongoose_acc:new(#{location => ?LOCATION,
host_type => HostType,
lserver => LServer,
Expand Down Expand Up @@ -593,3 +600,10 @@
{error, not_found} ->
true
end.

-spec instrumentation(mongooseim:host_type()) -> [mongoose_instrument:spec()].
instrumentation(HostType) ->
[{auth_register_user, #{host_type => HostType},
#{metrics => #{count => spiral}}},
{auth_unregister_user, #{host_type => HostType},
#{metrics => #{count => spiral}}}].
17 changes: 15 additions & 2 deletions src/auth/ejabberd_auth_anonymous.erl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
does_user_exist/3,
supports_sasl_module/2,
get_registered_users/3,
supported_features/0
supported_features/0,
instrumentation/1
]).

%% Internal
Expand Down Expand Up @@ -129,6 +130,8 @@
#{host_type := HostType})
when AuthModule =:= ejabberd_auth_anonymous; % login_anon
AuthModule =:= cyrsasl_anonymous -> % sasl_anon
mongoose_instrument:execute(auth_anonymous_register_user, #{host_type => HostType},
#{count => 1, user => LUser, server => LServer}),
mongoose_hooks:register_user(HostType, LServer, LUser),
US = {LUser, LServer},
ejabberd_auth_anonymous_backend:add_connection(HostType, SID, US),
Expand All @@ -141,7 +144,10 @@
Acc :: mongoose_acc:t(),
Params :: map(),
Extra :: map().
unregister_connection(Acc, #{sid := SID, jid := #jid{luser = LUser, lserver = LServer}}, #{host_type := HostType}) ->
unregister_connection(Acc, #{sid := SID, jid := #jid{luser = LUser, lserver = LServer}},
#{host_type := HostType}) ->
mongoose_instrument:execute(auth_anonymous_unregister_user, #{host_type => HostType},

Check warning on line 149 in src/auth/ejabberd_auth_anonymous.erl

View check run for this annotation

Codecov / codecov/patch

src/auth/ejabberd_auth_anonymous.erl#L149

Added line #L149 was not covered by tests
#{count => 1, user => LUser, server => LServer}),
purge_hook(does_anonymous_user_exist(HostType, LUser, LServer), HostType, LUser, LServer),
remove_connection(HostType, SID, LUser, LServer),
{ok, Acc}.
Expand Down Expand Up @@ -280,3 +286,10 @@

-spec supported_features() -> [atom()].
supported_features() -> [dynamic_domains].

-spec instrumentation(mongooseim:host_type()) -> [mongoose_instrument:spec()].
instrumentation(HostType) ->
[{auth_anonymous_register_user, #{host_type => HostType},
#{metrics => #{count => spiral}}},
{auth_anonymous_unregister_user, #{host_type => HostType},
#{metrics => #{count => spiral}}}].
Loading