fix(exhook): ExHook name limit with regular expression
This commit is contained in:
parent
93a65da6bc
commit
4d73fe83be
|
@ -45,6 +45,10 @@
|
||||||
-define(NOT_FOURD, 'NOT_FOUND').
|
-define(NOT_FOURD, 'NOT_FOUND').
|
||||||
-define(BAD_REQUEST, 'BAD_REQUEST').
|
-define(BAD_REQUEST, 'BAD_REQUEST').
|
||||||
-define(BAD_RPC, 'BAD_RPC').
|
-define(BAD_RPC, 'BAD_RPC').
|
||||||
|
-define(ERR_BADARGS(REASON), begin
|
||||||
|
R0 = err_msg(REASON),
|
||||||
|
<<"Bad Arguments: ", R0/binary>>
|
||||||
|
end).
|
||||||
|
|
||||||
-dialyzer([
|
-dialyzer([
|
||||||
{nowarn_function, [
|
{nowarn_function, [
|
||||||
|
@ -246,6 +250,11 @@ exhooks(post, #{body := #{<<"name">> := Name} = Body}) ->
|
||||||
{400, #{
|
{400, #{
|
||||||
code => <<"BAD_REQUEST">>,
|
code => <<"BAD_REQUEST">>,
|
||||||
message => <<"Already exists">>
|
message => <<"Already exists">>
|
||||||
|
}};
|
||||||
|
{error, Reason} ->
|
||||||
|
{400, #{
|
||||||
|
code => <<"BAD_REQUEST">>,
|
||||||
|
message => ?ERR_BADARGS(Reason)
|
||||||
}}
|
}}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
@ -265,10 +274,10 @@ action_with_name(put, #{bindings := #{name := Name}, body := Body}) ->
|
||||||
code => <<"NOT_FOUND">>,
|
code => <<"NOT_FOUND">>,
|
||||||
message => <<"Server not found">>
|
message => <<"Server not found">>
|
||||||
}};
|
}};
|
||||||
{error, Error} ->
|
{error, Reason} ->
|
||||||
{500, #{
|
{400, #{
|
||||||
code => <<"BAD_RPC">>,
|
code => <<"BAD_REQUEST">>,
|
||||||
message => Error
|
message => ?ERR_BADARGS(Reason)
|
||||||
}}
|
}}
|
||||||
end;
|
end;
|
||||||
action_with_name(delete, #{bindings := #{name := Name}}) ->
|
action_with_name(delete, #{bindings := #{name := Name}}) ->
|
||||||
|
@ -285,10 +294,10 @@ action_with_name(delete, #{bindings := #{name := Name}}) ->
|
||||||
code => <<"BAD_REQUEST">>,
|
code => <<"BAD_REQUEST">>,
|
||||||
message => <<"Server not found">>
|
message => <<"Server not found">>
|
||||||
}};
|
}};
|
||||||
{error, Error} ->
|
{error, Reason} ->
|
||||||
{500, #{
|
{400, #{
|
||||||
code => <<"BAD_RPC">>,
|
code => <<"BAD_REQUEST">>,
|
||||||
message => Error
|
message => ?ERR_BADARGS(Reason)
|
||||||
}}
|
}}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
@ -467,6 +476,26 @@ call_cluster(Fun) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Internal Funcs
|
%% Internal Funcs
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
err_msg({_, HoconErrors = [{Type, _} | _]}) when
|
||||||
|
Type == translation_error orelse Type == validation_error
|
||||||
|
->
|
||||||
|
MessageFormat = [hocon_error(HoconError) || HoconError <- HoconErrors],
|
||||||
|
list_to_binary(MessageFormat);
|
||||||
|
err_msg(Msg) ->
|
||||||
|
list_to_binary(io_lib:format("~0p", [Msg])).
|
||||||
|
|
||||||
|
hocon_error({Type, Message0}) when
|
||||||
|
Type == translation_error orelse Type == validation_error
|
||||||
|
->
|
||||||
|
case maps:get(reason, Message0, undefined) of
|
||||||
|
undefined ->
|
||||||
|
Message = maps:without([stacktrace], Message0),
|
||||||
|
emqx_logger_jsonfmt:best_effort_json(Message#{<<"type">> => Type}, []);
|
||||||
|
Reason when is_binary(Reason) ->
|
||||||
|
Reason;
|
||||||
|
Reason ->
|
||||||
|
list_to_binary(io_lib:format("~0p", [Reason]))
|
||||||
|
end.
|
||||||
|
|
||||||
get_raw_config() ->
|
get_raw_config() ->
|
||||||
RawConfig = emqx:get_raw_config([exhook, servers], []),
|
RawConfig = emqx:get_raw_config([exhook, servers], []),
|
||||||
|
|
|
@ -55,7 +55,11 @@ fields(server) ->
|
||||||
{name,
|
{name,
|
||||||
sc(
|
sc(
|
||||||
binary(),
|
binary(),
|
||||||
#{required => true, desc => ?DESC(name)}
|
#{
|
||||||
|
required => true,
|
||||||
|
validator => fun validate_name/1,
|
||||||
|
desc => ?DESC(name)
|
||||||
|
}
|
||||||
)},
|
)},
|
||||||
{enable,
|
{enable,
|
||||||
sc(
|
sc(
|
||||||
|
@ -126,5 +130,23 @@ failed_action() ->
|
||||||
}
|
}
|
||||||
).
|
).
|
||||||
|
|
||||||
|
validate_name(Name) ->
|
||||||
|
NameRE = "^[A-Za-z0-9]+[A-Za-z0-9-_]*$",
|
||||||
|
NameLen = byte_size(Name),
|
||||||
|
case NameLen > 0 andalso NameLen =< 256 of
|
||||||
|
true ->
|
||||||
|
case re:run(Name, NameRE) of
|
||||||
|
{match, _} ->
|
||||||
|
ok;
|
||||||
|
_Nomatch ->
|
||||||
|
Reason = list_to_binary(
|
||||||
|
io_lib:format("Bad ExHook Name ~p, expect ~p", [Name, NameRE])
|
||||||
|
),
|
||||||
|
{error, Reason}
|
||||||
|
end;
|
||||||
|
false ->
|
||||||
|
{error, "Name Length must =< 256"}
|
||||||
|
end.
|
||||||
|
|
||||||
server_config() ->
|
server_config() ->
|
||||||
fields(server).
|
fields(server).
|
||||||
|
|
Loading…
Reference in New Issue