From 79026d5900cfbbdb18196d2a12ca1f72d50990d8 Mon Sep 17 00:00:00 2001 From: zhouzb Date: Fri, 17 Sep 2021 14:17:36 +0800 Subject: [PATCH 1/2] chore(authn): add supervisor for authn and fix checking errors --- apps/emqx/src/emqx_authentication.erl | 3 +- apps/emqx/src/emqx_authentication_sup.erl | 48 +++++++++++++++++++ apps/emqx/src/emqx_broker_sup.erl | 16 +++---- apps/emqx_authn/src/emqx_authn_api.erl | 2 +- .../src/simple_authn/emqx_authn_http.erl | 2 - .../src/simple_authn/emqx_authn_mongodb.erl | 3 -- .../src/simple_authn/emqx_authn_mysql.erl | 1 - .../src/simple_authn/emqx_authn_pgsql.erl | 1 - .../src/simple_authn/emqx_authn_redis.erl | 1 - 9 files changed, 58 insertions(+), 19 deletions(-) create mode 100644 apps/emqx/src/emqx_authentication_sup.erl diff --git a/apps/emqx/src/emqx_authentication.erl b/apps/emqx/src/emqx_authentication.erl index bcb38471a..4200190ac 100644 --- a/apps/emqx/src/emqx_authentication.erl +++ b/apps/emqx/src/emqx_authentication.erl @@ -281,8 +281,7 @@ do_post_config_update({move_authenticator, ChainName, AuthenticatorID, Position} check_config(Config) -> #{authentication := CheckedConfig} = - hocon_schema:check_plain(?MODULE, #{<<"authentication">> => Config}, - #{nullable => true, atom_key => true}), + hocon_schema:check_plain(?MODULE, #{<<"authentication">> => Config}, #{atom_key => true}), CheckedConfig. %%------------------------------------------------------------------------------ diff --git a/apps/emqx/src/emqx_authentication_sup.erl b/apps/emqx/src/emqx_authentication_sup.erl new file mode 100644 index 000000000..cdae04f3c --- /dev/null +++ b/apps/emqx/src/emqx_authentication_sup.erl @@ -0,0 +1,48 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2017-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_authentication_sup). + +-behaviour(supervisor). + +-export([start_link/0]). + +-export([init/1]). + +%%-------------------------------------------------------------------- +%% API +%%-------------------------------------------------------------------- + +start_link() -> + supervisor:start_link({local, ?MODULE}, ?MODULE, []). + +%%-------------------------------------------------------------------- +%% Supervisor callbacks +%%-------------------------------------------------------------------- + +init([]) -> + SupFlags = #{strategy => one_for_one, + intensity => 100, + period => 10}, + + AuthN = #{id => emqx_authentication, + start => {emqx_authentication, start_link, []}, + restart => permanent, + shutdown => 1000, + type => worker, + modules => [emqx_authentication]}, + + {ok, {SupFlags, [AuthN]}}. \ No newline at end of file diff --git a/apps/emqx/src/emqx_broker_sup.erl b/apps/emqx/src/emqx_broker_sup.erl index a479e9ff1..761537e57 100644 --- a/apps/emqx/src/emqx_broker_sup.erl +++ b/apps/emqx/src/emqx_broker_sup.erl @@ -44,13 +44,13 @@ init([]) -> modules => [emqx_shared_sub]}, %% Authentication - AuthN = #{id => authn, - start => {emqx_authentication, start_link, []}, - restart => permanent, - shutdown => 2000, - type => worker, - modules => [emqx_authentication]}, - + AuthNSup = #{id => emqx_authentication_sup, + start => {emqx_authentication_sup, start_link, []}, + restart => permanent, + shutdown => infinity, + type => supervisor, + modules => [emqx_authentication_sup]}, + %% Broker helper Helper = #{id => helper, start => {emqx_broker_helper, start_link, []}, @@ -59,5 +59,5 @@ init([]) -> type => worker, modules => [emqx_broker_helper]}, - {ok, {{one_for_all, 0, 1}, [BrokerPool, SharedSub, AuthN, Helper]}}. + {ok, {{one_for_all, 0, 1}, [BrokerPool, SharedSub, AuthNSup, Helper]}}. diff --git a/apps/emqx_authn/src/emqx_authn_api.erl b/apps/emqx_authn/src/emqx_authn_api.erl index b58a2a214..540cf86e3 100644 --- a/apps/emqx_authn/src/emqx_authn_api.erl +++ b/apps/emqx_authn/src/emqx_authn_api.erl @@ -1971,7 +1971,7 @@ find_config(AuthenticatorID, AuthenticatorsConfig) -> fill_defaults(Config) -> #{<<"authentication">> := CheckedConfig} = hocon_schema:check_plain( - ?AUTHN, #{<<"authentication">> => Config}, #{nullable => true, no_conversion => true}), + ?AUTHN, #{<<"authentication">> => Config}, #{no_conversion => true}), CheckedConfig. convert_certs(#{<<"ssl">> := SSLOpts} = Config) -> diff --git a/apps/emqx_authn/src/simple_authn/emqx_authn_http.erl b/apps/emqx_authn/src/simple_authn/emqx_authn_http.erl index 9814d3e58..2fa29d2df 100644 --- a/apps/emqx_authn/src/simple_authn/emqx_authn_http.erl +++ b/apps/emqx_authn/src/simple_authn/emqx_authn_http.erl @@ -77,7 +77,6 @@ validations() -> ]. url(type) -> binary(); -url(nullable) -> false; url(validator) -> [fun check_url/1]; url(_) -> undefined. @@ -98,7 +97,6 @@ headers_no_content_type(default) -> default_headers_no_content_type(); headers_no_content_type(_) -> undefined. body(type) -> map(); -body(nullable) -> false; body(validator) -> [fun check_body/1]; body(_) -> undefined. diff --git a/apps/emqx_authn/src/simple_authn/emqx_authn_mongodb.erl b/apps/emqx_authn/src/simple_authn/emqx_authn_mongodb.erl index 9d77c673c..5ad148009 100644 --- a/apps/emqx_authn/src/simple_authn/emqx_authn_mongodb.erl +++ b/apps/emqx_authn/src/simple_authn/emqx_authn_mongodb.erl @@ -70,15 +70,12 @@ common_fields() -> ] ++ emqx_authn_schema:common_fields(). collection(type) -> binary(); -collection(nullable) -> false; collection(_) -> undefined. selector(type) -> map(); -selector(nullable) -> false; selector(_) -> undefined. password_hash_field(type) -> binary(); -password_hash_field(nullable) -> false; password_hash_field(_) -> undefined. salt_field(type) -> binary(); diff --git a/apps/emqx_authn/src/simple_authn/emqx_authn_mysql.erl b/apps/emqx_authn/src/simple_authn/emqx_authn_mysql.erl index 991bb6aee..87c61da1e 100644 --- a/apps/emqx_authn/src/simple_authn/emqx_authn_mysql.erl +++ b/apps/emqx_authn/src/simple_authn/emqx_authn_mysql.erl @@ -63,7 +63,6 @@ salt_position(default) -> prefix; salt_position(_) -> undefined. query(type) -> string(); -query(nullable) -> false; query(_) -> undefined. query_timeout(type) -> integer(); diff --git a/apps/emqx_authn/src/simple_authn/emqx_authn_pgsql.erl b/apps/emqx_authn/src/simple_authn/emqx_authn_pgsql.erl index c497074de..940c50519 100644 --- a/apps/emqx_authn/src/simple_authn/emqx_authn_pgsql.erl +++ b/apps/emqx_authn/src/simple_authn/emqx_authn_pgsql.erl @@ -59,7 +59,6 @@ password_hash_algorithm(default) -> sha256; password_hash_algorithm(_) -> undefined. query(type) -> string(); -query(nullable) -> false; query(_) -> undefined. %%------------------------------------------------------------------------------ diff --git a/apps/emqx_authn/src/simple_authn/emqx_authn_redis.erl b/apps/emqx_authn/src/simple_authn/emqx_authn_redis.erl index 949aeeaea..5926740a8 100644 --- a/apps/emqx_authn/src/simple_authn/emqx_authn_redis.erl +++ b/apps/emqx_authn/src/simple_authn/emqx_authn_redis.erl @@ -66,7 +66,6 @@ common_fields() -> ] ++ emqx_authn_schema:common_fields(). query(type) -> string(); -query(nullable) -> false; query(_) -> undefined. password_hash_algorithm(type) -> {enum, [plain, md5, sha, sha256, sha512, bcrypt]}; From 48600492680284d1cfef0c16df22ffb44df392af Mon Sep 17 00:00:00 2001 From: zhouzb Date: Fri, 17 Sep 2021 15:29:02 +0800 Subject: [PATCH 2/2] chore(authn): insert final newline --- apps/emqx/src/emqx_authentication_sup.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/emqx/src/emqx_authentication_sup.erl b/apps/emqx/src/emqx_authentication_sup.erl index cdae04f3c..7b0d77f71 100644 --- a/apps/emqx/src/emqx_authentication_sup.erl +++ b/apps/emqx/src/emqx_authentication_sup.erl @@ -45,4 +45,4 @@ init([]) -> type => worker, modules => [emqx_authentication]}, - {ok, {SupFlags, [AuthN]}}. \ No newline at end of file + {ok, {SupFlags, [AuthN]}}.