Skip to content

Commit 7d4ecb5

Browse files
committed
Refactor mqtt_processor get_vhost functions
In order to clarify preference of different methods. This commit oes not change functionality. This highlights some inconsistences: - If both User/Password and SslLogin are provided, in `check_credentials` User/Password takes precedence while in `get_vhost` SslLogin - If SslLogin is provided (but no mapping is found) vhost from port mapping has precedence over vhost from UserName, while in case of no ssl it is the other way around.
1 parent 10f1ea1 commit 7d4ecb5

File tree

2 files changed

+41
-40
lines changed

2 files changed

+41
-40
lines changed

deps/rabbitmq_mqtt/src/rabbit_mqtt_processor.erl

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,65 +1185,56 @@ get_vhost(UserBin, SslLogin, Port) ->
11851185
get_vhost_ssl(UserBin, SslLogin, Port).
11861186

11871187
get_vhost_no_ssl(UserBin, Port) ->
1188-
case vhost_in_username(UserBin) of
1189-
true ->
1190-
{vhost_in_username_or_default, get_vhost_username(UserBin)};
1191-
false ->
1192-
PortVirtualHostMapping = rabbit_runtime_parameters:value_global(
1193-
mqtt_port_to_vhost_mapping
1194-
),
1195-
case get_vhost_from_port_mapping(Port, PortVirtualHostMapping) of
1188+
case get_vhost_username(UserBin) of
1189+
undefined ->
1190+
case get_vhost_from_port_mapping(Port) of
11961191
undefined ->
1197-
{plugin_configuration_or_default_vhost, {rabbit_mqtt_util:env(vhost), UserBin}};
1198-
VHost ->
1199-
{port_to_vhost_mapping, {VHost, UserBin}}
1200-
end
1192+
VhostFromConfig = rabbit_mqtt_util:env(vhost),
1193+
{plugin_configuration_or_default_vhost, {VhostFromConfig, UserBin}};
1194+
VHostFromPortMapping ->
1195+
{port_to_vhost_mapping, {VHostFromPortMapping, UserBin}}
1196+
end;
1197+
VHostUser ->
1198+
{vhost_in_username, VHostUser}
12011199
end.
12021200

12031201
get_vhost_ssl(UserBin, SslLoginName, Port) ->
1204-
UserVirtualHostMapping = rabbit_runtime_parameters:value_global(
1205-
mqtt_default_vhosts
1206-
),
1207-
case get_vhost_from_user_mapping(SslLoginName, UserVirtualHostMapping) of
1202+
case get_vhost_from_user_mapping(SslLoginName) of
12081203
undefined ->
1209-
PortVirtualHostMapping = rabbit_runtime_parameters:value_global(
1210-
mqtt_port_to_vhost_mapping
1211-
),
1212-
case get_vhost_from_port_mapping(Port, PortVirtualHostMapping) of
1204+
case get_vhost_from_port_mapping(Port) of
12131205
undefined ->
1214-
{vhost_in_username_or_default, get_vhost_username(UserBin)};
1206+
case get_vhost_username(UserBin) of
1207+
undefined ->
1208+
VhostFromConfig = rabbit_mqtt_util:env(vhost),
1209+
{plugin_configuration_or_default_vhost, {VhostFromConfig, UserBin}};
1210+
VHostUser ->
1211+
{vhost_in_username, VHostUser}
1212+
end;
12151213
VHostFromPortMapping ->
12161214
{port_to_vhost_mapping, {VHostFromPortMapping, UserBin}}
12171215
end;
12181216
VHostFromCertMapping ->
12191217
{client_cert_to_vhost_mapping, {VHostFromCertMapping, UserBin}}
12201218
end.
12211219

1222-
vhost_in_username(UserBin) ->
1223-
case application:get_env(?APP_NAME, ignore_colons_in_username) of
1224-
{ok, true} -> false;
1225-
_ ->
1226-
%% split at the last colon, disallowing colons in username
1227-
case re:split(UserBin, ":(?!.*?:)") of
1228-
[_, _] -> true;
1229-
[UserBin] -> false;
1230-
[] -> false
1231-
end
1232-
end.
1233-
12341220
get_vhost_username(UserBin) ->
1235-
Default = {rabbit_mqtt_util:env(vhost), UserBin},
12361221
case application:get_env(?APP_NAME, ignore_colons_in_username) of
1237-
{ok, true} -> Default;
1222+
{ok, true} -> undefined;
12381223
_ ->
12391224
%% split at the last colon, disallowing colons in username
12401225
case re:split(UserBin, ":(?!.*?:)") of
12411226
[Vhost, UserName] -> {Vhost, UserName};
1242-
[UserBin] -> Default;
1243-
[] -> Default
1227+
[UserBin] -> undefined;
1228+
[] -> undefined
12441229
end
12451230
end.
12461231

1232+
get_vhost_from_user_mapping(User) ->
1233+
UserVirtualHostMapping = rabbit_runtime_parameters:value_global(
1234+
mqtt_default_vhosts
1235+
),
1236+
get_vhost_from_user_mapping(User, UserVirtualHostMapping).
1237+
12471238
get_vhost_from_user_mapping(_User, not_found) ->
12481239
undefined;
12491240
get_vhost_from_user_mapping(User, Mapping) ->
@@ -1255,6 +1246,12 @@ get_vhost_from_user_mapping(User, Mapping) ->
12551246
VHost
12561247
end.
12571248

1249+
get_vhost_from_port_mapping(Port) ->
1250+
PortVirtualHostMapping = rabbit_runtime_parameters:value_global(
1251+
mqtt_port_to_vhost_mapping
1252+
),
1253+
get_vhost_from_port_mapping(Port, PortVirtualHostMapping).
1254+
12581255
get_vhost_from_port_mapping(_Port, not_found) ->
12591256
undefined;
12601257
get_vhost_from_port_mapping(Port, Mapping) ->

deps/rabbitmq_mqtt/test/processor_SUITE.erl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@ end_per_testcase(_, Config) -> Config.
4545
ignore_colons(B) -> application:set_env(rabbitmq_mqtt, ignore_colons_in_username, B).
4646

4747
ignores_colons_in_username_if_option_set(_Config) ->
48-
ignore_colons(true),
49-
?assertEqual({rabbit_mqtt_util:env(vhost), <<"a:b:c">>},
50-
rabbit_mqtt_processor:get_vhost_username(<<"a:b:c">>)).
48+
clear_vhost_global_parameters(),
49+
ignore_colons(true),
50+
?assertEqual(undefined,
51+
rabbit_mqtt_processor:get_vhost_username(<<"a:b:c">>)),
52+
?assertEqual({plugin_configuration_or_default_vhost,
53+
{rabbit_mqtt_util:env(vhost), <<"a:b:c">>}},
54+
rabbit_mqtt_processor:get_vhost(<<"a:b:c">>, none, 1883)).
5155

5256
interprets_colons_in_username_if_option_not_set(_Config) ->
5357
ignore_colons(false),

0 commit comments

Comments
 (0)