Fix `peer_cert_as_username` option not working

see #3281
This commit is contained in:
JianBo He 2020-03-05 14:28:04 +08:00 committed by turtleDeng
parent cc25ec5835
commit 47720b906f
2 changed files with 33 additions and 6 deletions

View File

@ -1012,6 +1012,9 @@ maybe_username_as_clientid(_ConnPkt, ClientInfo = #{zone := Zone, username := Us
false -> ok
end.
maybe_assign_clientid(_ConnPkt, ClientInfo = #{clientid := ClientId})
when ClientId /= undefined ->
{ok, ClientInfo};
maybe_assign_clientid(#mqtt_packet_connect{clientid = <<>>}, ClientInfo) ->
%% Generate a rand clientId
{ok, ClientInfo#{clientid => emqx_guid:to_base62(emqx_guid:gen())}};

View File

@ -45,7 +45,8 @@
all() ->
[{group, mqttv3},
{group, mqttv4},
{group, mqttv5}
{group, mqttv5},
{group, others}
].
groups() ->
@ -66,17 +67,26 @@ groups() ->
]},
{mqttv5, [non_parallel_tests],
[t_basic_with_props_v5
]},
{others, [non_parallel_tests],
[t_username_as_clientid,
t_certcn_as_clientid
]}
].
init_per_suite(Config) ->
emqx_ct_helpers:boot_modules(all),
emqx_ct_helpers:start_apps([]),
emqx_ct_helpers:start_apps([], fun set_special_confs/1),
Config.
end_per_suite(_Config) ->
emqx_ct_helpers:stop_apps([]).
set_special_confs(emqx) ->
emqx_ct_helpers:change_emqx_opts(ssl_twoway, [{peer_cert_as_username, cn}]);
set_special_confs(_) ->
ok.
%%--------------------------------------------------------------------
%% Test cases for MQTT v3
%%--------------------------------------------------------------------
@ -110,10 +120,7 @@ t_cm_registry(_) ->
{_, Pid, _, _} = lists:keyfind(registry, 1, Info),
ignored = gen_server:call(Pid, <<"Unexpected call">>),
gen_server:cast(Pid, <<"Unexpected cast">>),
Pid ! <<"Unexpected info">>,
ok = application:stop(mnesia),
emqx_ct_helpers:stop_apps([]),
emqx_ct_helpers:start_apps([]).
Pid ! <<"Unexpected info">>.
t_will_message(_Config) ->
{ok, C1} = emqtt:start_link([{clean_start, true},
@ -263,6 +270,23 @@ t_basic(_Opts) ->
?assertEqual(3, length(recv_msgs(3))),
ok = emqtt:disconnect(C).
t_username_as_clientid(_) ->
emqx_zone:set_env(external, use_username_as_clientid, true),
Username = <<"usera">>,
{ok, C} = emqtt:start_link([{username, Username}]),
{ok, _} = emqtt:connect(C),
#{clientinfo := #{clientid := Username}} = emqx_cm:get_chan_info(Username),
emqtt:disconnect(C).
t_certcn_as_clientid(_) ->
CN = <<"0004.novalocal">>,
emqx_zone:set_env(external, use_username_as_clientid, true),
SslConf = emqx_ct_helpers:client_ssl_twoway(),
{ok, C} = emqtt:start_link([{port, 8883}, {ssl, true}, {ssl_opts, SslConf}]),
{ok, _} = emqtt:connect(C),
#{clientinfo := #{clientid := CN}} = emqx_cm:get_chan_info(CN),
emqtt:disconnect(C).
%%--------------------------------------------------------------------
%% Helper functions
%%--------------------------------------------------------------------