From 27f5e765b584e51f5476bd602610a7c4d1e479f7 Mon Sep 17 00:00:00 2001 From: zhongwencool Date: Thu, 13 Jan 2022 13:53:37 +0800 Subject: [PATCH] fix(export): emqx_auth_mnesia import failed after 4.3.x --- .../test/emqx_auth_mnesia_SUITE.erl | 1 - .../src/emqx_mgmt_data_backup.erl | 20 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/apps/emqx_auth_mnesia/test/emqx_auth_mnesia_SUITE.erl b/apps/emqx_auth_mnesia/test/emqx_auth_mnesia_SUITE.erl index 4246965d9..12b473815 100644 --- a/apps/emqx_auth_mnesia/test/emqx_auth_mnesia_SUITE.erl +++ b/apps/emqx_auth_mnesia/test/emqx_auth_mnesia_SUITE.erl @@ -73,7 +73,6 @@ set_default(ClientId, UserName, Pwd, HashType) -> application:set_env(emqx_auth_mnesia, clientid_list, [{ClientId, Pwd}]), application:set_env(emqx_auth_mnesia, username_list, [{UserName, Pwd}]), application:set_env(emqx_auth_mnesia, password_hash, HashType), - application:set_env(emqx_auth_mnesia, password_hash, HashType), ok. %%------------------------------------------------------------------------------ %% Testcases diff --git a/apps/emqx_management/src/emqx_mgmt_data_backup.erl b/apps/emqx_management/src/emqx_mgmt_data_backup.erl index 6e467a8ba..d17167785 100644 --- a/apps/emqx_management/src/emqx_mgmt_data_backup.erl +++ b/apps/emqx_management/src/emqx_mgmt_data_backup.erl @@ -602,7 +602,7 @@ import(Filename, OverridesJson) -> Overrides = emqx_json:decode(OverridesJson, [return_maps]), Data = maps:merge(Imported, Overrides), Version = to_version(maps:get(<<"version">>, Data)), - read_global_auth_type(Data), + read_global_auth_type(Data, Version), try do_import_data(Data, Version), logger:debug("The emqx data has been imported successfully"), @@ -621,7 +621,7 @@ import(Filename, OverridesJson) -> Overrides = emqx_json:decode(OverridesJson, [return_maps]), Data = maps:merge(Imported, Overrides), Version = to_version(maps:get(<<"version">>, Data)), - read_global_auth_type(Data), + read_global_auth_type(Data, Version), case is_version_supported(Data, Version) of true -> try @@ -696,17 +696,17 @@ is_version_supported2(Version) -> end. -endif. -read_global_auth_type(Data) -> +read_global_auth_type(Data, Version) -> case {maps:get(<<"auth_mnesia">>, Data, []), maps:get(<<"acl_mnesia">>, Data, [])} of {[], []} -> %% Auth mnesia plugin is not used: ok; _ -> - do_read_global_auth_type(Data) + do_read_global_auth_type(Data, Version) end. -ifdef(EMQX_ENTERPRISE). -do_read_global_auth_type(Data) -> +do_read_global_auth_type(Data, _Version) -> case Data of #{<<"auth.mnesia.as">> := <<"username">>} -> application:set_env(emqx_auth_mnesia, as, username); @@ -717,13 +717,15 @@ do_read_global_auth_type(Data) -> end. -else. -do_read_global_auth_type(Data) -> +do_read_global_auth_type(Data, FromVersion) -> case Data of #{<<"auth.mnesia.as">> := <<"username">>} -> application:set_env(emqx_auth_mnesia, as, username); #{<<"auth.mnesia.as">> := <<"clientid">>} -> application:set_env(emqx_auth_mnesia, as, clientid); - _ -> + _ when FromVersion =:= "4.0" orelse + FromVersion =:= "4.1" orelse + FromVersion =:= "4.2"-> logger:error("While importing data from EMQX versions prior to 4.3 " "it is necessary to specify the value of \"auth.mnesia.as\" parameter " "as it was configured in etc/plugins/emqx_auth_mnesia.conf.\n" @@ -732,7 +734,9 @@ do_read_global_auth_type(Data) -> "or\n" " $ emqx_ctl data import --env '{\"auth.mnesia.as\":\"clientid\"}'", []), - error(import_failed) + error(import_failed); + _ -> + ok end. -endif.