fix: get_sources return default authz when sources key not found
This commit is contained in:
parent
01671d82c7
commit
1ca1ba9e7a
|
@ -163,20 +163,19 @@ do_pre_config_update(?ROOT_KEY, NewConf, OldConf) ->
|
||||||
do_pre_config_replace(NewConf, OldConf).
|
do_pre_config_replace(NewConf, OldConf).
|
||||||
|
|
||||||
do_pre_config_merge(NewConf, OldConf) ->
|
do_pre_config_merge(NewConf, OldConf) ->
|
||||||
MergeConf0 = emqx_utils_maps:deep_merge(OldConf, NewConf),
|
MergeConf = emqx_utils_maps:deep_merge(OldConf, NewConf),
|
||||||
NewSources = merge_sources(NewConf, OldConf),
|
NewSources = merge_sources(OldConf, NewConf),
|
||||||
do_pre_config_replace(MergeConf0#{<<"sources">> := NewSources}, OldConf).
|
do_pre_config_replace(MergeConf#{<<"sources">> => NewSources}, OldConf).
|
||||||
|
|
||||||
%% override the entire config when updating the root key
|
%% override the entire config when updating the root key
|
||||||
%% emqx_conf:update(?ROOT_KEY, Conf);
|
%% emqx_conf:update(?ROOT_KEY, Conf);
|
||||||
do_pre_config_replace(Conf, Conf) ->
|
do_pre_config_replace(Conf, Conf) ->
|
||||||
Conf;
|
Conf;
|
||||||
do_pre_config_replace(NewConf, OldConf) ->
|
do_pre_config_replace(NewConf, OldConf) ->
|
||||||
Default = [emqx_authz_schema:default_authz()],
|
NewSources = get_sources(NewConf),
|
||||||
NewSources = maps:get(<<"sources">>, NewConf, Default),
|
OldSources = get_sources(OldConf),
|
||||||
OldSources = maps:get(<<"sources">>, OldConf, Default),
|
ReplaceSources = do_pre_config_update({?CMD_REPLACE, NewSources}, OldSources),
|
||||||
NewSources1 = do_pre_config_update({?CMD_REPLACE, NewSources}, OldSources),
|
NewConf#{<<"sources">> => ReplaceSources}.
|
||||||
NewConf#{<<"sources">> => NewSources1}.
|
|
||||||
|
|
||||||
do_pre_config_update({?CMD_MOVE, _, _} = Cmd, Sources) ->
|
do_pre_config_update({?CMD_MOVE, _, _} = Cmd, Sources) ->
|
||||||
do_move(Cmd, Sources);
|
do_move(Cmd, Sources);
|
||||||
|
@ -472,8 +471,8 @@ get_enabled_authzs() ->
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
||||||
import_config(#{?CONF_NS_BINARY := AuthzConf}) ->
|
import_config(#{?CONF_NS_BINARY := AuthzConf}) ->
|
||||||
Sources = maps:get(<<"sources">>, AuthzConf, []),
|
Sources = get_sources(AuthzConf),
|
||||||
OldSources = emqx:get_raw_config(?CONF_KEY_PATH, []),
|
OldSources = emqx:get_raw_config(?CONF_KEY_PATH, [emqx_authz_schema:default_authz()]),
|
||||||
MergedSources = emqx_utils:merge_lists(OldSources, Sources, fun type/1),
|
MergedSources = emqx_utils:merge_lists(OldSources, Sources, fun type/1),
|
||||||
MergedAuthzConf = AuthzConf#{<<"sources">> => MergedSources},
|
MergedAuthzConf = AuthzConf#{<<"sources">> => MergedSources},
|
||||||
case emqx_conf:update([?CONF_NS_ATOM], MergedAuthzConf, #{override_to => cluster}) of
|
case emqx_conf:update([?CONF_NS_ATOM], MergedAuthzConf, #{override_to => cluster}) of
|
||||||
|
@ -533,12 +532,12 @@ take(Type) -> take(Type, lookup()).
|
||||||
%% Take the source of give type, the sources list is split into two parts
|
%% Take the source of give type, the sources list is split into two parts
|
||||||
%% front part and rear part.
|
%% front part and rear part.
|
||||||
take(Type, Sources) ->
|
take(Type, Sources) ->
|
||||||
{Front, Rear} = lists:splitwith(fun(T) -> type(T) =/= type(Type) end, Sources),
|
Expect = type(Type),
|
||||||
case Rear =:= [] of
|
case lists:splitwith(fun(T) -> type(T) =/= Expect end, Sources) of
|
||||||
true ->
|
{_Front, []} ->
|
||||||
throw({not_found_source, Type});
|
throw({not_found_source, Type});
|
||||||
_ ->
|
{Front, [Found | Rear]} ->
|
||||||
{hd(Rear), Front, tl(Rear)}
|
{Found, Front, Rear}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
find_action_in_hooks() ->
|
find_action_in_hooks() ->
|
||||||
|
@ -636,14 +635,11 @@ check_acl_file_rules(Path, Rules) ->
|
||||||
_ = file:delete(TmpPath)
|
_ = file:delete(TmpPath)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
merge_sources(OldConf, NewConf) ->
|
merge_sources(OriginConf, NewConf) ->
|
||||||
Default = [emqx_authz_schema:default_authz()],
|
|
||||||
NewSources0 = maps:get(<<"sources">>, NewConf, Default),
|
|
||||||
OriginSources0 = maps:get(<<"sources">>, OldConf, Default),
|
|
||||||
{OriginSource, NewSources} =
|
{OriginSource, NewSources} =
|
||||||
lists:foldl(
|
lists:foldl(
|
||||||
fun(Old = #{<<"type">> := Type}, {OriginAcc, NewAcc}) ->
|
fun(Old = #{<<"type">> := Type}, {OriginAcc, NewAcc}) ->
|
||||||
case search(Type, NewAcc) of
|
case type_take(Type, NewAcc) of
|
||||||
not_found ->
|
not_found ->
|
||||||
{[Old | OriginAcc], NewAcc};
|
{[Old | OriginAcc], NewAcc};
|
||||||
{New, NewAcc1} ->
|
{New, NewAcc1} ->
|
||||||
|
@ -651,15 +647,20 @@ merge_sources(OldConf, NewConf) ->
|
||||||
{[MergeSource | OriginAcc], NewAcc1}
|
{[MergeSource | OriginAcc], NewAcc1}
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
{[], NewSources0},
|
{[], get_sources(NewConf)},
|
||||||
OriginSources0
|
get_sources(OriginConf)
|
||||||
),
|
),
|
||||||
lists:reverse(OriginSource) ++ NewSources.
|
lists:reverse(OriginSource) ++ NewSources.
|
||||||
|
|
||||||
search(Type, Sources) ->
|
get_sources(Conf) ->
|
||||||
case lists:splitwith(fun(T) -> type(T) =/= type(Type) end, Sources) of
|
Default = [emqx_authz_schema:default_authz()],
|
||||||
{_Front, []} -> not_found;
|
maps:get(<<"sources">>, Conf, Default).
|
||||||
{Front, [Target | Rear]} -> {Target, Front ++ Rear}
|
|
||||||
|
type_take(Type, Sources) ->
|
||||||
|
try take(Type, Sources) of
|
||||||
|
{Found, Front, Rear} -> {Found, Front ++ Rear}
|
||||||
|
catch
|
||||||
|
throw:{not_found_source, Type} -> not_found
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-ifdef(TEST).
|
-ifdef(TEST).
|
||||||
|
|
Loading…
Reference in New Issue