From 86942ffa14995bf9bb976a3f78f01194a4b7d58a Mon Sep 17 00:00:00 2001 From: JianBo He Date: Mon, 25 Jan 2021 15:45:47 +0800 Subject: [PATCH 01/15] fix(webhook): fix bad https confs --- apps/emqx_web_hook/src/emqx_web_hook_app.erl | 10 +- .../test/emqx_web_hook_SUITE.erl | 97 ++++++-------- apps/emqx_web_hook/test/http_server.erl | 124 +++++++++++------- 3 files changed, 124 insertions(+), 107 deletions(-) diff --git a/apps/emqx_web_hook/src/emqx_web_hook_app.erl b/apps/emqx_web_hook/src/emqx_web_hook_app.erl index 2c0697c81..c427d2050 100644 --- a/apps/emqx_web_hook/src/emqx_web_hook_app.erl +++ b/apps/emqx_web_hook/src/emqx_web_hook_app.erl @@ -85,11 +85,9 @@ translate_env() -> true -> verify_peer; false -> verify_none end, - TLSOpts = lists:filter(fun({_K, V}) when V =:= <<>> -> - false; - (_) -> - true - end, [{keyfile, KeyFile}, {certfile, CertFile}, {cacertfile, CACertFile}]), + TLSOpts = lists:filter(fun({_K, V}) -> + V /= <<>> andalso V /= undefined andalso V /= "" andalso true + end, [{keyfile, KeyFile}, {certfile, CertFile}, {cacertfile, CACertFile}]), TlsVers = ['tlsv1.2','tlsv1.1',tlsv1], NTLSOpts = [{verify, VerifyType}, {versions, TlsVers}, @@ -118,4 +116,4 @@ path(Path) -> set_content_type(Headers) -> NHeaders = proplists:delete(<<"Content-Type">>, proplists:delete(<<"content-type">>, Headers)), - [{<<"content-type">>, <<"application/json">>} | NHeaders]. \ No newline at end of file + [{<<"content-type">>, <<"application/json">>} | NHeaders]. diff --git a/apps/emqx_web_hook/test/emqx_web_hook_SUITE.erl b/apps/emqx_web_hook/test/emqx_web_hook_SUITE.erl index cd370a57b..94ea212ec 100644 --- a/apps/emqx_web_hook/test/emqx_web_hook_SUITE.erl +++ b/apps/emqx_web_hook/test/emqx_web_hook_SUITE.erl @@ -47,49 +47,38 @@ init_per_group(Name, Config) -> set_special_cfgs(), case Name of http -> - http_server:start_http(), emqx_ct_helpers:start_apps([emqx_web_hook], fun set_special_configs_http/1); https -> - http_server:start_https(), emqx_ct_helpers:start_apps([emqx_web_hook], fun set_special_configs_https/1); ipv6http -> - http_server:start_http(), emqx_ct_helpers:start_apps([emqx_web_hook], fun set_special_configs_ipv6_http/1); ipv6https -> - http_server:start_https(), emqx_ct_helpers:start_apps([emqx_web_hook], fun set_special_configs_ipv6_https/1) end, Config. -end_per_group(Name, Config) -> - case lists:member(Name,[http, ipv6http]) of - true -> - http_server:stop_http(); - _ -> - http_server:stop_https() - end, +end_per_group(_Name, Config) -> emqx_ct_helpers:stop_apps([emqx_web_hook]), Config. set_special_configs_http(_) -> - ok. + application:set_env(emqx_web_hook, url, "http://127.0.0.1:9999"). set_special_configs_https(_) -> Path = emqx_ct_helpers:deps_path(emqx_web_hook, "test/emqx_web_hook_SUITE_data/"), SslOpts = [{keyfile, Path ++ "/client-key.pem"}, {certfile, Path ++ "/client-cert.pem"}, - {cacertfile, Path ++ "/ca.pem"}], + {cafile, Path ++ "/ca.pem"}], application:set_env(emqx_web_hook, ssl, true), application:set_env(emqx_web_hook, ssloptions, SslOpts), - application:set_env(emqx_web_hook, url, "https://127.0.0.1:8081"). + application:set_env(emqx_web_hook, url, "https://127.0.0.1:8888"). -set_special_configs_ipv6_http(N) -> - set_special_configs_http(N), - application:set_env(emqx_web_hook, url, "http://[::1]:8080"). +set_special_configs_ipv6_http(_) -> + application:set_env(emqx_web_hook, url, "http://[::1]:9999"). set_special_configs_ipv6_https(N) -> set_special_configs_https(N), - application:set_env(emqx_web_hook, url, "https://[::1]:8081"). + application:set_env(emqx_web_hook, url, "https://[::1]:8888"). set_special_cfgs() -> AllRules = [{"message.acked", "{\"action\": \"on_message_acked\"}"}, @@ -109,6 +98,27 @@ set_special_cfgs() -> %% Test cases %%-------------------------------------------------------------------- +t_valid(Config) -> + {ok, ServerPid} = http_server:start_link(), + application:set_env(emqx_web_hook, headers, [{"k1","K1"}, {"k2", "K2"}]), + {ok, C} = emqtt:start_link([ {clientid, <<"simpleClient">>} + , {proto_ver, v5} + , {keepalive, 60} + ]), + {ok, _} = emqtt:connect(C), + emqtt:subscribe(C, <<"TopicA">>, qos2), + emqtt:publish(C, <<"TopicA">>, <<"Payload...">>, qos2), + emqtt:unsubscribe(C, <<"TopicA">>), + emqtt:disconnect(C), + [begin + Maps = emqx_json:decode(P, [return_maps]), + validate_hook_resp(Maps), + validate_hook_headers(Headers) + end + || {{P, _Bool}, Headers} <- http_server:get_received_data()], + http_server:stop(ServerPid), + Config. + t_check_hooked(_) -> {ok, Rules} = application:get_env(emqx_web_hook, rules), lists:foreach(fun({HookName, _Action}) -> @@ -127,90 +137,69 @@ t_change_config(_) -> application:set_env(emqx_web_hook, rules, Rules), emqx_web_hook:load(). -t_valid() -> - application:set_env(emqx_web_hook, headers, [{"k1","K1"}, {"k2", "K2"}]), - {ok, C} = emqtt:start_link([ {clientid, <<"simpleClient">>} - , {proto_ver, v5} - , {keepalive, 60} - ]), - {ok, _} = emqtt:connect(C), - emqtt:subscribe(C, <<"TopicA">>, qos2), - emqtt:publish(C, <<"TopicA">>, <<"Payload...">>, qos2), - emqtt:unsubscribe(C, <<"TopicA">>), - emqtt:disconnect(C), - {Params, Headers} = get_http_message(), - [validate_hook_resp(A) || A <- Params], - ?assertEqual(<<"K1">>, maps:get(<<"k1">>, Headers)), - ?assertEqual(<<"K2">>, maps:get(<<"k2">>, Headers)). - %%-------------------------------------------------------------------- %% Utils %%-------------------------------------------------------------------- -get_http_message() -> - receive - {Params, Headers} -> - L = [B || {B, _} <- Params], - {lists:reverse([emqx_json:decode(E, [return_maps]) || E <- L]), Headers} - after 500 -> - {null, null} - end. +validate_hook_headers(Headers) -> + ?assertEqual(<<"K1">>, maps:get(<<"k1">>, Headers)), + ?assertEqual(<<"K2">>, maps:get(<<"k2">>, Headers)). validate_hook_resp(Body = ?ACTION(<<"client_connect">>)) -> ?assertEqual(5, maps:get(<<"proto_ver">>, Body)), ?assertEqual(60, maps:get(<<"keepalive">>, Body)), ?assertEqual(<<"127.0.0.1">>, maps:get(<<"ipaddress">>, Body)), - ?assertEqual(<<"emqx@127.0.0.1">>, maps:get(<<"node">>, Body)), + ?assertEqual(<<"test@127.0.0.1">>, maps:get(<<"node">>, Body)), assert_username_clientid(Body); validate_hook_resp(Body = ?ACTION(<<"client_connack">>)) -> ?assertEqual(5, maps:get(<<"proto_ver">>, Body)), ?assertEqual(60, maps:get(<<"keepalive">>, Body)), ?assertEqual(<<"success">>, maps:get(<<"conn_ack">>, Body)), ?assertEqual(<<"127.0.0.1">>, maps:get(<<"ipaddress">>, Body)), - ?assertEqual(<<"emqx@127.0.0.1">>, maps:get(<<"node">>, Body)), + ?assertEqual(<<"test@127.0.0.1">>, maps:get(<<"node">>, Body)), assert_username_clientid(Body); validate_hook_resp(Body = ?ACTION(<<"client_connected">>)) -> _ = maps:get(<<"connected_at">>, Body), ?assertEqual(5, maps:get(<<"proto_ver">>, Body)), ?assertEqual(60, maps:get(<<"keepalive">>, Body)), ?assertEqual(<<"127.0.0.1">>, maps:get(<<"ipaddress">>, Body)), - ?assertEqual(<<"emqx@127.0.0.1">>, maps:get(<<"node">>, Body)), + ?assertEqual(<<"test@127.0.0.1">>, maps:get(<<"node">>, Body)), assert_username_clientid(Body); validate_hook_resp(Body = ?ACTION(<<"client_disconnected">>)) -> ?assertEqual(<<"normal">>, maps:get(<<"reason">>, Body)), - ?assertEqual(<<"emqx@127.0.0.1">>, maps:get(<<"node">>, Body)), + ?assertEqual(<<"test@127.0.0.1">>, maps:get(<<"node">>, Body)), assert_username_clientid(Body); validate_hook_resp(Body = ?ACTION(<<"client_subscribe">>)) -> _ = maps:get(<<"opts">>, Body), ?assertEqual(<<"TopicA">>, maps:get(<<"topic">>, Body)), - ?assertEqual(<<"emqx@127.0.0.1">>, maps:get(<<"node">>, Body)), + ?assertEqual(<<"test@127.0.0.1">>, maps:get(<<"node">>, Body)), assert_username_clientid(Body); validate_hook_resp(Body = ?ACTION(<<"client_unsubscribe">>)) -> _ = maps:get(<<"opts">>, Body), ?assertEqual(<<"TopicA">>, maps:get(<<"topic">>, Body)), - ?assertEqual(<<"emqx@127.0.0.1">>, maps:get(<<"node">>, Body)), + ?assertEqual(<<"test@127.0.0.1">>, maps:get(<<"node">>, Body)), assert_username_clientid(Body); validate_hook_resp(Body = ?ACTION(<<"session_subscribed">>)) -> _ = maps:get(<<"opts">>, Body), ?assertEqual(<<"TopicA">>, maps:get(<<"topic">>, Body)), - ?assertEqual(<<"emqx@127.0.0.1">>, maps:get(<<"node">>, Body)), + ?assertEqual(<<"test@127.0.0.1">>, maps:get(<<"node">>, Body)), assert_username_clientid(Body); validate_hook_resp(Body = ?ACTION(<<"session_unsubscribed">>)) -> ?assertEqual(<<"TopicA">>, maps:get(<<"topic">>, Body)), - ?assertEqual(<<"emqx@127.0.0.1">>, maps:get(<<"node">>, Body)), + ?assertEqual(<<"test@127.0.0.1">>, maps:get(<<"node">>, Body)), assert_username_clientid(Body); validate_hook_resp(Body = ?ACTION(<<"session_terminated">>)) -> ?assertEqual(<<"normal">>, maps:get(<<"reason">>, Body)), - ?assertEqual(<<"emqx@127.0.0.1">>, maps:get(<<"node">>, Body)), + ?assertEqual(<<"test@127.0.0.1">>, maps:get(<<"node">>, Body)), assert_username_clientid(Body); validate_hook_resp(Body = ?ACTION(<<"message_publish">>)) -> - ?assertEqual(<<"emqx@127.0.0.1">>, maps:get(<<"node">>, Body)), + ?assertEqual(<<"test@127.0.0.1">>, maps:get(<<"node">>, Body)), assert_messages_attrs(Body); validate_hook_resp(Body = ?ACTION(<<"message_delivered">>)) -> - ?assertEqual(<<"emqx@127.0.0.1">>, maps:get(<<"node">>, Body)), + ?assertEqual(<<"test@127.0.0.1">>, maps:get(<<"node">>, Body)), assert_messages_attrs(Body); validate_hook_resp(Body = ?ACTION(<<"message_acked">>)) -> - ?assertEqual(<<"emqx@127.0.0.1">>, maps:get(<<"node">>, Body)), + ?assertEqual(<<"test@127.0.0.1">>, maps:get(<<"node">>, Body)), assert_messages_attrs(Body). assert_username_clientid(#{<<"clientid">> := ClientId, <<"username">> := Username}) -> diff --git a/apps/emqx_web_hook/test/http_server.erl b/apps/emqx_web_hook/test/http_server.erl index 2404c7e78..e0f367eba 100644 --- a/apps/emqx_web_hook/test/http_server.erl +++ b/apps/emqx_web_hook/test/http_server.erl @@ -3,73 +3,103 @@ %% %% It will deliver the http-request params to initialer process %%-------------------------------------------------------------------- +%% +%% Author:wwhai +%% -module(http_server). +-behaviour(gen_server). --compile(export_all). --compile(nowarn_export_all). - +-export([start_link/0]). +-export([get_received_data/0]). +-export([stop/1]). +-export([code_change/3, handle_call/3, handle_cast/2, handle_info/2, init/1, terminate/2]). +-define(HTTP_PORT, 9999). +-define(HTTPS_PORT, 8888). +-record(state, {}). %%-------------------------------------------------------------------- %% APIs %%-------------------------------------------------------------------- -start() -> - {ok, _} = application:ensure_all_started(cowboy), - cowboy_router:compile([ - {'_', [ - {"/", ?MODULE, self()} - ]} - ]). +start_link() -> + gen_server:start_link(?MODULE, [], []). -start_http() -> - {ok, _Pid1} = cowboy:start_clear(http, [{port, 8080}], #{ - env => #{dispatch => start()} - }), - io:format("Start http server on 8080 successfully!~n"). +init([]) -> + EtsOptions = [named_table, public, set, {write_concurrency, true}, + {read_concurrency, true}], + emqx_web_hook_http_test = ets:new(emqx_web_hook_http_test, EtsOptions), + ok = start_http(?HTTP_PORT), + ok = start_https(?HTTPS_PORT), + {ok, #state{}}. -start_https() -> - Path = emqx_ct_helpers:deps_path(emqx_web_hook, "test/emqx_web_hook_SUITE_data/"), - SslOpts = [{keyfile, Path ++ "/server-key.pem"}, - {cacertfile, Path ++ "/ca.pem"}, - {certfile, Path ++ "/server-cert.pem"}], +handle_call(_Request, _From, State) -> + {reply, ignored, State}. - {ok, _Pid2} = cowboy:start_tls(https, [{port, 8081}] ++ SslOpts, - #{env => #{dispatch => start()}}), - io:format(standard_error, "Start https server on 8081 successfully!~n", []). +handle_cast(_Msg, State) -> + {noreply, State}. -stop_http() -> - ok = cowboy:stop_listener(http), - io:format("Stopped http server on 8080"). +handle_info(_Info, State) -> + {noreply, State}. -stop_https() -> - ok = cowboy:stop_listener(https), - io:format("Stopped https server on 8081"). +terminate(_Reason, _State) -> + stop_http(), + stop_https(). +code_change(_OldVsn, State, _Extra) -> + {ok, State}. + +get_received_data() -> + ets:tab2list(emqx_web_hook_http_test). + +stop(Pid) -> + ok = gen_server:stop(Pid). %%-------------------------------------------------------------------- %% Callbacks %%-------------------------------------------------------------------- -init(Req, ReceiverPid) -> - Req1 = handle_request(Req, ReceiverPid), - {ok, Req1, ReceiverPid}. +start_http(Port) -> + {ok, _Pid1} = cowboy:start_clear(http, [{port, Port}], #{ + env => #{dispatch => compile_router()} + }), + io:format(standard_error, "[TEST LOG] Start http server on 9999 successfully!~n", []). -%% @private -handle_request(Req, ReceiverPid) -> +start_https(Port) -> + Path = emqx_ct_helpers:deps_path(emqx_web_hook, "test/emqx_web_hook_SUITE_data/"), + SslOpts = [{keyfile, Path ++ "/server-key.pem"}, + {cacertfile, Path ++ "/ca.pem"}, + {certfile, Path ++ "/server-cert.pem"}], + + {ok, _Pid2} = cowboy:start_tls(https, [{port, Port}] ++ SslOpts, + #{env => #{dispatch => compile_router()}}), + io:format(standard_error, "[TEST LOG] Start https server on 8888 successfully!~n", []). + +stop_http() -> + ok = cowboy:stop_listener(http), + io:format("[TEST LOG] Stopped http server on 9999"). + +stop_https() -> + ok = cowboy:stop_listener(https), + io:format("[TEST LOG] Stopped https server on 8888"). + +compile_router() -> + {ok, _} = application:ensure_all_started(cowboy), + cowboy_router:compile([ + {'_', [{"/", ?MODULE, #{}}]} + ]). + +init(Req, State) -> Method = cowboy_req:method(Req), Headers = cowboy_req:headers(Req), - Params = - case Method of - <<"GET">> -> cowboy_req:parse_qs(Req); - <<"POST">> -> - {ok, PostVals, _Req2} = cowboy_req:read_urlencoded_body(Req), - PostVals - end, - io:format("Request Data:~p~nHeaders :~p~n", [Params, Headers]), - erlang:send(ReceiverPid, {Params, Headers}), - reply(Req, ok). + [Params] = case Method of + <<"GET">> -> cowboy_req:parse_qs(Req); + <<"POST">> -> + {ok, PostVals, _} = cowboy_req:read_urlencoded_body(Req), + PostVals + end, + ets:insert(emqx_web_hook_http_test, {Params, Headers}), + {ok, reply(Req, ok), State}. -%% @private reply(Req, ok) -> - cowboy_req:reply(200, #{<<"content-type">> => <<"text/plain">>}, <<"hello">>, Req); + cowboy_req:reply(200, #{<<"content-type">> => <<"text/plain">>}, <<"ok">>, Req); reply(Req, error) -> - cowboy_req:reply(404, #{<<"content-type">> => <<"text/plain">>}, <<"deny">>, Req). + cowboy_req:reply(404, #{<<"content-type">> => <<"text/plain">>}, <<"deny">>, Req). \ No newline at end of file From 81da182e1bc7339168ea7eef2291b3f0146fc00c Mon Sep 17 00:00:00 2001 From: wwhai <751957846@qq.com> Date: Mon, 25 Jan 2021 19:43:44 +0800 Subject: [PATCH 02/15] feat(rule-engine): change init resource to test resource --- .../emqx_rule_engine/src/emqx_rule_engine.erl | 65 ++++++++++--------- .../src/emqx_rule_engine_api.erl | 10 +-- .../src/emqx_rule_engine_cli.erl | 10 +-- 3 files changed, 44 insertions(+), 41 deletions(-) diff --git a/apps/emqx_rule_engine/src/emqx_rule_engine.erl b/apps/emqx_rule_engine/src/emqx_rule_engine.erl index 4f41f9d55..bbf82f7db 100644 --- a/apps/emqx_rule_engine/src/emqx_rule_engine.erl +++ b/apps/emqx_rule_engine/src/emqx_rule_engine.erl @@ -249,13 +249,13 @@ create_resource(#{type := Type, config := Config0} = Params) -> update_resource(ResId, NewParams) -> try lists:foreach(fun(#rule{id = RuleId, enabled = Enabled, actions = Actions}) -> - lists:foreach( - fun (#action_instance{args = #{<<"$resource">> := ResId1}}) - when ResId =:= ResId1, Enabled == true -> - throw({dependency_exists, RuleId}); - (_) -> ok - end, Actions) - end, ets:tab2list(?RULE_TAB)), + lists:foreach( + fun (#action_instance{args = #{<<"$resource">> := ResId1}}) + when ResId =:= ResId1, Enabled == true -> + throw({dependency_exists, RuleId}); + (_) -> ok + end, Actions) + end, ets:tab2list(?RULE_TAB)), do_update_resource_check(ResId, NewParams) catch _ : Reason -> {error, Reason} @@ -267,37 +267,40 @@ do_update_resource_check(Id, NewParams) -> type = Type, config = OldConfig, description = OldDescription} = _OldResource} -> - try - do_update_resource(#{id => Id, - config => case maps:find(<<"config">>, NewParams) of - {ok, NewConfig} -> NewConfig; - error -> OldConfig - end, - type => Type, - description => case maps:find(<<"description">>, NewParams) of - {ok, NewDescription} -> NewDescription; - error -> OldDescription - end}), - ok - catch _ : Reason -> - {error, Reason} - end; + try + do_update_resource(#{id => Id, + config => case maps:find(<<"config">>, NewParams) of + {ok, NewConfig} -> NewConfig; + error -> OldConfig + end, + type => Type, + description => case maps:find(<<"description">>, NewParams) of + {ok, NewDescription} -> NewDescription; + error -> OldDescription + end}), + ok + catch _ : Reason -> + {error, Reason} + end; _Other -> {error, not_found} end. do_update_resource(#{id := Id, type := Type, description:= NewDescription, config:= NewConfig}) -> case emqx_rule_registry:find_resource_type(Type) of - {ok, #resource_type{on_create = {Module, Create}, - on_destroy = {Module, Destroy}, - params_spec = ParamSpec}} -> + {ok, #resource_type{params_spec = ParamSpec}} -> Config = emqx_rule_validator:validate_params(NewConfig, ParamSpec), - cluster_call(init_resource, [Module, Create, Id, Config]), - emqx_rule_registry:add_resource(#resource{id = Id, - type = Type, - config = Config, - description = NewDescription, - created_at = erlang:system_time(millisecond)}) + case test_resource(#{type => Type, config => NewConfig}) of + ok -> + Resource = #resource{id = Id, + type = Type, + config = Config, + description = NewDescription, + created_at = erlang:system_time(millisecond)}, + emqx_rule_registry:add_resource(Resource); + {error, Reason} -> + {error, Reason} + end end. -spec(start_resource(resource_id()) -> ok | {error, Reason :: term()}). diff --git a/apps/emqx_rule_engine/src/emqx_rule_engine_api.erl b/apps/emqx_rule_engine/src/emqx_rule_engine_api.erl index 987157d63..1c391525f 100644 --- a/apps/emqx_rule_engine/src/emqx_rule_engine_api.erl +++ b/apps/emqx_rule_engine/src/emqx_rule_engine_api.erl @@ -329,13 +329,13 @@ start_resource(#{id := Id}, _Params) -> update_resource(#{id := Id}, NewParams) -> P1 = case proplists:get_value(<<"description">>, NewParams) of - undefined -> #{}; - Value -> #{<<"description">> => Value} + undefined -> #{}; + Value -> #{<<"description">> => Value} end, P2 = case proplists:get_value(<<"config">>, NewParams) of - undefined -> #{}; - <<"{}">> -> #{}; - Map -> #{<<"config">> => ?RAISE(maps:from_list(Map), {invalid_config, Map})} + undefined -> #{}; + <<"{}">> -> #{}; + Map -> #{<<"config">> => ?RAISE(maps:from_list(Map), {invalid_config, Map})} end, case emqx_rule_engine:update_resource(Id, maps:merge(P1, P2)) of ok -> diff --git a/apps/emqx_rule_engine/src/emqx_rule_engine_cli.erl b/apps/emqx_rule_engine/src/emqx_rule_engine_cli.erl index 8f950ab43..55131408d 100644 --- a/apps/emqx_rule_engine/src/emqx_rule_engine_cli.erl +++ b/apps/emqx_rule_engine/src/emqx_rule_engine_cli.erl @@ -167,7 +167,7 @@ resources(["create" | Params]) -> resources(["update" | Params]) -> with_opts(fun({Opts, _}) -> - Id = maps:get(id, maps:from_list(Opts)), + Id = proplists:get_value(id, Opts), Maps = make_updated_resource(Opts), case emqx_rule_engine:update_resource(Id, Maps) of ok -> @@ -324,12 +324,12 @@ make_resource(Opts) -> make_updated_resource(Opts) -> P1 = case proplists:get_value(description, Opts) of - undefined -> #{}; - Value -> #{<<"description">> => Value} + undefined -> #{}; + Value -> #{<<"description">> => Value} end, P2 = case proplists:get_value(config, Opts) of - undefined -> #{}; - Map -> #{<<"config">> => ?RAISE((emqx_json:decode(Map, [return_maps])), {invalid_config, Map})} + undefined -> #{}; + Map -> #{<<"config">> => ?RAISE((emqx_json:decode(Map, [return_maps])), {invalid_config, Map})} end, maps:merge(P1, P2). From 068c3519368ea4136087f43b050aefa926f6afdf Mon Sep 17 00:00:00 2001 From: zhanghongtong Date: Tue, 26 Jan 2021 10:04:23 +0800 Subject: [PATCH 03/15] chore(docker): controls the timeout of the container through environmental variables --- deploy/charts/emqx/values.yaml | 2 +- deploy/docker/start.sh | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/deploy/charts/emqx/values.yaml b/deploy/charts/emqx/values.yaml index f75afcc06..45b966c3b 100644 --- a/deploy/charts/emqx/values.yaml +++ b/deploy/charts/emqx/values.yaml @@ -40,7 +40,7 @@ initContainers: {} # image: alpine # command: ["sh", "-c", "for i in $(seq 1 300); do nc -zvw1 mysql 3306 && exit 0 || sleep 3; done; exit 1"] -## EMQX configuration item, see the documentation (https://github.com/emqx/emqx-docker#emq-x-configuration) +## EMQX configuration item, see the documentation (https://hub.docker.com/r/emqx/emqx) emqxConfig: EMQX_CLUSTER__K8S__APISERVER: "https://kubernetes.default.svc:443" ## The address type is used to extract host from k8s service. diff --git a/deploy/docker/start.sh b/deploy/docker/start.sh index e4e590363..0964bdf63 100755 --- a/deploy/docker/start.sh +++ b/deploy/docker/start.sh @@ -1,6 +1,8 @@ #!/bin/sh set -e -u +EMQX_WAIT_TIME=${EMQX_WAIT_TIME:-5} + emqx_exit(){ # At least erlang.log.1 exists if [ -f /opt/emqx/log/erlang.log.1 ]; then @@ -53,7 +55,7 @@ while [ $IDLE_TIME -lt 5 ]; do else echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:emqx not running, waiting for recovery in $((25-IDLE_TIME*5)) seconds" fi - sleep 5 + sleep $EMQX_WAIT_TIME done # If running to here (the result 5 times not is running, thus in 25s emqx is not running), exit docker image From e7153c4266e839f3ea768d76790ca65f73753d6b Mon Sep 17 00:00:00 2001 From: wwhai <751957846@qq.com> Date: Wed, 27 Jan 2021 15:06:12 +0800 Subject: [PATCH 04/15] fix(rule-engine): add cluster init callback --- apps/emqx_rule_engine/src/emqx_rule_engine.erl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/emqx_rule_engine/src/emqx_rule_engine.erl b/apps/emqx_rule_engine/src/emqx_rule_engine.erl index bbf82f7db..c45a3a20c 100644 --- a/apps/emqx_rule_engine/src/emqx_rule_engine.erl +++ b/apps/emqx_rule_engine/src/emqx_rule_engine.erl @@ -288,7 +288,8 @@ do_update_resource_check(Id, NewParams) -> do_update_resource(#{id := Id, type := Type, description:= NewDescription, config:= NewConfig}) -> case emqx_rule_registry:find_resource_type(Type) of - {ok, #resource_type{params_spec = ParamSpec}} -> + {ok, #resource_type{on_create = {Module, Create}, + params_spec = ParamSpec}} -> Config = emqx_rule_validator:validate_params(NewConfig, ParamSpec), case test_resource(#{type => Type, config => NewConfig}) of ok -> @@ -297,6 +298,7 @@ do_update_resource(#{id := Id, type := Type, description:= NewDescription, confi config = Config, description = NewDescription, created_at = erlang:system_time(millisecond)}, + cluster_call(init_resource, [Module, Create, Id, Config]), emqx_rule_registry:add_resource(Resource); {error, Reason} -> {error, Reason} From fc3ebf41fa9d5dee9e3766b2fbe07d9745fc3757 Mon Sep 17 00:00:00 2001 From: zhanghongtong Date: Wed, 27 Jan 2021 13:59:28 +0800 Subject: [PATCH 05/15] fix(docker): fix docker entryponit script remove useless environmental variables update the key of the environment variable fix issue #4011 --- deploy/docker/docker-entrypoint.sh | 38 +++++++++++++++++------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/deploy/docker/docker-entrypoint.sh b/deploy/docker/docker-entrypoint.sh index 29c55bf83..77d40d4fa 100755 --- a/deploy/docker/docker-entrypoint.sh +++ b/deploy/docker/docker-entrypoint.sh @@ -62,32 +62,36 @@ if [[ -z "$EMQX_NODE__MAX_ETS_TABLES" ]]; then export EMQX_NODE__MAX_ETS_TABLES=2097152 fi -if [[ -z "$EMQX__LOG_CONSOLE" ]]; then - export EMQX__LOG_CONSOLE='console' -fi - if [[ -z "$EMQX_LISTENER__TCP__EXTERNAL__ACCEPTORS" ]]; then export EMQX_LISTENER__TCP__EXTERNAL__ACCEPTORS=64 fi -if [[ -z "$EMQX_LISTENER__TCP__EXTERNAL__MAX_CLIENTS" ]]; then - export EMQX_LISTENER__TCP__EXTERNAL__MAX_CLIENTS=1000000 +if [[ -z "$EMQX_LISTENER__TCP__EXTERNAL__MAX_CONNECTIONS" ]]; then + export EMQX_LISTENER__TCP__EXTERNAL__MAX_CONNECTIONS=1024000 fi if [[ -z "$EMQX_LISTENER__SSL__EXTERNAL__ACCEPTORS" ]]; then export EMQX_LISTENER__SSL__EXTERNAL__ACCEPTORS=32 fi -if [[ -z "$EMQX_LISTENER__SSL__EXTERNAL__MAX_CLIENTS" ]]; then - export EMQX_LISTENER__SSL__EXTERNAL__MAX_CLIENTS=500000 +if [[ -z "$EMQX_LISTENER__SSL__EXTERNAL__MAX_CONNECTIONS" ]]; then + export EMQX_LISTENER__SSL__EXTERNAL__MAX_CONNECTIONS=102400 fi if [[ -z "$EMQX_LISTENER__WS__EXTERNAL__ACCEPTORS" ]]; then export EMQX_LISTENER__WS__EXTERNAL__ACCEPTORS=16 fi -if [[ -z "$EMQX_LISTENER__WS__EXTERNAL__MAX_CLIENTS" ]]; then - export EMQX_LISTENER__WS__EXTERNAL__MAX_CLIENTS=250000 +if [[ -z "$EMQX_LISTENER__WS__EXTERNAL__MAX_CONNECTIONS" ]]; then + export EMQX_LISTENER__WS__EXTERNAL__MAX_CONNECTIONS=102400 +fi + +if [[ -z "$EMQX_LISTENER__WSS__EXTERNAL__ACCEPTORS" ]]; then + export EMQX_LISTENER__WSS__EXTERNAL__ACCEPTORS=16 +fi + +if [[ -z "$EMQX_LISTENER__WSS__EXTERNAL__MAX_CONNECTIONS" ]]; then + export EMQX_LISTENER__WSS__EXTERNAL__MAX_CONNECTIONS=102400 fi # Fix issue #42 - export env EMQX_DASHBOARD__DEFAULT_USER__PASSWORD to configure @@ -128,9 +132,9 @@ try_fill_config() { if grep -qE "^[#[:space:]]*$escaped_key\s*=" "$file"; then echo_value "$key" "$value" if [[ -z "$value" ]]; then - sed -i -r "s/^[#[:space:]]*($escaped_key)\s*=\s*(.*)/# \1 = \2/" "$file" + echo "$(sed -r "s/^[#[:space:]]*($escaped_key)\s*=\s*(.*)/# \1 = \2/" "$file")" > "$file" else - sed -i -r "s/^[#[:space:]]*($escaped_key)\s*=\s*(.*)/\1 = $escaped_value/" "$file" + echo "$(sed -r "s/^[#[:space:]]*($escaped_key)\s*=\s*(.*)/\1 = $escaped_value/" "$file")" > "$file" fi # Check if config has a numbering system, but no existing configuration line in file elif echo "$key" | grep -qE '\.\d+|\d+\.'; then @@ -139,7 +143,7 @@ try_fill_config() { template="$(echo "$escaped_key" | sed -r -e 's/\\\.[0-9]+/\\.[0-9]+/g' -e 's/[0-9]+\\\./[0-9]+\\./g')" if grep -qE "^[#[:space:]]*$template\s*=" "$file"; then echo_value "$key" "$value" - sed -i '$a'\\ "$file" + echo "$(sed '$a'\\ "$file")" > "$file" echo "$key = $value" >> "$file" fi fi @@ -171,13 +175,13 @@ fill_tuples() { local elements=${*:2} for var in $elements; do if grep -qE "\{\s*$var\s*,\s*(true|false)\s*\}\s*\." "$file"; then - sed -i -r "s/\{\s*($var)\s*,\s*(true|false)\s*\}\s*\./{\1, true}./1" "$file" + echo "$(sed -r "s/\{\s*($var)\s*,\s*(true|false)\s*\}\s*\./{\1, true}./1" "$file")" > "$file" elif grep -q "$var\s*\." "$file"; then # backward compatible. - sed -i -r "s/($var)\s*\./{\1, true}./1" "$file" + echo "$(sed -r "s/($var)\s*\./{\1, true}./1" "$file")" > "$file" else - sed -i '$a'\\ "$file" - echo "{$var, true}." >>"$file" + echo "$(sed '$a'\\ "$file")" > "$file" + echo "{$var, true}." >> "$file" fi done } From 02cb8d021148236f90754ba47a052088872c263d Mon Sep 17 00:00:00 2001 From: JianBo He Date: Wed, 27 Jan 2021 10:55:18 +0800 Subject: [PATCH 06/15] chore(style): set warnings_as_errors compile option --- apps/emqx_coap/src/emqx_coap_mqtt_adapter.erl | 2 -- rebar.config | 2 +- rebar.config.erl | 10 +++++----- src/emqx_alarm.erl | 1 - 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/apps/emqx_coap/src/emqx_coap_mqtt_adapter.erl b/apps/emqx_coap/src/emqx_coap_mqtt_adapter.erl index 48fd61a80..537f5137b 100644 --- a/apps/emqx_coap/src/emqx_coap_mqtt_adapter.erl +++ b/apps/emqx_coap/src/emqx_coap_mqtt_adapter.erl @@ -50,8 +50,6 @@ -record(state, {peername, clientid, username, password, sub_topics = [], connected_at}). --type(state() :: #state{}). - -define(ALIVE_INTERVAL, 20000). -define(CONN_STATS, [recv_pkt, recv_msg, send_pkt, send_msg]). diff --git a/rebar.config b/rebar.config index bed07d569..50ec93045 100644 --- a/rebar.config +++ b/rebar.config @@ -17,7 +17,7 @@ {extra_src_dirs, [{"etc", [{recursive,true}]}]}. {xref_checks,[undefined_function_calls,undefined_functions,locals_not_used, - deprecated_function_calls,warnings_as_errors, deprecated_functions]}. + deprecated_function_calls,warnings_as_errors,deprecated_functions]}. {dialyzer, [ {warnings, [unmatched_returns, error_handling, race_conditions]}, diff --git a/rebar.config.erl b/rebar.config.erl index 64286de12..8c9caf138 100644 --- a/rebar.config.erl +++ b/rebar.config.erl @@ -34,19 +34,19 @@ test_deps() -> ]. profiles() -> - [ {'emqx', [ {erl_opts, [no_debug_info, {parse_transform, mod_vsn}]} + [ {'emqx', [ {erl_opts, [no_debug_info, warnings_as_errors, {parse_transform, mod_vsn}]} , {relx, relx('emqx')} ]} - , {'emqx-pkg', [ {erl_opts, [no_debug_info, {parse_transform, mod_vsn}]} + , {'emqx-pkg', [ {erl_opts, [no_debug_info, warnings_as_errors, {parse_transform, mod_vsn}]} , {relx, relx('emqx-pkg')} ]} - , {'emqx-edge', [ {erl_opts, [no_debug_info, {parse_transform, mod_vsn}]} + , {'emqx-edge', [ {erl_opts, [no_debug_info, warnings_as_errors, {parse_transform, mod_vsn}]} , {relx, relx('emqx-edge')} ]} - , {'emqx-edge-pkg', [ {erl_opts, [no_debug_info, {parse_transform, mod_vsn}]} + , {'emqx-edge-pkg', [ {erl_opts, [no_debug_info, warnings_as_errors, {parse_transform, mod_vsn}]} , {relx, relx('emqx-edge-pkg')} ]} - , {check, [ {erl_opts, [debug_info, {parse_transform, mod_vsn}]} + , {check, [ {erl_opts, [debug_info, warnings_as_errors, {parse_transform, mod_vsn}]} ]} , {test, [ {deps, test_deps()} , {erl_opts, [debug_info, {parse_transform, mod_vsn}] ++ erl_opts_i()} diff --git a/src/emqx_alarm.erl b/src/emqx_alarm.erl index 9737b1463..f62e9e500 100644 --- a/src/emqx_alarm.erl +++ b/src/emqx_alarm.erl @@ -83,7 +83,6 @@ timer = undefined :: undefined | reference() }). --type state() :: #state{}. -type action() :: log | publish | event. -define(ACTIVATED_ALARM, emqx_activated_alarm). From eaa7394554273d2caee15999b21b060f80430b61 Mon Sep 17 00:00:00 2001 From: zhanghongtong Date: Wed, 27 Jan 2021 18:01:19 +0800 Subject: [PATCH 07/15] chore(ldap): delete uesless dir --- apps/emqx_auth_ldap/.ci/docker-compose.yml | 26 -------------------- apps/emqx_auth_ldap/.ci/emqx-ldap/Dockerfile | 26 -------------------- apps/emqx_auth_ldap/.ci/emqx-ldap/slapd.conf | 16 ------------ 3 files changed, 68 deletions(-) delete mode 100644 apps/emqx_auth_ldap/.ci/docker-compose.yml delete mode 100644 apps/emqx_auth_ldap/.ci/emqx-ldap/Dockerfile delete mode 100644 apps/emqx_auth_ldap/.ci/emqx-ldap/slapd.conf diff --git a/apps/emqx_auth_ldap/.ci/docker-compose.yml b/apps/emqx_auth_ldap/.ci/docker-compose.yml deleted file mode 100644 index bba9b711f..000000000 --- a/apps/emqx_auth_ldap/.ci/docker-compose.yml +++ /dev/null @@ -1,26 +0,0 @@ -version: '3' - -services: - erlang: - image: erlang:22.1 - volumes: - - ../:/emqx_auth_ldap - networks: - - emqx_bridge - depends_on: - - ldap_server - tty: true - - ldap_server: - build: ./emqx-ldap - image: emqx-ldap:1.0 - restart: always - ports: - - 389:389 - - 636:636 - networks: - - emqx_bridge - -networks: - emqx_bridge: - driver: bridge diff --git a/apps/emqx_auth_ldap/.ci/emqx-ldap/Dockerfile b/apps/emqx_auth_ldap/.ci/emqx-ldap/Dockerfile deleted file mode 100644 index 0a01572c4..000000000 --- a/apps/emqx_auth_ldap/.ci/emqx-ldap/Dockerfile +++ /dev/null @@ -1,26 +0,0 @@ -FROM buildpack-deps:stretch - -ENV VERSION=2.4.50 - -RUN apt-get update && apt-get install -y groff groff-base -RUN wget ftp://ftp.openldap.org/pub/OpenLDAP/openldap-release/openldap-${VERSION}.tgz \ - && gunzip -c openldap-${VERSION}.tgz | tar xvfB - \ - && cd openldap-${VERSION} \ - && ./configure && make depend && make && make install \ - && cd .. && rm -rf openldap-${VERSION} - -COPY ./slapd.conf /usr/local/etc/openldap/slapd.conf -COPY ./emqx.io.ldif /usr/local/etc/openldap/schema/emqx.io.ldif -COPY ./emqx.schema /usr/local/etc/openldap/schema/emqx.schema -COPY ./*.pem /usr/local/etc/openldap/ - -RUN mkdir -p /usr/local/etc/openldap/data \ - && slapadd -l /usr/local/etc/openldap/schema/emqx.io.ldif -f /usr/local/etc/openldap/slapd.conf - -WORKDIR /usr/local/etc/openldap - -EXPOSE 389 636 - -ENTRYPOINT ["/usr/local/libexec/slapd", "-h", "ldap:/// ldaps:///", "-d", "3", "-f", "/usr/local/etc/openldap/slapd.conf"] - -CMD [] diff --git a/apps/emqx_auth_ldap/.ci/emqx-ldap/slapd.conf b/apps/emqx_auth_ldap/.ci/emqx-ldap/slapd.conf deleted file mode 100644 index d6ba20caa..000000000 --- a/apps/emqx_auth_ldap/.ci/emqx-ldap/slapd.conf +++ /dev/null @@ -1,16 +0,0 @@ -include /usr/local/etc/openldap/schema/core.schema -include /usr/local/etc/openldap/schema/cosine.schema -include /usr/local/etc/openldap/schema/inetorgperson.schema -include /usr/local/etc/openldap/schema/ppolicy.schema -include /usr/local/etc/openldap/schema/emqx.schema - -TLSCACertificateFile /usr/local/etc/openldap/cacert.pem -TLSCertificateFile /usr/local/etc/openldap/cert.pem -TLSCertificateKeyFile /usr/local/etc/openldap/key.pem - -database bdb -suffix "dc=emqx,dc=io" -rootdn "cn=root,dc=emqx,dc=io" -rootpw {SSHA}eoF7NhNrejVYYyGHqnt+MdKNBh4r1w3W - -directory /usr/local/etc/openldap/data From b77ac247eb9d8e6790774c457c824ae8613950ef Mon Sep 17 00:00:00 2001 From: JianBo He Date: Wed, 27 Jan 2021 17:46:00 +0800 Subject: [PATCH 08/15] chore(auth-http): remove needless lines --- apps/emqx_auth_http/src/emqx_auth_http_cli.erl | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/emqx_auth_http/src/emqx_auth_http_cli.erl b/apps/emqx_auth_http/src/emqx_auth_http_cli.erl index 6ff8c445a..02fdd9862 100644 --- a/apps/emqx_auth_http/src/emqx_auth_http_cli.erl +++ b/apps/emqx_auth_http/src/emqx_auth_http_cli.erl @@ -80,7 +80,6 @@ feedvar(Params, ClientInfo = #{clientid := ClientId, ({Param, "%A"}) -> {Param, maps:get(access, ClientInfo, null)}; ({Param, "%t"}) -> {Param, maps:get(topic, ClientInfo, null)}; ({Param, "%m"}) -> {Param, maps:get(mountpoint, ClientInfo, null)}; - ({Param, "%k"}) -> {Param, emqx_json:encode(maps:get(ws_cookie, ClientInfo, null))}; ({Param, Var}) -> {Param, Var} end, Params). From 689b530b4fe6a7c6d8b7d3c24e5eaeff5d326cbb Mon Sep 17 00:00:00 2001 From: zhanghongtong Date: Wed, 27 Jan 2021 18:44:12 +0800 Subject: [PATCH 09/15] chore(auth-http): remove needless test case --- apps/emqx_auth_http/test/emqx_auth_http_SUITE.erl | 8 -------- 1 file changed, 8 deletions(-) diff --git a/apps/emqx_auth_http/test/emqx_auth_http_SUITE.erl b/apps/emqx_auth_http/test/emqx_auth_http_SUITE.erl index 235f1b5d8..76426a6d1 100644 --- a/apps/emqx_auth_http/test/emqx_auth_http_SUITE.erl +++ b/apps/emqx_auth_http/test/emqx_auth_http_SUITE.erl @@ -171,11 +171,3 @@ t_comment_config(_) -> ?assertEqual([], emqx_hooks:lookup('client.authenticate')), ?assertEqual(AuthCount - 1, length(emqx_hooks:lookup('client.authenticate'))), ?assertEqual(AclCount - 1, length(emqx_hooks:lookup('client.check_acl'))). - -t_feedvar(_) -> - Params = [{"cookie", "%k"}], - User0 = ?USER(<<"client1">>, <<"testuser">>, mqtt, {127,0,0,1}, external), - ?assertEqual([{"cookie", <<"null">>}], emqx_auth_http_cli:feedvar(Params, User0)), - - User1 = User0#{ws_cookie => [{<<"k">>, <<"v">>}]}, - ?assertEqual([{"cookie", <<"{\"k\":\"v\"}">>}], emqx_auth_http_cli:feedvar(Params, User1)). From b5c435e2111e60e157119142ecfd395ba1e8beb2 Mon Sep 17 00:00:00 2001 From: wwhai <751957846@qq.com> Date: Wed, 27 Jan 2021 23:14:45 +0800 Subject: [PATCH 10/15] fix(rule-engine): add format space --- apps/emqx_rule_engine/src/emqx_rule_engine.erl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/emqx_rule_engine/src/emqx_rule_engine.erl b/apps/emqx_rule_engine/src/emqx_rule_engine.erl index c45a3a20c..4c6c9a3c3 100644 --- a/apps/emqx_rule_engine/src/emqx_rule_engine.erl +++ b/apps/emqx_rule_engine/src/emqx_rule_engine.erl @@ -251,7 +251,7 @@ update_resource(ResId, NewParams) -> lists:foreach(fun(#rule{id = RuleId, enabled = Enabled, actions = Actions}) -> lists:foreach( fun (#action_instance{args = #{<<"$resource">> := ResId1}}) - when ResId =:= ResId1, Enabled == true -> + when ResId =:= ResId1, Enabled =:= true -> throw({dependency_exists, RuleId}); (_) -> ok end, Actions) @@ -286,7 +286,7 @@ do_update_resource_check(Id, NewParams) -> {error, not_found} end. -do_update_resource(#{id := Id, type := Type, description:= NewDescription, config:= NewConfig}) -> +do_update_resource(#{id := Id, type := Type, description := NewDescription, config := NewConfig}) -> case emqx_rule_registry:find_resource_type(Type) of {ok, #resource_type{on_create = {Module, Create}, params_spec = ParamSpec}} -> From a631a2d64fbc02c92110167747da7773496e5825 Mon Sep 17 00:00:00 2001 From: zhouzb Date: Wed, 27 Jan 2021 18:31:56 +0800 Subject: [PATCH 11/15] fix(ssl): fix ssl option --- apps/emqx_auth_http/src/emqx_auth_http_app.erl | 2 +- apps/emqx_auth_http/test/emqx_auth_http_SUITE.erl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/emqx_auth_http/src/emqx_auth_http_app.erl b/apps/emqx_auth_http/src/emqx_auth_http_app.erl index e857fc681..9b73405dd 100644 --- a/apps/emqx_auth_http/src/emqx_auth_http_app.erl +++ b/apps/emqx_auth_http/src/emqx_auth_http_app.erl @@ -66,7 +66,7 @@ translate_env(EnvName) -> "http" -> [{transport_opts, [Inet]}]; "https" -> - CACertFile = application:get_env(?APP, cafile, undefined), + CACertFile = application:get_env(?APP, cacertfile, undefined), CertFile = application:get_env(?APP, certfile, undefined), KeyFile = application:get_env(?APP, keyfile, undefined), TLSOpts = lists:filter(fun({_K, V}) when V =:= <<>> -> diff --git a/apps/emqx_auth_http/test/emqx_auth_http_SUITE.erl b/apps/emqx_auth_http/test/emqx_auth_http_SUITE.erl index 76426a6d1..28d5c00fb 100644 --- a/apps/emqx_auth_http/test/emqx_auth_http_SUITE.erl +++ b/apps/emqx_auth_http/test/emqx_auth_http_SUITE.erl @@ -88,7 +88,7 @@ set_special_configs(emqx_auth_http, Schema, Inet) -> %% @private set_https_client_opts() -> SSLOpt = emqx_ct_helpers:client_ssl_twoway(), - application:set_env(emqx_auth_http, cafile, proplists:get_value(cacertfile, SSLOpt, undefined)), + application:set_env(emqx_auth_http, cacertfile, proplists:get_value(cacertfile, SSLOpt, undefined)), application:set_env(emqx_auth_http, certfile, proplists:get_value(certfile, SSLOpt, undefined)), application:set_env(emqx_auth_http, keyfile, proplists:get_value(keyfile, SSLOpt, undefined)). From 311df2f8a6affaa93092017f35435c5a3fa9bb67 Mon Sep 17 00:00:00 2001 From: zhouzb Date: Wed, 27 Jan 2021 18:47:27 +0800 Subject: [PATCH 12/15] fix(scheme): fix scheme parsing --- apps/emqx_auth_http/src/emqx_auth_http_app.erl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/emqx_auth_http/src/emqx_auth_http_app.erl b/apps/emqx_auth_http/src/emqx_auth_http_app.erl index 9b73405dd..7b2be26e7 100644 --- a/apps/emqx_auth_http/src/emqx_auth_http_app.erl +++ b/apps/emqx_auth_http/src/emqx_auth_http_app.erl @@ -55,10 +55,11 @@ translate_env(EnvName) -> URL = proplists:get_value(url, Req), #{host := Host0, path := Path0, - scheme := Scheme} = URIMap = uri_string:parse(add_default_scheme(URL)), + scheme := Scheme0} = URIMap = uri_string:parse(add_default_scheme(URL)), + Scheme = string:to_lower(Scheme0), Port = maps:get(port, URIMap, case Scheme of "https" -> 443; - _ -> 80 + "http" -> 80 end), Path = path(Path0), {Inet, Host} = parse_host(Host0), From 45aafc75dddaa3aee5b7882aed17ca552aa0d87d Mon Sep 17 00:00:00 2001 From: zhouzb Date: Wed, 27 Jan 2021 18:59:45 +0800 Subject: [PATCH 13/15] fix(scheme): using uri_string:normalize/1 --- apps/emqx_auth_http/src/emqx_auth_http_app.erl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/emqx_auth_http/src/emqx_auth_http_app.erl b/apps/emqx_auth_http/src/emqx_auth_http_app.erl index 7b2be26e7..a09f2b46f 100644 --- a/apps/emqx_auth_http/src/emqx_auth_http_app.erl +++ b/apps/emqx_auth_http/src/emqx_auth_http_app.erl @@ -55,8 +55,7 @@ translate_env(EnvName) -> URL = proplists:get_value(url, Req), #{host := Host0, path := Path0, - scheme := Scheme0} = URIMap = uri_string:parse(add_default_scheme(URL)), - Scheme = string:to_lower(Scheme0), + scheme := Scheme} = URIMap = uri_string:parse(uri_string:normalize(add_default_scheme(URL))), Port = maps:get(port, URIMap, case Scheme of "https" -> 443; "http" -> 80 From 92d307b1b3dc64c96d19b8288760cf281f4d78fd Mon Sep 17 00:00:00 2001 From: zhouzb Date: Wed, 27 Jan 2021 19:03:33 +0800 Subject: [PATCH 14/15] fix(scheme): update test case --- apps/emqx_auth_http/test/emqx_auth_http_SUITE.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/emqx_auth_http/test/emqx_auth_http_SUITE.erl b/apps/emqx_auth_http/test/emqx_auth_http_SUITE.erl index 28d5c00fb..c2ad0ac43 100644 --- a/apps/emqx_auth_http/test/emqx_auth_http_SUITE.erl +++ b/apps/emqx_auth_http/test/emqx_auth_http_SUITE.erl @@ -66,7 +66,7 @@ set_special_configs(emqx, _Schmea, _Inet) -> set_special_configs(emqx_auth_http, Schema, Inet) -> ServerAddr = http_server(Schema, Inet), - AuthReq = #{method => post, + AuthReq = #{method => get, url => ServerAddr ++ "/mqtt/auth", headers => [{"content-type", "application/json"}], params => [{"clientid", "%c"}, {"username", "%u"}, {"password", "%P"}]}, From e6235d3bc937a9d3f2931c07c7535602578bd359 Mon Sep 17 00:00:00 2001 From: zhouzb Date: Wed, 27 Jan 2021 19:19:15 +0800 Subject: [PATCH 15/15] fix(scheme): fix default scheme --- apps/emqx_auth_http/src/emqx_auth_http_app.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/emqx_auth_http/src/emqx_auth_http_app.erl b/apps/emqx_auth_http/src/emqx_auth_http_app.erl index a09f2b46f..8e84f314f 100644 --- a/apps/emqx_auth_http/src/emqx_auth_http_app.erl +++ b/apps/emqx_auth_http/src/emqx_auth_http_app.erl @@ -55,7 +55,7 @@ translate_env(EnvName) -> URL = proplists:get_value(url, Req), #{host := Host0, path := Path0, - scheme := Scheme} = URIMap = uri_string:parse(uri_string:normalize(add_default_scheme(URL))), + scheme := Scheme} = URIMap = uri_string:parse(add_default_scheme(uri_string:normalize(URL))), Port = maps:get(port, URIMap, case Scheme of "https" -> 443; "http" -> 80