fix(emqx_resource): do not allow leading _ or - as resource name
This commit is contained in:
parent
869e73d637
commit
3261a12140
|
@ -199,7 +199,7 @@ t_create_with_bad_name(_Config) ->
|
||||||
?assertMatch(
|
?assertMatch(
|
||||||
{error,
|
{error,
|
||||||
{pre_config_update, emqx_bridge_app, #{
|
{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
|
kind := validation_error
|
||||||
}}},
|
}}},
|
||||||
emqx:update_config(Path, Conf)
|
emqx:update_config(Path, Conf)
|
||||||
|
|
|
@ -1365,7 +1365,7 @@ t_create_with_bad_name(Config) ->
|
||||||
?assertMatch(
|
?assertMatch(
|
||||||
#{
|
#{
|
||||||
<<"kind">> := <<"validation_error">>,
|
<<"kind">> := <<"validation_error">>,
|
||||||
<<"reason">> := <<"only 0-9a-zA-Z_- is allowed in resource name", _/binary>>
|
<<"reason">> := <<"Invalid name format.", _/binary>>
|
||||||
},
|
},
|
||||||
Msg
|
Msg
|
||||||
),
|
),
|
||||||
|
|
|
@ -829,7 +829,7 @@ t_create_with_bad_name(_Config) ->
|
||||||
<<"code">> := <<"BAD_REQUEST">>,
|
<<"code">> := <<"BAD_REQUEST">>,
|
||||||
<<"message">> := #{
|
<<"message">> := #{
|
||||||
<<"kind">> := <<"validation_error">>,
|
<<"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),
|
}}} = create_bridge_http_api_v1(Opts),
|
||||||
ok.
|
ok.
|
||||||
|
|
|
@ -1034,10 +1034,8 @@ t_bad_name(Config) ->
|
||||||
Msg = emqx_utils_json:decode(Msg0, [return_maps]),
|
Msg = emqx_utils_json:decode(Msg0, [return_maps]),
|
||||||
?assertMatch(
|
?assertMatch(
|
||||||
#{
|
#{
|
||||||
<<"got">> := [<<"_bad_name">>],
|
|
||||||
<<"kind">> := <<"validation_error">>,
|
<<"kind">> := <<"validation_error">>,
|
||||||
<<"path">> := <<"actions.kafka_producer">>,
|
<<"reason">> := <<"Invalid name format.", _/binary>>
|
||||||
<<"reason">> := <<"invalid_map_key">>
|
|
||||||
},
|
},
|
||||||
Msg
|
Msg
|
||||||
),
|
),
|
||||||
|
|
|
@ -229,7 +229,7 @@ t_create_with_bad_name_direct_path(_Config) ->
|
||||||
{error,
|
{error,
|
||||||
{pre_config_update, _ConfigHandlerMod, #{
|
{pre_config_update, _ConfigHandlerMod, #{
|
||||||
kind := validation_error,
|
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)
|
emqx:update_config(Path, ConnConfig)
|
||||||
),
|
),
|
||||||
|
|
|
@ -812,11 +812,11 @@ validate_name(Name) ->
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
validate_name(<<>>, _Opts) ->
|
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 ->
|
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) ->
|
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 ->
|
match ->
|
||||||
case maps:get(atom_name, Opts, true) of
|
case maps:get(atom_name, Opts, true) of
|
||||||
%% NOTE
|
%% NOTE
|
||||||
|
@ -827,7 +827,12 @@ validate_name(Name, Opts) ->
|
||||||
end;
|
end;
|
||||||
nomatch ->
|
nomatch ->
|
||||||
invalid_data(
|
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.
|
end.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue