mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(apigateway): send x-amzn-ErrorType on REST gateway responses (#6687)
* fix(apigateway): send x-amzn-ErrorType on the API key 403 * fix(apigateway): send x-amzn-ErrorType on every REST gateway response Authorizer, validation, media-type, configuration and Lambda proxy failures now carry the error type AWS pairs with their gateway response type, customised or not, measured on a REST API in eu-west-1. * fix(apigateway): keep a gateway response's own x-amzn-ErrorType where AWS does A gateway response that maps gatewayresponse.header.x-amzn-ErrorType sends only that value for the validation, media type, configuration and integration types, and sends it ahead of the default for the authorizer and API key types, as measured on AWS.
This commit is contained in:
1 parent
64069f9ccd
commit
3f294c3529
9 files changed
+162
-35
No files matched your search
+40
-9
@@ -72,6 +72,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
@@ -579,7 +580,7 @@ public class ApiGatewayExecuteController {
|
||||
// A function error or a payload that is not a proxy response is answered by the
|
||||
// gateway itself, so a configured INTEGRATION_FAILURE / DEFAULT_5XX shapes it.
|
||||
return gatewayResponseOr(response, scope, GatewayResponseType.INTEGRATION_FAILURE,
|
||||
"Internal server error");
|
||||
"Internal server error", null);
|
||||
}
|
||||
return response;
|
||||
} catch (AwsException e) {
|
||||
@@ -3969,7 +3970,35 @@ public class ApiGatewayExecuteController {
|
||||
|
||||
private Response gatewayResponse(GatewayResponseScope scope, GatewayResponseType type, int status,
|
||||
String message, String validationError) {
|
||||
return gatewayResponseBuilder(scope, type, status, message, validationError).build();
|
||||
Response fallback = Response.status(status).entity(jsonMessage(message)).type(MediaType.APPLICATION_JSON)
|
||||
.build();
|
||||
return gatewayResponseOr(fallback, scope, type, message, validationError);
|
||||
}
|
||||
|
||||
/**
|
||||
* The types whose gateway response sends only its own {@code x-amzn-ErrorType} when it maps one
|
||||
* (that exact key); the others send the mapped value ahead of theirs (measured on a REST API,
|
||||
* eu-west-1, 2026-10-26).
|
||||
*/
|
||||
private static final Set<GatewayResponseType> MAPPED_ERROR_TYPE_REPLACES = EnumSet.of(
|
||||
GatewayResponseType.BAD_REQUEST_PARAMETERS, GatewayResponseType.BAD_REQUEST_BODY,
|
||||
GatewayResponseType.UNSUPPORTED_MEDIA_TYPE, GatewayResponseType.API_CONFIGURATION_ERROR,
|
||||
GatewayResponseType.INTEGRATION_FAILURE);
|
||||
|
||||
/**
|
||||
* The {@code x-amzn-ErrorType} AWS sends with a gateway response of {@code type}, customised or
|
||||
* not (measured on a REST API, eu-west-1, 2026-10-26). Null for the types these sites never emit.
|
||||
*/
|
||||
private static String gatewayErrorType(GatewayResponseType type) {
|
||||
return switch (type) {
|
||||
case INVALID_API_KEY -> "ForbiddenException";
|
||||
case UNAUTHORIZED -> "UnauthorizedException";
|
||||
case ACCESS_DENIED -> "AccessDeniedException";
|
||||
case AUTHORIZER_FAILURE, AUTHORIZER_CONFIGURATION_ERROR -> "AuthorizerConfigurationException";
|
||||
case BAD_REQUEST_PARAMETERS, BAD_REQUEST_BODY, UNSUPPORTED_MEDIA_TYPE -> "BadRequestException";
|
||||
case API_CONFIGURATION_ERROR, INTEGRATION_FAILURE -> "InternalServerErrorException";
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
private Response.ResponseBuilder gatewayResponseBuilder(GatewayResponseScope scope, GatewayResponseType type,
|
||||
@@ -3983,18 +4012,20 @@ public class ApiGatewayExecuteController {
|
||||
}
|
||||
|
||||
/**
|
||||
* For the sites whose uncustomised answer is not the {@code {"message"}} body (a bare status,
|
||||
* or the Lambda payload passed through): the answer stays as it was unless the API customised
|
||||
* a gateway response for it.
|
||||
* The {@code fallback} answer stays as it was (a {@code {"message"}} body, a bare status, or the
|
||||
* Lambda payload passed through) unless the API customised a gateway response for it.
|
||||
*/
|
||||
private Response gatewayResponseOr(Response fallback, GatewayResponseScope scope, GatewayResponseType type,
|
||||
String message) {
|
||||
String message, String validationError) {
|
||||
GatewayResponse configured = apiGatewayService.resolveGatewayResponse(scope.region(), scope.apiId(),
|
||||
type, fallback.getStatus());
|
||||
if (configured == null) {
|
||||
return fallback;
|
||||
Response.ResponseBuilder builder = configured == null ? Response.fromResponse(fallback)
|
||||
: renderGatewayResponse(scope, configured, type, fallback.getStatus(), message, validationError);
|
||||
if (configured != null && MAPPED_ERROR_TYPE_REPLACES.contains(type)
|
||||
&& configured.getResponseParameters().containsKey(GATEWAY_RESPONSE_HEADER_PREFIX + "x-amzn-ErrorType")) {
|
||||
return builder.build();
|
||||
}
|
||||
return renderGatewayResponse(scope, configured, type, fallback.getStatus(), message, null).build();
|
||||
return builder.header("x-amzn-ErrorType", gatewayErrorType(type)).build();
|
||||
}
|
||||
|
||||
private Response.ResponseBuilder renderGatewayResponse(GatewayResponseScope scope, GatewayResponse configured,
|
||||
|
||||
Reference in new issue
Block a user