fix: check authn's mechanism field

This commit is contained in:
Zhongwen Deng 2022-09-14 10:44:36 +08:00
parent 0ca89da1a7
commit 6bd72fe5a1
3 changed files with 28 additions and 15 deletions

View File

@ -415,7 +415,8 @@ check_config(SchemaMod, RawConf, Opts0) ->
Opts1 = #{ Opts1 = #{
return_plain => true, return_plain => true,
format => map, format => map,
check_lazy => true %% Don't check lazy types, such as authenticate
check_lazy => false
}, },
Opts = maps:merge(Opts0, Opts1), Opts = maps:merge(Opts0, Opts1),
{AppEnvs, CheckedConf} = {AppEnvs, CheckedConf} =

View File

@ -70,7 +70,9 @@ do_check_config(#{<<"mechanism">> := Mec} = Config, Opts) ->
#{?CONF_NS_BINARY => Config}, #{?CONF_NS_BINARY => Config},
Opts#{atom_key => true} Opts#{atom_key => true}
) )
end. end;
do_check_config(_Config, _Opts) ->
throw({invalid_config, "mechanism_field_required"}).
atom(Bin) -> atom(Bin) ->
try try

View File

@ -37,8 +37,10 @@
start(_StartType, _StartArgs) -> start(_StartType, _StartArgs) ->
ok = mria_rlog:wait_for_shards([?AUTH_SHARD], infinity), ok = mria_rlog:wait_for_shards([?AUTH_SHARD], infinity),
{ok, Sup} = emqx_authn_sup:start_link(), {ok, Sup} = emqx_authn_sup:start_link(),
ok = initialize(), case initialize() of
{ok, Sup}. ok -> {ok, Sup};
{error, Reason} -> {error, Reason}
end.
stop(_State) -> stop(_State) ->
ok = deinitialize(), ok = deinitialize(),
@ -49,18 +51,26 @@ stop(_State) ->
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
initialize() -> initialize() ->
ok = ?AUTHN:register_providers(emqx_authn:providers()), try
ok = ?AUTHN:register_providers(emqx_authn:providers()),
lists:foreach( lists:foreach(
fun({ChainName, RawAuthConfigs}) -> fun({ChainName, RawAuthConfigs}) ->
AuthConfig = emqx_authn:check_configs(RawAuthConfigs), AuthConfig = emqx_authn:check_configs(RawAuthConfigs),
?AUTHN:initialize_authentication( ?AUTHN:initialize_authentication(
ChainName, ChainName,
AuthConfig AuthConfig
) )
end, end,
chain_configs() chain_configs()
). )
of
ok -> ok
catch
throw:Reason ->
?SLOG(error, #{msg => "Failed to initialize authentication", reason => Reason}),
{error, {failed_to_initialize_authentication, Reason}}
end.
deinitialize() -> deinitialize() ->
ok = ?AUTHN:deregister_providers(provider_types()), ok = ?AUTHN:deregister_providers(provider_types()),