Skip to content

Commit b30d1b1

Browse files
gomoripetimergify[bot]
authored andcommitted
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. (cherry picked from commit 7d4ecb5)
1 parent ef19253 commit b30d1b1

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
@@ -1178,65 +1178,56 @@ get_vhost(UserBin, SslLogin, Port) ->
11781178
get_vhost_ssl(UserBin, SslLogin, Port).
11791179

11801180
get_vhost_no_ssl(UserBin, Port) ->
1181-
case vhost_in_username(UserBin) of
1182-
true ->
1183-
{vhost_in_username_or_default, get_vhost_username(UserBin)};
1184-
false ->
1185-
PortVirtualHostMapping = rabbit_runtime_parameters:value_global(
1186-
mqtt_port_to_vhost_mapping
1187-
),
1188-
case get_vhost_from_port_mapping(Port, PortVirtualHostMapping) of
1181+
case get_vhost_username(UserBin) of
1182+
undefined ->
1183+
case get_vhost_from_port_mapping(Port) of
11891184
undefined ->
1190-
{plugin_configuration_or_default_vhost, {rabbit_mqtt_util:env(vhost), UserBin}};
1191-
VHost ->
1192-
{port_to_vhost_mapping, {VHost, UserBin}}
1193-
end
1185+
VhostFromConfig = rabbit_mqtt_util:env(vhost),
1186+
{plugin_configuration_or_default_vhost, {VhostFromConfig, UserBin}};
1187+
VHostFromPortMapping ->
1188+
{port_to_vhost_mapping, {VHostFromPortMapping, UserBin}}
1189+
end;
1190+
VHostUser ->
1191+
{vhost_in_username, VHostUser}
11941192
end.
11951193

11961194
get_vhost_ssl(UserBin, SslLoginName, Port) ->
1197-
UserVirtualHostMapping = rabbit_runtime_parameters:value_global(
1198-
mqtt_default_vhosts
1199-
),
1200-
case get_vhost_from_user_mapping(SslLoginName, UserVirtualHostMapping) of
1195+
case get_vhost_from_user_mapping(SslLoginName) of
12011196
undefined ->
1202-
PortVirtualHostMapping = rabbit_runtime_parameters:value_global(
1203-
mqtt_port_to_vhost_mapping
1204-
),
1205-
case get_vhost_from_port_mapping(Port, PortVirtualHostMapping) of
1197+
case get_vhost_from_port_mapping(Port) of
12061198
undefined ->
1207-
{vhost_in_username_or_default, get_vhost_username(UserBin)};
1199+
case get_vhost_username(UserBin) of
1200+
undefined ->
1201+
VhostFromConfig = rabbit_mqtt_util:env(vhost),
1202+
{plugin_configuration_or_default_vhost, {VhostFromConfig, UserBin}};
1203+
VHostUser ->
1204+
{vhost_in_username, VHostUser}
1205+
end;
12081206
VHostFromPortMapping ->
12091207
{port_to_vhost_mapping, {VHostFromPortMapping, UserBin}}
12101208
end;
12111209
VHostFromCertMapping ->
12121210
{client_cert_to_vhost_mapping, {VHostFromCertMapping, UserBin}}
12131211
end.
12141212

1215-
vhost_in_username(UserBin) ->
1216-
case application:get_env(?APP_NAME, ignore_colons_in_username) of
1217-
{ok, true} -> false;
1218-
_ ->
1219-
%% split at the last colon, disallowing colons in username
1220-
case re:split(UserBin, ":(?!.*?:)") of
1221-
[_, _] -> true;
1222-
[UserBin] -> false;
1223-
[] -> false
1224-
end
1225-
end.
1226-
12271213
get_vhost_username(UserBin) ->
1228-
Default = {rabbit_mqtt_util:env(vhost), UserBin},
12291214
case application:get_env(?APP_NAME, ignore_colons_in_username) of
1230-
{ok, true} -> Default;
1215+
{ok, true} -> undefined;
12311216
_ ->
12321217
%% split at the last colon, disallowing colons in username
12331218
case re:split(UserBin, ":(?!.*?:)") of
12341219
[Vhost, UserName] -> {Vhost, UserName};
1235-
[UserBin] -> Default;
1236-
[] -> Default
1220+
[UserBin] -> undefined;
1221+
[] -> undefined
12371222
end
12381223
end.
12391224

1225+
get_vhost_from_user_mapping(User) ->
1226+
UserVirtualHostMapping = rabbit_runtime_parameters:value_global(
1227+
mqtt_default_vhosts
1228+
),
1229+
get_vhost_from_user_mapping(User, UserVirtualHostMapping).
1230+
12401231
get_vhost_from_user_mapping(_User, not_found) ->
12411232
undefined;
12421233
get_vhost_from_user_mapping(User, Mapping) ->
@@ -1248,6 +1239,12 @@ get_vhost_from_user_mapping(User, Mapping) ->
12481239
VHost
12491240
end.
12501241

1242+
get_vhost_from_port_mapping(Port) ->
1243+
PortVirtualHostMapping = rabbit_runtime_parameters:value_global(
1244+
mqtt_port_to_vhost_mapping
1245+
),
1246+
get_vhost_from_port_mapping(Port, PortVirtualHostMapping).
1247+
12511248
get_vhost_from_port_mapping(_Port, not_found) ->
12521249
undefined;
12531250
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)