fix: don't allow `rules` to be set from `/rule_engine`
This commit is contained in:
parent
5cd8865a93
commit
9cfe9cc709
|
@ -56,7 +56,7 @@ roots() ->
|
||||||
].
|
].
|
||||||
|
|
||||||
fields("rule_engine") ->
|
fields("rule_engine") ->
|
||||||
emqx_rule_engine_schema:fields("rule_engine");
|
emqx_rule_engine_schema:fields("rule_engine_api");
|
||||||
fields("rule_creation") ->
|
fields("rule_creation") ->
|
||||||
emqx_rule_engine_schema:fields("rules");
|
emqx_rule_engine_schema:fields("rules");
|
||||||
fields("rule_info") ->
|
fields("rule_info") ->
|
||||||
|
|
|
@ -447,16 +447,20 @@ param_path_id() ->
|
||||||
'/rule_engine'(get, _Params) ->
|
'/rule_engine'(get, _Params) ->
|
||||||
{200, format_rule_engine_resp(emqx_conf:get([rule_engine]))};
|
{200, format_rule_engine_resp(emqx_conf:get([rule_engine]))};
|
||||||
'/rule_engine'(put, #{body := Params}) ->
|
'/rule_engine'(put, #{body := Params}) ->
|
||||||
case emqx_conf:update([rule_engine], Params, #{override_to => cluster}) of
|
?CHECK_PARAMS(
|
||||||
{ok, #{config := Config}} ->
|
Params,
|
||||||
{200, format_rule_engine_resp(Config)};
|
rule_engine,
|
||||||
{error, Reason} ->
|
case emqx_conf:update([rule_engine], Params, #{override_to => cluster}) of
|
||||||
?SLOG(error, #{
|
{ok, #{config := Config}} ->
|
||||||
msg => "update_rule_engine_failed",
|
{200, format_rule_engine_resp(Config)};
|
||||||
reason => Reason
|
{error, Reason} ->
|
||||||
}),
|
?SLOG(error, #{
|
||||||
{400, #{code => 'BAD_REQUEST', message => ?ERR_BADARGS(Reason)}}
|
msg => "update_rule_engine_failed",
|
||||||
end.
|
reason => Reason
|
||||||
|
}),
|
||||||
|
{400, #{code => 'BAD_REQUEST', message => ?ERR_BADARGS(Reason)}}
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
|
@ -507,22 +511,29 @@ format_rule_info_resp(#{
|
||||||
format_rule_engine_resp(#{rules := Rules} = Config) ->
|
format_rule_engine_resp(#{rules := Rules} = Config) ->
|
||||||
Config#{rules => maps:map(fun format_rule_resp/2, Rules)}.
|
Config#{rules => maps:map(fun format_rule_resp/2, Rules)}.
|
||||||
|
|
||||||
format_rule_resp(_Id, #{
|
format_rule_resp(
|
||||||
name := Name,
|
_Id,
|
||||||
metadata := MetaData = #{created_at := CreatedAt},
|
|
||||||
actions := Action,
|
|
||||||
sql := SQL,
|
|
||||||
enable := Enable,
|
|
||||||
description := Descr
|
|
||||||
}) ->
|
|
||||||
#{
|
#{
|
||||||
|
name := Name,
|
||||||
|
actions := Action,
|
||||||
|
sql := SQL,
|
||||||
|
enable := Enable,
|
||||||
|
description := Descr
|
||||||
|
} = Rule
|
||||||
|
) ->
|
||||||
|
Format = #{
|
||||||
name => Name,
|
name => Name,
|
||||||
actions => format_action(Action),
|
actions => format_action(Action),
|
||||||
sql => SQL,
|
sql => SQL,
|
||||||
enable => Enable,
|
enable => Enable,
|
||||||
metadata => MetaData#{created_at => format_datetime(CreatedAt, millisecond)},
|
|
||||||
description => Descr
|
description => Descr
|
||||||
}.
|
},
|
||||||
|
case Rule of
|
||||||
|
#{metadata := MetaData = #{created_at := CreatedAt}} ->
|
||||||
|
Format#{metadata => MetaData#{created_at => format_datetime(CreatedAt, millisecond)}};
|
||||||
|
_ ->
|
||||||
|
Format
|
||||||
|
end.
|
||||||
|
|
||||||
format_datetime(Timestamp, Unit) ->
|
format_datetime(Timestamp, Unit) ->
|
||||||
list_to_binary(calendar:system_time_to_rfc3339(Timestamp, [{unit, Unit}])).
|
list_to_binary(calendar:system_time_to_rfc3339(Timestamp, [{unit, Unit}])).
|
||||||
|
|
|
@ -40,13 +40,17 @@ tags() ->
|
||||||
roots() -> ["rule_engine"].
|
roots() -> ["rule_engine"].
|
||||||
|
|
||||||
fields("rule_engine") ->
|
fields("rule_engine") ->
|
||||||
|
fields("rule_engine_api") ++
|
||||||
|
[
|
||||||
|
{rules,
|
||||||
|
?HOCON(hoconsc:map("id", ?R_REF("rules")), #{
|
||||||
|
desc => ?DESC("rule_engine_rules"), default => #{}
|
||||||
|
})}
|
||||||
|
];
|
||||||
|
fields("rule_engine_api") ->
|
||||||
[
|
[
|
||||||
{ignore_sys_message,
|
{ignore_sys_message,
|
||||||
?HOCON(boolean(), #{default => true, desc => ?DESC("rule_engine_ignore_sys_message")})},
|
?HOCON(boolean(), #{default => true, desc => ?DESC("rule_engine_ignore_sys_message")})},
|
||||||
{rules,
|
|
||||||
?HOCON(hoconsc:map("id", ?R_REF("rules")), #{
|
|
||||||
desc => ?DESC("rule_engine_rules"), default => #{}
|
|
||||||
})},
|
|
||||||
{jq_function_default_timeout,
|
{jq_function_default_timeout,
|
||||||
?HOCON(
|
?HOCON(
|
||||||
emqx_schema:duration_ms(),
|
emqx_schema:duration_ms(),
|
||||||
|
|
|
@ -285,12 +285,17 @@ test_rule_params(Sql, Payload) ->
|
||||||
t_rule_engine(_) ->
|
t_rule_engine(_) ->
|
||||||
{200, _} = emqx_rule_engine_api:'/rule_engine'(get, foo),
|
{200, _} = emqx_rule_engine_api:'/rule_engine'(get, foo),
|
||||||
{200, #{
|
{200, #{
|
||||||
jq_function_default_timeout := 12000,
|
%,
|
||||||
jq_implementation_module := jq_port
|
jq_function_default_timeout := 12000
|
||||||
|
% hidden! jq_implementation_module := jq_port
|
||||||
}} = emqx_rule_engine_api:'/rule_engine'(put, #{
|
}} = emqx_rule_engine_api:'/rule_engine'(put, #{
|
||||||
body => #{
|
body => #{
|
||||||
<<"jq_function_default_timeout">> => <<"12s">>,
|
<<"jq_function_default_timeout">> => <<"12s">>,
|
||||||
<<"jq_implementation_module">> => <<"jq_port">>
|
<<"jq_implementation_module">> => <<"jq_port">>
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
SomeRule = #{<<"sql">> => <<"SELECT * FROM \"t/#\"">>},
|
||||||
|
{400, _} = emqx_rule_engine_api:'/rule_engine'(put, #{
|
||||||
|
body => #{<<"rules">> => #{<<"some_rule">> => SomeRule}}
|
||||||
|
}),
|
||||||
{400, _} = emqx_rule_engine_api:'/rule_engine'(put, #{body => #{<<"something">> => <<"weird">>}}).
|
{400, _} = emqx_rule_engine_api:'/rule_engine'(put, #{body => #{<<"something">> => <<"weird">>}}).
|
||||||
|
|
Loading…
Reference in New Issue