From 2cc1c563df26771cc488ae26a0391f0d5e9934ac Mon Sep 17 00:00:00 2001 From: zmstone Date: Fri, 8 Mar 2024 16:46:54 +0100 Subject: [PATCH 1/3] fix(mgmt): avoid using base64:decode/2 and encode/2 they are not available in otp 25 --- apps/emqx_management/include/emqx_mgmt.hrl | 3 +++ apps/emqx_management/src/emqx_mgmt_api.erl | 23 +++++++++---------- .../src/emqx_mgmt_api_clients.erl | 4 ++-- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/apps/emqx_management/include/emqx_mgmt.hrl b/apps/emqx_management/include/emqx_mgmt.hrl index 8ad1cd871..a802bad21 100644 --- a/apps/emqx_management/include/emqx_mgmt.hrl +++ b/apps/emqx_management/include/emqx_mgmt.hrl @@ -15,3 +15,6 @@ %%-------------------------------------------------------------------- -define(DEFAULT_ROW_LIMIT, 100). + +-define(URL_PARAM_INTEGER, url_param_integer). +-define(URL_PARAM_BINARY, url_param_binary). diff --git a/apps/emqx_management/src/emqx_mgmt_api.erl b/apps/emqx_management/src/emqx_mgmt_api.erl index bd3e5723c..abb4e18d5 100644 --- a/apps/emqx_management/src/emqx_mgmt_api.erl +++ b/apps/emqx_management/src/emqx_mgmt_api.erl @@ -17,13 +17,12 @@ -module(emqx_mgmt_api). -include_lib("stdlib/include/qlc.hrl"). +-include("emqx_mgmt.hrl"). -elvis([{elvis_style, dont_repeat_yourself, #{min_complexity => 100}}]). -define(LONG_QUERY_TIMEOUT, 50000). --define(CONT_BASE64_OPTS, #{mode => urlsafe, padding => false}). - -export([ paginate/3 ]). @@ -151,19 +150,19 @@ decode_continuation(none, _Encoding) -> decode_continuation(end_of_data, _Encoding) -> %% Clients should not send "after=end_of_data" back to the server error; -decode_continuation(Cont, none) -> - Cont; -decode_continuation(Cont, base64) -> - base64:decode(Cont, ?CONT_BASE64_OPTS). +decode_continuation(Cont, ?URL_PARAM_INTEGER) -> + binary_to_integer(Cont); +decode_continuation(Cont, ?URL_PARAM_BINARY) -> + emqx_utils:hexstr_to_bin(Cont). encode_continuation(none, _Encoding) -> none; encode_continuation(end_of_data, _Encoding) -> end_of_data; -encode_continuation(Cont, none) -> - emqx_utils_conv:bin(Cont); -encode_continuation(Cont, base64) -> - base64:encode(emqx_utils_conv:bin(Cont), ?CONT_BASE64_OPTS). +encode_continuation(Cont, ?URL_PARAM_INTEGER) -> + integer_to_binary(Cont); +encode_continuation(Cont, ?URL_PARAM_BINARY) -> + emqx_utils:bin_to_hexstr(Cont). %%-------------------------------------------------------------------- %% Node Query @@ -663,7 +662,7 @@ parse_pager_params(Params) -> false end. --spec parse_cont_pager_params(map(), none | base64) -> +-spec parse_cont_pager_params(map(), ?URL_PARAM_INTEGER | ?URL_PARAM_BINARY) -> #{limit := pos_integer(), continuation := none | end_of_table | binary()} | false. parse_cont_pager_params(Params, Encoding) -> Cont = continuation(Params, Encoding), @@ -675,7 +674,7 @@ parse_cont_pager_params(Params, Encoding) -> false end. --spec encode_cont_pager_params(map(), none | base64) -> map(). +-spec encode_cont_pager_params(map(), ?URL_PARAM_INTEGER | ?URL_PARAM_BINARY) -> map(). encode_cont_pager_params(#{continuation := Cont} = Meta, ContEncoding) -> Meta1 = maps:remove(continuation, Meta), Meta1#{last => encode_continuation(Cont, ContEncoding)}; diff --git a/apps/emqx_management/src/emqx_mgmt_api_clients.erl b/apps/emqx_management/src/emqx_mgmt_api_clients.erl index 3555b5df6..98a6d64d5 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_clients.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_clients.erl @@ -1166,9 +1166,9 @@ maybe_cast_cont(_, PagerParams) -> PagerParams. %% integer packet id -cont_encoding(inflight_msgs) -> none; +cont_encoding(inflight_msgs) -> ?URL_PARAM_INTEGER; %% binary message id -cont_encoding(mqueue_msgs) -> base64. +cont_encoding(mqueue_msgs) -> ?URL_PARAM_BINARY. %%-------------------------------------------------------------------- %% QueryString to Match Spec From f376aa80726bc122460de6f4da55efef14e63021 Mon Sep 17 00:00:00 2001 From: Serge Tupchii Date: Mon, 11 Mar 2024 10:39:09 +0200 Subject: [PATCH 2/3] refactor(emqx_mgmt_api_clients): remove unnecessary function --- .../src/emqx_mgmt_api_clients.erl | 20 +------------------ 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/apps/emqx_management/src/emqx_mgmt_api_clients.erl b/apps/emqx_management/src/emqx_mgmt_api_clients.erl index 98a6d64d5..cf80f7e73 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_clients.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_clients.erl @@ -1135,7 +1135,7 @@ remove_live_sessions(Rows) -> ). list_client_msgs(MsgType, ClientID, QString) -> - case parse_cont_pager_params(QString, MsgType) of + case emqx_mgmt_api:parse_cont_pager_params(QString, cont_encoding(MsgType)) of false -> {400, #{code => <<"INVALID_PARAMETER">>, message => <<"after_limit_invalid">>}}; PagerParams = #{} -> @@ -1147,24 +1147,6 @@ list_client_msgs(MsgType, ClientID, QString) -> end end. -parse_cont_pager_params(QString, MsgType) -> - case emqx_mgmt_api:parse_cont_pager_params(QString, cont_encoding(MsgType)) of - false -> - false; - PagerParams -> - maybe_cast_cont(MsgType, PagerParams) - end. - -maybe_cast_cont(inflight_msgs, #{continuation := Cont} = PagerParams) when is_binary(Cont) -> - try - PagerParams#{continuation => emqx_utils_conv:int(Cont)} - catch - _:_ -> - false - end; -maybe_cast_cont(_, PagerParams) -> - PagerParams. - %% integer packet id cont_encoding(inflight_msgs) -> ?URL_PARAM_INTEGER; %% binary message id From 980a4c3a4da56e2ae7edd9f29e6ab9790d23a8b6 Mon Sep 17 00:00:00 2001 From: Serge Tupchii Date: Mon, 11 Mar 2024 10:40:19 +0200 Subject: [PATCH 3/3] fix(emqx_mgmt_api): use emqx_utils:bin_to_hexstr/2 --- apps/emqx_management/src/emqx_mgmt_api.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/emqx_management/src/emqx_mgmt_api.erl b/apps/emqx_management/src/emqx_mgmt_api.erl index abb4e18d5..69081fbf1 100644 --- a/apps/emqx_management/src/emqx_mgmt_api.erl +++ b/apps/emqx_management/src/emqx_mgmt_api.erl @@ -162,7 +162,7 @@ encode_continuation(end_of_data, _Encoding) -> encode_continuation(Cont, ?URL_PARAM_INTEGER) -> integer_to_binary(Cont); encode_continuation(Cont, ?URL_PARAM_BINARY) -> - emqx_utils:bin_to_hexstr(Cont). + emqx_utils:bin_to_hexstr(Cont, lower). %%-------------------------------------------------------------------- %% Node Query