From a34e750ebe129e442ecf193b76d67200c8e68576 Mon Sep 17 00:00:00 2001 From: EMQ-YangM Date: Mon, 21 Mar 2022 14:16:52 +0800 Subject: [PATCH 1/2] fix: unify http response code for auhtn & authz --- apps/emqx_authn/src/emqx_authn_api.erl | 23 +++++++++++-------- .../emqx_authz/src/emqx_authz_api_sources.erl | 8 +++---- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/apps/emqx_authn/src/emqx_authn_api.erl b/apps/emqx_authn/src/emqx_authn_api.erl index c0fabe219..7a24b4190 100644 --- a/apps/emqx_authn/src/emqx_authn_api.erl +++ b/apps/emqx_authn/src/emqx_authn_api.erl @@ -29,7 +29,7 @@ -define(BAD_REQUEST, 'BAD_REQUEST'). -define(NOT_FOUND, 'NOT_FOUND'). --define(CONFLICT, 'CONFLICT'). +-define(ALREADY_EXISTS, 'ALREADY_EXISTS'). % Swagger @@ -170,7 +170,7 @@ schema("/authentication") -> emqx_authn_schema:authenticator_type(), authenticator_examples()), 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>), - 409 => error_codes([?CONFLICT], <<"Conflict">>) + 409 => error_codes([?ALREADY_EXISTS], <<"ALREADY_EXISTS">>) } } }; @@ -203,7 +203,7 @@ schema("/authentication/:id") -> authenticator_examples()), 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>), 404 => error_codes([?NOT_FOUND], <<"Not Found">>), - 409 => error_codes([?CONFLICT], <<"Conflict">>) + 409 => error_codes([?ALREADY_EXISTS], <<"ALREADY_EXISTS">>) } }, delete => #{ @@ -259,7 +259,7 @@ schema("/listeners/:listener_id/authentication") -> emqx_authn_schema:authenticator_type(), authenticator_examples()), 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>), - 409 => error_codes([?CONFLICT], <<"Conflict">>) + 409 => error_codes([?ALREADY_EXISTS], <<"ALREADY_EXISTS">>) } } }; @@ -291,7 +291,7 @@ schema("/listeners/:listener_id/authentication/:id") -> authenticator_examples()), 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>), 404 => error_codes([?NOT_FOUND], <<"Not Found">>), - 409 => error_codes([?CONFLICT], <<"Conflict">>) + 409 => error_codes([?ALREADY_EXISTS], <<"ALREADY_EXISTS">>) } }, delete => #{ @@ -842,7 +842,9 @@ lookup_from_all_nodes(ChainName, AuthenticatorID) -> MKMap = fun (Name) -> fun ({Key, Val}) -> #{ node => Key, Name => Val } end end, HelpFun = fun (M, Name) -> lists:map(MKMap(Name), maps:to_list(M)) end, case AggregateStatus of - empty_metrics_and_status -> serialize_error(unsupported_operation); + empty_metrics_and_status -> + {400, #{code => <<"BAD_REQUEST">>, + message => <<"Resource Not Support Status">>}}; _ -> {200, #{node_status => HelpFun(StatusMap, status), node_metrics => HelpFun(maps:map(Fun, MetricsMap), metrics), status => AggregateStatus, @@ -851,7 +853,8 @@ lookup_from_all_nodes(ChainName, AuthenticatorID) -> } end; {error, ErrL} -> - serialize_error(ErrL) + {500, #{code => <<"INTERNAL_ERROR">>, + message => list_to_binary(io_lib:format("~p", [ErrL]))}} end. aggregate_status([]) -> empty_metrics_and_status; @@ -1052,7 +1055,7 @@ serialize_error({user_error, not_found}) -> {404, #{code => <<"NOT_FOUND">>, message => binfmt("User not found", [])}}; serialize_error({user_error, already_exist}) -> - {409, #{code => <<"BAD_REQUEST">>, + {409, #{code => <<"ALREADY_EXISTS">>, message => binfmt("User already exists", [])}}; serialize_error({user_error, Reason}) -> {400, #{code => <<"BAD_REQUEST">>, @@ -1085,10 +1088,10 @@ serialize_error({bad_ssl_config, Details}) -> {400, #{code => <<"BAD_REQUEST">>, message => binfmt("bad_ssl_config ~p", [Details])}}; serialize_error({missing_parameter, Detail}) -> - {400, #{code => <<"MISSING_PARAMETER">>, + {400, #{code => <<"BAD_REQUEST">>, message => binfmt("Missing required parameter: ~p", [Detail])}}; serialize_error({invalid_parameter, Name}) -> - {400, #{code => <<"INVALID_PARAMETER">>, + {400, #{code => <<"BAD_REQUEST">>, message => binfmt("Invalid value for '~p'", [Name])}}; serialize_error({unknown_authn_type, Type}) -> {400, #{code => <<"BAD_REQUEST">>, diff --git a/apps/emqx_authz/src/emqx_authz_api_sources.erl b/apps/emqx_authz/src/emqx_authz_api_sources.erl index 0b4b98bd6..e6cfd70bf 100644 --- a/apps/emqx_authz/src/emqx_authz_api_sources.erl +++ b/apps/emqx_authz/src/emqx_authz_api_sources.erl @@ -138,7 +138,6 @@ schema("/authorization/sources/:type") -> , responses => #{ 204 => <<"Authorization source updated successfully">> , 400 => emqx_dashboard_swagger:error_codes([?BAD_REQUEST], <<"Bad Request">>) - , 404 => emqx_dashboard_swagger:error_codes([?NOT_FOUND], <<"Not Found">>) } } , delete => @@ -160,6 +159,7 @@ schema("/authorization/sources/:type/status") -> hoconsc:ref(emqx_authn_schema, "metrics_status_fields"), status_metrics_example()) , 400 => emqx_dashboard_swagger:error_codes([?BAD_REQUEST], <<"Bad request">>) + , 404 => emqx_dashboard_swagger:error_codes([?NOT_FOUND], <<"Not Found">>) } } }; @@ -239,7 +239,7 @@ source(get, #{bindings := #{type := Type}}) -> } }; {error, Reason} -> - {400, #{code => <<"BAD_REQUEST">>, + {500, #{code => <<"INTERNAL_ERROR">>, message => bin(Reason)}} end; [Source] -> {200, read_certs(Source)} @@ -268,7 +268,7 @@ source(delete, #{bindings := #{type := Type}}) -> source_status(get, #{bindings := #{type := Type}}) -> BinType = atom_to_binary(Type, utf8), case get_raw_source(BinType) of - [] -> {400, #{code => <<"BAD_REQUEST">>, + [] -> {404, #{code => <<"BAD_REQUEST">>, message => <<"Not found", BinType/binary>>}}; [#{<<"type">> := <<"file">>}] -> {400, #{code => <<"BAD_REQUEST">>, @@ -341,7 +341,7 @@ lookup_from_all_nodes(ResourceId) -> } end; {error, ErrL} -> - {400, #{code => <<"BAD_REQUEST">>, + {500, #{code => <<"INTERNAL_ERROR">>, message => bin_t(io_lib:format("~p", [ErrL]))}} end. From dc8408e378617a889660da5a7b2f026287aa0be2 Mon Sep 17 00:00:00 2001 From: EMQ-YangM Date: Mon, 21 Mar 2022 14:42:09 +0800 Subject: [PATCH 2/2] fix(emqx_authz_api): fix error code --- apps/emqx_authz/src/emqx_authz_api_sources.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/emqx_authz/src/emqx_authz_api_sources.erl b/apps/emqx_authz/src/emqx_authz_api_sources.erl index e6cfd70bf..780ca67a5 100644 --- a/apps/emqx_authz/src/emqx_authz_api_sources.erl +++ b/apps/emqx_authz/src/emqx_authz_api_sources.erl @@ -268,7 +268,7 @@ source(delete, #{bindings := #{type := Type}}) -> source_status(get, #{bindings := #{type := Type}}) -> BinType = atom_to_binary(Type, utf8), case get_raw_source(BinType) of - [] -> {404, #{code => <<"BAD_REQUEST">>, + [] -> {404, #{code => <<"NOT_FOUND">>, message => <<"Not found", BinType/binary>>}}; [#{<<"type">> := <<"file">>}] -> {400, #{code => <<"BAD_REQUEST">>,