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

Multi tenancy pm #3075

Merged
merged 8 commits into from
Apr 14, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions big_tests/tests/connect_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ end_per_group(_, Config) ->
Config.

init_per_testcase(close_connection_if_service_type_is_hidden = CN, Config) ->
OptName = {hide_service_name, <<"localhost">>},
OptName = hide_service_name,
mongoose_helper:successful_rpc(ejabberd_config, add_local_option, [OptName, true]),
escalus:init_per_testcase(CN, Config);
init_per_testcase(replaced_session_cannot_terminate = CN, Config) ->
Expand All @@ -197,7 +197,7 @@ init_per_testcase(CaseName, Config) ->
escalus:init_per_testcase(CaseName, Config).

end_per_testcase(close_connection_if_service_type_is_hidden = CN, Config) ->
OptName = {hide_service_name, <<"localhost">>},
OptName = hide_service_name,
mongoose_helper:successful_rpc(ejabberd_config, del_local_option, [OptName]),
escalus:end_per_testcase(CN, Config);
end_per_testcase(replaced_session_cannot_terminate = CN, Config) ->
Expand Down
45 changes: 40 additions & 5 deletions big_tests/tests/dynamic_domains_pm_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@
-compile(export_all).
-import(distributed_helper, [mim/0, mim2/0, require_rpc_nodes/1, rpc/4]).

-define(TEST_NODES, [mim(), mim2()]).
-define(TEST_NODES, [mim() | ?CLUSTER_NODES]).
-define(CLUSTER_NODES, [mim2()]).
-define(DOMAINS, [<<"example.com">>, <<"example.org">>]).

suite() ->
require_rpc_nodes([mim, mim2]).

all() ->
[can_authenticate].
[can_authenticate,
pm_messages,
disconnected_on_domain_disabling].

init_per_suite(Config) ->
init_per_suite(Config0) ->
Config = cluster_nodes(?CLUSTER_NODES, Config0),
insert_domains(?TEST_NODES, ?DOMAINS),
escalus:init_per_suite(Config).

end_per_suite(Config) ->
end_per_suite(Config0) ->
Config = escalus:end_per_suite(Config0),
remove_domains(?TEST_NODES, ?DOMAINS),
escalus:end_per_suite(Config).
uncluster_nodes(?CLUSTER_NODES, Config).

init_per_testcase(CN, Config) ->
escalus:init_per_testcase(CN, Config).
Expand All @@ -35,6 +40,26 @@ can_authenticate(Config) ->
escalus_connection:stop(ClientA),
escalus_connection:stop(ClientB).

pm_messages(Config) ->
StoryFn =
fun(Alice, Bob) ->
escalus:send(Alice, escalus_stanza:chat_to(Bob, <<"OH, HAI!">>)),
escalus:assert(is_chat_message, [<<"OH, HAI!">>], escalus:wait_for_stanza(Bob)),
escalus:send(Bob, escalus_stanza:chat_to(Alice, <<"Hello there!">>)),
escalus:assert(is_chat_message, [<<"Hello there!">>], escalus:wait_for_stanza(Alice))
end,
escalus:story(Config, [{alice3, 1}, {bob3, 1}], StoryFn).

disconnected_on_domain_disabling(Config) ->
StoryFn =
fun(Alice, Bob) ->
remove_domains(?TEST_NODES, ?DOMAINS),
escalus_connection:wait_for_close(Alice, timer:seconds(5)),
escalus_connection:wait_for_close(Bob, timer:seconds(5)),
insert_domains(?TEST_NODES, ?DOMAINS)
end,
escalus:story(Config, [{alice3, 1}, {bob3, 1}], StoryFn).

%% helper functions
insert_domains(Nodes, Domains) ->
Source = dummy_source, %% can be anything, we don't care about it
Expand All @@ -45,3 +70,13 @@ insert_domains(Nodes, Domains) ->
remove_domains(Nodes, Domains) ->
[ok = rpc(Node, mongoose_domain_core, delete, [Domain]) ||
Node <- Nodes, Domain <- Domains].

cluster_nodes([], Config) -> Config;
cluster_nodes([Node | T], Config) ->
NewConfig = distributed_helper:add_node_to_cluster(Node, Config),
cluster_nodes(T, NewConfig).

uncluster_nodes([], Config) -> Config;
uncluster_nodes([Node | T], Config) ->
NewConfig = distributed_helper:remove_node_from_cluster(Node, Config),
cluster_nodes(T, NewConfig).
6 changes: 2 additions & 4 deletions doc/advanced-configuration/host_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,22 @@ The following options are allowed:
* [`pgsql_users_number_estimate`](general.md#generalpgsql_users_number_estimate)
* [`route_subdomains`](general.md#generalroute_subdomains)
* [`replaced_wait_timeout`](general.md#generalreplaced_wait_timeout)
* [`hide_service_name`](general.md#generalhide_service_name)

#### Example

The `hide_service_name` option is set to `false` only for `domain2.com`.
The `replaced_wait_timeout` option is set to `2000` only for `domain2.com`.

```toml
[general]
hosts = ["domain1.com", "domain2.com", "domain3.com"]
loglevel = "info"
hide_service_name = true
replaced_wait_timeout = 1000

[[host_config]]
host = "domain2.com"

[host_config.general]
hide_service_name = false
replaced_wait_timeout = 2000
```

### `host_config.auth`
Expand Down
2 changes: 1 addition & 1 deletion include/ejabberd_c2s.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
tls_options = [],
tls_verify :: verify_none | verify_peer,
authenticated = false :: authenticated_state(),
host_type = <<>> :: binary(),
host_type :: binary() | undefined,
jid :: jid:jid() | undefined,
user = <<>> :: jid:user(),
server = <<>> :: jid:server(),
Expand Down
2 changes: 1 addition & 1 deletion include/mongoose.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
-define(MYHOSTS, ejabberd_config:get_global_option(hosts)).
-define(ALL_HOST_TYPES, ejabberd_config:get_global_option_or_default(hosts, []) ++
ejabberd_config:get_global_option_or_default(host_types, [])).
-define(MYNAME, hd(ejabberd_config:get_global_option(hosts))).
-define(MYNAME, ejabberd_config:get_global_option(default_server_domain)).
-define(MYLANG, ejabberd_config:get_global_option(language)).

-define(CONFIG_PATH, "etc/mongooseim.toml").
Expand Down
1 change: 1 addition & 0 deletions rel/fed1.vars-toml.config
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
%% This node is for s2s testing.
%% "localhost" host should NOT be defined.
{hosts, "\"fed1\""}.
{default_server_domain, "\"fed1\""}.

{s2s_addr, "[[s2s.address]]
host = \"localhost\"
Expand Down
1 change: 1 addition & 0 deletions rel/files/mongooseim.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
loglevel = "warning"
hosts = [{{{hosts}}}]
host_types = [{{{host_types}}}]
default_server_domain = {{{default_server_domain}}}
registration_timeout = "infinity"
language = "en"
all_metrics_are_global = {{{all_metrics_are_global}}}
Expand Down
1 change: 1 addition & 0 deletions rel/mim1.vars-toml.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

{hosts, "\"localhost\", \"anonymous.localhost\", \"localhost.bis\""}.
{host_types, "\"test type\""}.
{default_server_domain, "\"localhost\""}.

{host_config,
"[[host_config]]
Expand Down
1 change: 1 addition & 0 deletions rel/mim2.vars-toml.config
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
{service_port, 8899}.

{hosts, "\"localhost\", \"anonymous.localhost\", \"localhost.bis\""}.
{default_server_domain, "\"localhost\""}.
{s2s_addr, "[[s2s.address]]
host = \"localhost2\"
ip_address = \"127.0.0.1\""}.
Expand Down
1 change: 1 addition & 0 deletions rel/mim3.vars-toml.config
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
{http_api_client_endpoint_port, 8093}.

{hosts, "\"localhost\", \"anonymous.localhost\", \"localhost.bis\""}.
{default_server_domain, "\"localhost\""}.

{s2s_addr, "[[s2s.address]]
host = \"localhost2\"
Expand Down
1 change: 1 addition & 0 deletions rel/reg1.vars-toml.config
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
%% "localhost" is a global host.
%% "reg1" is a local host.
{hosts, "\"reg1\", \"localhost\""}.
{default_server_domain, "\"reg1\""}.
{s2s_addr, "[[s2s.address]]
host = \"localhost\"
ip_address = \"127.0.0.1\"
Expand Down
1 change: 1 addition & 0 deletions rel/vars-toml.config.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

% TOML config
{hosts, "\"localhost\""}.
{default_server_domain, "\"localhost\""}.
{s2s_default_policy, "\"deny\""}.
{listen_service, "[[listen.service]]
port = 8888
Expand Down
9 changes: 7 additions & 2 deletions src/config/mongoose_config_spec.erl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@
root() ->
General = general(),
#section{
items = #{<<"general">> => General#section{process = fun ?MODULE:process_general/1},
items = #{<<"general">> => General#section{required = [<<"default_server_domain">>],
process = fun ?MODULE:process_general/1},
<<"listen">> => listen(),
<<"auth">> => auth(),
<<"outgoing_pools">> => outgoing_pools(),
Expand Down Expand Up @@ -188,6 +189,10 @@ general() ->
validate = non_empty},
validate = unique,
format = config},
<<"default_server_domain">> =># option{type = binary,
validate = non_empty,
process = fun ?MODULE:process_host/1,
format = config},
<<"registration_timeout">> => #option{type = int_or_infinity,
validate = positive,
format = local_config},
Expand Down Expand Up @@ -228,7 +233,7 @@ general() ->
validate = positive,
format = host_local_config},
<<"hide_service_name">> => #option{type = boolean,
format = host_local_config}
format = local_config}
},
format = none
}.
Expand Down
10 changes: 7 additions & 3 deletions src/domain/mongoose_domain_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,17 @@ insert_host_types(Tab, AllowedHostTypes) ->
ok.

handle_delete(Domain) ->
case is_static(Domain) of
true ->
case ets:lookup(?TABLE, Domain) of
[{Domain, _HostType, _Source = config}] ->
%% Ignore any static domains
?LOG_ERROR(#{what => domain_static_but_was_in_db, domain => Domain}),
{error, static};
false ->
[] ->
%% nothing to remove
ok;
[{Domain, HostType, _Source}] ->
ets:delete(?TABLE, Domain),
mongoose_hooks:disable_domain(HostType, Domain),
ok
end.

Expand Down
16 changes: 9 additions & 7 deletions src/ejabberd_c2s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,9 @@ wait_for_stream(timeout, StateData) ->
{stop, normal, StateData};
wait_for_stream(closed, StateData) ->
{stop, normal, StateData};
wait_for_stream(_UnexpectedItem, #state{ server = Server } = StateData) ->
case ejabberd_config:get_local_option(hide_service_name, Server) of
wait_for_stream(_UnexpectedItem, #state{ server = Server,
host_type = HostType } = StateData) ->
case ejabberd_config:get_local_option(hide_service_name) of
true ->
{stop, normal, StateData};
_ ->
Expand Down Expand Up @@ -519,7 +520,7 @@ wait_for_feature_before_auth({xmlstreamelement, El}, StateData) ->
TLSEnabled == false,
SockMod == gen_tcp ->
TLSOpts = case ejabberd_config:get_local_option(
{domain_certfile, StateData#state.server}) of
{domain_certfile, StateData#state.host_type}) of
undefined ->
StateData#state.tls_options;
CertFile ->
Expand Down Expand Up @@ -773,7 +774,8 @@ do_open_session(Acc, JID, StateData) ->
end
end.

do_open_session_common(Acc, JID, #state{jid = JID, server = S} = NewStateData0) ->
do_open_session_common(Acc, JID, #state{jid = JID, server = S,
host_type = HostType} = NewStateData0) ->
change_shaper(NewStateData0, JID),
Acc1 = mongoose_hooks:roster_get_subscription_lists(S, Acc, JID),
{Fs, Ts, Pending} = mongoose_acc:get(roster, subscription_lists, {[], [], []}, Acc1),
Expand All @@ -792,7 +794,7 @@ do_open_session_common(Acc, JID, #state{jid = JID, server = S} = NewStateData0)
[] ->
ok;
_ ->
Timeout = get_replaced_wait_timeout(S),
Timeout = get_replaced_wait_timeout(HostType),
erlang:send_after(Timeout, self(), replaced_wait_timeout)
end,

Expand All @@ -806,8 +808,8 @@ do_open_session_common(Acc, JID, #state{jid = JID, server = S} = NewStateData0)
privacy_list = PrivList},
{established, Acc1, NewStateData}.

get_replaced_wait_timeout(S) ->
ejabberd_config:get_local_option_or_default({replaced_wait_timeout, S},
get_replaced_wait_timeout(HostType) ->
ejabberd_config:get_local_option_or_default({replaced_wait_timeout, HostType},
default_replaced_wait_timeout()).

default_replaced_wait_timeout() ->
Expand Down
14 changes: 13 additions & 1 deletion src/ejabberd_local.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
%%%
%%%----------------------------------------------------------------------

%%%----------------------------------------------------------------------
%%% FIXME: the code in this module uses Host term to identify domain
%%% name, not a host type.
%%%----------------------------------------------------------------------

-module(ejabberd_local).
-author('alexey@process-one.net').

Expand Down Expand Up @@ -261,6 +266,9 @@ node_cleanup(Acc, Node) ->
Res = mnesia:async_dirty(F),
maps:put(?MODULE, Res, Acc).

disable_domain(_Acc, _HostType, Domain) ->
unregister_host(Domain).

%%====================================================================
%% gen_server callbacks
%%====================================================================
Expand All @@ -280,6 +288,7 @@ init([]) ->
[{ram_copies, [node()]},
{attributes, record_info(fields, iq_response)}]),
mnesia:add_table_copy(iq_response, node(), ram_copies),
ejabberd_hooks:add(disable_domain, global, fun disable_domain/3,50),
ejabberd_hooks:add(node_cleanup, global, ?MODULE, node_cleanup, 50),
{ok, #state{}}.

Expand All @@ -293,8 +302,10 @@ init([]) ->
%% Description: Handling call messages
%%--------------------------------------------------------------------
handle_call({unregister_host, Host}, _From, State) ->
Node = node(),
[ejabberd_c2s:stop(Pid)
|| #session{sid = {_, Pid}} <- ejabberd_sm:get_vh_session_list(Host)],
|| #session{sid = {_, Pid}} <- ejabberd_sm:get_vh_session_list(Host),
node(Pid) =:= Node],
do_unregister_host(Host),
mongoose_metrics:remove_host_metrics(Host),
{reply, ok, State};
Expand Down Expand Up @@ -370,6 +381,7 @@ handle_info(_Info, State) ->
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
ejabberd_hooks:delete(node_cleanup, global, ?MODULE, node_cleanup, 50),
ejabberd_hooks:delete(disable_domain, global, fun disable_domain/3, 50),
ok.

%%--------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/ejabberd_router.erl
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ default_routing_modules() ->
mongoose_router_localdomain,
mongoose_router_external_localnode,
mongoose_router_external,
mongoose_router_dynamic_domains,
ejabberd_s2s].

make_routing_module_source(Mods) ->
Expand Down
17 changes: 17 additions & 0 deletions src/mongoose_hooks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@

-export([c2s_remote_hook/5]).

-export([disable_domain/2,
remove_domain/2]).

-spec c2s_remote_hook(LServer, Tag, Args, HandlerState, C2SState) -> Result when
LServer :: jid:lserver(),
Tag :: atom(),
Expand Down Expand Up @@ -197,6 +200,20 @@ anonymous_purge_hook(LServer, Acc, LUser) ->
auth_failed(Server, Username) ->
ejabberd_hooks:run_fold(auth_failed, Server, ok, [Username, Server]).

-spec disable_domain(HostType, Domain) -> Result when
HostType :: binary(),
Domain :: jid:lserver(),
Result :: ok.
disable_domain(HostType, Domain) ->
ejabberd_hooks:run(disable_domain, [HostType, Domain]).

-spec remove_domain(HostType, Domain) -> Result when
HostType :: binary(),
Domain :: jid:lserver(),
Result :: ok.
remove_domain(HostType, Domain) ->
ejabberd_hooks:run(remove_domain, [HostType, Domain]).

-spec ejabberd_ctl_process(Acc, Args) -> Result when
Acc :: any(),
Args :: [string()],
Expand Down
Loading