Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validate for topic name #212

Merged
merged 1 commit into from
Apr 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 29 additions & 8 deletions src/emqx_mgmt_api_topic_metrics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,14 @@
list(#{topic := Topic0}, _Params) ->
execute_when_enabled(fun() ->
Topic = http_uri:decode(Topic0),
case emqx_mgmt:get_topic_metrics(Topic) of
{error, Reason} -> return({error, Reason});
Metrics -> return({ok, maps:from_list(Metrics)})
case safe_validate(Topic) of
true ->
case emqx_mgmt:get_topic_metrics(Topic) of
{error, Reason} -> return({error, Reason});
Metrics -> return({ok, maps:from_list(Metrics)})
end;
false ->
return({error, invalid_topic_name})
end
end);

Expand All @@ -76,8 +81,13 @@ register(_Bindings, Params) ->
undefined ->
return({error, missing_required_params});
Topic ->
emqx_mgmt:register_topic_metrics(Topic),
return(ok)
case safe_validate(Topic) of
true ->
emqx_mgmt:register_topic_metrics(Topic),
return(ok);
false ->
return({error, invalid_topic_name})
end
end
end).

Expand All @@ -90,8 +100,13 @@ unregister(Bindings, _Params) when map_size(Bindings) =:= 0 ->
unregister(#{topic := Topic0}, _Params) ->
execute_when_enabled(fun() ->
Topic = http_uri:decode(Topic0),
emqx_mgmt:unregister_topic_metrics(Topic),
return(ok)
case safe_validate(Topic) of
true ->
emqx_mgmt:unregister_topic_metrics(Topic),
return(ok);
false ->
return({error, invalid_topic_name})
end
end).

execute_when_enabled(Fun) ->
Expand All @@ -106,4 +121,10 @@ execute_when_enabled(Fun) ->
return({error, module_not_loaded})
end.


safe_validate(Topic) ->
try emqx_topic:validate(name, Topic) of
true -> true
catch
error:_Error ->
false
end.