From c0b5b887c35c1d324a98e877423245f21cdae956 Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Fri, 21 Oct 2022 14:58:04 -0300 Subject: [PATCH 1/2] fix(mgmt_api): return 503 when emqx is not running in `/status` --- .../src/emqx_management.app.src | 2 +- apps/emqx_management/src/emqx_mgmt_http.erl | 6 +- .../test/emqx_mgmt_api_SUITE.erl | 69 +++++++++++++++++++ .../test/emqx_mgmt_api_test_helpers.erl | 12 +++- changes/v4.3.22-en.md | 2 + changes/v4.3.22-zh.md | 2 + 6 files changed, 89 insertions(+), 4 deletions(-) diff --git a/apps/emqx_management/src/emqx_management.app.src b/apps/emqx_management/src/emqx_management.app.src index 1003a1140..5ab803c51 100644 --- a/apps/emqx_management/src/emqx_management.app.src +++ b/apps/emqx_management/src/emqx_management.app.src @@ -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]}, diff --git a/apps/emqx_management/src/emqx_mgmt_http.erl b/apps/emqx_management/src/emqx_mgmt_http.erl index fae1bd86a..8cddc11b2 100644 --- a/apps/emqx_management/src/emqx_mgmt_http.erl +++ b/apps/emqx_management/src/emqx_mgmt_http.erl @@ -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). diff --git a/apps/emqx_management/test/emqx_mgmt_api_SUITE.erl b/apps/emqx_management/test/emqx_mgmt_api_SUITE.erl index fda5c2adf..6b15bcfff 100644 --- a/apps/emqx_management/test/emqx_mgmt_api_SUITE.erl +++ b/apps/emqx_management/test/emqx_mgmt_api_SUITE.erl @@ -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 diff --git a/apps/emqx_management/test/emqx_mgmt_api_test_helpers.erl b/apps/emqx_management/test/emqx_mgmt_api_test_helpers.erl index a943ca760..952d71b9b 100644 --- a/apps/emqx_management/test/emqx_mgmt_api_test_helpers.erl +++ b/apps/emqx_management/test/emqx_mgmt_api_test_helpers.erl @@ -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]), diff --git a/changes/v4.3.22-en.md b/changes/v4.3.22-en.md index 77a197624..7851139e2 100644 --- a/changes/v4.3.22-en.md +++ b/changes/v4.3.22-en.md @@ -15,3 +15,5 @@ - "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. Before the fix, it always returned 200 even if the EMQX application was not running. Now it returns 503 in that case. diff --git a/changes/v4.3.22-zh.md b/changes/v4.3.22-zh.md index 58c82588d..6865a8614 100644 --- a/changes/v4.3.22-zh.md +++ b/changes/v4.3.22-zh.md @@ -15,3 +15,5 @@ - 限速 “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`端点的响应状态代码。 在修复之前,它总是返回200,即使EMQX应用程序没有运行。 现在它在这种情况下返回503。 From 51f2414eaa70e1f631d9a72e92620946899a9fd8 Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Mon, 24 Oct 2022 15:24:49 -0300 Subject: [PATCH 2/2] docs: improve changelog Co-authored-by: Zaiming (Stone) Shi --- changes/v4.3.22-en.md | 3 ++- changes/v4.3.22-zh.md | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/changes/v4.3.22-en.md b/changes/v4.3.22-en.md index 7851139e2..db76d3423 100644 --- a/changes/v4.3.22-en.md +++ b/changes/v4.3.22-en.md @@ -16,4 +16,5 @@ - 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. Before the fix, it always returned 200 even if the EMQX application was not running. Now it returns 503 in that case. +- 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. diff --git a/changes/v4.3.22-zh.md b/changes/v4.3.22-zh.md index 6865a8614..e7d343db0 100644 --- a/changes/v4.3.22-zh.md +++ b/changes/v4.3.22-zh.md @@ -16,4 +16,5 @@ - 保留老的 `emqx_auth_jwt` 模块的接口函数,保障热升级之前添加的回调函数在热升级之后也不会失效 [#9144](https://github.com/emqx/emqx/pull/9144)。 -- 修正了`/status`端点的响应状态代码。 在修复之前,它总是返回200,即使EMQX应用程序没有运行。 现在它在这种情况下返回503。 +- 修正了 `/status` API 的响应状态代码 [#9210](https://github.com/emqx/emqx/pull/9210)。 + 在修复之前,它总是返回 `200`,即使 EMQX 应用程序没有运行。 现在它在这种情况下返回 `503`。