From 425eba8b132ba0ca5f4f7da5bef16585115d8ba2 Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Fri, 30 Jun 2023 13:42:48 -0300 Subject: [PATCH] fix(mqtt_bridge): ensure `server` key is a binary Fixes https://emqx.atlassian.net/browse/EMQX-10461 So that it can be JSON encoded correctly. ``` 2023-06-30T02:00:41.160110+00:00 [debug] msg: publish_to, mfa: emqx_trace:publish/1, line: 73, topic: b/$, payload: {"topic":"t/1","server":[49,48,46,52,50,46,51,46,49,56,48,58,49,56,56,51],"retain":false,"qos":1,"pub_props":{},"payload":"{\"msg\": \"hello\"}","message_received_at":1688090441159,"id":"0005FF4F2F181488103417000C2E0000","dup":false}, tag: PUBLISH ``` --- .../src/emqx_bridge_mqtt.app.src | 2 +- .../src/emqx_bridge_mqtt_ingress.erl | 5 ++- .../test/emqx_bridge_mqtt_SUITE.erl | 44 +++++++++++++++++++ changes/ce/fix-11174.en.md | 3 ++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 changes/ce/fix-11174.en.md diff --git a/apps/emqx_bridge_mqtt/src/emqx_bridge_mqtt.app.src b/apps/emqx_bridge_mqtt/src/emqx_bridge_mqtt.app.src index 660daca9c..853e98eb5 100644 --- a/apps/emqx_bridge_mqtt/src/emqx_bridge_mqtt.app.src +++ b/apps/emqx_bridge_mqtt/src/emqx_bridge_mqtt.app.src @@ -1,7 +1,7 @@ %% -*- mode: erlang -*- {application, emqx_bridge_mqtt, [ {description, "EMQX MQTT Broker Bridge"}, - {vsn, "0.1.1"}, + {vsn, "0.1.2"}, {registered, []}, {applications, [ kernel, diff --git a/apps/emqx_bridge_mqtt/src/emqx_bridge_mqtt_ingress.erl b/apps/emqx_bridge_mqtt/src/emqx_bridge_mqtt_ingress.erl index c0b2da908..3174cdb6f 100644 --- a/apps/emqx_bridge_mqtt/src/emqx_bridge_mqtt_ingress.erl +++ b/apps/emqx_bridge_mqtt/src/emqx_bridge_mqtt_ingress.erl @@ -221,7 +221,7 @@ import_msg( ) -> #{ id => emqx_guid:to_hexstr(emqx_guid:gen()), - server => Server, + server => to_bin(Server), payload => Payload, topic => Topic, qos => QoS, @@ -272,3 +272,6 @@ to_broker_msg(#{dup := Dup} = Msg, Local, Props) -> emqx_message:make(bridge, QoS, Topic, Payload) ) ). + +to_bin(B) when is_binary(B) -> B; +to_bin(Str) when is_list(Str) -> iolist_to_binary(Str). diff --git a/apps/emqx_bridge_mqtt/test/emqx_bridge_mqtt_SUITE.erl b/apps/emqx_bridge_mqtt/test/emqx_bridge_mqtt_SUITE.erl index d14e0597e..29cfe976a 100644 --- a/apps/emqx_bridge_mqtt/test/emqx_bridge_mqtt_SUITE.erl +++ b/apps/emqx_bridge_mqtt/test/emqx_bridge_mqtt_SUITE.erl @@ -248,6 +248,50 @@ t_mqtt_conn_bridge_ingress(_) -> ok. +t_mqtt_conn_bridge_ingress_full_context(_Config) -> + User1 = <<"user1">>, + IngressConf = + emqx_utils_maps:deep_merge( + ?INGRESS_CONF, + #{<<"local">> => #{<<"payload">> => <<"${.}">>}} + ), + {ok, 201, _Bridge} = request( + post, + uri(["bridges"]), + ?SERVER_CONF(User1)#{ + <<"type">> => ?TYPE_MQTT, + <<"name">> => ?BRIDGE_NAME_INGRESS, + <<"ingress">> => IngressConf + } + ), + + RemoteTopic = <>, + LocalTopic = <>, + Payload = <<"hello">>, + emqx:subscribe(LocalTopic), + timer:sleep(100), + %% PUBLISH a message to the 'remote' broker, as we have only one broker, + %% the remote broker is also the local one. + emqx:publish(emqx_message:make(RemoteTopic, Payload)), + %% we should receive a message on the local broker, with specified topic + #message{payload = EncodedPayload} = assert_mqtt_msg_received(LocalTopic), + ?assertMatch( + #{ + <<"dup">> := false, + <<"id">> := _, + <<"message_received_at">> := _, + <<"payload">> := <<"hello">>, + <<"pub_props">> := #{}, + <<"qos">> := 0, + <<"retain">> := false, + <<"server">> := <<"127.0.0.1:1883">>, + <<"topic">> := <<"ingress_remote_topic/1">> + }, + emqx_utils_json:decode(EncodedPayload, [return_maps]) + ), + + ok. + t_mqtt_conn_bridge_ingress_shared_subscription(_) -> PoolSize = 4, Ns = lists:seq(1, 10), diff --git a/changes/ce/fix-11174.en.md b/changes/ce/fix-11174.en.md new file mode 100644 index 000000000..9595a2e55 --- /dev/null +++ b/changes/ce/fix-11174.en.md @@ -0,0 +1,3 @@ +Fixed the encoding of the `server` key coming from an ingress MQTT bridge. + +Before the fix, it was being encoded as a list of integers corresponding to the ASCII characters of the server string.