chore: apply review suggestions

This commit is contained in:
Zhongwen Deng 2023-04-21 11:54:11 +08:00
parent ad6090a778
commit fdf9b2a383
5 changed files with 101 additions and 119 deletions

View File

@ -264,10 +264,9 @@ transform_header_name(Headers) ->
). ).
check_ssl_opts(Conf) -> check_ssl_opts(Conf) ->
case get_conf_val("url", Conf) of case is_backend_http(Conf) of
undefined -> true ->
ok; Url = get_conf_val("url", Conf),
Url ->
{BaseUrl, _Path, _Query} = parse_url(Url), {BaseUrl, _Path, _Query} = parse_url(Url),
case BaseUrl of case BaseUrl of
<<"https://", _/binary>> -> <<"https://", _/binary>> ->
@ -279,14 +278,15 @@ check_ssl_opts(Conf) ->
end; end;
<<"http://", _/binary>> -> <<"http://", _/binary>> ->
ok ok
end end;
false ->
ok
end. end.
check_headers(Conf) -> check_headers(Conf) ->
case get_conf_val("headers", Conf) of case is_backend_http(Conf) of
undefined -> true ->
ok; Headers = get_conf_val("headers", Conf),
Headers ->
case to_bin(get_conf_val("method", Conf)) of case to_bin(get_conf_val("method", Conf)) of
<<"post">> -> <<"post">> ->
ok; ok;
@ -295,7 +295,15 @@ check_headers(Conf) ->
false -> ok; false -> ok;
true -> <<"HTTP GET requests cannot include content-type header.">> true -> <<"HTTP GET requests cannot include content-type header.">>
end end
end end;
false ->
ok
end.
is_backend_http(Conf) ->
case get_conf_val("backend", Conf) of
http -> true;
_ -> false
end. end.
parse_url(Url) -> parse_url(Url) ->

View File

@ -1,7 +1,7 @@
%% -*- mode: erlang -*- %% -*- mode: erlang -*-
{application, emqx_authz, [ {application, emqx_authz, [
{description, "An OTP application"}, {description, "An OTP application"},
{vsn, "0.1.17"}, {vsn, "0.1.18"},
{registered, []}, {registered, []},
{mod, {emqx_authz_app, []}}, {mod, {emqx_authz_app, []}},
{applications, [ {applications, [

View File

@ -5,27 +5,28 @@
-module(emqx_conf_schema_tests). -module(emqx_conf_schema_tests).
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
%% erlfmt-ignore
-define(BASE_CONF, -define(BASE_CONF,
"" """
"\n" node {
" node {\n" name = \"emqx1@127.0.0.1\"
" name = \"emqx1@127.0.0.1\"\n" cookie = \"emqxsecretcookie\"
" cookie = \"emqxsecretcookie\"\n" data_dir = \"data\"
" data_dir = \"data\"\n" }
" }\n" cluster {
" cluster {\n" name = emqxcl
" name = emqxcl\n" discovery_strategy = static
" discovery_strategy = static\n" static.seeds = ~p
" static.seeds = ~p\n" core_nodes = ~p
" core_nodes = ~p\n" }
" }\n" """).
""
).
array_nodes_test() -> array_nodes_test() ->
ExpectNodes = ['emqx1@127.0.0.1', 'emqx2@127.0.0.1'], ExpectNodes = ['emqx1@127.0.0.1', 'emqx2@127.0.0.1'],
lists:foreach( lists:foreach(
fun(Nodes) -> fun(Nodes) ->
ConfFile = iolist_to_binary(io_lib:format(?BASE_CONF, [Nodes, Nodes])), ConfFile = to_bin(?BASE_CONF, [Nodes, Nodes]),
{ok, Conf} = hocon:binary(ConfFile, #{format => richmap}), {ok, Conf} = hocon:binary(ConfFile, #{format => richmap}),
ConfList = hocon_tconf:generate(emqx_conf_schema, Conf), ConfList = hocon_tconf:generate(emqx_conf_schema, Conf),
ClusterDiscovery = proplists:get_value( ClusterDiscovery = proplists:get_value(
@ -46,101 +47,73 @@ array_nodes_test() ->
), ),
ok. ok.
%% erlfmt-ignore
-define(BASE_AUTHN_ARRAY,
"""
authentication = [
{backend = \"http\"
body {password = \"${password}\", username = \"${username}\"}
connect_timeout = \"15s\"
enable_pipelining = 100
headers {\"content-type\" = \"application/json\"}
mechanism = \"password_based\"
method = \"~p\"
pool_size = 8
request_timeout = \"5s\"
ssl {enable = ~p, verify = \"verify_peer\"}
url = \"~ts\"
}
]
"""
).
-define(ERROR(Reason),
{emqx_conf_schema, [
#{
kind := validation_error,
reason := integrity_validation_failure,
result := _,
validation_name := Reason
}
]}
).
authn_validations_test() -> authn_validations_test() ->
BaseConf = iolist_to_binary(io_lib:format(?BASE_CONF, ["emqx1@127.0.0.1", "emqx1@127.0.0.1"])), BaseConf = to_bin(?BASE_CONF, ["emqx1@127.0.0.1", "emqx1@127.0.0.1"]),
DisableSSLWithHttps =
"" OKHttps = to_bin(?BASE_AUTHN_ARRAY, [post, true, <<"https://127.0.0.1:8080">>]),
"\n" Conf0 = <<BaseConf/binary, OKHttps/binary>>,
"authentication = [\n" {ok, ConfMap0} = hocon:binary(Conf0, #{format => richmap}),
"{backend = \"http\"\n" ?assert(is_list(hocon_tconf:generate(emqx_conf_schema, ConfMap0))),
"body {password = \"${password}\", username = \"${username}\"}\n"
"connect_timeout = \"15s\"\n" OKHttp = to_bin(?BASE_AUTHN_ARRAY, [post, false, <<"http://127.0.0.1:8080">>]),
"enable_pipelining = 100\n" Conf1 = <<BaseConf/binary, OKHttp/binary>>,
"headers {\"content-type\" = \"application/json\"}\n"
"mechanism = \"password_based\"\n"
"method = \"post\"\n"
"pool_size = 8\n"
"request_timeout = \"5s\"\n"
"ssl {enable = false, verify = \"verify_peer\"}\n"
"url = \"https://127.0.0.1:8080\"\n"
"}\n"
"]\n"
"",
Conf = <<BaseConf/binary, (list_to_binary(DisableSSLWithHttps))/binary>>,
{ok, ConfMap} = hocon:binary(Conf, #{format => richmap}),
?assertThrow(
{emqx_conf_schema, [
#{
kind := validation_error,
reason := integrity_validation_failure,
result := _,
validation_name := check_http_ssl_opts
}
]},
hocon_tconf:generate(emqx_conf_schema, ConfMap)
),
BadHeader =
""
"\n"
"authentication = [\n"
"{backend = \"http\"\n"
"body {password = \"${password}\", username = \"${username}\"}\n"
"connect_timeout = \"15s\"\n"
"enable_pipelining = 100\n"
"headers {\"content-type\" = \"application/json\"}\n"
"mechanism = \"password_based\"\n"
"method = \"get\"\n"
"pool_size = 8\n"
"request_timeout = \"5s\"\n"
"ssl {enable = false, verify = \"verify_peer\"}\n"
"url = \"http://127.0.0.1:8080\"\n"
"}\n"
"]\n"
"",
Conf1 = <<BaseConf/binary, (list_to_binary(BadHeader))/binary>>,
{ok, ConfMap1} = hocon:binary(Conf1, #{format => richmap}), {ok, ConfMap1} = hocon:binary(Conf1, #{format => richmap}),
?assertThrow( ?assert(is_list(hocon_tconf:generate(emqx_conf_schema, ConfMap1))),
{emqx_conf_schema, [
#{ DisableSSLWithHttps = to_bin(?BASE_AUTHN_ARRAY, [post, false, <<"https://127.0.0.1:8080">>]),
kind := validation_error, Conf2 = <<BaseConf/binary, DisableSSLWithHttps/binary>>,
reason := integrity_validation_failure,
result := _,
validation_name := check_http_headers
}
]},
hocon_tconf:generate(emqx_conf_schema, ConfMap1)
),
BadHeader2 =
""
"\n"
"authentication = \n"
"{backend = \"http\"\n"
"body {password = \"${password}\", username = \"${username}\"}\n"
"connect_timeout = \"15s\"\n"
"enable_pipelining = 100\n"
"headers {\"content-type\" = \"application/json\"}\n"
"mechanism = \"password_based\"\n"
"method = \"get\"\n"
"pool_size = 8\n"
"request_timeout = \"5s\"\n"
"ssl {enable = false, verify = \"verify_peer\"}\n"
"url = \"http://127.0.0.1:8080\"\n"
"}\n"
"\n"
"",
Conf2 = <<BaseConf/binary, (list_to_binary(BadHeader2))/binary>>,
{ok, ConfMap2} = hocon:binary(Conf2, #{format => richmap}), {ok, ConfMap2} = hocon:binary(Conf2, #{format => richmap}),
?assertThrow( ?assertThrow(
{emqx_conf_schema, [ ?ERROR(check_http_ssl_opts),
#{
kind := validation_error,
reason := integrity_validation_failure,
result := _,
validation_name := check_http_headers
}
]},
hocon_tconf:generate(emqx_conf_schema, ConfMap2) hocon_tconf:generate(emqx_conf_schema, ConfMap2)
), ),
BadHeader = to_bin(?BASE_AUTHN_ARRAY, [get, true, <<"https://127.0.0.1:8080">>]),
Conf3 = <<BaseConf/binary, BadHeader/binary>>,
{ok, ConfMap3} = hocon:binary(Conf3, #{format => richmap}),
?assertThrow(
?ERROR(check_http_headers),
hocon_tconf:generate(emqx_conf_schema, ConfMap3)
),
BadHeaderWithTuple = binary:replace(BadHeader, [<<"[">>, <<"]">>], <<"">>, [global]),
Conf4 = <<BaseConf/binary, BadHeaderWithTuple/binary>>,
{ok, ConfMap4} = hocon:binary(Conf4, #{format => richmap}),
?assertThrow(
?ERROR(check_http_headers),
hocon_tconf:generate(emqx_conf_schema, ConfMap4)
),
ok. ok.
doc_gen_test() -> doc_gen_test() ->
@ -163,3 +136,6 @@ doc_gen_test() ->
ok ok
end end
}. }.
to_bin(Format, Args) ->
iolist_to_binary(io_lib:format(Format, Args)).

View File

@ -1,7 +1,7 @@
%% -*- mode: erlang -*- %% -*- mode: erlang -*-
{application, emqx_connector, [ {application, emqx_connector, [
{description, "EMQX Data Integration Connectors"}, {description, "EMQX Data Integration Connectors"},
{vsn, "0.1.20"}, {vsn, "0.1.21"},
{registered, []}, {registered, []},
{mod, {emqx_connector_app, []}}, {mod, {emqx_connector_app, []}},
{applications, [ {applications, [

View File

@ -1,2 +0,0 @@
1. Add more VM Metrics
2. Add more emqx Metrics