Merge pull request #10225 from SergeTupchiy/EMQX-9290-fix-plugin-name-clash
fix(emqx_management): resolve plugin name clashes
This commit is contained in:
commit
f4472f66de
|
@ -48,6 +48,9 @@
|
|||
|
||||
-define(NAME_RE, "^[A-Za-z]+[A-Za-z0-9-_.]*$").
|
||||
-define(TAGS, [<<"Plugins">>]).
|
||||
%% Plugin NameVsn must follow the pattern <app_name>-<vsn>,
|
||||
%% app_name must be a snake_case (no '-' allowed).
|
||||
-define(VSN_WILDCARD, "-*.tar.gz").
|
||||
|
||||
namespace() -> "plugins".
|
||||
|
||||
|
@ -334,7 +337,7 @@ upload_install(post, #{body := #{<<"plugin">> := Plugin}}) when is_map(Plugin) -
|
|||
case emqx_plugins:parse_name_vsn(FileName) of
|
||||
{ok, AppName, _Vsn} ->
|
||||
AppDir = filename:join(emqx_plugins:install_dir(), AppName),
|
||||
case filelib:wildcard(AppDir ++ "*.tar.gz") of
|
||||
case filelib:wildcard(AppDir ++ ?VSN_WILDCARD) of
|
||||
[] ->
|
||||
do_install_package(FileName, Bin);
|
||||
OtherVsn ->
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
-define(EMQX_PLUGIN_TEMPLATE_NAME, "emqx_plugin_template").
|
||||
-define(EMQX_PLUGIN_TEMPLATE_VSN, "5.0.0").
|
||||
-define(PACKAGE_SUFFIX, ".tar.gz").
|
||||
|
||||
|
@ -89,6 +90,27 @@ t_plugins(Config) ->
|
|||
{ok, []} = uninstall_plugin(NameVsn),
|
||||
ok.
|
||||
|
||||
t_install_plugin_matching_exisiting_name(Config) ->
|
||||
DemoShDir = proplists:get_value(demo_sh_dir, Config),
|
||||
PackagePath = get_demo_plugin_package(DemoShDir),
|
||||
NameVsn = filename:basename(PackagePath, ?PACKAGE_SUFFIX),
|
||||
ok = emqx_plugins:ensure_uninstalled(NameVsn),
|
||||
ok = emqx_plugins:delete_package(NameVsn),
|
||||
NameVsn1 = ?EMQX_PLUGIN_TEMPLATE_NAME ++ "_a" ++ "-" ++ ?EMQX_PLUGIN_TEMPLATE_VSN,
|
||||
PackagePath1 = create_renamed_package(PackagePath, NameVsn1),
|
||||
NameVsn1 = filename:basename(PackagePath1, ?PACKAGE_SUFFIX),
|
||||
ok = emqx_plugins:ensure_uninstalled(NameVsn1),
|
||||
ok = emqx_plugins:delete_package(NameVsn1),
|
||||
%% First, install plugin "emqx_plugin_template_a", then:
|
||||
%% "emqx_plugin_template" which matches the beginning
|
||||
%% of the previously installed plugin name
|
||||
ok = install_plugin(PackagePath1),
|
||||
ok = install_plugin(PackagePath),
|
||||
{ok, _} = describe_plugins(NameVsn),
|
||||
{ok, _} = describe_plugins(NameVsn1),
|
||||
{ok, _} = uninstall_plugin(NameVsn),
|
||||
{ok, _} = uninstall_plugin(NameVsn1).
|
||||
|
||||
t_bad_plugin(Config) ->
|
||||
DemoShDir = proplists:get_value(demo_sh_dir, Config),
|
||||
PackagePathOrig = get_demo_plugin_package(DemoShDir),
|
||||
|
@ -160,9 +182,31 @@ uninstall_plugin(Name) ->
|
|||
|
||||
get_demo_plugin_package(Dir) ->
|
||||
#{package := Pkg} = emqx_plugins_SUITE:get_demo_plugin_package(),
|
||||
FileName = "emqx_plugin_template-" ++ ?EMQX_PLUGIN_TEMPLATE_VSN ++ ?PACKAGE_SUFFIX,
|
||||
FileName = ?EMQX_PLUGIN_TEMPLATE_NAME ++ "-" ++ ?EMQX_PLUGIN_TEMPLATE_VSN ++ ?PACKAGE_SUFFIX,
|
||||
PluginPath = "./" ++ FileName,
|
||||
Pkg = filename:join([Dir, FileName]),
|
||||
_ = os:cmd("cp " ++ Pkg ++ " " ++ PluginPath),
|
||||
true = filelib:is_regular(PluginPath),
|
||||
PluginPath.
|
||||
|
||||
create_renamed_package(PackagePath, NewNameVsn) ->
|
||||
{ok, Content} = erl_tar:extract(PackagePath, [compressed, memory]),
|
||||
{ok, NewName, _Vsn} = emqx_plugins:parse_name_vsn(NewNameVsn),
|
||||
NewNameB = atom_to_binary(NewName, utf8),
|
||||
Content1 = lists:map(
|
||||
fun({F, B}) ->
|
||||
[_ | PathPart] = filename:split(F),
|
||||
B1 = update_release_json(PathPart, B, NewNameB),
|
||||
{filename:join([NewNameVsn | PathPart]), B1}
|
||||
end,
|
||||
Content
|
||||
),
|
||||
NewPackagePath = filename:join(filename:dirname(PackagePath), NewNameVsn ++ ?PACKAGE_SUFFIX),
|
||||
ok = erl_tar:create(NewPackagePath, Content1, [compressed]),
|
||||
NewPackagePath.
|
||||
|
||||
update_release_json(["release.json"], FileContent, NewName) ->
|
||||
ContentMap = emqx_json:decode(FileContent, [return_maps]),
|
||||
emqx_json:encode(ContentMap#{<<"name">> => NewName});
|
||||
update_release_json(_FileName, FileContent, _NewName) ->
|
||||
FileContent.
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
Allow installing a plugin if its name matches the beginning of another (already installed) plugin name.
|
||||
For example: if plugin "emqx_plugin_template_a" is installed, it must not block installing plugin "emqx_plugin_template".
|
Loading…
Reference in New Issue