refactor(emqx_authn): make error messages more readable
This commit is contained in:
parent
abef881a73
commit
7137a422df
|
@ -402,6 +402,7 @@ merge_envs(SchemaMod, RawConf) ->
|
||||||
required => false,
|
required => false,
|
||||||
format => map,
|
format => map,
|
||||||
apply_override_envs => true,
|
apply_override_envs => true,
|
||||||
|
remove_env_meta => true,
|
||||||
check_lazy => true
|
check_lazy => true
|
||||||
},
|
},
|
||||||
hocon_tconf:merge_env_overrides(SchemaMod, RawConf, all, Opts).
|
hocon_tconf:merge_env_overrides(SchemaMod, RawConf, all, Opts).
|
||||||
|
|
|
@ -39,12 +39,15 @@ providers() ->
|
||||||
{{scram, built_in_database}, emqx_enhanced_authn_scram_mnesia}
|
{{scram, built_in_database}, emqx_enhanced_authn_scram_mnesia}
|
||||||
].
|
].
|
||||||
|
|
||||||
check_configs(C) when is_map(C) ->
|
check_configs(CM) when is_map(CM) ->
|
||||||
check_configs([C]);
|
check_configs([CM]);
|
||||||
check_configs([]) ->
|
check_configs(CL) ->
|
||||||
|
check_configs(CL, 1).
|
||||||
|
|
||||||
|
check_configs([], _Nth) ->
|
||||||
[];
|
[];
|
||||||
check_configs([Config | Configs]) ->
|
check_configs([Config | Configs], Nth) ->
|
||||||
[check_config(Config) | check_configs(Configs)].
|
[check_config(Config, #{id_for_log => Nth}) | check_configs(Configs, Nth + 1)].
|
||||||
|
|
||||||
check_config(Config) ->
|
check_config(Config) ->
|
||||||
check_config(Config, #{}).
|
check_config(Config, #{}).
|
||||||
|
@ -55,15 +58,16 @@ check_config(Config, Opts) ->
|
||||||
#{?CONF_NS_BINARY := WithDefaults} -> WithDefaults
|
#{?CONF_NS_BINARY := WithDefaults} -> WithDefaults
|
||||||
end.
|
end.
|
||||||
|
|
||||||
do_check_config(#{<<"mechanism">> := Mec} = Config, Opts) ->
|
do_check_config(#{<<"mechanism">> := Mec0} = Config, Opts) ->
|
||||||
|
Mec = atom(Mec0, #{error => unknown_mechanism}),
|
||||||
Key =
|
Key =
|
||||||
case maps:get(<<"backend">>, Config, false) of
|
case maps:get(<<"backend">>, Config, false) of
|
||||||
false -> atom(Mec);
|
false -> Mec;
|
||||||
Backend -> {atom(Mec), atom(Backend)}
|
Backend -> {Mec, atom(Backend, #{error => unknown_backend})}
|
||||||
end,
|
end,
|
||||||
case lists:keyfind(Key, 1, providers()) of
|
case lists:keyfind(Key, 1, providers()) of
|
||||||
false ->
|
false ->
|
||||||
throw({unknown_handler, Key});
|
throw(#{error => unknown_authn_provider, which => Key});
|
||||||
{_, ProviderModule} ->
|
{_, ProviderModule} ->
|
||||||
hocon_tconf:check_plain(
|
hocon_tconf:check_plain(
|
||||||
ProviderModule,
|
ProviderModule,
|
||||||
|
@ -71,22 +75,22 @@ do_check_config(#{<<"mechanism">> := Mec} = Config, Opts) ->
|
||||||
Opts#{atom_key => true}
|
Opts#{atom_key => true}
|
||||||
)
|
)
|
||||||
end;
|
end;
|
||||||
do_check_config(Config, _Opts) when is_map(Config) ->
|
do_check_config(Config, Opts) when is_map(Config) ->
|
||||||
throw({invalid_config, "mechanism_field_required", Config});
|
throw(#{
|
||||||
do_check_config(RawConf, Opts) ->
|
error => invalid_config,
|
||||||
%% authentication conf is lazy type, when it comes from ENV, it is a string
|
which => maps:get(id_for_log, Opts, unknown),
|
||||||
%% EMQX_AUTHENTICATION__1="{mechanism=\"password_based\"...}"
|
reason => "mechanism_field_required"
|
||||||
case hocon:binary(RawConf, Opts) of
|
}).
|
||||||
{ok, Conf} -> do_check_config(Conf, Opts);
|
|
||||||
{error, Reason} -> throw({invalid_config, Reason})
|
|
||||||
end.
|
|
||||||
|
|
||||||
atom(Bin) ->
|
%% The atoms have to be loaded already,
|
||||||
|
%% which might be an issue for plugins which are loaded after node boot
|
||||||
|
%% but they should really manage their own configs in that case.
|
||||||
|
atom(Bin, ErrorContext) ->
|
||||||
try
|
try
|
||||||
binary_to_existing_atom(Bin, utf8)
|
binary_to_existing_atom(Bin, utf8)
|
||||||
catch
|
catch
|
||||||
_:_ ->
|
_:_ ->
|
||||||
throw({unknown_auth_provider, Bin})
|
throw(ErrorContext#{value => Bin})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec get_enabled_authns() ->
|
-spec get_enabled_authns() ->
|
||||||
|
|
Loading…
Reference in New Issue