fix: ensure atom key for emqx_config:get

This commit is contained in:
某文 2023-05-10 22:58:59 +08:00 committed by Zhongwen Deng
parent 78c2c53daa
commit 6056b0e7a8
6 changed files with 28 additions and 17 deletions

View File

@ -23,9 +23,7 @@
%% Application callbacks %% Application callbacks
-export([ -export([
start/2, start/2,
stop/1, stop/1
chain_configs/0,
initialize/0
]). ]).
-include_lib("emqx/include/emqx_authentication.hrl"). -include_lib("emqx/include/emqx_authentication.hrl").
@ -56,7 +54,6 @@ stop(_State) ->
initialize() -> initialize() ->
ok = ?AUTHN:register_providers(emqx_authn:providers()), ok = ?AUTHN:register_providers(emqx_authn:providers()),
io:format("init:~p~n", [chain_configs()]),
lists:foreach( lists:foreach(
fun({ChainName, AuthConfig}) -> fun({ChainName, AuthConfig}) ->
?AUTHN:initialize_authentication( ?AUTHN:initialize_authentication(
@ -86,9 +83,11 @@ listener_chain_configs() ->
). ).
auth_config_path(ListenerID) -> auth_config_path(ListenerID) ->
[<<"listeners">>] ++ Names = [
binary:split(atom_to_binary(ListenerID), <<":">>) ++ binary_to_existing_atom(N, utf8)
[?EMQX_AUTHENTICATION_CONFIG_ROOT_NAME_BINARY]. || N <- binary:split(atom_to_binary(ListenerID), <<":">>)
],
[listeners] ++ Names ++ [?EMQX_AUTHENTICATION_CONFIG_ROOT_NAME_ATOM].
provider_types() -> provider_types() ->
lists:map(fun({Type, _Module}) -> Type end, emqx_authn:providers()). lists:map(fun({Type, _Module}) -> Type end, emqx_authn:providers()).

View File

@ -54,13 +54,14 @@
-define(BRIDGE_NOT_FOUND(BRIDGE_TYPE, BRIDGE_NAME), -define(BRIDGE_NOT_FOUND(BRIDGE_TYPE, BRIDGE_NAME),
?NOT_FOUND( ?NOT_FOUND(
<<"Bridge lookup failed: bridge named '", (BRIDGE_NAME)/binary, "' of type ", <<"Bridge lookup failed: bridge named '", (bin(BRIDGE_NAME))/binary, "' of type ",
(bin(BRIDGE_TYPE))/binary, " does not exist.">> (bin(BRIDGE_TYPE))/binary, " does not exist.">>
) )
). ).
%% Don't turn bridge_name to atom, it's maybe not a existing atom.
-define(TRY_PARSE_ID(ID, EXPR), -define(TRY_PARSE_ID(ID, EXPR),
try emqx_bridge_resource:parse_bridge_id(Id) of try emqx_bridge_resource:parse_bridge_id(Id, #{atom_name => false}) of
{BridgeType, BridgeName} -> {BridgeType, BridgeName} ->
EXPR EXPR
catch catch

View File

@ -25,6 +25,7 @@
resource_id/2, resource_id/2,
bridge_id/2, bridge_id/2,
parse_bridge_id/1, parse_bridge_id/1,
parse_bridge_id/2,
bridge_hookpoint/1, bridge_hookpoint/1,
bridge_hookpoint_to_bridge_id/1 bridge_hookpoint_to_bridge_id/1
]). ]).
@ -86,11 +87,15 @@ bridge_id(BridgeType, BridgeName) ->
Type = bin(BridgeType), Type = bin(BridgeType),
<<Type/binary, ":", Name/binary>>. <<Type/binary, ":", Name/binary>>.
-spec parse_bridge_id(list() | binary() | atom()) -> {atom(), binary()}.
parse_bridge_id(BridgeId) -> parse_bridge_id(BridgeId) ->
parse_bridge_id(BridgeId, #{atom_name => true}).
-spec parse_bridge_id(list() | binary() | atom(), #{atom_name => boolean()}) ->
{atom(), atom() | binary()}.
parse_bridge_id(BridgeId, Opts) ->
case string:split(bin(BridgeId), ":", all) of case string:split(bin(BridgeId), ":", all) of
[Type, Name] -> [Type, Name] ->
{to_type_atom(Type), validate_name(Name)}; {to_type_atom(Type), validate_name(Name, Opts)};
_ -> _ ->
invalid_data( invalid_data(
<<"should be of pattern {type}:{name}, but got ", BridgeId/binary>> <<"should be of pattern {type}:{name}, but got ", BridgeId/binary>>
@ -105,13 +110,16 @@ bridge_hookpoint_to_bridge_id(?BRIDGE_HOOKPOINT(BridgeId)) ->
bridge_hookpoint_to_bridge_id(_) -> bridge_hookpoint_to_bridge_id(_) ->
{error, bad_bridge_hookpoint}. {error, bad_bridge_hookpoint}.
validate_name(Name0) -> validate_name(Name0, Opts) ->
Name = unicode:characters_to_list(Name0, utf8), Name = unicode:characters_to_list(Name0, utf8),
case is_list(Name) andalso Name =/= [] of case is_list(Name) andalso Name =/= [] of
true -> true ->
case lists:all(fun is_id_char/1, Name) of case lists:all(fun is_id_char/1, Name) of
true -> true ->
Name0; case maps:get(atom_name, Opts, true) of
true -> list_to_existing_atom(Name);
false -> Name0
end;
false -> false ->
invalid_data(<<"bad name: ", Name0/binary>>) invalid_data(<<"bad name: ", Name0/binary>>)
end; end;

View File

@ -583,7 +583,7 @@ config(Args0, More) ->
ct:pal("Running tests with conf:\n~p", [Conf]), ct:pal("Running tests with conf:\n~p", [Conf]),
InstId = maps:get("instance_id", Args), InstId = maps:get("instance_id", Args),
<<"bridge:", BridgeId/binary>> = InstId, <<"bridge:", BridgeId/binary>> = InstId,
{Type, Name} = emqx_bridge_resource:parse_bridge_id(BridgeId), {Type, Name} = emqx_bridge_resource:parse_bridge_id(BridgeId, #{atom_name => false}),
TypeBin = atom_to_binary(Type), TypeBin = atom_to_binary(Type),
hocon_tconf:check_plain( hocon_tconf:check_plain(
emqx_bridge_schema, emqx_bridge_schema,
@ -596,7 +596,7 @@ config(Args0, More) ->
hocon_config(Args) -> hocon_config(Args) ->
InstId = maps:get("instance_id", Args), InstId = maps:get("instance_id", Args),
<<"bridge:", BridgeId/binary>> = InstId, <<"bridge:", BridgeId/binary>> = InstId,
{_Type, Name} = emqx_bridge_resource:parse_bridge_id(BridgeId), {_Type, Name} = emqx_bridge_resource:parse_bridge_id(BridgeId, #{atom_name => false}),
AuthConf = maps:get("authentication", Args), AuthConf = maps:get("authentication", Args),
AuthTemplate = iolist_to_binary(hocon_config_template_authentication(AuthConf)), AuthTemplate = iolist_to_binary(hocon_config_template_authentication(AuthConf)),
AuthConfRendered = bbmustache:render(AuthTemplate, AuthConf), AuthConfRendered = bbmustache:render(AuthTemplate, AuthConf),

View File

@ -2,7 +2,7 @@
{application, emqx_rule_engine, [ {application, emqx_rule_engine, [
{description, "EMQX Rule Engine"}, {description, "EMQX Rule Engine"},
% strict semver, bump manually! % strict semver, bump manually!
{vsn, "5.0.15"}, {vsn, "5.0.16"},
{modules, []}, {modules, []},
{registered, [emqx_rule_engine_sup, emqx_rule_engine]}, {registered, [emqx_rule_engine_sup, emqx_rule_engine]},
{applications, [kernel, stdlib, rulesql, getopt, emqx_ctl]}, {applications, [kernel, stdlib, rulesql, getopt, emqx_ctl]},

View File

@ -341,7 +341,10 @@ get_basic_usage_info() ->
tally_referenced_bridges(BridgeIDs, Acc0) -> tally_referenced_bridges(BridgeIDs, Acc0) ->
lists:foldl( lists:foldl(
fun(BridgeID, Acc) -> fun(BridgeID, Acc) ->
{BridgeType, _BridgeName} = emqx_bridge_resource:parse_bridge_id(BridgeID), {BridgeType, _BridgeName} = emqx_bridge_resource:parse_bridge_id(
BridgeID,
#{atom_name => false}
),
maps:update_with( maps:update_with(
BridgeType, BridgeType,
fun(X) -> X + 1 end, fun(X) -> X + 1 end,