Преглед изворни кода

Merge pull request #7815 from terry-xiaoyu/fix_bridge_ssl_dry_run_fail

fix: convert ssl certfiles for dry-run creating bridges
Xinyu Liu пре 3 година
родитељ
комит
8528d76371

+ 1 - 0
apps/emqx/test/emqx_metrics_SUITE.erl

@@ -166,6 +166,7 @@ t_trans(_) ->
     ).
 
 with_metrics_server(Fun) ->
+    _ = supervisor:terminate_child(emqx_kernel_sup, emqx_metrics),
     {ok, _} = emqx_metrics:start_link(),
     _ = Fun(),
     ok = emqx_metrics:stop().

+ 23 - 12
apps/emqx_bridge/src/emqx_bridge.erl

@@ -343,20 +343,18 @@ recreate(Type, Name, Conf) ->
     ).
 
 create_dry_run(Type, Conf) ->
-    Conf0 = Conf#{
-        <<"egress">> =>
-            #{
-                <<"remote_topic">> => <<"t">>,
-                <<"remote_qos">> => 0,
-                <<"retain">> => true,
-                <<"payload">> => <<"val">>
-            },
-        <<"ingress">> =>
-            #{<<"remote_topic">> => <<"t">>}
-    },
+    Conf0 = fill_dry_run_conf(Conf),
     case emqx_resource:check_config(emqx_bridge:resource_type(Type), Conf0) of
         {ok, Conf1} ->
-            emqx_resource:create_dry_run_local(emqx_bridge:resource_type(Type), Conf1);
+            TmpPath = iolist_to_binary(["bridges-create-dry-run:", emqx_misc:gen_id(8)]),
+            try emqx_connector_ssl:convert_certs(TmpPath, Conf1) of
+                {error, Reason} ->
+                    {error, Reason};
+                {ok, ConfNew} ->
+                    emqx_resource:create_dry_run_local(emqx_bridge:resource_type(Type), ConfNew)
+            after
+                emqx_connector_ssl:clear_certs(TmpPath, Conf1)
+            end;
         {error, _} = Error ->
             Error
     end.
@@ -557,6 +555,19 @@ get_basic_usage_info() ->
             InitialAcc
     end.
 
+fill_dry_run_conf(Conf) ->
+    Conf#{
+        <<"egress">> =>
+            #{
+                <<"remote_topic">> => <<"t">>,
+                <<"remote_qos">> => 0,
+                <<"retain">> => true,
+                <<"payload">> => <<"val">>
+            },
+        <<"ingress">> =>
+            #{<<"remote_topic">> => <<"t">>}
+    }.
+
 bin(Bin) when is_binary(Bin) -> Bin;
 bin(Str) when is_list(Str) -> list_to_binary(Str);
 bin(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8).

+ 7 - 3
apps/emqx_bridge/src/emqx_bridge_api.erl

@@ -648,11 +648,11 @@ fill_defaults(Type, RawConf) ->
     unpack_bridge_conf(Type, FullConf).
 
 pack_bridge_conf(Type, RawConf) ->
-    #{<<"bridges">> => #{Type => #{<<"foo">> => RawConf}}}.
+    #{<<"bridges">> => #{bin(Type) => #{<<"foo">> => RawConf}}}.
 
 unpack_bridge_conf(Type, PackedConf) ->
     #{<<"bridges">> := Bridges} = PackedConf,
-    #{<<"foo">> := RawConf} = maps:get(Type, Bridges),
+    #{<<"foo">> := RawConf} = maps:get(bin(Type), Bridges),
     RawConf.
 
 is_ok(ResL) ->
@@ -689,4 +689,8 @@ error_msg(Code, Msg) ->
     #{code => Code, message => bin(io_lib:format("~p", [Msg]))}.
 
 bin(S) when is_list(S) ->
-    list_to_binary(S).
+    list_to_binary(S);
+bin(S) when is_atom(S) ->
+    atom_to_binary(S, utf8);
+bin(S) when is_binary(S) ->
+    S.

+ 7 - 0
apps/emqx_bridge/test/emqx_bridge_api_SUITE.erl

@@ -171,6 +171,7 @@ t_http_crud_apis(_) ->
     #{
         <<"type">> := ?BRIDGE_TYPE,
         <<"name">> := ?BRIDGE_NAME,
+        <<"enable">> := true,
         <<"status">> := _,
         <<"node_status">> := [_ | _],
         <<"metrics">> := _,
@@ -208,6 +209,7 @@ t_http_crud_apis(_) ->
         #{
             <<"type">> := ?BRIDGE_TYPE,
             <<"name">> := ?BRIDGE_NAME,
+            <<"enable">> := true,
             <<"status">> := _,
             <<"node_status">> := [_ | _],
             <<"metrics">> := _,
@@ -224,6 +226,7 @@ t_http_crud_apis(_) ->
             #{
                 <<"type">> := ?BRIDGE_TYPE,
                 <<"name">> := ?BRIDGE_NAME,
+                <<"enable">> := true,
                 <<"status">> := _,
                 <<"node_status">> := [_ | _],
                 <<"metrics">> := _,
@@ -240,6 +243,7 @@ t_http_crud_apis(_) ->
         #{
             <<"type">> := ?BRIDGE_TYPE,
             <<"name">> := ?BRIDGE_NAME,
+            <<"enable">> := true,
             <<"status">> := _,
             <<"node_status">> := [_ | _],
             <<"metrics">> := _,
@@ -305,6 +309,7 @@ do_start_stop_bridges(Type) ->
     #{
         <<"type">> := ?BRIDGE_TYPE,
         <<"name">> := ?BRIDGE_NAME,
+        <<"enable">> := true,
         <<"status">> := <<"connected">>,
         <<"node_status">> := [_ | _],
         <<"metrics">> := _,
@@ -349,6 +354,7 @@ t_enable_disable_bridges(_) ->
     #{
         <<"type">> := ?BRIDGE_TYPE,
         <<"name">> := ?BRIDGE_NAME,
+        <<"enable">> := true,
         <<"status">> := <<"connected">>,
         <<"node_status">> := [_ | _],
         <<"metrics">> := _,
@@ -397,6 +403,7 @@ t_reset_bridges(_) ->
     #{
         <<"type">> := ?BRIDGE_TYPE,
         <<"name">> := ?BRIDGE_NAME,
+        <<"enable">> := true,
         <<"status">> := <<"connected">>,
         <<"node_status">> := [_ | _],
         <<"metrics">> := _,

+ 3 - 2
apps/emqx_connector/src/emqx_connector_ssl.erl

@@ -22,7 +22,7 @@
 ]).
 
 convert_certs(RltvDir, NewConfig) ->
-    NewSSL = drop_invalid_certs(maps:get(<<"ssl">>, NewConfig, undefined)),
+    NewSSL = drop_invalid_certs(map_get_oneof([<<"ssl">>, ssl], NewConfig, undefined)),
     case emqx_tls_lib:ensure_ssl_files(RltvDir, NewSSL) of
         {ok, NewSSL1} ->
             {ok, new_ssl_config(NewConfig, NewSSL1)};
@@ -35,7 +35,8 @@ clear_certs(RltvDir, Config) ->
     ok = emqx_tls_lib:delete_ssl_files(RltvDir, undefined, OldSSL).
 
 new_ssl_config(Config, undefined) -> Config;
-new_ssl_config(Config, SSL) -> Config#{<<"ssl">> => SSL}.
+new_ssl_config(Config, #{<<"enable">> := _} = SSL) -> Config#{<<"ssl">> => SSL};
+new_ssl_config(Config, #{enable := _} = SSL) -> Config#{ssl => SSL}.
 
 drop_invalid_certs(undefined) -> undefined;
 drop_invalid_certs(SSL) -> emqx_tls_lib:drop_invalid_certs(SSL).