fix(backup): accept files outside `data/dir` when importing

Fixes https://github.com/emqx/emqx/issues/7990

Currently, when importing a data backup using `emqx_ctl data import
/some/data.json`, it'll only search in the `data/backup` directory and
fail if the file is not inside that dir.
This commit is contained in:
Thales Macedo Garitezi 2022-05-19 11:24:57 -03:00
parent b1d642bd6a
commit 67e3e2de96
No known key found for this signature in database
GPG Key ID: DD279F8152A9B6DD
2 changed files with 21 additions and 1 deletions

View File

@ -666,7 +666,10 @@ look_up_file(Filename) ->
end,
case lists:filter(Filter, backup_files()) of
[] ->
{error, not_found};
case filelib:is_file(Filename) of
true -> {ok, Filename};
false -> {error, not_found}
end;
List ->
{ok, hd(List)}
end.

View File

@ -72,6 +72,13 @@ init_per_testcase(t_plugins_cmd, Config) ->
meck:expect(emqx_plugins, reload, fun(_) -> ok end),
mock_print(),
Config;
init_per_testcase(t_import_outside_backup_dir, Config) ->
RandomName = emqx_guid:to_hexstr(emqx_guid:gen()),
Filepath = "/tmp/" ++ binary_to_list(RandomName) ++ ".json",
FakeData = #{version => "4.4"},
ok = file:write_file(Filepath, emqx_json:encode(FakeData)),
[ {tmp_file, Filepath}
| Config];
init_per_testcase(_Case, Config) ->
mock_print(),
Config.
@ -79,6 +86,10 @@ init_per_testcase(_Case, Config) ->
end_per_testcase(t_plugins_cmd, _Config) ->
meck:unload(emqx_plugins),
unmock_print();
end_per_testcase(t_import_outside_backup_dir, Config) ->
Filepath = ?config(tmp_file, Config),
file:delete(Filepath),
ok;
end_per_testcase(_Case, _Config) ->
unmock_print().
@ -384,6 +395,12 @@ t_backup_file(_)->
{error, not_found} = emqx_mgmt_data_backup:delete_backup_file(BadFilename),
ok.
t_import_outside_backup_dir(Config) ->
Filepath = ?config(tmp_file, Config),
Env = "{}",
?assertEqual(ok, emqx_mgmt_data_backup:import(Filepath, Env)),
ok.
mock_print() ->
ok = safe_unmeck(emqx_ctl),
meck:new(emqx_ctl, [non_strict, passthrough]),