From 005332d45de743e17cdd830641458ae755d47a35 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Wed, 25 Aug 2021 22:59:53 +0800 Subject: [PATCH] fix(config): do not allow default values for configs in zones --- apps/emqx/etc/emqx.conf | 3 +++ apps/emqx/src/emqx_schema.erl | 29 ++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/apps/emqx/etc/emqx.conf b/apps/emqx/etc/emqx.conf index 8dba8b0f8..c85e7aa29 100644 --- a/apps/emqx/etc/emqx.conf +++ b/apps/emqx/etc/emqx.conf @@ -979,6 +979,9 @@ quota { ## - `flapping_detect.*` ## - `force_shutdown.*` ## - `conn_congestion.*` +## - `rate_limit.*` +## - `quota.*` +## - `force_gc.*` ## ## syntax: zones. ## example: zones.my_zone diff --git a/apps/emqx/src/emqx_schema.erl b/apps/emqx/src/emqx_schema.erl index 347f9068b..3bbeb1d07 100644 --- a/apps/emqx/src/emqx_schema.erl +++ b/apps/emqx/src/emqx_schema.erl @@ -126,19 +126,14 @@ fields("zones") -> [ {"$name", ref("zone_settings")}]; fields("zone_settings") -> - [ {"mqtt", ref("mqtt")} - , {"stats", ref("stats")} - , {"flapping_detect", ref("flapping_detect")} - , {"force_shutdown", ref("force_shutdown")} - , {"conn_congestion", ref("conn_congestion")} - , {"force_gc", ref("force_gc")} - ]; + Fields = ["mqtt", "stats", "authorization", "flapping_detect", "force_shutdown", + "conn_congestion", "rate_limit", "quota", "force_gc"], + [{F, ref("strip_default:" ++ F)} || F <- Fields]; fields("rate_limit") -> [ {"max_conn_rate", maybe_infinity(integer(), 1000)} , {"conn_messages_in", maybe_infinity(comma_separated_list())} , {"conn_bytes_in", maybe_infinity(comma_separated_list())} - , {"quota", ref("quota")} ]; fields("quota") -> @@ -334,7 +329,10 @@ fields("alarm") -> [ {"actions", t(hoconsc:array(atom()), undefined, [log, publish])} , {"size_limit", t(integer(), undefined, 1000)} , {"validity_period", t(duration(), undefined, "24h")} - ]. + ]; + +fields("strip_default:" ++ Name) -> + strip_default(fields(Name)). mqtt_listener() -> base_listener() ++ @@ -547,6 +545,19 @@ to_erl_cipher_suite(Str) -> Cipher -> Cipher end. +strip_default(Fields) -> + [do_strip_default(F) || F <- Fields]. + +do_strip_default({Name, #{type := {ref, Ref}}}) -> + {Name, nullable_no_def(ref("strip_default:" ++ Ref))}; +do_strip_default({Name, #{type := {ref, _Mod, Ref}}}) -> + {Name, nullable_no_def(ref("strip_default:" ++ Ref))}; +do_strip_default({Name, Type}) -> + {Name, nullable_no_def(Type)}. + +nullable_no_def(Type) when is_map(Type) -> + Type#{default => undefined, nullable => true}. + to_atom(Atom) when is_atom(Atom) -> Atom; to_atom(Str) when is_list(Str) ->