From 914c375d9e3b88202bc1f4b8e9516b5bf1f9d20a Mon Sep 17 00:00:00 2001 From: JianBo He Date: Mon, 23 Aug 2021 11:48:08 +0800 Subject: [PATCH] chore(gw): adjust the configuration format --- apps/emqx_gateway/etc/emqx_gateway.conf | 18 +++---- apps/emqx_gateway/include/emqx_gateway.hrl | 9 +++- .../src/coap/emqx_coap_channel.erl | 11 +++- apps/emqx_gateway/src/emqx_gateway.app.src | 2 +- apps/emqx_gateway/src/emqx_gateway_schema.erl | 52 +++++++++++++------ apps/emqx_gateway/src/emqx_gateway_utils.erl | 15 ++---- 6 files changed, 66 insertions(+), 41 deletions(-) diff --git a/apps/emqx_gateway/etc/emqx_gateway.conf b/apps/emqx_gateway/etc/emqx_gateway.conf index 41d1d10de..15aeb2e29 100644 --- a/apps/emqx_gateway/etc/emqx_gateway.conf +++ b/apps/emqx_gateway/etc/emqx_gateway.conf @@ -6,6 +6,7 @@ ## In the final version, it will be commented out. gateway.stomp { + frame { max_headers = 10 max_headers_length = 1024 @@ -18,14 +19,13 @@ gateway.stomp { } authenticator { - #enable = true name = "authenticator1" mechanism = password-based server_type = built-in-database user_id_type = clientid } - listener.tcp.1 { + listeners.tcp.default { bind = 61613 acceptors = 16 max_connections = 1024000 @@ -49,7 +49,7 @@ gateway.coap { notify_type = qos subscribe_qos = qos0 publish_qos = qos1 - listener.udp.1 { + listeners.udp.default { bind = 5683 } } @@ -90,7 +90,7 @@ gateway.mqttsn { password = "abc" } - listener.udp.1 { + listeners.udp.default { bind = 1884 max_connections = 10240000 max_conn_rate = 1000 @@ -113,16 +113,16 @@ gateway.exproto { #ssl.cacertfile: } - listener.tcp.1 { + listeners.tcp.default { bind = 7993 acceptors = 8 max_connections = 10240 max_conn_rate = 1000 } - #listener.ssl.1: {} - #listener.udp.1: {} - #listener.dtls.1: {} + #listeners.ssl.default: {} + #listeners.udp.default: {} + #listeners.dtls.default: {} } gateway.lwm2m { @@ -147,7 +147,7 @@ gateway.lwm2m { update = "up/resp" } - listener.udp.1 { + listeners.udp.default { bind = 5783 } } diff --git a/apps/emqx_gateway/include/emqx_gateway.hrl b/apps/emqx_gateway/include/emqx_gateway.hrl index d959eac8b..baa7a1ce7 100644 --- a/apps/emqx_gateway/include/emqx_gateway.hrl +++ b/apps/emqx_gateway/include/emqx_gateway.hrl @@ -19,8 +19,15 @@ -type gateway_name() :: atom(). +-type listener() :: #{}. + %% The RawConf got from emqx:get_config/1 --type rawconf() :: map(). +-type rawconf() :: + #{ clientinfo_override => map() + , authenticator => map() + , listeners => listener() + , atom() => any() + }. %% @doc The Gateway defination -type gateway() :: diff --git a/apps/emqx_gateway/src/coap/emqx_coap_channel.erl b/apps/emqx_gateway/src/coap/emqx_coap_channel.erl index 760a832ae..ccf42343c 100644 --- a/apps/emqx_gateway/src/coap/emqx_coap_channel.erl +++ b/apps/emqx_gateway/src/coap/emqx_coap_channel.erl @@ -106,7 +106,7 @@ init(ConnInfo = #{peername := {PeerHost, _}, #{ctx := Ctx} = Config) -> Peercert = maps:get(peercert, ConnInfo, undefined), Mountpoint = maps:get(mountpoint, Config, undefined), - EnableAuth = maps:get(enable, maps:get(authentication, Config)), + EnableAuth = is_authenticator_enabled(Config), ClientInfo = set_peercert_infos( Peercert, #{ zone => default @@ -134,6 +134,13 @@ init(ConnInfo = #{peername := {PeerHost, _}, , keepalive = emqx_keepalive:init(maps:get(heartbeat, Config)) }. +is_authenticator_enabled(Cfg) -> + case maps:get(authenticator, Cfg, #{enable => false}) of + AuthCfg when is_map(AuthCfg) -> + maps:get(enable, AuthCfg, true); + _ -> false + end. + validator(Type, Topic, #exec_ctx{ctx = Ctx, clientinfo = ClientInfo}) -> emqx_gateway_ctx:authorize(Ctx, ClientInfo, Type, Topic). @@ -290,7 +297,7 @@ handle_result(_, _, _, Channel) -> {ok, Channel}. check_auth_state(Msg, #channel{config = Cfg} = Channel) -> - #{authentication := #{enable := Enable}} = Cfg, + Enable = is_authenticator_enabled(Cfg), check_token(Enable, Msg, Channel). check_token(true, diff --git a/apps/emqx_gateway/src/emqx_gateway.app.src b/apps/emqx_gateway/src/emqx_gateway.app.src index 2fc329711..e25b767cc 100644 --- a/apps/emqx_gateway/src/emqx_gateway.app.src +++ b/apps/emqx_gateway/src/emqx_gateway.app.src @@ -3,7 +3,7 @@ {vsn, "0.1.0"}, {registered, []}, {mod, {emqx_gateway_app, []}}, - {applications, [kernel, stdlib, grpc, lwm2m_coap, emqx]}, + {applications, [kernel, stdlib, grpc, lwm2m_coap, emqx, emqx_authn]}, {env, []}, {modules, []}, {licenses, ["Apache 2.0"]}, diff --git a/apps/emqx_gateway/src/emqx_gateway_schema.erl b/apps/emqx_gateway/src/emqx_gateway_schema.erl index facbe9026..9a0e75a37 100644 --- a/apps/emqx_gateway/src/emqx_gateway_schema.erl +++ b/apps/emqx_gateway/src/emqx_gateway_schema.erl @@ -1,5 +1,23 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2021 EMQ Technologies Co., Ltd. All Rights Reserved. +%% +%% Licensed under the Apache License, Version 2.0 (the "License"); +%% you may not use this file except in compliance with the License. +%% You may obtain a copy of the License at +%% +%% http://www.apache.org/licenses/LICENSE-2.0 +%% +%% Unless required by applicable law or agreed to in writing, software +%% distributed under the License is distributed on an "AS IS" BASIS, +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +%% See the License for the specific language governing permissions and +%% limitations under the License. +%%-------------------------------------------------------------------- + -module(emqx_gateway_schema). +-behaviour(hocon_schema). + -dialyzer(no_return). -dialyzer(no_match). -dialyzer(no_contracts). @@ -8,17 +26,16 @@ -include_lib("typerefl/include/types.hrl"). +-type ip_port() :: tuple(). -type duration() :: integer(). -type bytesize() :: integer(). -type comma_separated_list() :: list(). --type ip_port() :: tuple(). +-typerefl_from_string({ip_port/0, emqx_schema, to_ip_port}). -typerefl_from_string({duration/0, emqx_schema, to_duration}). -typerefl_from_string({bytesize/0, emqx_schema, to_bytesize}). --typerefl_from_string({comma_separated_list/0, emqx_schema, to_comma_separated_list}). --typerefl_from_string({ip_port/0, emqx_schema, to_ip_port}). - --behaviour(hocon_schema). +-typerefl_from_string({comma_separated_list/0, emqx_schema, + to_comma_separated_list}). -reflect_type([ duration/0 , bytesize/0 @@ -27,11 +44,15 @@ ]). -export([structs/0 , fields/1]). + -export([t/1, t/3, t/4, ref/1]). -structs() -> ["gateway"]. +%%-------------------------------------------------------------------- +%% Structs -fields("gateway") -> +structs() -> [gateway]. + +fields(gateway) -> [{stomp, t(ref(stomp_structs))}, {mqttsn, t(ref(mqttsn_structs))}, {coap, t(ref(coap_structs))}, @@ -43,7 +64,7 @@ fields(stomp_structs) -> [ {frame, t(ref(stomp_frame))} , {clientinfo_override, t(ref(clientinfo_override))} , {authenticator, t(authenticator(), undefined, undefined)} - , {listener, t(ref(tcp_listener_group))} + , {listeners, t(ref(tcp_listener_group))} ]; fields(stomp_frame) -> @@ -61,11 +82,10 @@ fields(mqttsn_structs) -> , {predefined, hoconsc:array(ref(mqttsn_predefined))} , {clientinfo_override, t(ref(clientinfo_override))} , {authenticator, t(authenticator(), undefined, undefined)} - , {listener, t(ref(udp_listener_group))} + , {listeners, t(ref(udp_listener_group))} ]; fields(mqttsn_predefined) -> - %% FIXME: How to check the $id is a integer ??? [ {id, t(integer())} , {topic, t(string())} ]; @@ -80,18 +100,18 @@ fields(lwm2m_structs) -> , {update_msg_publish_condition, t(union([always, contains_object_list]))} , {translators, t(ref(translators))} , {authenticator, t(authenticator(), undefined, undefined)} - , {listener, t(ref(udp_listener_group))} + , {listeners, t(ref(udp_listener_group))} ]; fields(exproto_structs) -> [ {server, t(ref(exproto_grpc_server))} , {handler, t(ref(exproto_grpc_handler))} , {authenticator, t(authenticator(), undefined, undefined)} - , {listener, t(ref(udp_tcp_listener_group))} + , {listeners, t(ref(udp_tcp_listener_group))} ]; fields(exproto_grpc_server) -> - [ {bind, t(integer())} + [ {bind, t(union(ip_port(), integer()))} %% TODO: ssl options ]; @@ -139,9 +159,7 @@ fields(dtls_listener) -> [ {"$name", t(ref(dtls_listener_settings))}]; fields(listener_settings) -> - % FIXME: - %[ {"bind", t(union(ip_port(), integer()))} - [ {bind, t(integer())} + [ {bind, t(union(ip_port(), integer()))} , {acceptors, t(integer(), undefined, 8)} , {max_connections, t(integer(), undefined, 1024)} , {max_conn_rate, t(integer())} @@ -203,7 +221,7 @@ fields(coap_structs) -> , {subscribe_qos, t(union([qos0, qos1, qos2, coap]), undefined, coap)} , {publish_qos, t(union([qos0, qos1, qos2, coap]), undefined, coap)} , {authenticator, t(authenticator(), undefined, undefined)} - , {listener, t(ref(udp_listener_group))} + , {listeners, t(ref(udp_listener_group))} ]; fields(ExtraField) -> diff --git a/apps/emqx_gateway/src/emqx_gateway_utils.erl b/apps/emqx_gateway/src/emqx_gateway_utils.erl index 8a4d24691..d8c2b9be7 100644 --- a/apps/emqx_gateway/src/emqx_gateway_utils.erl +++ b/apps/emqx_gateway/src/emqx_gateway_utils.erl @@ -17,6 +17,8 @@ %% @doc Utils funcs for emqx-gateway -module(emqx_gateway_utils). +-include("emqx_gateway.hrl"). + -export([ childspec/2 , childspec/3 , childspec/4 @@ -105,15 +107,6 @@ format_listenon({Addr, Port}) when is_list(Addr) -> format_listenon({Addr, Port}) when is_tuple(Addr) -> io_lib:format("~s:~w", [inet:ntoa(Addr), Port]). --type listener() :: #{}. - --type rawconf() :: - #{ clientinfo_override => #{} - , authenticators := list() - , listeners => listener() - , atom() => any() - }. - -spec normalize_rawconf(rawconf()) -> list({ Type :: udp | tcp | ssl | dtls , ListenOn :: esockd:listen_on() @@ -121,8 +114,8 @@ format_listenon({Addr, Port}) when is_tuple(Addr) -> , Cfg :: map() }). normalize_rawconf(RawConf) -> - LisMap = maps:get(listener, RawConf, #{}), - Cfg0 = maps:without([listener], RawConf), + LisMap = maps:get(listeners, RawConf, #{}), + Cfg0 = maps:without([listeners], RawConf), lists:append(maps:fold(fun(Type, Liss, AccIn1) -> Listeners = maps:fold(fun(_Name, Confs, AccIn2) ->