test: ensure verify_none to ssl client opts as default value

This commit is contained in:
Zaiming (Stone) Shi 2023-12-12 23:36:54 +01:00
parent 13541690cc
commit 19051f639b
4 changed files with 39 additions and 14 deletions

View File

@ -1008,7 +1008,8 @@ do_t_update_listener(Config) ->
{ssl, true}, {ssl, true},
{ssl_opts, [ {ssl_opts, [
{certfile, ClientCert}, {certfile, ClientCert},
{keyfile, ClientKey} {keyfile, ClientKey},
{verify, verify_none}
]}, ]},
{port, 8883} {port, 8883}
]), ]),

View File

@ -261,9 +261,10 @@ request_dashboard(Method, Url, Auth) ->
request_dashboard(Method, Url, QueryParams, Auth) -> request_dashboard(Method, Url, QueryParams, Auth) ->
Request = {Url ++ "?" ++ QueryParams, [Auth]}, Request = {Url ++ "?" ++ QueryParams, [Auth]},
do_request_dashboard(Method, Request). do_request_dashboard(Method, Request).
do_request_dashboard(Method, Request) ->
do_request_dashboard(Method, {Url, _} = Request) ->
ct:pal("Method: ~p, Request: ~p", [Method, Request]), ct:pal("Method: ~p, Request: ~p", [Method, Request]),
case httpc:request(Method, Request, [], []) of case httpc:request(Method, Request, maybe_ssl(Url), []) of
{error, socket_closed_remotely} -> {error, socket_closed_remotely} ->
{error, socket_closed_remotely}; {error, socket_closed_remotely};
{ok, {{"HTTP/1.1", Code, _}, _Headers, Return}} when {ok, {{"HTTP/1.1", Code, _}, _Headers, Return}} when
@ -276,6 +277,9 @@ do_request_dashboard(Method, Request) ->
{error, Reason} {error, Reason}
end. end.
maybe_ssl("http://" ++ _) -> [];
maybe_ssl("https://" ++ _) -> [{ssl, [{verify, verify_none}]}].
auth_header_() -> auth_header_() ->
auth_header_(<<"admin">>, <<"public">>). auth_header_(<<"admin">>, <<"public">>).

View File

@ -198,8 +198,25 @@ t_verify_cacertfile(_Config) ->
VerifyPeerConf1, VerifyPeerConf1,
naive_env_interpolation(<<"${EMQX_ETC_DIR}/certs/cacert.pem">>) naive_env_interpolation(<<"${EMQX_ETC_DIR}/certs/cacert.pem">>)
), ),
validate_https(VerifyPeerConf2, MaxConnection, DefaultSSLCert, verify_peer), %% we always test client with verify_none and no client cert is sent
ok. %% since the server is configured with verify_peer
%% hence the expected observation on the client side is an error
ErrorReason =
try
validate_https(VerifyPeerConf2, MaxConnection, DefaultSSLCert, verify_peer)
catch
error:{https_client_error, Reason} ->
Reason
end,
%% There seems to be a race-condition causing the return value to vary a bit
case ErrorReason of
socket_closed_remotely ->
ok;
{ssl_error, _SslSock, {tls_alert, {certificate_required, _}}} ->
ok;
Other ->
throw({unexpected, Other})
end.
t_bad_certfile(_Config) -> t_bad_certfile(_Config) ->
Conf = #{ Conf = #{
@ -219,9 +236,12 @@ t_bad_certfile(_Config) ->
validate_https(Conf, MaxConnection, SSLCert, Verify) -> validate_https(Conf, MaxConnection, SSLCert, Verify) ->
emqx_common_test_helpers:load_config(emqx_dashboard_schema, Conf), emqx_common_test_helpers:load_config(emqx_dashboard_schema, Conf),
emqx_mgmt_api_test_util:init_suite([emqx_management], fun(X) -> X end), emqx_mgmt_api_test_util:init_suite([emqx_management], fun(X) -> X end),
try
assert_ranch_options(MaxConnection, SSLCert, Verify), assert_ranch_options(MaxConnection, SSLCert, Verify),
assert_https_request(), assert_https_request()
emqx_mgmt_api_test_util:end_suite([emqx_management]). after
emqx_mgmt_api_test_util:end_suite([emqx_management])
end.
assert_ranch_options(MaxConnections0, SSLCert, Verify) -> assert_ranch_options(MaxConnections0, SSLCert, Verify) ->
Middlewares = [emqx_dashboard_middleware, cowboy_router, cowboy_handler], Middlewares = [emqx_dashboard_middleware, cowboy_router, cowboy_handler],
@ -286,10 +306,10 @@ assert_https_request() ->
lists:foreach( lists:foreach(
fun(Path) -> fun(Path) ->
ApiPath = https_api_path([Path]), ApiPath = https_api_path([Path]),
?assertMatch( case emqx_dashboard_SUITE:request_dashboard(get, ApiPath, Headers) of
{ok, _}, {ok, _} -> ok;
emqx_dashboard_SUITE:request_dashboard(get, ApiPath, Headers) {error, Reason} -> error({https_client_error, Reason})
) end
end, end,
?OVERVIEWS ?OVERVIEWS
). ).

View File

@ -155,7 +155,7 @@ ssl_opts(Endpoint, SSLOpts) ->
[] []
end. end.
is_ssl(<<"https://", _/binary>> = _Endpoint) -> is_ssl(<<"https://", _/binary>>) ->
true; true;
is_ssl(_Endpoint) -> is_ssl(<<"http://", _/binary>>) ->
false. false.