fix(emqx_resource): do not allow leading _ or - as resource name

This commit is contained in:
Zaiming (Stone) Shi 2023-11-21 21:42:55 +01:00
parent 869e73d637
commit 3261a12140
6 changed files with 14 additions and 11 deletions

View File

@ -199,7 +199,7 @@ t_create_with_bad_name(_Config) ->
?assertMatch(
{error,
{pre_config_update, emqx_bridge_app, #{
reason := <<"only 0-9a-zA-Z_- is allowed in resource name", _/binary>>,
reason := <<"Invalid name format.", _/binary>>,
kind := validation_error
}}},
emqx:update_config(Path, Conf)

View File

@ -1365,7 +1365,7 @@ t_create_with_bad_name(Config) ->
?assertMatch(
#{
<<"kind">> := <<"validation_error">>,
<<"reason">> := <<"only 0-9a-zA-Z_- is allowed in resource name", _/binary>>
<<"reason">> := <<"Invalid name format.", _/binary>>
},
Msg
),

View File

@ -829,7 +829,7 @@ t_create_with_bad_name(_Config) ->
<<"code">> := <<"BAD_REQUEST">>,
<<"message">> := #{
<<"kind">> := <<"validation_error">>,
<<"reason">> := <<"only 0-9a-zA-Z_- is allowed in resource name", _/binary>>
<<"reason">> := <<"Invalid name format.", _/binary>>
}
}}} = create_bridge_http_api_v1(Opts),
ok.

View File

@ -1034,10 +1034,8 @@ t_bad_name(Config) ->
Msg = emqx_utils_json:decode(Msg0, [return_maps]),
?assertMatch(
#{
<<"got">> := [<<"_bad_name">>],
<<"kind">> := <<"validation_error">>,
<<"path">> := <<"actions.kafka_producer">>,
<<"reason">> := <<"invalid_map_key">>
<<"reason">> := <<"Invalid name format.", _/binary>>
},
Msg
),

View File

@ -229,7 +229,7 @@ t_create_with_bad_name_direct_path(_Config) ->
{error,
{pre_config_update, _ConfigHandlerMod, #{
kind := validation_error,
reason := <<"only 0-9a-zA-Z_- is allowed in resource name", _/binary>>
reason := <<"Invalid name format.", _/binary>>
}}},
emqx:update_config(Path, ConnConfig)
),

View File

@ -812,11 +812,11 @@ validate_name(Name) ->
ok.
validate_name(<<>>, _Opts) ->
invalid_data("name cannot be empty string");
invalid_data("Name cannot be empty string");
validate_name(Name, _Opts) when size(Name) >= 255 ->
invalid_data("name length must be less than 255");
invalid_data("Name length must be less than 255");
validate_name(Name, Opts) ->
case re:run(Name, <<"^[-0-9a-zA-Z_]+$">>, [{capture, none}]) of
case re:run(Name, <<"^[0-9a-zA-Z][-0-9a-zA-Z_]*$">>, [{capture, none}]) of
match ->
case maps:get(atom_name, Opts, true) of
%% NOTE
@ -827,7 +827,12 @@ validate_name(Name, Opts) ->
end;
nomatch ->
invalid_data(
<<"only 0-9a-zA-Z_- is allowed in resource name, got: ", Name/binary>>
<<
"Invalid name format. The name must begin with a letter or number "
"(0-9, a-z, A-Z) and can only include underscores and hyphens as "
"non-initial characters. Got: ",
Name/binary
>>
)
end.