fix(sso): updates the SSO backend when the `[dashboard]` has updated
This commit is contained in:
parent
e63d484632
commit
cfdb25b213
|
@ -678,7 +678,7 @@ return_change_result(ConfKeyPath, {{update, Req}, Opts}) ->
|
||||||
case Req =/= ?TOMBSTONE_CONFIG_CHANGE_REQ of
|
case Req =/= ?TOMBSTONE_CONFIG_CHANGE_REQ of
|
||||||
true ->
|
true ->
|
||||||
#{
|
#{
|
||||||
config => emqx_config:get(ConfKeyPath),
|
config => emqx_config:get(ConfKeyPath, undefined),
|
||||||
raw_config => return_rawconf(ConfKeyPath, Opts)
|
raw_config => return_rawconf(ConfKeyPath, Opts)
|
||||||
};
|
};
|
||||||
false ->
|
false ->
|
||||||
|
|
|
@ -35,12 +35,14 @@
|
||||||
update/2,
|
update/2,
|
||||||
delete/1,
|
delete/1,
|
||||||
pre_config_update/3,
|
pre_config_update/3,
|
||||||
post_config_update/5
|
post_config_update/5,
|
||||||
|
propagated_post_config_update/5
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-import(emqx_dashboard_sso, [provider/1]).
|
-import(emqx_dashboard_sso, [provider/1]).
|
||||||
|
|
||||||
-define(MOD_KEY_PATH, [dashboard, sso]).
|
-define(MOD_KEY_PATH, [dashboard, sso]).
|
||||||
|
-define(MOD_KEY_PATH(Sub), [dashboard, sso, Sub]).
|
||||||
-define(RESOURCE_GROUP, <<"emqx_dashboard_sso">>).
|
-define(RESOURCE_GROUP, <<"emqx_dashboard_sso">>).
|
||||||
-define(DEFAULT_RESOURCE_OPTS, #{
|
-define(DEFAULT_RESOURCE_OPTS, #{
|
||||||
start_after_created => false
|
start_after_created => false
|
||||||
|
@ -110,7 +112,7 @@ call(Req) ->
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
init([]) ->
|
init([]) ->
|
||||||
process_flag(trap_exit, true),
|
process_flag(trap_exit, true),
|
||||||
emqx_conf:add_handler(?MOD_KEY_PATH, ?MODULE),
|
add_handler(),
|
||||||
emqx_utils_ets:new(
|
emqx_utils_ets:new(
|
||||||
dashboard_sso,
|
dashboard_sso,
|
||||||
[
|
[
|
||||||
|
@ -138,7 +140,7 @@ handle_info(_Info, State) ->
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
terminate(_Reason, _State) ->
|
terminate(_Reason, _State) ->
|
||||||
emqx_conf:remove_handler(?MOD_KEY_PATH),
|
remove_handler(),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
code_change(_OldVsn, State, _Extra) ->
|
code_change(_OldVsn, State, _Extra) ->
|
||||||
|
@ -174,7 +176,7 @@ start_backend_services() ->
|
||||||
).
|
).
|
||||||
|
|
||||||
update_config(Backend, UpdateReq) ->
|
update_config(Backend, UpdateReq) ->
|
||||||
case emqx_conf:update(?MOD_KEY_PATH, UpdateReq, #{override_to => cluster}) of
|
case emqx_conf:update(?MOD_KEY_PATH(Backend), UpdateReq, #{override_to => cluster}) of
|
||||||
{ok, UpdateResult} ->
|
{ok, UpdateResult} ->
|
||||||
#{post_config_update := #{?MODULE := Result}} = UpdateResult,
|
#{post_config_update := #{?MODULE := Result}} = UpdateResult,
|
||||||
?SLOG(info, #{
|
?SLOG(info, #{
|
||||||
|
@ -192,25 +194,38 @@ update_config(Backend, UpdateReq) ->
|
||||||
Error
|
Error
|
||||||
end.
|
end.
|
||||||
|
|
||||||
pre_config_update(_Path, {update, Backend, Config}, OldConf) ->
|
pre_config_update(_, {update, _Backend, Config}, _OldConf) ->
|
||||||
BackendBin = bin(Backend),
|
{ok, Config};
|
||||||
{ok, update_raw_sso_cfg(OldConf, BackendBin, Config)};
|
pre_config_update(_, {delete, _Backend}, undefined) ->
|
||||||
pre_config_update(_Path, {delete, Backend}, OldConf) ->
|
|
||||||
BackendBin = bin(Backend),
|
|
||||||
case find_raw_sso_cfg(BackendBin, OldConf) of
|
|
||||||
error ->
|
|
||||||
throw(not_exists);
|
throw(not_exists);
|
||||||
{ok, _} ->
|
pre_config_update(_, {delete, _Backend}, _OldConf) ->
|
||||||
{ok, maps:remove(BackendBin, OldConf)}
|
{ok, null}.
|
||||||
end.
|
|
||||||
|
|
||||||
post_config_update(_Path, UpdateReq, NewConf, _OldConf, _AppEnvs) ->
|
post_config_update(_, UpdateReq, NewConf, _OldConf, _AppEnvs) ->
|
||||||
Result = call({update_config, UpdateReq, NewConf}),
|
Result = call({update_config, UpdateReq, NewConf}),
|
||||||
{ok, Result}.
|
{ok, Result}.
|
||||||
|
|
||||||
on_config_update({update, Backend, _Config}, NewConf) ->
|
propagated_post_config_update(
|
||||||
|
?MOD_KEY_PATH(BackendBin) = Path, _UpdateReq, undefined, OldConf, AppEnvs
|
||||||
|
) ->
|
||||||
|
case atom(BackendBin) of
|
||||||
|
{ok, Backend} ->
|
||||||
|
post_config_update(Path, {delete, Backend}, undefined, OldConf, AppEnvs);
|
||||||
|
Error ->
|
||||||
|
Error
|
||||||
|
end;
|
||||||
|
propagated_post_config_update(
|
||||||
|
?MOD_KEY_PATH(BackendBin) = Path, _UpdateReq, NewConf, OldConf, AppEnvs
|
||||||
|
) ->
|
||||||
|
case atom(BackendBin) of
|
||||||
|
{ok, Backend} ->
|
||||||
|
post_config_update(Path, {update, Backend, undefined}, NewConf, OldConf, AppEnvs);
|
||||||
|
Error ->
|
||||||
|
Error
|
||||||
|
end.
|
||||||
|
|
||||||
|
on_config_update({update, Backend, _RawConfig}, Config) ->
|
||||||
Provider = provider(Backend),
|
Provider = provider(Backend),
|
||||||
Config = maps:get(Backend, NewConf),
|
|
||||||
case lookup(Backend) of
|
case lookup(Backend) of
|
||||||
undefined ->
|
undefined ->
|
||||||
on_backend_updated(
|
on_backend_updated(
|
||||||
|
@ -268,12 +283,11 @@ bin(A) when is_atom(A) -> atom_to_binary(A, utf8);
|
||||||
bin(L) when is_list(L) -> list_to_binary(L);
|
bin(L) when is_list(L) -> list_to_binary(L);
|
||||||
bin(X) -> X.
|
bin(X) -> X.
|
||||||
|
|
||||||
update_raw_sso_cfg(undefined, Backend, BackendCfg) ->
|
atom(B) ->
|
||||||
#{Backend => BackendCfg};
|
emqx_utils:safe_to_existing_atom(B).
|
||||||
update_raw_sso_cfg(RAW_SSOCfg, Backend, BackendCfg) ->
|
|
||||||
RAW_SSOCfg#{Backend => BackendCfg}.
|
|
||||||
|
|
||||||
find_raw_sso_cfg(_Backend, undefined) ->
|
add_handler() ->
|
||||||
error;
|
ok = emqx_conf:add_handler(?MOD_KEY_PATH('?'), ?MODULE).
|
||||||
find_raw_sso_cfg(Backend, RAW_SSOCfg) ->
|
|
||||||
maps:find(Backend, RAW_SSOCfg).
|
remove_handler() ->
|
||||||
|
ok = emqx_conf:remove_handler(?MOD_KEY_PATH('?')).
|
||||||
|
|
Loading…
Reference in New Issue