From 75ff76a16b54c80247bb17181784d5ea3d3b3891 Mon Sep 17 00:00:00 2001 From: Kjell Winblad Date: Thu, 1 Jun 2023 15:30:06 +0200 Subject: [PATCH 1/3] fix: friendly error message when creating bridges with too long names This commit makes the error message and log entry that appear when one tries to create a bridge with a name the exceeds 255 bytes (the max length for atoms) more friendly and easier to understand. An even better fix would be to not store bridge names as atoms but this probably requires a more substantial change. Fixes: https://emqx.atlassian.net/browse/EMQX-9609 --- apps/emqx/src/emqx_config_handler.erl | 11 +++++++++++ apps/emqx_bridge/src/emqx_bridge_api.erl | 2 +- apps/emqx_rule_engine/src/emqx_rule_engine.app.src | 2 +- lib-ee/emqx_ee_bridge/src/emqx_ee_bridge.app.src | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/apps/emqx/src/emqx_config_handler.erl b/apps/emqx/src/emqx_config_handler.erl index 0bad19f9e..365dd25ac 100644 --- a/apps/emqx/src/emqx_config_handler.erl +++ b/apps/emqx/src/emqx_config_handler.erl @@ -473,6 +473,17 @@ bin_path(ConfKeyPath) -> [bin(Key) || Key <- ConfKeyPath]. bin(A) when is_atom(A) -> atom_to_binary(A, utf8); bin(B) when is_binary(B) -> B. +atom(Bin) when is_binary(Bin), size(Bin) > 255 -> + erlang:throw( + iolist_to_binary( + io_lib:format( + "Name is is too long." + " Please provide a shorter name (<= 255 bytes)." + " The name that is too long: \"~s\"", + [Bin] + ) + ) + ); atom(Bin) when is_binary(Bin) -> binary_to_atom(Bin, utf8); atom(Str) when is_list(Str) -> diff --git a/apps/emqx_bridge/src/emqx_bridge_api.erl b/apps/emqx_bridge/src/emqx_bridge_api.erl index bffa7b7f9..46387c90b 100644 --- a/apps/emqx_bridge/src/emqx_bridge_api.erl +++ b/apps/emqx_bridge/src/emqx_bridge_api.erl @@ -605,7 +605,7 @@ create_or_update_bridge(BridgeType, BridgeName, Conf, HttpStatusCode) -> case emqx_bridge:create(BridgeType, BridgeName, Conf) of {ok, _} -> lookup_from_all_nodes(BridgeType, BridgeName, HttpStatusCode); - {error, #{kind := validation_error} = Reason} -> + {error, Reason} when is_map(Reason) -> ?BAD_REQUEST(map_to_json(Reason)) end. diff --git a/apps/emqx_rule_engine/src/emqx_rule_engine.app.src b/apps/emqx_rule_engine/src/emqx_rule_engine.app.src index c6f94f5ea..7b4d1ee98 100644 --- a/apps/emqx_rule_engine/src/emqx_rule_engine.app.src +++ b/apps/emqx_rule_engine/src/emqx_rule_engine.app.src @@ -2,7 +2,7 @@ {application, emqx_rule_engine, [ {description, "EMQX Rule Engine"}, % strict semver, bump manually! - {vsn, "5.0.18"}, + {vsn, "5.0.19"}, {modules, []}, {registered, [emqx_rule_engine_sup, emqx_rule_engine]}, {applications, [kernel, stdlib, rulesql, getopt, emqx_ctl]}, diff --git a/lib-ee/emqx_ee_bridge/src/emqx_ee_bridge.app.src b/lib-ee/emqx_ee_bridge/src/emqx_ee_bridge.app.src index 4a52b2c35..4b6acf915 100644 --- a/lib-ee/emqx_ee_bridge/src/emqx_ee_bridge.app.src +++ b/lib-ee/emqx_ee_bridge/src/emqx_ee_bridge.app.src @@ -1,6 +1,6 @@ {application, emqx_ee_bridge, [ {description, "EMQX Enterprise data bridges"}, - {vsn, "0.1.14"}, + {vsn, "0.1.15"}, {registered, []}, {applications, [ kernel, From c2c87d276f25ab29efa4adadfce9465b33070857 Mon Sep 17 00:00:00 2001 From: Kjell Winblad Date: Thu, 1 Jun 2023 15:38:54 +0200 Subject: [PATCH 2/3] docs: add changelog entry for too long bridge name improvement --- changes/ce/fix-10911.en.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/ce/fix-10911.en.md diff --git a/changes/ce/fix-10911.en.md b/changes/ce/fix-10911.en.md new file mode 100644 index 000000000..8fafb7ce4 --- /dev/null +++ b/changes/ce/fix-10911.en.md @@ -0,0 +1 @@ +The error message and log entry that appear when one tries to create a bridge with a name the exceeds 255 bytes is now easier to understand. From 375661c6a1c7817f4d178b13c9fcc8b199815d14 Mon Sep 17 00:00:00 2001 From: Kjell Winblad Date: Mon, 5 Jun 2023 17:02:08 +0200 Subject: [PATCH 3/3] test: add test case for error when bridge name is too long --- .../test/emqx_bridge_api_SUITE.erl | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/apps/emqx_bridge/test/emqx_bridge_api_SUITE.erl b/apps/emqx_bridge/test/emqx_bridge_api_SUITE.erl index 1ac6750a4..ba65bdd34 100644 --- a/apps/emqx_bridge/test/emqx_bridge_api_SUITE.erl +++ b/apps/emqx_bridge/test/emqx_bridge_api_SUITE.erl @@ -415,6 +415,26 @@ t_http_crud_apis(Config) -> ), %% Test bad updates + %% ================ + + %% Add bridge with a name that is too long + %% We only support bridge names up to 255 characters + LongName = list_to_binary(lists:duplicate(256, $a)), + NameTooLongRequestResult = request_json( + post, + uri(["bridges"]), + ?HTTP_BRIDGE(URL1, LongName), + Config + ), + ?assertMatch( + {ok, 400, _}, + NameTooLongRequestResult + ), + {ok, 400, #{<<"message">> := NameTooLongMessage}} = NameTooLongRequestResult, + %% Use regex to check that the message contains the name + Match = re:run(NameTooLongMessage, LongName), + ?assertMatch({match, _}, Match), + %% Add bridge without the URL field {ok, 400, PutFail1} = request_json( put, uri(["bridges", BridgeID]),