Merge pull request #9210 from thalesmg/fix-mgmt-endpoint-status-code-v43

fix(mgmt_api): return 503 when emqx is not running in `/status` (v4.3)
This commit is contained in:
Thales Macedo Garitezi 2022-10-24 16:24:53 -03:00 committed by GitHub
commit 41a488b6ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 91 additions and 4 deletions

View File

@ -1,6 +1,6 @@
{application, emqx_management,
[{description, "EMQ X Management API and CLI"},
{vsn, "4.3.18"}, % strict semver, bump manually!
{vsn, "4.3.19"}, % strict semver, bump manually!
{modules, []},
{registered, [emqx_management_sup]},
{applications, [kernel,stdlib,minirest]},

View File

@ -114,7 +114,11 @@ handle_request(<<"GET">>, <<"/status">>, Req) ->
end,
Status = io_lib:format("Node ~s is ~s~nemqx is ~s",
[node(), InternalStatus, AppStatus]),
cowboy_req:reply(200, #{<<"content-type">> => <<"text/plain">>}, Status, Req);
StatusCode = case AppStatus of
running -> 200;
not_running -> 503
end,
cowboy_req:reply(StatusCode, #{<<"content-type">> => <<"text/plain">>}, Status, Req);
handle_request(_Method, _Path, Req) ->
cowboy_req:reply(400, #{<<"content-type">> => <<"text/plain">>}, <<"Not found.">>, Req).

View File

@ -25,6 +25,8 @@
-include_lib("emqx/include/emqx_mqtt.hrl").
-include_lib("emqx_management/include/emqx_mgmt.hrl").
-define(HOST, "http://127.0.0.1:8081/").
-import(emqx_mgmt_api_test_helpers,
[request_api/3,
request_api/4,
@ -44,9 +46,31 @@ end_per_suite(Config) ->
emqx_ct_helpers:stop_apps([emqx_management]),
Config.
init_per_testcase(t_status_ok, Config) ->
ok = emqx_rule_registry:mnesia(boot),
ok = emqx_dashboard_admin:mnesia(boot),
application:ensure_all_started(emqx_rule_engine),
application:ensure_all_started(emqx_dashboard),
Config;
init_per_testcase(t_status_not_ok, Config) ->
ok = emqx_rule_registry:mnesia(boot),
ok = emqx_dashboard_admin:mnesia(boot),
application:ensure_all_started(emqx_rule_engine),
application:ensure_all_started(emqx_dashboard),
application:stop(emqx),
Config;
init_per_testcase(_, Config) ->
Config.
end_per_testcase(t_status_ok, _Config) ->
application:stop(emqx_rule_engine),
application:stop(emqx_dashboard),
ok;
end_per_testcase(t_status_not_ok, _Config) ->
application:stop(emqx_rule_engine),
application:stop(emqx_dashboard),
application:ensure_all_started(emqx),
ok;
end_per_testcase(_, Config) ->
Config.
@ -656,6 +680,51 @@ t_data_import_content(_) ->
application:stop(emqx_rule_engine),
application:stop(emqx_dashboard).
t_status_ok(_Config) ->
{ok, #{ body := Resp
, status_code := StatusCode
}} = do_request(#{method => get, path => ["status"], headers => [],
body => no_body}),
?assertMatch(
{match, _},
re:run(Resp, <<"emqx is running$">>)),
?assertEqual(200, StatusCode),
ok.
t_status_not_ok(_Config) ->
{ok, #{ body := Resp
, status_code := StatusCode
}} = do_request(#{method => get, path => ["status"], headers => [],
body => no_body}),
?assertMatch(
{match, _},
re:run(Resp, <<"emqx is not_running$">>)),
?assertEqual(503, StatusCode),
ok.
do_request(Opts) ->
#{ path := Path
, method := Method
, headers := Headers
, body := Body0
} = Opts,
URL = ?HOST ++ filename:join(Path),
Request = case Body0 of
no_body -> {URL, Headers};
{Encoding, Body} -> {URL, Headers, Encoding, Body}
end,
ct:pal("Method: ~p, Request: ~p", [Method, Request]),
case httpc:request(Method, Request, [], []) of
{error, socket_closed_remotely} ->
{error, socket_closed_remotely};
{ok, {{_, StatusCode, _}, Headers1, Body1}} ->
Body2 = case emqx_json:safe_decode(Body1, [return_maps]) of
{ok, Json} -> Json;
{error, _} -> Body1
end,
{ok, #{status_code => StatusCode, headers => Headers1, body => Body2}}
end.
filter(List, Key, Value) ->
lists:filter(fun(Item) ->
maps:get(Key, Item) == Value

View File

@ -36,13 +36,21 @@ request_api(Method, Url, QueryParams, Auth, []) ->
"" -> Url;
_ -> Url ++ "?" ++ QueryParams
end,
do_request_api(Method, {NewUrl, [Auth]});
Headers = case Auth of
no_auth -> [];
Header -> [Header]
end,
do_request_api(Method, {NewUrl, Headers});
request_api(Method, Url, QueryParams, Auth, Body) ->
NewUrl = case QueryParams of
"" -> Url;
_ -> Url ++ "?" ++ QueryParams
end,
do_request_api(Method, {NewUrl, [Auth], "application/json", emqx_json:encode(Body)}).
Headers = case Auth of
no_auth -> [];
Header -> [Header]
end,
do_request_api(Method, {NewUrl, Headers, "application/json", emqx_json:encode(Body)}).
do_request_api(Method, Request)->
ct:pal("Method: ~p, Request: ~p", [Method, Request]),

View File

@ -15,3 +15,6 @@
- "Pause due to rate limit" log level demoted from warning to notice [#9134](https://github.com/emqx/emqx/pull/9134).
- Restore old `emqx_auth_jwt` module API, so the hook callback functions registered in older version will not be invalidated after hot-upgrade [#9144](https://github.com/emqx/emqx/pull/9144).
- Fixed the response status code for the `/status` endpoint [#9210](https://github.com/emqx/emqx/pull/9210).
Before the fix, it always returned `200` even if the EMQX application was not running. Now it returns `503` in that case.

View File

@ -15,3 +15,6 @@
- 限速 “Pause due to rate limit” 的日志级别从原先的 `warning` 降级到 `notice` [#9134](https://github.com/emqx/emqx/pull/9134)。
- 保留老的 `emqx_auth_jwt` 模块的接口函数,保障热升级之前添加的回调函数在热升级之后也不会失效 [#9144](https://github.com/emqx/emqx/pull/9144)。
- 修正了 `/status` API 的响应状态代码 [#9210](https://github.com/emqx/emqx/pull/9210)。
在修复之前,它总是返回 `200`,即使 EMQX 应用程序没有运行。 现在它在这种情况下返回 `503`