fix(ec2): return modeled authentication error envelopes (#7613)

This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-05 15:36:40 -04:00
1 parent f2debe4ce0
commit d34c8443cd
11 files changed
+285 -31

No files matched your search

@@ -0,0 +1,32 @@
package io.github.hectorvent.floci.core.common;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.util.UUID;
/** EC2's error envelope differs from the other Query services. */
public final class Ec2ErrorResponses {
private Ec2ErrorResponses() {
}
public static Response authFailure() {
return of("AuthFailure", "AWS was not able to validate the provided access credentials", 401);
}
public static Response of(String code, String message, int status) {
String xml = new XmlBuilder()
.start("Response")
.start("Errors")
.start("Error")
.elem("Code", code)
.elem("Message", message)
.end("Error")
.end("Errors")
.elem("RequestID", UUID.randomUUID().toString())
.end("Response")
.build();
return Response.status(status).entity(xml).type(MediaType.APPLICATION_XML).build();
}
}
@@ -1920,6 +1920,10 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
private static Response accessDeniedResponse(String action, String credentialScope, MediaType requestMediaType,
String resourceArn, WireProtocol protocol, boolean restXml) {
String message = accessDeniedMessage(action);
if ("ec2".equals(credentialScope)) {
return Ec2ErrorResponses.of("UnauthorizedOperation",
"You are not authorized to perform this operation. " + message, 403);
}
if ("s3".equals(credentialScope)) {
String resourcePath = formatS3ResourcePath(resourceArn);
return s3XmlAccessDenied(message, resourcePath);
@@ -2031,7 +2035,8 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
/**
* The response for an access key that exists nowhere, in each protocol's own vocabulary for
* the same failure. S3 answers with {@code InvalidAccessKeyId}, as
* {@code S3HeaderSignatureFilter} already does. Query services answer with
* {@code S3HeaderSignatureFilter} already does. EC2 answers {@code AuthFailure}
* in its own error envelope. Other Query services answer with
* {@code InvalidClientTokenId}: neither code is modelled by sts, iam or sqs, but botocore's
* own integration tests retry on {@code InvalidClientTokenId} from {@code sts:AssumeRole} and
* {@code iam:ListGroups} while credentials propagate. JSON services answer with
@@ -2039,6 +2044,9 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
* and what the API Gateway execute path already returns.
*/
static Response unrecognizedClientResponse(String credentialScope, ContainerRequestContext ctx) {
if ("ec2".equals(credentialScope)) {
return Ec2ErrorResponses.authFailure();
}
if ("s3".equals(credentialScope)) {
return s3XmlError("InvalidAccessKeyId", S3_INVALID_ACCESS_KEY_ID, 403);
}
@@ -88,7 +88,8 @@ import java.util.function.UnaryOperator;
* everything else JSON, which REST-JSON clients read through {@code X-Amzn-Errortype}. The codes
* are AWS's: {@code IncompleteSignature} for a header that cannot be checked,
* {@code InvalidClientTokenId} / {@code UnrecognizedClientException} for an access key Floci does
* not know, and {@code SignatureDoesNotMatch} / {@code InvalidSignatureException} for a signature
* not know (EC2 instead answers {@code AuthFailure} in its own envelope), and
* {@code SignatureDoesNotMatch} / {@code InvalidSignatureException} for a signature
* that does not verify, exceeds its query expiry or falls outside the header clock-skew window.
*
* <p>Nothing here runs with the flag off: the filter returns before reading a single header.
@@ -234,6 +235,10 @@ public class SigV4HeaderSignatureFilter implements ContainerRequestFilter {
if (secretKey.isEmpty()) {
LOG.debugv("Refusing {0} request signed with unknown access key {1}",
scope.service(), SigV4RequestValidator.sanitizeForLog(scope.accessKeyId()));
if (resource == AwsQueryController.class && "ec2".equals(scope.service())) {
ctx.abortWith(Ec2ErrorResponses.authFailure());
return;
}
abort(ctx, resource, 403, "InvalidClientTokenId", "UnrecognizedClientException", INVALID_TOKEN_MESSAGE);
return;
}
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.common.AwsNamespaces;
import io.github.hectorvent.floci.core.common.AwsPartition;
import io.github.hectorvent.floci.core.common.AwsPartitions;
import io.github.hectorvent.floci.core.common.AwsRegionFacts;
import io.github.hectorvent.floci.core.common.Ec2ErrorResponses;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.SshPublicKeys;
@@ -378,34 +379,14 @@ public class Ec2QueryHandler {
case "AllocateIpamPoolCidr" -> handleAllocateIpamPoolCidr(params, region);
case "ReleaseIpamPoolAllocation" -> handleReleaseIpamPoolAllocation(params, region);
case "GetIpamPoolAllocations" -> handleGetIpamPoolAllocations(params, region);
default -> ec2Error("UnsupportedOperation",
default -> Ec2ErrorResponses.of("UnsupportedOperation",
"Operation " + action + " is not supported.", 400);
};
} catch (AwsException e) {
return ec2Error(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
return Ec2ErrorResponses.of(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
}
}
/**
* EC2 uses a different error envelope than other Query-protocol services.
* The AWS SDK v2 EC2 client parses {@code <Response><Errors><Error><Code>},
* not the standard {@code <ErrorResponse><Error><Code>} shape.
*/
private Response ec2Error(String code, String message, int status) {
String xml = new XmlBuilder()
.start("Response")
.start("Errors")
.start("Error")
.elem("Code", code)
.elem("Message", message)
.end("Error")
.end("Errors")
.elem("RequestID", UUID.randomUUID().toString())
.end("Response")
.build();
return Response.status(status).entity(xml).type(MediaType.APPLICATION_XML).build();
}
// ─── Parameter helpers ────────────────────────────────────────────────────
private String firstPresent(MultivaluedMap<String, String> p, String first, String second) {
@@ -1963,7 +1944,7 @@ public class Ec2QueryHandler {
// Apply filters
services = applyVpcEndpointServiceFilters(services, filters);
} catch (AwsException e) {
return ec2Error(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
return Ec2ErrorResponses.of(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
}
XmlBuilder xml = new XmlBuilder()
@@ -2158,7 +2139,7 @@ public class Ec2QueryHandler {
resourceIds = getList(p, "ResourceIds.member");
}
if (resourceIds.isEmpty()) {
return ec2Error("MissingParameter", "The request must contain at least one ResourceId.", 400);
return Ec2ErrorResponses.of("MissingParameter", "The request must contain at least one ResourceId.", 400);
}
XmlBuilder xml = new XmlBuilder()