From 2416fa4e13cc1a78454da6f52e56e52dc1ff2550 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Mon, 18 Oct 2021 19:23:19 +0800 Subject: [PATCH] chore(gw): return 204 if no authn config --- apps/emqx_gateway/src/emqx_gateway_api.erl | 2 +- apps/emqx_gateway/src/emqx_gateway_api_authn.erl | 10 +++++++++- apps/emqx_gateway/src/emqx_gateway_api_listeners.erl | 10 +++++++++- apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl | 6 +++--- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/apps/emqx_gateway/src/emqx_gateway_api.erl b/apps/emqx_gateway/src/emqx_gateway_api.erl index e8c13e7ac..1b7dbf146 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api.erl @@ -112,7 +112,7 @@ gateway_insta(get, #{bindings := #{name := Name0}}) -> {200, maps:merge(GwConf, GwInfo1)} end catch - error : badname -> + error : badarg -> return_http_error(400, "Bad gateway name") end; gateway_insta(put, #{body := GwConf, diff --git a/apps/emqx_gateway/src/emqx_gateway_api_authn.erl b/apps/emqx_gateway/src/emqx_gateway_api_authn.erl index 2136a5b3d..4cd2d8867 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api_authn.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api_authn.erl @@ -53,7 +53,14 @@ apis() -> authn(get, #{bindings := #{name := Name0}}) -> with_gateway(Name0, fun(GwName, _) -> - {200, emqx_gateway_http:authn(GwName)} + try + emqx_gateway_http:authn(GwName) + of + Authn -> {200, Authn} + catch + error : {config_not_found, _} -> + {204} + end end); authn(put, #{bindings := #{name := Name0}, @@ -104,6 +111,7 @@ swagger("/gateway/:name/authentication", get) -> , <<"404">> => schema_not_found() , <<"500">> => schema_internal_error() , <<"200">> => schema_authn() + , <<"204">> => schema_no_content() } }; swagger("/gateway/:name/authentication", put) -> diff --git a/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl b/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl index b3df7bb35..0ab054df8 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl @@ -112,7 +112,14 @@ listeners_insta_authn(get, #{bindings := #{name := Name0, id := ListenerId0}}) -> ListenerId = emqx_mgmt_util:urldecode(ListenerId0), with_gateway(Name0, fun(GwName, _) -> - {200, emqx_gateway_http:authn(GwName, ListenerId)} + try + emqx_gateway_http:authn(GwName, ListenerId) + of + Authn -> {200, Authn} + catch + error : {config_not_found, _} -> + {204} + end end); listeners_insta_authn(post, #{body := Conf, bindings := #{name := Name0, @@ -222,6 +229,7 @@ swagger("/gateway/:name/listeners/:id/authentication", get) -> , <<"404">> => schema_not_found() , <<"500">> => schema_internal_error() , <<"200">> => schema_authn() + , <<"204">> => schema_no_content() } }; swagger("/gateway/:name/listeners/:id/authentication", post) -> diff --git a/apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl b/apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl index 85125f466..3f709fa5a 100644 --- a/apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl @@ -32,7 +32,7 @@ %% Setup %%-------------------------------------------------------------------- -all() -> emqx_ct:all(?MODULE). +all() -> emqx_common_test_helpers:all(?MODULE). init_per_suite(Conf) -> %% FIXME: Magic line. for saving gateway schema name for emqx_config @@ -189,7 +189,7 @@ t_gateway_exproto(_) -> t_authn(_) -> GwConf = #{name => <<"stomp">>}, {204, _} = request(post, "/gateway", GwConf), - {404, _} = request(get, "/gateway/stomp/authentication"), + {204, _} = request(get, "/gateway/stomp/authentication"), AuthConf = #{mechanism => <<"password-based">>, backend => <<"built-in-database">>, @@ -206,7 +206,7 @@ t_authn(_) -> assert_confs(AuthConf2, ConfResp2), {204, _} = request(delete, "/gateway/stomp/authentication"), - {404, _} = request(get, "/gateway/stomp/authentication"), + {204, _} = request(get, "/gateway/stomp/authentication"), {204, _} = request(delete, "/gateway/stomp"). t_listeners(_) ->