Skip to content

Commit

Permalink
Merge pull request #3196 from esl/multitenancy/blocking
Browse files Browse the repository at this point in the history
Implement multitenancy for mod_blocking
  • Loading branch information
vkatsuba committed Jul 28, 2021
2 parents 9a3cd46 + 6db1192 commit 554cd09
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 48 deletions.
7 changes: 6 additions & 1 deletion big_tests/dynamic_domains.config
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,10 @@
{server, <<"example.org">>},
{host, <<"localhost">>},
{password, <<"makota3">>},
{port, 5232}]}
{port, 5232}]},
{john, [
{username, <<"john">>},
{server, <<"domain.example.com">>},
{host, <<"localhost">>},
{password, <<"cosontuma">>}]}
]}.
2 changes: 2 additions & 0 deletions big_tests/dynamic_domains.spec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

{suites, "tests", mam_SUITE}.

{suites, "tests", mod_blocking_SUITE}.

{suites, "tests", mod_ping_SUITE}.

{suites, "tests", muc_SUITE}.
Expand Down
4 changes: 4 additions & 0 deletions big_tests/test.config
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@
{username, <<"mike">>},
{server, <<"localhost">>},
{password, <<"nicniema">>}]},
{john, [
{username, <<"john">>},
{server, <<"localhost">>},
{password, <<"cosontuma">>}]},
{geralt, [
{username, <<"geralt">>},
{server, <<"localhost">>},
Expand Down
36 changes: 20 additions & 16 deletions big_tests/tests/mod_blocking_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ end_per_suite(Config) ->
escalus:end_per_suite(Config).

init_per_group(_GroupName, Config) ->
escalus:create_users(Config, escalus:get_users([alice, bob, carol, mike, geralt])).
escalus_fresh:create_users(Config, escalus:get_users([alice, bob, kate, mike, john])).

end_per_group(_GroupName, Config) ->
escalus:delete_users(Config, escalus:get_users([alice, bob, carol, mike, geralt])).
Config.

init_per_testcase(CaseName, Config) ->
escalus:init_per_testcase(CaseName, Config).
Expand Down Expand Up @@ -176,7 +176,7 @@ add_another_user_to_blocklist(Config) ->

add_many_users_to_blocklist(Config) ->
escalus:fresh_story(
Config, [{alice, 1}, {bob, 1}, {carol, 1}, {mike, 1}],
Config, [{alice, 1}, {bob, 1}, {kate, 1}, {mike, 1}],
fun(User1, User2, User3, User4) ->
user_blocks(User1, [User2, User3, User4]),
BlockList = get_blocklist(User1),
Expand All @@ -197,7 +197,7 @@ remove_user_from_blocklist(Config) ->

remove_many_user_from_blocklist(Config) ->
escalus:fresh_story(
Config, [{alice, 1}, {bob, 1}, {geralt, 1}],
Config, [{alice, 1}, {bob, 1}, {kate, 1}],
fun(User1, User2, User3) ->
user_blocks(User1, [User2, User3]),
user_unblocks(User1, [User2, User3]),
Expand All @@ -208,7 +208,7 @@ remove_many_user_from_blocklist(Config) ->

clear_blocklist(Config) ->
escalus:fresh_story(
Config, [{alice, 1}, {bob, 1}, {geralt, 1}],
Config, [{alice, 1}, {bob, 1}, {kate, 1}],
fun(User1, User2, User3) ->
user_blocks(User1, [User2, User3]),
user_unblocks_all(User1),
Expand All @@ -231,7 +231,8 @@ messages_from_blocked_user_dont_arrive(Config) ->
fun(User1, User2) ->
user_blocks(User1, [User2]),
message(User2, User1, <<"Hi!">>),
client_gets_nothing(User1),
ct:sleep(100),
escalus_assert:has_no_stanzas(User1),
privacy_helper:gets_error(User2, <<"cancel">>, <<"service-unavailable">>)
end).

Expand All @@ -255,18 +256,24 @@ messages_from_any_blocked_resource_dont_arrive(Config) ->
user_blocks(User2, [User1a]),
%% then
message_is_not_delivered(User1a, [User2], <<"roar!">>),
privacy_helper:gets_error(User1a, <<"cancel">>, <<"service-unavailable">>),
message_is_not_delivered(User1b, [User2], <<"woof!">>),
message_is_not_delivered(User1c, [User2], <<"grrr!">>)
privacy_helper:gets_error(User1b, <<"cancel">>, <<"service-unavailable">>),
message_is_not_delivered(User1c, [User2], <<"grrr!">>),
privacy_helper:gets_error(User1c, <<"cancel">>, <<"service-unavailable">>),
ct:sleep(100),
escalus_assert:has_no_stanzas(User2)
end).

blocking_doesnt_interfere(Config) ->
escalus:fresh_story(
Config, [{alice, 1}, {bob, 1}, {geralt, 1}],
Config, [{alice, 1}, {bob, 1}, {kate, 1}],
fun(User1, User2, User3) ->
%% given
user_blocks(User1, [User2]),
%% then
message_is_not_delivered(User2, [User1], <<"!@#@$@#$%">>),
privacy_helper:gets_error(User2, <<"cancel">>, <<"service-unavailable">>),
message_is_delivered(User3, [User1], <<"Ni hao.">>)
end).

Expand All @@ -285,11 +292,13 @@ blocking_propagates_to_resources(Config) ->
client_gets_blocking_error(User1b),
% Bob can't send to any of Alice's resources
message_is_not_delivered(User2, [User1a], <<"hau!">>),
message_is_not_delivered(User2, [User1b], <<"miau!">>)
privacy_helper:gets_error(User2, <<"cancel">>, <<"service-unavailable">>),
message_is_not_delivered(User2, [User1b], <<"miau!">>),
privacy_helper:gets_error(User2, <<"cancel">>, <<"service-unavailable">>)
end).

iq_reply_doesnt_crash_user_process(Config) ->
escalus:story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->
escalus:fresh_story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->

QueryWithBlockingNS = escalus_stanza:query_el(?NS_BLOCKING, []),
%% Send IQ reply with blocking ns
Expand Down Expand Up @@ -367,7 +376,7 @@ blocking_and_relogin_many(Config) ->

simple_story(Config, Fun) ->
escalus:story(
Config, [{alice, 1}, {bob, 1}, {carol, 1}, {mike, 1}, {geralt, 1}],
Config, [{alice, 1}, {bob, 1}, {kate, 1}, {mike, 1}, {john, 1}],
Fun
).

Expand Down Expand Up @@ -630,10 +639,6 @@ user_unblocks_all(User) ->
message(From, To, MsgTxt) ->
escalus_client:send(From, escalus_stanza:chat_to(To, MsgTxt)).

client_gets_nothing(Client) ->
ct:sleep(500),
escalus_assert:has_no_stanzas(Client).

message_is_delivered(From, [To|_] = Tos, MessageText) ->
BareTo = escalus_utils:jid_to_lower(escalus_client:short_jid(To)),
escalus:send(From, escalus_stanza:chat_to(BareTo, MessageText)),
Expand All @@ -647,7 +652,6 @@ message_is_delivered(From, To, MessageText) ->
message_is_not_delivered(From, [To|_] = Tos, MessageText) ->
BareTo = escalus_utils:jid_to_lower(escalus_client:short_jid(To)),
escalus:send(From, escalus_stanza:chat_to(BareTo, MessageText)),
timer:sleep(300),
clients_have_no_messages(Tos).

clients_have_no_messages(Cs) when is_list (Cs) -> [ client_has_no_messages(C) || C <- Cs ].
Expand Down
2 changes: 1 addition & 1 deletion big_tests/tests/mod_global_distrib_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ init_per_group_generic(Config0) ->

%% To reduce load when sending many messages
VirtHosts = virtual_hosts(),
ModulesToStop = [mod_offline, mod_privacy, mod_roster, mod_last],
ModulesToStop = [mod_offline, mod_blocking, mod_privacy, mod_roster, mod_last],

OldMods = save_modules(NodeName, VirtHosts),

Expand Down
2 changes: 1 addition & 1 deletion rel/files/mongooseim.toml
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@
{{{mod_privacy}}}
{{/mod_privacy}}
{{#mod_blocking}}
[modules.mod_blocking]
{{{mod_blocking}}}

{{/mod_blocking}}
{{#mod_private}}
{{{mod_private}}}
Expand Down
7 changes: 6 additions & 1 deletion rel/mim1.vars-toml.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{default_server_domain, "\"localhost\""}.

{mod_privacy, ""}.
{mod_blocking, ""}.
{host_config,
"[[host_config]]
host = \"anonymous.localhost\"
Expand Down Expand Up @@ -47,7 +48,11 @@
{{#mod_privacy}}
[host_config.modules.mod_privacy]
{{{mod_privacy}}}
{{/mod_privacy}}"}.
{{/mod_privacy}}
{{#mod_blocking}}
[host_config.modules.mod_blocking]
{{{mod_blocking}}}
{{/mod_blocking}}"}.
{password_format, "password.format = \"scram\"
password.hash = [\"sha256\"]"}.
{scram_iterations, 64}.
Expand Down
2 changes: 1 addition & 1 deletion rel/vars-toml.config.in
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{mod_last, ""}.
{mod_offline, ""}.
{mod_privacy, ""}.
{mod_blocking, "[modules.mod_blocking]"}.
{mod_blocking, ""}.
{mod_private, "[modules.mod_private]"}.
{mod_roster, ""}.
{mod_vcard, "[modules.mod_vcard]
Expand Down
57 changes: 30 additions & 27 deletions src/mod_blocking.erl
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
%%%-------------------------------------------------------------------
%%% @author bartek
%%% @copyright (C) 2016, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 16. May 2016 18:17
%%%-------------------------------------------------------------------
-module(mod_blocking).

-xep([{xep, 191}, {version, "1.2"}]).
-xep([{xep, 191}, {version, "1.3"}]).

-behaviour(gen_mod).
-behaviour(mongoose_module_metrics).

-export([start/2,
-export([start/2]).
-export([stop/1]).
-export([deps/2]).
-export([supported_features/0]).
-export([config_spec/0]).

-export([
process_iq_get/5,
process_iq_set/4,
stop/1,
disco_local_features/1
]).

-ignore_xref([disco_local_features/1, process_iq_get/5, process_iq_set/4]).

-include("mongoose.hrl").
-include("jlib.hrl").
-include("mod_privacy.hrl").

-type listitem() :: #listitem{}.

start(Host, _Opts) ->
ejabberd_hooks:add(hooks(Host)).
start(HostType, _Opts) ->
ejabberd_hooks:add(hooks(HostType)).

stop(HostType) ->
ejabberd_hooks:delete(hooks(HostType)).

deps(_HostType, Opts) ->
[{mod_privacy, Opts, hard}].

stop(Host) ->
ejabberd_hooks:delete(hooks(Host)).
-spec supported_features() -> [atom()].
supported_features() ->
[dynamic_domains].

hooks(Host) ->
[{disco_local_features, Host, ?MODULE, disco_local_features, 99},
{privacy_iq_get, Host, ?MODULE, process_iq_get, 50},
{privacy_iq_set, Host, ?MODULE, process_iq_set, 50}].
config_spec() ->
mod_privacy:config_spec().

hooks(HostType) ->
[{disco_local_features, HostType, ?MODULE, disco_local_features, 99},
{privacy_iq_get, HostType, ?MODULE, process_iq_get, 50},
{privacy_iq_set, HostType, ?MODULE, process_iq_set, 50}].

-spec disco_local_features(mongoose_disco:feature_acc()) -> mongoose_disco:feature_acc().
disco_local_features(Acc = #{node := <<>>}) ->
Expand Down Expand Up @@ -211,13 +218,9 @@ make_blocking_list_entry(J) ->
broadcast_blocking_command(Acc, LUser, LServer, UserList, _Changed, unblock_all) ->
broadcast_blocking_command(Acc, LUser, LServer, UserList, [], unblock);
broadcast_blocking_command(Acc, LUser, LServer, UserList, Changed, Type) ->
case jid:make(LUser, LServer, <<>>) of
error ->
Acc;
UserJID ->
Bcast = {blocking, UserList, Type, Changed},
ejabberd_sm:route(UserJID, UserJID, Acc, {broadcast, Bcast})
end.
UserJID = jid:make_noprep(LUser, LServer, <<>>),
Bcast = {blocking, UserList, Type, Changed},
ejabberd_sm:route(UserJID, UserJID, Acc, {broadcast, Bcast}).

blocking_query_response(Lst) ->
#xmlel{
Expand Down

0 comments on commit 554cd09

Please sign in to comment.