fix(gw): override configs while updating gw or listeners

This commit is contained in:
JianBo He 2022-05-17 11:58:03 +08:00
parent 636626f65f
commit 57bd862298
2 changed files with 41 additions and 12 deletions

View File

@ -392,7 +392,7 @@ pre_config_update(_, {load_gateway, GwName, Conf}, RawConf) ->
case maps:get(GwName, RawConf, undefined) of case maps:get(GwName, RawConf, undefined) of
undefined -> undefined ->
NConf = tune_gw_certs(fun convert_certs/2, GwName, Conf), NConf = tune_gw_certs(fun convert_certs/2, GwName, Conf),
{ok, emqx_map_lib:deep_merge(RawConf, #{GwName => NConf})}; {ok, emqx_map_lib:deep_put([GwName], RawConf, NConf)};
_ -> _ ->
badres_gateway(already_exist, GwName) badres_gateway(already_exist, GwName)
end; end;
@ -403,7 +403,7 @@ pre_config_update(_, {update_gateway, GwName, Conf}, RawConf) ->
_ -> _ ->
Conf1 = maps:without([<<"listeners">>, ?AUTHN_BIN], Conf), Conf1 = maps:without([<<"listeners">>, ?AUTHN_BIN], Conf),
NConf = tune_gw_certs(fun convert_certs/2, GwName, Conf1), NConf = tune_gw_certs(fun convert_certs/2, GwName, Conf1),
{ok, emqx_map_lib:deep_merge(RawConf, #{GwName => NConf})} {ok, emqx_map_lib:deep_put([GwName], RawConf, NConf)}
end; end;
pre_config_update(_, {unload_gateway, GwName}, RawConf) -> pre_config_update(_, {unload_gateway, GwName}, RawConf) ->
_ = tune_gw_certs( _ = tune_gw_certs(
@ -439,12 +439,12 @@ pre_config_update(_, {update_listener, GwName, {LType, LName}, Conf}, RawConf) -
badres_listener(not_found, GwName, LType, LName); badres_listener(not_found, GwName, LType, LName);
OldConf -> OldConf ->
NConf = convert_certs(certs_dir(GwName), Conf, OldConf), NConf = convert_certs(certs_dir(GwName), Conf, OldConf),
NListener = #{LType => #{LName => NConf}}, NRawConf = emqx_map_lib:deep_put(
{ok, [GwName, <<"listeners">>, LType, LName],
emqx_map_lib:deep_merge( RawConf,
RawConf, NConf
#{GwName => #{<<"listeners">> => NListener}} ),
)} {ok, NRawConf}
end; end;
pre_config_update(_, {remove_listener, GwName, {LType, LName}}, RawConf) -> pre_config_update(_, {remove_listener, GwName, {LType, LName}}, RawConf) ->
Path = [GwName, <<"listeners">>, LType, LName], Path = [GwName, <<"listeners">>, LType, LName],

View File

@ -485,6 +485,20 @@ reason2msg(
"The authentication already exist on ~s", "The authentication already exist on ~s",
[listener_id(GwName, LType, LName)] [listener_id(GwName, LType, LName)]
); );
reason2msg(
{bad_ssl_config, #{
reason := Reason,
which_options := Options
}}
) ->
fmtstr("Bad TLS configuration for ~p, reason: ~s", [Options, Reason]);
reason2msg(
{#{roots := [{gateway, _}]}, ErrReports}
) ->
fmtstr(
"Invalid configurations, reason: ~s",
[validation_error_stringfy(ErrReports, [])]
);
reason2msg(_) -> reason2msg(_) ->
error. error.
@ -498,6 +512,25 @@ codestr(501) -> 'NOT_IMPLEMENTED'.
fmtstr(Fmt, Args) -> fmtstr(Fmt, Args) ->
lists:flatten(io_lib:format(Fmt, Args)). lists:flatten(io_lib:format(Fmt, Args)).
validation_error_stringfy([], Reasons) ->
lists:join(", ", lists:reverse(Reasons));
validation_error_stringfy(
[
{validation_error, #{
path := Path,
reason := unknown_fields,
unknown_fields := Fields
}}
| More
],
Reasons
) ->
ReasonStr = fmtstr("unknown fields ~p for ~s", [Fields, Path]),
validation_error_stringfy(More, [ReasonStr | Reasons]);
validation_error_stringfy([Other | More], Reasons) ->
ReasonStr = <<(emqx_gateway_utils:stringfy(Other))/binary>>,
validation_error_stringfy(More, [ReasonStr | Reasons]).
-spec with_authn(binary(), function()) -> any(). -spec with_authn(binary(), function()) -> any().
with_authn(GwName0, Fun) -> with_authn(GwName0, Fun) ->
with_gateway(GwName0, fun(GwName, _GwConf) -> with_gateway(GwName0, fun(GwName, _GwConf) ->
@ -542,10 +575,6 @@ with_gateway(GwName0, Fun) ->
lists:join(".", lists:map(fun to_list/1, Path0)) lists:join(".", lists:map(fun to_list/1, Path0))
), ),
return_http_error(404, "Resource not found. path: " ++ Path); return_http_error(404, "Resource not found. path: " ++ Path);
%% Exceptions from emqx_gateway_conf:convert_certs/2,3
error:{bad_ssl_config, Reason0} ->
Reason = emqx_gateway_utils:stringfy(Reason0),
return_http_error(400, ["Bad SSL config, reason: ", Reason]);
Class:Reason:Stk -> Class:Reason:Stk ->
?SLOG(error, #{ ?SLOG(error, #{
msg => "uncatched_error", msg => "uncatched_error",