mirror of
https://github.com/floci-io/floci.git
synced 2026-11-26 23:29:28 -05:00
fix(ec2): use EC2 envelopes for token and signature errors (#7640)
This commit is contained in:
1 parent
07d75c8c8e
commit
81ff58630e
7 files changed
+216
-5
No files matched your search
@@ -1,5 +1,7 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import io.github.hectorvent.floci.services.iam.RequestBodyReader;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
@@ -11,6 +13,25 @@ public final class Ec2ErrorResponses {
|
||||
private Ec2ErrorResponses() {
|
||||
}
|
||||
|
||||
static boolean isEc2QueryRequest(ContainerRequestContext ctx) {
|
||||
if (!(ctx.getProperty(AwsProtocolClaimFilter.CLAIM_PROPERTY) instanceof ProtocolClaim claim)
|
||||
|| claim.protocol() != WireProtocol.AWS_QUERY) {
|
||||
return false;
|
||||
}
|
||||
if (claim.service() != null) {
|
||||
return "ec2".equals(claim.service().externalKey());
|
||||
}
|
||||
try {
|
||||
String action = AwsQueryServiceResolver.action(RequestBodyReader.formField(ctx, "Action"),
|
||||
RequestBodyReader.formField(ctx, "Operation"));
|
||||
return action != null && "ec2".equals(AwsQueryController.inferServiceFromAction(action,
|
||||
RequestBodyReader.formField(ctx, "Version")));
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
// A malformed form cannot identify a service; retain the generic protocol error.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static Response authFailure() {
|
||||
return of("AuthFailure", "AWS was not able to validate the provided access credentials", 401);
|
||||
}
|
||||
|
||||
@@ -2064,10 +2064,13 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
|
||||
/**
|
||||
* The response for a session whose temporary credentials have expired. S3's error list names it
|
||||
* {@code ExpiredToken}, 400. The other services' common errors name it
|
||||
* {@code ExpiredToken}, 400. EC2 names it {@code RequestExpired}. The other services' common errors name it
|
||||
* {@code ExpiredTokenException}, 403, under the same name for Query services as for JSON ones.
|
||||
*/
|
||||
static Response expiredTokenResponse(String credentialScope, ContainerRequestContext ctx) {
|
||||
if ("ec2".equals(credentialScope)) {
|
||||
return Ec2ErrorResponses.of("RequestExpired", EXPIRED_TOKEN, 400);
|
||||
}
|
||||
if ("s3".equals(credentialScope)) {
|
||||
return s3XmlError("ExpiredToken", S3_EXPIRED_TOKEN, 400);
|
||||
}
|
||||
@@ -2094,6 +2097,9 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
*/
|
||||
static Response incompleteSignatureResponse(String credentialScope, ContainerRequestContext ctx,
|
||||
String message) {
|
||||
if ("ec2".equals(credentialScope) || Ec2ErrorResponses.isEc2QueryRequest(ctx)) {
|
||||
return Ec2ErrorResponses.of("IncompleteSignature", message, 400);
|
||||
}
|
||||
if ("s3".equals(credentialScope)) {
|
||||
String header = ctx.getHeaderString("Authorization");
|
||||
if (header == null || header.isBlank()) {
|
||||
@@ -2163,6 +2169,9 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
* returns for the same failure on its own path.
|
||||
*/
|
||||
static Response missingAuthenticationTokenResponse(WireProtocol protocol, ContainerRequestContext ctx) {
|
||||
if (protocol == WireProtocol.AWS_QUERY && Ec2ErrorResponses.isEc2QueryRequest(ctx)) {
|
||||
return Ec2ErrorResponses.of("MissingAuthenticationToken", MISSING_AUTHENTICATION_TOKEN, 403);
|
||||
}
|
||||
return protocolError(protocol, ctx, "MissingAuthenticationToken", "MissingAuthenticationTokenException",
|
||||
MISSING_AUTHENTICATION_TOKEN, 403);
|
||||
}
|
||||
|
||||
+22
-4
@@ -86,11 +86,13 @@ import java.util.function.UnaryOperator;
|
||||
* <p>Errors follow the protocol the request used: a Query or REST-XML request (Route 53,
|
||||
* CloudFront, S3 Control) receives an {@code <ErrorResponse>}, a CBOR request a CBOR body, and
|
||||
* 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,
|
||||
* use EC2's {@code Response/Errors/Error} envelope when the Query request identifies EC2.
|
||||
* 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 (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.
|
||||
* EC2 uses {@code RequestExpired} for expiry and clock skew, including expired temporary credentials.
|
||||
*
|
||||
* <p>Nothing here runs with the flag off: the filter returns before reading a single header.
|
||||
*/
|
||||
@@ -236,6 +238,10 @@ public class SigV4HeaderSignatureFilter implements ContainerRequestFilter {
|
||||
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())) {
|
||||
if (iamService.isExpiredSessionToken(scope.accessKeyId(), token)) {
|
||||
ctx.abortWith(IamEnforcementFilter.expiredTokenResponse(scope.service(), ctx));
|
||||
return;
|
||||
}
|
||||
ctx.abortWith(Ec2ErrorResponses.authFailure());
|
||||
return;
|
||||
}
|
||||
@@ -246,17 +252,17 @@ public class SigV4HeaderSignatureFilter implements ContainerRequestFilter {
|
||||
String amzDate = AMZ_DATE.format(requestTime);
|
||||
Instant now = Instant.now();
|
||||
if (presigned && !now.isBefore(requestTime.plusSeconds(expires))) {
|
||||
signatureDoesNotMatch(ctx, resource, "Signature expired: " + amzDate + " is now earlier than "
|
||||
requestExpired(ctx, resource, "Signature expired: " + amzDate + " is now earlier than "
|
||||
+ AMZ_DATE.format(now.minusSeconds(expires)));
|
||||
return;
|
||||
}
|
||||
if (requestTime.isAfter(now.plus(MAX_CLOCK_SKEW))) {
|
||||
signatureDoesNotMatch(ctx, resource, "Signature not yet current: " + amzDate + " is still later than "
|
||||
requestExpired(ctx, resource, "Signature not yet current: " + amzDate + " is still later than "
|
||||
+ AMZ_DATE.format(now.plus(MAX_CLOCK_SKEW)));
|
||||
return;
|
||||
}
|
||||
if (!presigned && requestTime.isBefore(now.minus(MAX_CLOCK_SKEW))) {
|
||||
signatureDoesNotMatch(ctx, resource, "Signature expired: " + amzDate + " is now earlier than "
|
||||
requestExpired(ctx, resource, "Signature expired: " + amzDate + " is now earlier than "
|
||||
+ AMZ_DATE.format(now.minus(MAX_CLOCK_SKEW)));
|
||||
return;
|
||||
}
|
||||
@@ -449,6 +455,14 @@ public class SigV4HeaderSignatureFilter implements ContainerRequestFilter {
|
||||
abort(ctx, resource, 400, "IncompleteSignature", "IncompleteSignatureException", message);
|
||||
}
|
||||
|
||||
private void requestExpired(ContainerRequestContext ctx, Class<?> resource, String message) {
|
||||
if (resource == AwsQueryController.class && Ec2ErrorResponses.isEc2QueryRequest(ctx)) {
|
||||
ctx.abortWith(Ec2ErrorResponses.of("RequestExpired", message, 400));
|
||||
return;
|
||||
}
|
||||
signatureDoesNotMatch(ctx, resource, message);
|
||||
}
|
||||
|
||||
private void signatureDoesNotMatch(ContainerRequestContext ctx, Class<?> resource, String message) {
|
||||
abort(ctx, resource, 403, "SignatureDoesNotMatch", "InvalidSignatureException", message);
|
||||
}
|
||||
@@ -462,6 +476,10 @@ public class SigV4HeaderSignatureFilter implements ContainerRequestFilter {
|
||||
*/
|
||||
private void abort(ContainerRequestContext ctx, Class<?> resource, int status, String queryCode,
|
||||
String jsonCode, String message) {
|
||||
if (resource == AwsQueryController.class && Ec2ErrorResponses.isEc2QueryRequest(ctx)) {
|
||||
ctx.abortWith(Ec2ErrorResponses.of(queryCode, message, status));
|
||||
return;
|
||||
}
|
||||
String contentType = requestContentType(ctx);
|
||||
if (isCbor(ctx, contentType)) {
|
||||
ctx.abortWith(CborErrorResponses.of(new AwsException(jsonCode, message, status),
|
||||
|
||||
Reference in new issue
Block a user