From 50c546290c12e9ef8da266659774a016905e6608 Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Wed, 22 Jun 2022 18:05:18 -0300 Subject: [PATCH 01/14] feat: add redbug --- rebar.config | 1 + rebar.config.erl | 1 + 2 files changed, 2 insertions(+) diff --git a/rebar.config b/rebar.config index 48053b235..87009a154 100644 --- a/rebar.config +++ b/rebar.config @@ -39,6 +39,7 @@ {deps, [ {gpb, "4.11.2"} %% gpb only used to build, but not for release, pin it here to avoid fetching a wrong version due to rebar plugins scattered in all the deps + , {redbug, "2.0.7"} , {ehttpc, {git, "https://github.com/emqx/ehttpc", {tag, "0.2.0"}}} , {eredis_cluster, {git, "https://github.com/emqx/eredis_cluster", {tag, "0.7.2"}}} , {gproc, {git, "https://github.com/uwiger/gproc", {tag, "0.8.0"}}} diff --git a/rebar.config.erl b/rebar.config.erl index 226597967..c94f80c18 100644 --- a/rebar.config.erl +++ b/rebar.config.erl @@ -257,6 +257,7 @@ relx_apps(ReleaseType) -> , inets , compiler , runtime_tools + , redbug , cuttlefish , emqx , {mnesia, load} From 0092d55b469e2266d10000e63b645b8e9e9010d6 Mon Sep 17 00:00:00 2001 From: DDDHuang <44492639+DDDHuang@users.noreply.github.com> Date: Fri, 24 Jun 2022 11:33:01 +0800 Subject: [PATCH 02/14] fix(topic-rewrite): better hooks, max priority 1000 --- lib-ce/emqx_modules/src/emqx_mod_rewrite.erl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib-ce/emqx_modules/src/emqx_mod_rewrite.erl b/lib-ce/emqx_modules/src/emqx_mod_rewrite.erl index b51d29291..73f4be5be 100644 --- a/lib-ce/emqx_modules/src/emqx_mod_rewrite.erl +++ b/lib-ce/emqx_modules/src/emqx_mod_rewrite.erl @@ -45,9 +45,9 @@ load(RawRules) -> {PubRules, SubRules} = compile(RawRules), - emqx_hooks:put('client.subscribe', {?MODULE, rewrite_subscribe, [SubRules]}), - emqx_hooks:put('client.unsubscribe', {?MODULE, rewrite_unsubscribe, [SubRules]}), - emqx_hooks:put('message.publish', {?MODULE, rewrite_publish, [PubRules]}). + emqx_hooks:put('client.subscribe', {?MODULE, rewrite_subscribe, [SubRules]}, 1000), + emqx_hooks:put('client.unsubscribe', {?MODULE, rewrite_unsubscribe, [SubRules]}, 1000), + emqx_hooks:put('message.publish', {?MODULE, rewrite_publish, [PubRules]}, 1000). rewrite_subscribe(ClientInfo, _Properties, TopicFilters, Rules) -> Binds = fill_client_binds(ClientInfo), From 0a99e835e782589f535be614d7620071bb4524c0 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Fri, 24 Jun 2022 14:34:40 +0800 Subject: [PATCH 03/14] fix: fallback action not triggered when send to webhook failed --- apps/emqx_rule_engine/src/emqx_rule_actions.erl | 3 ++- apps/emqx_web_hook/src/emqx_web_hook_actions.erl | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/apps/emqx_rule_engine/src/emqx_rule_actions.erl b/apps/emqx_rule_engine/src/emqx_rule_actions.erl index ae82d56c9..d665a0c96 100644 --- a/apps/emqx_rule_engine/src/emqx_rule_actions.erl +++ b/apps/emqx_rule_engine/src/emqx_rule_actions.erl @@ -175,7 +175,8 @@ on_action_republish(_Selected, Envs = #{ }) -> ?LOG(error, "[republish] recursively republish detected, msg topic: ~p, target topic: ~p", [Topic, ?bound_v('TargetTopic', Envs)]), - emqx_rule_metrics:inc_actions_error(?bound_v('Id', Envs)); + emqx_rule_metrics:inc_actions_error(?bound_v('Id', Envs)), + {badact, recursively_republish}; on_action_republish(Selected, _Envs = #{ qos := QoS, flags := Flags, timestamp := Timestamp, diff --git a/apps/emqx_web_hook/src/emqx_web_hook_actions.erl b/apps/emqx_web_hook/src/emqx_web_hook_actions.erl index aa0face38..ae68e79e1 100644 --- a/apps/emqx_web_hook/src/emqx_web_hook_actions.erl +++ b/apps/emqx_web_hook/src/emqx_web_hook_actions.erl @@ -268,13 +268,16 @@ on_action_data_to_webserver(Selected, _Envs = emqx_rule_metrics:inc_actions_success(Id); {ok, StatusCode, _} -> ?LOG(warning, "HTTP request failed with path: ~p status code: ~p", [NPath, StatusCode]), - emqx_rule_metrics:inc_actions_error(Id); + emqx_rule_metrics:inc_actions_error(Id), + {badact, StatusCode}; {ok, StatusCode, _, _} -> ?LOG(warning, "HTTP request failed with path: ~p status code: ~p", [NPath, StatusCode]), - emqx_rule_metrics:inc_actions_error(Id); + emqx_rule_metrics:inc_actions_error(Id), + {badact, StatusCode}; {error, Reason} -> ?LOG(error, "HTTP request failed path: ~p error: ~p", [NPath, Reason]), - emqx_rule_metrics:inc_actions_error(Id) + emqx_rule_metrics:inc_actions_error(Id), + {badact, Reason} end. format_msg([], Data) -> From a48ba40f1b2fdfddec83da9e47ec93fc20f16046 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Fri, 24 Jun 2022 15:02:35 +0800 Subject: [PATCH 04/14] fix: fix LWM2M can't bind IP address error --- rebar.config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rebar.config b/rebar.config index 48053b235..4fc8edae1 100644 --- a/rebar.config +++ b/rebar.config @@ -44,7 +44,7 @@ , {gproc, {git, "https://github.com/uwiger/gproc", {tag, "0.8.0"}}} , {jiffy, {git, "https://github.com/emqx/jiffy", {tag, "1.0.5"}}} , {cowboy, {git, "https://github.com/emqx/cowboy", {tag, "2.8.2"}}} - , {esockd, {git, "https://github.com/emqx/esockd", {tag, "5.8.5"}}} + , {esockd, {git, "https://github.com/emqx/esockd", {tag, "5.8.6"}}} , {ekka, {git, "https://github.com/emqx/ekka", {tag, "0.8.1.10"}}} , {gen_rpc, {git, "https://github.com/emqx/gen_rpc", {tag, "2.5.2"}}} , {cuttlefish, {git, "https://github.com/emqx/cuttlefish", {tag, "v3.3.6"}}} From abe2a9cb45537befb4255497c1d21aec98b5b7b6 Mon Sep 17 00:00:00 2001 From: firest Date: Fri, 24 Jun 2022 14:45:15 +0800 Subject: [PATCH 05/14] feat(exhook): expose tcp some options for grpc client --- apps/emqx_exhook/etc/emqx_exhook.conf | 17 ++++++++++ apps/emqx_exhook/priv/emqx_exhook.schema | 37 +++++++++++++++++++-- apps/emqx_exhook/src/emqx_exhook_server.erl | 7 ++-- apps/emqx_exhook/test/emqx_exhook_SUITE.erl | 3 +- 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/apps/emqx_exhook/etc/emqx_exhook.conf b/apps/emqx_exhook/etc/emqx_exhook.conf index 6a4725e02..40b07caf5 100644 --- a/apps/emqx_exhook/etc/emqx_exhook.conf +++ b/apps/emqx_exhook/etc/emqx_exhook.conf @@ -47,3 +47,20 @@ exhook.server.default.url = http://127.0.0.1:9000 #exhook.server.default.ssl.cacertfile = {{ platform_etc_dir }}/certs/cacert.pem #exhook.server.default.ssl.certfile = {{ platform_etc_dir }}/certs/cert.pem #exhook.server.default.ssl.keyfile = {{ platform_etc_dir }}/certs/key.pem + +## Enables/disables periodic transmission on a connected socket when no other data is exchanged. +## If the other end does not respond, the connection is considered broken and an error message is sent to the controlling process. +## +## Default: true +#exhook.server.default.socket_options.keepalive = true + +## If true, option TCP_NODELAY is turned on for the socket, which means that also small amounts of data are sent immediately. +## +## Default: true +#exhook.server.default.socket_options.nodelay = true + +## The minimum size of the receive buffer to use for the socket. +#exhook.server.default.socket_options.recbuf = 64KB + +## The minimum size of the send buffer to use for the socket. +#exhook.server.default.socket_options.sndbuf = 16KB \ No newline at end of file diff --git a/apps/emqx_exhook/priv/emqx_exhook.schema b/apps/emqx_exhook/priv/emqx_exhook.schema index 4f6419c6a..b18bad70a 100644 --- a/apps/emqx_exhook/priv/emqx_exhook.schema +++ b/apps/emqx_exhook/priv/emqx_exhook.schema @@ -47,12 +47,42 @@ end}. {datatype, string} ]}. +{mapping, "exhook.server.$name.socket_options.keepalive", "emqx_exhook.servers", [ + {default, true}, + {datatype, {enum, [true, false]}} +]}. + +{mapping, "exhook.server.$name.socket_options.nodelay", "emqx_exhook.servers", [ + {default, true}, + {datatype, {enum, [true, false]}} +]}. + +{mapping, "exhook.server.$name.socket_options.recbuf", "emqx_exhook.servers", [ + {datatype, bytesize} +]}. + +{mapping, "exhook.server.$name.socket_options.sndbuf", "emqx_exhook.servers", [ + {datatype, bytesize} +]}. + {translation, "emqx_exhook.servers", fun(Conf) -> Filter = fun(Opts) -> [{K, V} || {K, V} <- Opts, V =/= undefined] end, + + MkSockOpts = fun(Prefix, Conf) -> + {socket_options, + Filter([{Opt, + cuttlefish:conf_get(Prefix ++ ".socket_options." ++ atom_to_list(Opt), + Conf, + undefined)} + || Opt <- [keepalive, nodelay, recbuf, sndbuf]])} + end, + ServerOptions = fun(Prefix) -> case http_uri:parse(cuttlefish:conf_get(Prefix ++ ".url", Conf)) of {ok, {http, _, Host, Port, _, _}} -> - [{scheme, http}, {host, Host}, {port, Port}]; + [{scheme, http}, {host, Host}, {port, Port}, + MkSockOpts(Prefix, Conf) + ]; {ok, {https, _, Host, Port, _, _}} -> [{scheme, https}, {host, Host}, {port, Port}, {ssl_options, @@ -60,10 +90,13 @@ end}. {certfile, cuttlefish:conf_get(Prefix ++ ".ssl.certfile", Conf, undefined)}, {keyfile, cuttlefish:conf_get(Prefix ++ ".ssl.keyfile", Conf, undefined)}, {cacertfile, cuttlefish:conf_get(Prefix ++ ".ssl.cacertfile", Conf, undefined)} - ])}]; + ])}, + MkSockOpts(Prefix, Conf) + ]; _ -> error(invalid_server_options) end end, + [{list_to_atom(Name), ServerOptions("exhook.server." ++ Name)} || {["exhook", "server", Name, "url"], _} <- cuttlefish_variable:filter_by_prefix("exhook.server", Conf)] end}. diff --git a/apps/emqx_exhook/src/emqx_exhook_server.erl b/apps/emqx_exhook/src/emqx_exhook_server.erl index 9caa166ea..e767cd84b 100644 --- a/apps/emqx_exhook/src/emqx_exhook_server.erl +++ b/apps/emqx_exhook/src/emqx_exhook_server.erl @@ -124,13 +124,16 @@ channel_opts(Opts) -> Host = proplists:get_value(host, Opts), Port = proplists:get_value(port, Opts), SvrAddr = format_http_uri(Scheme, Host, Port), + SockOpts = proplists:get_value(socket_options, Opts), ClientOpts = case Scheme of https -> SslOpts = lists:keydelete(ssl, 1, proplists:get_value(ssl_options, Opts, [])), #{gun_opts => #{transport => ssl, - transport_opts => SslOpts}}; - _ -> #{} + transport_opts => SockOpts ++ SslOpts}}; + _ -> + #{gun_opts => + #{transport_opts => SockOpts}} end, {SvrAddr, ClientOpts}. diff --git a/apps/emqx_exhook/test/emqx_exhook_SUITE.erl b/apps/emqx_exhook/test/emqx_exhook_SUITE.erl index daa2e25b3..a8bd34b5d 100644 --- a/apps/emqx_exhook/test/emqx_exhook_SUITE.erl +++ b/apps/emqx_exhook/test/emqx_exhook_SUITE.erl @@ -46,7 +46,8 @@ set_special_cfgs(emqx) -> application:set_env(emqx, allow_anonymous, false), application:set_env(emqx, enable_acl_cache, false), application:set_env(emqx, plugins_loaded_file, undefined), - application:set_env(emqx, modules_loaded_file, undefined); + application:set_env(emqx, modules_loaded_file, undefined), + application:set_env(ekka, cluster_name, ?OTHER_CLUSTER_NAME_ATOM); set_special_cfgs(emqx_exhook) -> ok. From 9ad1e5c08ec09f256334e4ac48cbdf85d8a06b46 Mon Sep 17 00:00:00 2001 From: firest Date: Fri, 24 Jun 2022 17:52:29 +0800 Subject: [PATCH 06/14] chore(exhook): bump version && update appup --- apps/emqx_exhook/src/emqx_exhook.app.src | 2 +- apps/emqx_exhook/src/emqx_exhook.appup.src | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/apps/emqx_exhook/src/emqx_exhook.app.src b/apps/emqx_exhook/src/emqx_exhook.app.src index 6075d50db..cf39d9e42 100644 --- a/apps/emqx_exhook/src/emqx_exhook.app.src +++ b/apps/emqx_exhook/src/emqx_exhook.app.src @@ -1,7 +1,7 @@ %% -*- mode: erlang -*- {application, emqx_exhook, [{description, "EMQ X Extension for Hook"}, - {vsn, "4.3.5"}, + {vsn, "4.3.6"}, {modules, []}, {registered, []}, {mod, {emqx_exhook_app, []}}, diff --git a/apps/emqx_exhook/src/emqx_exhook.appup.src b/apps/emqx_exhook/src/emqx_exhook.appup.src index 8245576ec..d3b73d98a 100644 --- a/apps/emqx_exhook/src/emqx_exhook.appup.src +++ b/apps/emqx_exhook/src/emqx_exhook.appup.src @@ -1,6 +1,8 @@ %% -*- mode: erlang -*- {VSN, - [ + [ {"4.3.5", [ + {load_module, emqx_exhook_server, brutal_purge, soft_purge, []} + ]}, {"4.3.4", [ {load_module, emqx_exhook_sup, brutal_purge, soft_purge, []}, {load_module, emqx_exhook_server, brutal_purge, soft_purge, []}, @@ -14,7 +16,9 @@ ]}, {<<".*">>, []} ], - [ + [ {"4.3.5", [ + {load_module, emqx_exhook_server, brutal_purge, soft_purge, []} + ]}, {"4.3.4", [ {load_module, emqx_exhook_sup, brutal_purge, soft_purge, []}, {load_module, emqx_exhook_server, brutal_purge, soft_purge, []}, From c4f1c83003810bac3a73cb6d526218ca8760838b Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Sun, 26 Jun 2022 21:16:55 +0200 Subject: [PATCH 07/14] chore: return system total and used memory in node_info --- apps/emqx_management/src/emqx_mgmt.erl | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/apps/emqx_management/src/emqx_mgmt.erl b/apps/emqx_management/src/emqx_mgmt.erl index 51bc193d8..948088f8e 100644 --- a/apps/emqx_management/src/emqx_mgmt.erl +++ b/apps/emqx_management/src/emqx_mgmt.erl @@ -136,13 +136,13 @@ list_nodes() -> lookup_node(Node) -> node_info(Node). node_info(Node) when Node =:= node() -> - Memory = emqx_vm:get_memory(), + {UsedRatio, Total} = get_sys_memory(), Info = maps:from_list([{K, list_to_binary(V)} || {K, V} <- emqx_vm:loads()]), BrokerInfo = emqx_sys:info(), Info#{node => node(), otp_release => iolist_to_binary(otp_rel()), - memory_total => proplists:get_value(allocated, Memory), - memory_used => proplists:get_value(used, Memory), + memory_total => Total, + memory_used => erlang:round(Total * UsedRatio), process_available => erlang:system_info(process_limit), process_used => erlang:system_info(process_count), max_fds => @@ -156,6 +156,14 @@ node_info(Node) when Node =:= node() -> node_info(Node) -> rpc_call(Node, node_info, [Node]). +get_sys_memory() -> + case os:type() of + {unix, linux} -> + load_ctl:get_sys_memory(); + _ -> + {0, 0} + end. + stopped_node_info(Node) -> #{name => Node, node_status => 'Stopped'}. From 9bb3533d112f1ed05474146e03844916a135986c Mon Sep 17 00:00:00 2001 From: JianBo He Date: Fri, 24 Jun 2022 14:13:27 +0800 Subject: [PATCH 08/14] fix(exhook): guarantee sequential dispatch of client's events from the same node fix: https://github.com/emqx/emqx/issues/7569 --- apps/emqx_exhook/src/emqx_exhook.appup.src | 6 ++++-- apps/emqx_exhook/src/emqx_exhook_server.erl | 12 +++++++++++- rebar.config | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/apps/emqx_exhook/src/emqx_exhook.appup.src b/apps/emqx_exhook/src/emqx_exhook.appup.src index d3b73d98a..f286255f0 100644 --- a/apps/emqx_exhook/src/emqx_exhook.appup.src +++ b/apps/emqx_exhook/src/emqx_exhook.appup.src @@ -1,6 +1,7 @@ %% -*- mode: erlang -*- {VSN, - [ {"4.3.5", [ + [ + {"4.3.5", [ {load_module, emqx_exhook_server, brutal_purge, soft_purge, []} ]}, {"4.3.4", [ @@ -16,7 +17,8 @@ ]}, {<<".*">>, []} ], - [ {"4.3.5", [ + [ + {"4.3.5", [ {load_module, emqx_exhook_server, brutal_purge, soft_purge, []} ]}, {"4.3.4", [ diff --git a/apps/emqx_exhook/src/emqx_exhook_server.erl b/apps/emqx_exhook/src/emqx_exhook_server.erl index e767cd84b..93568783a 100644 --- a/apps/emqx_exhook/src/emqx_exhook_server.erl +++ b/apps/emqx_exhook/src/emqx_exhook_server.erl @@ -273,7 +273,7 @@ match_topic_filter(TopicName, TopicFilter) -> -spec do_call(string(), atom(), map(), map()) -> {ok, map()} | {error, term()}. do_call(ChannName, Fun, Req, ReqOpts) -> NReq = Req#{meta => emqx_exhook:request_meta()}, - Options = ReqOpts#{channel => ChannName}, + Options = ReqOpts#{channel => ChannName, key_dispatch => key_dispatch(NReq)}, ?LOG(debug, "Call ~0p:~0p(~0p, ~0p)", [?PB_CLIENT_MOD, Fun, NReq, Options]), case catch apply(?PB_CLIENT_MOD, Fun, [NReq, Options]) of {ok, Resp, _Metadata} -> @@ -331,3 +331,13 @@ available_hooks() -> 'session.created', 'session.subscribed', 'session.unsubscribed', 'session.resumed', 'session.discarded', 'session.takeovered', 'session.terminated' | message_hooks()]. + +%% @doc Get dispatch_key for each request +key_dispatch(_Req = #{clientinfo := #{clientid := ClientId}}) -> + ClientId; +key_dispatch(_Req = #{conninfo := #{clientid := ClientId}}) -> + ClientId; +key_dispatch(_Req = #{message := #{from := From}}) -> + From; +key_dispatch(_Req) -> + self(). diff --git a/rebar.config b/rebar.config index 1cd36d47c..7c5e01ff9 100644 --- a/rebar.config +++ b/rebar.config @@ -62,7 +62,7 @@ , {lc, {git, "https://github.com/emqx/lc.git", {tag, "0.3.1"}}} , {mongodb, {git,"https://github.com/emqx/mongodb-erlang", {tag, "v3.0.13"}}} , {epgsql, {git, "https://github.com/emqx/epgsql.git", {tag, "4.6.0"}}} - , {grpc, {git, "https://github.com/emqx/grpc-erl", {tag, "0.6.5"}}} + , {grpc, {git, "https://github.com/emqx/grpc-erl", {tag, "0.6.6"}}} ]}. {xref_ignores, From e4df71d90ef149ec4a129e5a75738e27ffb5cfc0 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Fri, 24 Jun 2022 14:18:41 +0800 Subject: [PATCH 09/14] chore: update changes-4.3 --- CHANGES-4.3.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES-4.3.md b/CHANGES-4.3.md index 51c5e7cb8..7a3d6f4f3 100644 --- a/CHANGES-4.3.md +++ b/CHANGES-4.3.md @@ -29,6 +29,7 @@ File format: information to fill them. [#8280] - Fixed issue in Lua hook that didn't prevent a topic from being subscribed to. [#8288] +- Ensuring that exhook dispatches the client events are sequential. [#8311] ## v4.3.15 From ee0589505848ed8e5e8b1e8e222855deb5d0f92d Mon Sep 17 00:00:00 2001 From: JianBo He Date: Mon, 27 Jun 2022 11:30:54 +0800 Subject: [PATCH 10/14] chore(exhook): update changes-4.3 for #8314 --- CHANGES-4.3.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES-4.3.md b/CHANGES-4.3.md index 7a3d6f4f3..e73e45124 100644 --- a/CHANGES-4.3.md +++ b/CHANGES-4.3.md @@ -19,6 +19,7 @@ File format: management HTTPS listeners. [#8129] - Add message republish supports using placeholder variables to specify QoS and Retain values. Set `${qos}` and `${flags.retain}` use the original QoS & Retain flag. - Add supports specifying the network interface address of the cluster listener & rcp call listener. Specify `0.0.0.0` use all network interfaces, or a particular network interface IP address. +- ExHook supports to customize the socket parameters for gRPC client. [#8314] ### Bug fixes From 4a87d77be33963246cbf9b36e726a0cc0fcb2cb6 Mon Sep 17 00:00:00 2001 From: zhongwencool Date: Thu, 23 Jun 2022 10:27:31 +0800 Subject: [PATCH 11/14] fix: can't start dashboard if default_username is missing --- CHANGES-4.3.md | 1 + .../src/emqx_dashboard_admin.erl | 27 ++++++++++--------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/CHANGES-4.3.md b/CHANGES-4.3.md index e73e45124..7799eedda 100644 --- a/CHANGES-4.3.md +++ b/CHANGES-4.3.md @@ -31,6 +31,7 @@ File format: - Fixed issue in Lua hook that didn't prevent a topic from being subscribed to. [#8288] - Ensuring that exhook dispatches the client events are sequential. [#8311] +- Ensure start dashboard ok event if default_username is missing. ## v4.3.15 diff --git a/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl b/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl index a4504fe29..a3a773114 100644 --- a/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl +++ b/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl @@ -206,11 +206,15 @@ is_valid_pwd(<>, Password) -> %%-------------------------------------------------------------------- init([]) -> - %% Add default admin user - {ok, _} = mnesia:subscribe({table, mqtt_admin, simple}), - PasswordHash = ensure_default_user_in_db(binenv(default_user_username)), - ok = ensure_default_user_passwd_hashed_in_pt(PasswordHash), - ok = maybe_warn_default_pwd(), + case binenv(default_user_username) of + <<>> -> ok; + UserName -> + %% Add default admin user + {ok, _} = mnesia:subscribe({table, mqtt_admin, simple}), + PasswordHash = ensure_default_user_in_db(UserName), + ok = ensure_default_user_passwd_hashed_in_pt(PasswordHash), + ok = maybe_warn_default_pwd() + end, {ok, state}. handle_call(_Req, _From, State) -> @@ -220,7 +224,7 @@ handle_cast(_Msg, State) -> {noreply, State}. handle_info({mnesia_table_event, {write, Admin, _}}, State) -> - %% the password is chagned from another node, sync it to persistent_term + %% the password is changed from another node, sync it to persistent_term #mqtt_admin{username = Username, password = HashedPassword} = Admin, case binenv(default_user_username) of Username -> @@ -258,6 +262,7 @@ salt() -> binenv(Key) -> iolist_to_binary(application:get_env(emqx_dashboard, Key, <<>>)). +ensure_default_user_in_db(<<>>) -> <<>>; ensure_default_user_in_db(Username) -> F = fun() -> @@ -279,13 +284,8 @@ ensure_default_user_in_db(Username) -> initial_default_user_passwd_hashed() -> case get_default_user_passwd_hashed_from_pt() of Empty when ?EMPTY_KEY(Empty) -> - %% in case it's not set yet - case binenv(default_user_passwd) of - Empty when ?EMPTY_KEY(Empty) -> - error({missing_configuration, default_user_passwd}); - Pwd -> - hash(Pwd) - end; + Pwd = binenv(default_user_passwd), + hash(Pwd); PwdHash -> PwdHash end. @@ -293,6 +293,7 @@ initial_default_user_passwd_hashed() -> %% use this persistent_term for a copy of the value in mnesia database %% so that after the node leaves a cluster, db gets purged, %% we can still find the changed password back from PT +ensure_default_user_passwd_hashed_in_pt(<<>>) -> ok; ensure_default_user_passwd_hashed_in_pt(Hashed) -> ok = persistent_term:put({?MODULE, default_user_passwd_hashed}, Hashed). From fd22639bcb2f1ec66eb6a2326ecadfeef933ed17 Mon Sep 17 00:00:00 2001 From: zhongwencool Date: Thu, 23 Jun 2022 12:09:26 +0800 Subject: [PATCH 12/14] chore: remove unnessary function case --- lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl | 1 - 1 file changed, 1 deletion(-) diff --git a/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl b/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl index a3a773114..b7c729955 100644 --- a/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl +++ b/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl @@ -293,7 +293,6 @@ initial_default_user_passwd_hashed() -> %% use this persistent_term for a copy of the value in mnesia database %% so that after the node leaves a cluster, db gets purged, %% we can still find the changed password back from PT -ensure_default_user_passwd_hashed_in_pt(<<>>) -> ok; ensure_default_user_passwd_hashed_in_pt(Hashed) -> ok = persistent_term:put({?MODULE, default_user_passwd_hashed}, Hashed). From ab2e477ffd93cf6469aa09b5ff2b8cad7be360db Mon Sep 17 00:00:00 2001 From: zhongwencool Date: Mon, 27 Jun 2022 14:33:37 +0800 Subject: [PATCH 13/14] fix: default_password should not be empty --- lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl b/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl index b7c729955..a76ed9cff 100644 --- a/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl +++ b/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl @@ -22,6 +22,7 @@ -include("emqx_dashboard.hrl"). -include_lib("emqx/include/logger.hrl"). +-define(DEFAULT_PASSWORD, <<"public">>). -boot_mnesia({mnesia, [boot]}). -copy_mnesia({mnesia, [copy]}). @@ -284,8 +285,10 @@ ensure_default_user_in_db(Username) -> initial_default_user_passwd_hashed() -> case get_default_user_passwd_hashed_from_pt() of Empty when ?EMPTY_KEY(Empty) -> - Pwd = binenv(default_user_passwd), - hash(Pwd); + case binenv(default_user_passwd) of + Empty when ?EMPTY_KEY(Empty) -> hash(?DEFAULT_PASSWORD); + Pwd -> hash(Pwd) + end; PwdHash -> PwdHash end. @@ -300,7 +303,7 @@ get_default_user_passwd_hashed_from_pt() -> persistent_term:get({?MODULE, default_user_passwd_hashed}, <<>>). maybe_warn_default_pwd() -> - case is_valid_pwd(initial_default_user_passwd_hashed(), <<"public">>) of + case is_valid_pwd(initial_default_user_passwd_hashed(), ?DEFAULT_PASSWORD) of true -> ?LOG(warning, "[Dashboard] Using default password for dashboard 'admin' user. " From 5e21c60fe2fe956073a89d0234825e10f3930754 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Mon, 27 Jun 2022 18:56:19 +0800 Subject: [PATCH 14/14] chore: release v4.3.16-beta.3 --- include/emqx_release.hrl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/emqx_release.hrl b/include/emqx_release.hrl index e7aa4c8ad..83d774252 100644 --- a/include/emqx_release.hrl +++ b/include/emqx_release.hrl @@ -29,7 +29,7 @@ -ifndef(EMQX_ENTERPRISE). --define(EMQX_RELEASE, {opensource, "4.3.16-beta.2"}). +-define(EMQX_RELEASE, {opensource, "4.3.16-beta.3"}). -else.