Merge pull request #7349 from EMQ-YangM/http_code

fix: unify http response code for auhtn & authz
This commit is contained in:
Yang Miao 2022-03-21 15:32:32 +08:00 committed by GitHub
commit fe95e6ffbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 14 deletions

View File

@ -29,7 +29,7 @@
-define(BAD_REQUEST, 'BAD_REQUEST'). -define(BAD_REQUEST, 'BAD_REQUEST').
-define(NOT_FOUND, 'NOT_FOUND'). -define(NOT_FOUND, 'NOT_FOUND').
-define(CONFLICT, 'CONFLICT'). -define(ALREADY_EXISTS, 'ALREADY_EXISTS').
% Swagger % Swagger
@ -170,7 +170,7 @@ schema("/authentication") ->
emqx_authn_schema:authenticator_type(), emqx_authn_schema:authenticator_type(),
authenticator_examples()), authenticator_examples()),
400 => error_codes([?BAD_REQUEST], <<"Bad Request">>), 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()), authenticator_examples()),
400 => error_codes([?BAD_REQUEST], <<"Bad Request">>), 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>),
404 => error_codes([?NOT_FOUND], <<"Not Found">>), 404 => error_codes([?NOT_FOUND], <<"Not Found">>),
409 => error_codes([?CONFLICT], <<"Conflict">>) 409 => error_codes([?ALREADY_EXISTS], <<"ALREADY_EXISTS">>)
} }
}, },
delete => #{ delete => #{
@ -259,7 +259,7 @@ schema("/listeners/:listener_id/authentication") ->
emqx_authn_schema:authenticator_type(), emqx_authn_schema:authenticator_type(),
authenticator_examples()), authenticator_examples()),
400 => error_codes([?BAD_REQUEST], <<"Bad Request">>), 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()), authenticator_examples()),
400 => error_codes([?BAD_REQUEST], <<"Bad Request">>), 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>),
404 => error_codes([?NOT_FOUND], <<"Not Found">>), 404 => error_codes([?NOT_FOUND], <<"Not Found">>),
409 => error_codes([?CONFLICT], <<"Conflict">>) 409 => error_codes([?ALREADY_EXISTS], <<"ALREADY_EXISTS">>)
} }
}, },
delete => #{ delete => #{
@ -842,7 +842,9 @@ lookup_from_all_nodes(ChainName, AuthenticatorID) ->
MKMap = fun (Name) -> fun ({Key, Val}) -> #{ node => Key, Name => Val } end end, 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, HelpFun = fun (M, Name) -> lists:map(MKMap(Name), maps:to_list(M)) end,
case AggregateStatus of 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), _ -> {200, #{node_status => HelpFun(StatusMap, status),
node_metrics => HelpFun(maps:map(Fun, MetricsMap), metrics), node_metrics => HelpFun(maps:map(Fun, MetricsMap), metrics),
status => AggregateStatus, status => AggregateStatus,
@ -851,7 +853,8 @@ lookup_from_all_nodes(ChainName, AuthenticatorID) ->
} }
end; end;
{error, ErrL} -> {error, ErrL} ->
serialize_error(ErrL) {500, #{code => <<"INTERNAL_ERROR">>,
message => list_to_binary(io_lib:format("~p", [ErrL]))}}
end. end.
aggregate_status([]) -> empty_metrics_and_status; aggregate_status([]) -> empty_metrics_and_status;
@ -1052,7 +1055,7 @@ serialize_error({user_error, not_found}) ->
{404, #{code => <<"NOT_FOUND">>, {404, #{code => <<"NOT_FOUND">>,
message => binfmt("User not found", [])}}; message => binfmt("User not found", [])}};
serialize_error({user_error, already_exist}) -> serialize_error({user_error, already_exist}) ->
{409, #{code => <<"BAD_REQUEST">>, {409, #{code => <<"ALREADY_EXISTS">>,
message => binfmt("User already exists", [])}}; message => binfmt("User already exists", [])}};
serialize_error({user_error, Reason}) -> serialize_error({user_error, Reason}) ->
{400, #{code => <<"BAD_REQUEST">>, {400, #{code => <<"BAD_REQUEST">>,
@ -1085,10 +1088,10 @@ serialize_error({bad_ssl_config, Details}) ->
{400, #{code => <<"BAD_REQUEST">>, {400, #{code => <<"BAD_REQUEST">>,
message => binfmt("bad_ssl_config ~p", [Details])}}; message => binfmt("bad_ssl_config ~p", [Details])}};
serialize_error({missing_parameter, Detail}) -> serialize_error({missing_parameter, Detail}) ->
{400, #{code => <<"MISSING_PARAMETER">>, {400, #{code => <<"BAD_REQUEST">>,
message => binfmt("Missing required parameter: ~p", [Detail])}}; message => binfmt("Missing required parameter: ~p", [Detail])}};
serialize_error({invalid_parameter, Name}) -> serialize_error({invalid_parameter, Name}) ->
{400, #{code => <<"INVALID_PARAMETER">>, {400, #{code => <<"BAD_REQUEST">>,
message => binfmt("Invalid value for '~p'", [Name])}}; message => binfmt("Invalid value for '~p'", [Name])}};
serialize_error({unknown_authn_type, Type}) -> serialize_error({unknown_authn_type, Type}) ->
{400, #{code => <<"BAD_REQUEST">>, {400, #{code => <<"BAD_REQUEST">>,

View File

@ -138,7 +138,6 @@ schema("/authorization/sources/:type") ->
, responses => , responses =>
#{ 204 => <<"Authorization source updated successfully">> #{ 204 => <<"Authorization source updated successfully">>
, 400 => emqx_dashboard_swagger:error_codes([?BAD_REQUEST], <<"Bad Request">>) , 400 => emqx_dashboard_swagger:error_codes([?BAD_REQUEST], <<"Bad Request">>)
, 404 => emqx_dashboard_swagger:error_codes([?NOT_FOUND], <<"Not Found">>)
} }
} }
, delete => , delete =>
@ -160,6 +159,7 @@ schema("/authorization/sources/:type/status") ->
hoconsc:ref(emqx_authn_schema, "metrics_status_fields"), hoconsc:ref(emqx_authn_schema, "metrics_status_fields"),
status_metrics_example()) status_metrics_example())
, 400 => emqx_dashboard_swagger:error_codes([?BAD_REQUEST], <<"Bad request">>) , 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} -> {error, Reason} ->
{400, #{code => <<"BAD_REQUEST">>, {500, #{code => <<"INTERNAL_ERROR">>,
message => bin(Reason)}} message => bin(Reason)}}
end; end;
[Source] -> {200, read_certs(Source)} [Source] -> {200, read_certs(Source)}
@ -268,7 +268,7 @@ source(delete, #{bindings := #{type := Type}}) ->
source_status(get, #{bindings := #{type := Type}}) -> source_status(get, #{bindings := #{type := Type}}) ->
BinType = atom_to_binary(Type, utf8), BinType = atom_to_binary(Type, utf8),
case get_raw_source(BinType) of case get_raw_source(BinType) of
[] -> {400, #{code => <<"BAD_REQUEST">>, [] -> {404, #{code => <<"NOT_FOUND">>,
message => <<"Not found", BinType/binary>>}}; message => <<"Not found", BinType/binary>>}};
[#{<<"type">> := <<"file">>}] -> [#{<<"type">> := <<"file">>}] ->
{400, #{code => <<"BAD_REQUEST">>, {400, #{code => <<"BAD_REQUEST">>,
@ -341,7 +341,7 @@ lookup_from_all_nodes(ResourceId) ->
} }
end; end;
{error, ErrL} -> {error, ErrL} ->
{400, #{code => <<"BAD_REQUEST">>, {500, #{code => <<"INTERNAL_ERROR">>,
message => bin_t(io_lib:format("~p", [ErrL]))}} message => bin_t(io_lib:format("~p", [ErrL]))}}
end. end.