Merge pull request #9950 from zhongwencool/prometheus_push_server_url_check

fix: add push_gateway_server's  validator
This commit is contained in:
Zaiming (Stone) Shi 2023-02-10 12:41:50 +01:00 committed by GitHub
commit 5d59fcbf23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -26,7 +26,8 @@
fields/1,
desc/1,
translation/1,
convert_headers/1
convert_headers/1,
validate_push_gateway_server/1
]).
namespace() -> "prometheus".
@ -41,6 +42,7 @@ fields("prometheus") ->
#{
default => "http://127.0.0.1:9091",
required => true,
validator => fun ?MODULE:validate_push_gateway_server/1,
desc => ?DESC(push_gateway_server)
}
)},
@ -158,6 +160,12 @@ convert_headers(Headers) when is_map(Headers) ->
convert_headers(Headers) when is_list(Headers) ->
Headers.
validate_push_gateway_server(Url) ->
case uri_string:parse(Url) of
#{scheme := S} when S =:= "https" orelse S =:= "http" -> ok;
_ -> {error, "Invalid url"}
end.
%% for CI test, CI don't load the whole emqx_conf_schema.
translation(Name) ->
emqx_conf_schema:translation(Name).

View File

@ -92,6 +92,12 @@ t_prometheus_api(_) ->
NewConf1 = Conf#{<<"enable">> => (not Enable)},
{ok, _Response3} = emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, NewConf1),
?assertEqual((not Enable), undefined =/= erlang:whereis(emqx_prometheus)),
ConfWithoutScheme = Conf#{<<"push_gateway_server">> => "127.0.0.1:8081"},
?assertMatch(
{error, {"HTTP/1.1", 400, _}},
emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, ConfWithoutScheme)
),
ok.
t_stats_api(_) ->