chore(auth): make schema injection be more universal
This commit is contained in:
parent
abcdf18ca4
commit
67e06b3171
|
@ -222,7 +222,7 @@ roots(high) ->
|
||||||
ref(?EMQX_AUTHORIZATION_CONFIG_ROOT_NAME),
|
ref(?EMQX_AUTHORIZATION_CONFIG_ROOT_NAME),
|
||||||
#{importance => ?IMPORTANCE_HIDDEN}
|
#{importance => ?IMPORTANCE_HIDDEN}
|
||||||
)}
|
)}
|
||||||
] ++ emqx_schema_hooks:injection_point('authentication');
|
] ++ emqx_schema_hooks:injection_point('roots.high');
|
||||||
roots(medium) ->
|
roots(medium) ->
|
||||||
[
|
[
|
||||||
{"broker",
|
{"broker",
|
||||||
|
@ -1749,7 +1749,7 @@ mqtt_listener(Bind) ->
|
||||||
default => <<"3s">>
|
default => <<"3s">>
|
||||||
}
|
}
|
||||||
)}
|
)}
|
||||||
] ++ emqx_schema_hooks:injection_point('listeners.authentication').
|
] ++ emqx_schema_hooks:injection_point('mqtt.listener').
|
||||||
|
|
||||||
base_listener(Bind) ->
|
base_listener(Bind) ->
|
||||||
[
|
[
|
||||||
|
|
|
@ -25,11 +25,12 @@
|
||||||
-optional_callbacks([injected_fields/0]).
|
-optional_callbacks([injected_fields/0]).
|
||||||
|
|
||||||
-define(HOOKPOINT_PT_KEY(POINT_NAME), {?MODULE, fields, POINT_NAME}).
|
-define(HOOKPOINT_PT_KEY(POINT_NAME), {?MODULE, fields, POINT_NAME}).
|
||||||
-define(MODULE_PT_KEY(MOD_NAME), {?MODULE, mod, MOD_NAME}).
|
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
injection_point/1,
|
injection_point/1,
|
||||||
inject_fields_from_mod/1
|
any_injections/1,
|
||||||
|
inject_fields/2,
|
||||||
|
inject_from_modules/1
|
||||||
]).
|
]).
|
||||||
|
|
||||||
%% for tests
|
%% for tests
|
||||||
|
@ -45,22 +46,15 @@
|
||||||
injection_point(PointName) ->
|
injection_point(PointName) ->
|
||||||
persistent_term:get(?HOOKPOINT_PT_KEY(PointName), []).
|
persistent_term:get(?HOOKPOINT_PT_KEY(PointName), []).
|
||||||
|
|
||||||
inject_fields_from_mod(Module) ->
|
inject_fields(PointName, Fields) ->
|
||||||
case persistent_term:get(?MODULE_PT_KEY(Module), false) of
|
Key = ?HOOKPOINT_PT_KEY(PointName),
|
||||||
false ->
|
persistent_term:put(Key, Fields).
|
||||||
persistent_term:put(?MODULE_PT_KEY(Module), true),
|
|
||||||
do_inject_fields_from_mod(Module);
|
|
||||||
true ->
|
|
||||||
ok
|
|
||||||
end.
|
|
||||||
|
|
||||||
erase_injections() ->
|
erase_injections() ->
|
||||||
lists:foreach(
|
lists:foreach(
|
||||||
fun
|
fun
|
||||||
({?HOOKPOINT_PT_KEY(_) = Key, _}) ->
|
({?HOOKPOINT_PT_KEY(_) = Key, _}) ->
|
||||||
persistent_term:erase(Key);
|
persistent_term:erase(Key);
|
||||||
({?MODULE_PT_KEY(_) = Key, _}) ->
|
|
||||||
persistent_term:erase(Key);
|
|
||||||
(_) ->
|
(_) ->
|
||||||
ok
|
ok
|
||||||
end,
|
end,
|
||||||
|
@ -72,35 +66,53 @@ any_injections() ->
|
||||||
fun
|
fun
|
||||||
({?HOOKPOINT_PT_KEY(_), _}) ->
|
({?HOOKPOINT_PT_KEY(_), _}) ->
|
||||||
true;
|
true;
|
||||||
({?MODULE_PT_KEY(_), _}) ->
|
|
||||||
true;
|
|
||||||
(_) ->
|
(_) ->
|
||||||
false
|
false
|
||||||
end,
|
end,
|
||||||
persistent_term:get()
|
persistent_term:get()
|
||||||
).
|
).
|
||||||
|
|
||||||
|
any_injections(PointName) ->
|
||||||
|
persistent_term:get(?HOOKPOINT_PT_KEY(PointName), undefined) =/= undefined.
|
||||||
|
|
||||||
|
inject_from_modules(Modules) ->
|
||||||
|
Injections =
|
||||||
|
lists:foldl(
|
||||||
|
fun append_module_injections/2,
|
||||||
|
#{},
|
||||||
|
Modules
|
||||||
|
),
|
||||||
|
ok = inject_fields(maps:to_list(Injections)).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
do_inject_fields_from_mod(Module) ->
|
append_module_injections(Module, AllInjections) when is_atom(Module) ->
|
||||||
_ = Module:module_info(),
|
append_module_injections(Module:injected_fields(), AllInjections);
|
||||||
case erlang:function_exported(Module, injected_fields, 0) of
|
append_module_injections(ModuleInjections, AllInjections) when is_map(ModuleInjections) ->
|
||||||
true ->
|
maps:fold(
|
||||||
do_inject_fields_from_mod(Module, Module:injected_fields());
|
fun(PointName, Fields, Acc) ->
|
||||||
false ->
|
maps:update_with(
|
||||||
ok
|
PointName,
|
||||||
end.
|
fun(Fields0) ->
|
||||||
|
Fields0 ++ Fields
|
||||||
do_inject_fields_from_mod(_Module, HookFields) ->
|
end,
|
||||||
maps:foreach(
|
Fields,
|
||||||
fun(PointName, Fields) ->
|
Acc
|
||||||
inject_fields(PointName, Fields)
|
)
|
||||||
end,
|
end,
|
||||||
HookFields
|
AllInjections,
|
||||||
|
ModuleInjections
|
||||||
).
|
).
|
||||||
|
|
||||||
inject_fields(PointName, Fields) ->
|
inject_fields([]) ->
|
||||||
Key = ?HOOKPOINT_PT_KEY(PointName),
|
ok;
|
||||||
persistent_term:put(Key, Fields).
|
inject_fields([{PointName, Fields} | Rest]) ->
|
||||||
|
case emqx_schema_hooks:any_injections(PointName) of
|
||||||
|
true ->
|
||||||
|
inject_fields(Rest);
|
||||||
|
false ->
|
||||||
|
ok = emqx_schema_hooks:inject_fields(PointName, Fields),
|
||||||
|
inject_fields(Rest)
|
||||||
|
end.
|
||||||
|
|
|
@ -42,8 +42,8 @@ roots() -> [].
|
||||||
|
|
||||||
injected_fields() ->
|
injected_fields() ->
|
||||||
#{
|
#{
|
||||||
'authentication' => global_auth_fields(),
|
'mqtt.listener' => global_auth_fields(),
|
||||||
'listeners.authentication' => mqtt_listener_auth_fields()
|
'roots.high' => mqtt_listener_auth_fields()
|
||||||
}.
|
}.
|
||||||
|
|
||||||
tags() ->
|
tags() ->
|
||||||
|
|
|
@ -80,7 +80,7 @@ tags() ->
|
||||||
[<<"EMQX">>].
|
[<<"EMQX">>].
|
||||||
|
|
||||||
roots() ->
|
roots() ->
|
||||||
ok = ensure_fields_injected(),
|
ok = emqx_schema_hooks:inject_from_modules(?INJECTING_CONFIGS),
|
||||||
emqx_schema_high_prio_roots() ++
|
emqx_schema_high_prio_roots() ++
|
||||||
[
|
[
|
||||||
{"node",
|
{"node",
|
||||||
|
@ -1434,9 +1434,3 @@ ensure_unicode_path(Path, _) when is_list(Path) ->
|
||||||
Path;
|
Path;
|
||||||
ensure_unicode_path(Path, _) ->
|
ensure_unicode_path(Path, _) ->
|
||||||
throw({"not_string", Path}).
|
throw({"not_string", Path}).
|
||||||
|
|
||||||
ensure_fields_injected() ->
|
|
||||||
lists:foreach(
|
|
||||||
fun(Module) -> emqx_schema_hooks:inject_fields_from_mod(Module) end,
|
|
||||||
?INJECTING_CONFIGS
|
|
||||||
).
|
|
||||||
|
|
Loading…
Reference in New Issue