Merge pull request #5848 from savonarola/mqtt-http-api-validations
Add validations to management API
This commit is contained in:
commit
da2c41702d
|
@ -1,6 +1,6 @@
|
||||||
{application, emqx_management,
|
{application, emqx_management,
|
||||||
[{description, "EMQ X Management API and CLI"},
|
[{description, "EMQ X Management API and CLI"},
|
||||||
{vsn, "4.3.6"}, % strict semver, bump manually!
|
{vsn, "4.3.7"}, % strict semver, bump manually!
|
||||||
{modules, []},
|
{modules, []},
|
||||||
{registered, [emqx_management_sup]},
|
{registered, [emqx_management_sup]},
|
||||||
{applications, [kernel,stdlib,minirest]},
|
{applications, [kernel,stdlib,minirest]},
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
%% -*- mode: erlang -*-
|
%% -*- mode: erlang -*-
|
||||||
{VSN,
|
{VSN,
|
||||||
[ {<<"4.3.[0-5]">>,
|
[ {<<"4.3.[0-6]">>,
|
||||||
[ {apply,{minirest,stop_http,['http:management']}},
|
[ {apply,{minirest,stop_http,['http:management']}},
|
||||||
{apply,{minirest,stop_http,['https:management']}},
|
{apply,{minirest,stop_http,['https:management']}},
|
||||||
{restart_application, emqx_management}
|
{restart_application, emqx_management}
|
||||||
]},
|
]},
|
||||||
{<<".*">>, []}
|
{<<".*">>, []}
|
||||||
],
|
],
|
||||||
[ {<<"4.3.[0-5]">>,
|
[ {<<"4.3.[0-6]">>,
|
||||||
[ {apply,{minirest,stop_http,['http:management']}},
|
[ {apply,{minirest,stop_http,['http:management']}},
|
||||||
{apply,{minirest,stop_http,['https:management']}},
|
{apply,{minirest,stop_http,['https:management']}},
|
||||||
{restart_application, emqx_management}
|
{restart_application, emqx_management}
|
||||||
|
|
|
@ -147,6 +147,8 @@ loop_unsubscribe([Params | ParamsN], Acc) ->
|
||||||
code => Code},
|
code => Code},
|
||||||
loop_unsubscribe(ParamsN, [Result | Acc]).
|
loop_unsubscribe(ParamsN, [Result | Acc]).
|
||||||
|
|
||||||
|
do_subscribe(ClientId, _Topics, _QoS) when not is_binary(ClientId) ->
|
||||||
|
{ok, ?ERROR8, <<"bad clientid: must be string">>};
|
||||||
do_subscribe(_ClientId, [], _QoS) ->
|
do_subscribe(_ClientId, [], _QoS) ->
|
||||||
{ok, ?ERROR15, bad_topic};
|
{ok, ?ERROR15, bad_topic};
|
||||||
do_subscribe(ClientId, Topics, QoS) ->
|
do_subscribe(ClientId, Topics, QoS) ->
|
||||||
|
@ -156,6 +158,8 @@ do_subscribe(ClientId, Topics, QoS) ->
|
||||||
_ -> ok
|
_ -> ok
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
do_publish(ClientId, _Topics, _Qos, _Retain, _Payload) when not is_binary(ClientId) ->
|
||||||
|
{ok, ?ERROR8, <<"bad clientid: must be string">>};
|
||||||
do_publish(_ClientId, [], _Qos, _Retain, _Payload) ->
|
do_publish(_ClientId, [], _Qos, _Retain, _Payload) ->
|
||||||
{ok, ?ERROR15, bad_topic};
|
{ok, ?ERROR15, bad_topic};
|
||||||
do_publish(ClientId, Topics, Qos, Retain, Payload) ->
|
do_publish(ClientId, Topics, Qos, Retain, Payload) ->
|
||||||
|
@ -166,6 +170,8 @@ do_publish(ClientId, Topics, Qos, Retain, Payload) ->
|
||||||
end, Topics),
|
end, Topics),
|
||||||
{ok, MsgIds}.
|
{ok, MsgIds}.
|
||||||
|
|
||||||
|
do_unsubscribe(ClientId, _Topic) when not is_binary(ClientId) ->
|
||||||
|
{ok, ?ERROR8, <<"bad clientid: must be string">>};
|
||||||
do_unsubscribe(ClientId, Topic) ->
|
do_unsubscribe(ClientId, Topic) ->
|
||||||
case validate_by_filter(Topic) of
|
case validate_by_filter(Topic) of
|
||||||
true ->
|
true ->
|
||||||
|
|
|
@ -403,6 +403,26 @@ t_pubsub(_) ->
|
||||||
<<"topic">> => <<"">>}),
|
<<"topic">> => <<"">>}),
|
||||||
?assertEqual(?ERROR15, get(<<"code">>, BadTopic3)),
|
?assertEqual(?ERROR15, get(<<"code">>, BadTopic3)),
|
||||||
|
|
||||||
|
|
||||||
|
{ok, BadClient1} = request_api(post, api_path(["mqtt/subscribe"]), [], auth_header_(),
|
||||||
|
#{<<"clientid">> => 1,
|
||||||
|
<<"topics">> => <<"mytopic">>,
|
||||||
|
<<"qos">> => 2}),
|
||||||
|
?assertEqual(?ERROR8, get(<<"code">>, BadClient1)),
|
||||||
|
|
||||||
|
{ok, BadClient2} = request_api(post, api_path(["mqtt/publish"]), [], auth_header_(),
|
||||||
|
#{<<"clientid">> => 1,
|
||||||
|
<<"topics">> => <<"mytopic">>,
|
||||||
|
<<"qos">> => 1,
|
||||||
|
<<"payload">> => <<"hello">>}),
|
||||||
|
?assertEqual(?ERROR8, get(<<"code">>, BadClient2)),
|
||||||
|
|
||||||
|
{ok, BadClient3} = request_api(post, api_path(["mqtt/unsubscribe"]), [], auth_header_(),
|
||||||
|
#{<<"clientid">> => 1,
|
||||||
|
<<"topic">> => <<"mytopic">>}),
|
||||||
|
?assertEqual(?ERROR8, get(<<"code">>, BadClient3)),
|
||||||
|
|
||||||
|
|
||||||
meck:new(emqx_mgmt, [passthrough, no_history]),
|
meck:new(emqx_mgmt, [passthrough, no_history]),
|
||||||
meck:expect(emqx_mgmt, unsubscribe, 2, fun(_, _) -> {error, undefined} end),
|
meck:expect(emqx_mgmt, unsubscribe, 2, fun(_, _) -> {error, undefined} end),
|
||||||
{ok, NotFound2} = request_api(post, api_path(["mqtt/unsubscribe"]), [], auth_header_(),
|
{ok, NotFound2} = request_api(post, api_path(["mqtt/unsubscribe"]), [], auth_header_(),
|
||||||
|
|
Loading…
Reference in New Issue