refactor: remove lazy type for authentication

The idea of using lazy type for authentication was to make the
config check easy to extend at runtime. However the reality
is: it's overly complicated.

It's more likely that people will just continue to implementing
the auth hook callbacks instead of injecting config schema
at runtime.
This commit is contained in:
Zaiming (Stone) Shi 2023-01-08 21:57:53 +01:00
parent 3587c4c04a
commit 15035f7eb0
4 changed files with 12 additions and 33 deletions

View File

@ -366,13 +366,6 @@ schema_default(Schema) ->
case hocon_schema:field_schema(Schema, type) of case hocon_schema:field_schema(Schema, type) of
?ARRAY(_) -> ?ARRAY(_) ->
[]; [];
?LAZY(?ARRAY(_)) ->
[];
?LAZY(?UNION(Members)) ->
case [A || ?ARRAY(A) <- hoconsc:union_members(Members)] of
[_ | _] -> [];
_ -> #{}
end;
_ -> _ ->
#{} #{}
end. end.
@ -407,8 +400,7 @@ merge_envs(SchemaMod, RawConf) ->
Opts = #{ Opts = #{
required => false, required => false,
format => map, format => map,
apply_override_envs => true, apply_override_envs => true
check_lazy => true
}, },
hocon_tconf:merge_env_overrides(SchemaMod, RawConf, all, Opts). hocon_tconf:merge_env_overrides(SchemaMod, RawConf, all, Opts).
@ -451,9 +443,7 @@ compact_errors(Schema, Errors) ->
do_check_config(SchemaMod, RawConf, Opts0) -> do_check_config(SchemaMod, RawConf, Opts0) ->
Opts1 = #{ Opts1 = #{
return_plain => true, return_plain => true,
format => map, format => map
%% 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

@ -2352,25 +2352,18 @@ authentication(Which) ->
global -> ?DESC(global_authentication); global -> ?DESC(global_authentication);
listener -> ?DESC(listener_authentication) listener -> ?DESC(listener_authentication)
end, end,
%% The runtime module injection %% poor man's dependency injection
%% from EMQX_AUTHENTICATION_SCHEMA_MODULE_PT_KEY %% this is due to the fact that authn is implemented outside of 'emqx' app.
%% is for now only affecting document generation. %% so it can not be a part of emqx_schema since 'emqx' app is supposed to
%% maybe in the future, we can find a more straightforward way to support %% work standalone.
%% * document generation (at compile time) Type =
%% * type checks before boot (in bin/emqx config generation)
%% * type checks at runtime (when changing configs via management API)
Type0 =
case persistent_term:get(?EMQX_AUTHENTICATION_SCHEMA_MODULE_PT_KEY, undefined) of case persistent_term:get(?EMQX_AUTHENTICATION_SCHEMA_MODULE_PT_KEY, undefined) of
undefined -> hoconsc:array(typerefl:map()); undefined ->
Module -> Module:root_type() hoconsc:array(typerefl:map());
Module ->
Module:root_type()
end, end,
%% It is a lazy type because when handling runtime update requests hoconsc:mk(Type, #{desc => Desc}).
%% the config is not checked by emqx_schema, but by the injected schema
Type = hoconsc:lazy(Type0),
#{
type => Type,
desc => Desc
}.
-spec qos() -> typerefl:type(). -spec qos() -> typerefl:type().
qos() -> qos() ->

View File

@ -296,8 +296,6 @@ hocon_schema_to_spec(Type, LocalModule) when ?IS_TYPEREFL(Type) ->
hocon_schema_to_spec(?ARRAY(Item), LocalModule) -> hocon_schema_to_spec(?ARRAY(Item), LocalModule) ->
{Schema, Refs} = hocon_schema_to_spec(Item, LocalModule), {Schema, Refs} = hocon_schema_to_spec(Item, LocalModule),
{#{type => array, items => Schema}, Refs}; {#{type => array, items => Schema}, Refs};
hocon_schema_to_spec(?LAZY(Item), LocalModule) ->
hocon_schema_to_spec(Item, LocalModule);
hocon_schema_to_spec(?ENUM(Items), _LocalModule) -> hocon_schema_to_spec(?ENUM(Items), _LocalModule) ->
{#{type => enum, symbols => Items}, []}; {#{type => enum, symbols => Items}, []};
hocon_schema_to_spec(?MAP(Name, Type), LocalModule) -> hocon_schema_to_spec(?MAP(Name, Type), LocalModule) ->

View File

@ -609,8 +609,6 @@ hocon_schema_to_spec(Type, LocalModule) when ?IS_TYPEREFL(Type) ->
hocon_schema_to_spec(?ARRAY(Item), LocalModule) -> hocon_schema_to_spec(?ARRAY(Item), LocalModule) ->
{Schema, Refs} = hocon_schema_to_spec(Item, LocalModule), {Schema, Refs} = hocon_schema_to_spec(Item, LocalModule),
{#{type => array, items => Schema}, Refs}; {#{type => array, items => Schema}, Refs};
hocon_schema_to_spec(?LAZY(Item), LocalModule) ->
hocon_schema_to_spec(Item, LocalModule);
hocon_schema_to_spec(?ENUM(Items), _LocalModule) -> hocon_schema_to_spec(?ENUM(Items), _LocalModule) ->
{#{type => string, enum => Items}, []}; {#{type => string, enum => Items}, []};
hocon_schema_to_spec(?MAP(Name, Type), LocalModule) -> hocon_schema_to_spec(?MAP(Name, Type), LocalModule) ->