Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(s3): wrap S3 Control XML errors in ErrorResponse wrapper (#560)

Mateus Scheuer Macedobdc13f894e07
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3ControlController.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3ControlController.java
index bd06a7aa83..b484a1d5b3 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3ControlController.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3ControlController.java
@@ -35,6 +35,10 @@ import java.util.UUID;
@Produces(MediaType.APPLICATION_XML)
public class S3ControlController {
+ private static final String AMZ_REQUEST_ID = "x-amz-request-id";
+ private static final String AMZN_REQUEST_ID = "x-amzn-RequestId";
+ private static final String AMZ_ID_2 = "x-amz-id-2";
+
private final S3Service s3Service;
@Inject
@@ -150,20 +154,31 @@ public class S3ControlController {
/**
* S3 Control is a REST-XML protocol, so error responses must also be XML.
- * The global {@code AwsExceptionMapper} emits JSON, which trips up the
- * Go SDK's XML error deserializer with "unexpected EOF".
+ * AWS S3 Control wraps errors in an {@code <ErrorResponse xmlns=...>} envelope
+ * containing the inner {@code <Error>} block and a top-level {@code <RequestId>}.
+ *
+ * <p>References: AWS Go SDK v2 s3control error deserializer expects this wrapper;
+ * bare {@code <Error>} collapses to "UnknownError" at the SDK layer.
+ * See issue #557.
*/
private Response xmlErrorResponse(AwsException e) {
+ String requestId = UUID.randomUUID().toString();
String xml = new XmlBuilder()
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
+ .start("ErrorResponse", AwsNamespaces.S3_CONTROL)
.start("Error")
.elem("Code", e.getErrorCode())
.elem("Message", e.getMessage())
- .elem("RequestId", UUID.randomUUID().toString())
+ .elem("RequestId", requestId)
.end("Error")
+ .elem("RequestId", requestId)
+ .end("ErrorResponse")
.build();
return Response.status(e.getHttpStatus())
.type(MediaType.APPLICATION_XML)
+ .header(AMZ_REQUEST_ID, requestId)
+ .header(AMZN_REQUEST_ID, requestId)
+ .header(AMZ_ID_2, requestId)
.entity(xml)
.build();
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ControlUrlEncodedArnIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ControlUrlEncodedArnIntegrationTest.java
index 0f6846023b..cbe5d5ed0f 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3ControlUrlEncodedArnIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ControlUrlEncodedArnIntegrationTest.java
@@ -1,5 +1,7 @@
package io.github.hectorvent.floci.services.s3;
+import io.github.hectorvent.floci.core.common.XmlParser;
+import io.restassured.response.Response;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.MethodOrderer;
@@ -7,9 +9,14 @@ import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
+import java.util.List;
+
import static io.restassured.RestAssured.given;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Integration tests for S3 Control API handling of URL-encoded ARN path
@@ -33,6 +40,25 @@ class S3ControlUrlEncodedArnIntegrationTest {
private static final String ENCODED_ARN =
"arn%3Aaws%3As3%3A" + REGION + "%3A" + ACCOUNT + "%3Abucket%2F" + BUCKET;
+ private void assertS3ControlErrorResponse(Response response) {
+ String body = response.getBody().asString();
+ List<String> requestIds = XmlParser.extractAll(body, "RequestId");
+
+ assertEquals(400, response.statusCode());
+ assertThat(response.getContentType(), containsString("xml"));
+ assertThat(body, containsString("<ErrorResponse xmlns=\"http://awss3control.amazonaws.com/doc/2018-08-20/\">"));
+ assertThat(body, containsString("<Error>"));
+ assertThat(body, containsString("<Code>InvalidRequest</Code>"));
+ assertTrue(body.contains("</Error><RequestId>"),
+ "expected top-level RequestId sibling after the Error block");
+ assertEquals(2, requestIds.size(), "expected inner and top-level RequestId elements");
+ assertEquals(requestIds.get(0), requestIds.get(1),
+ "expected inner and top-level RequestId values to match");
+ assertEquals(requestIds.get(0), response.getHeader("x-amz-request-id"));
+ assertEquals(requestIds.get(0), response.getHeader("x-amzn-RequestId"));
+ assertEquals(requestIds.get(0), response.getHeader("x-amz-id-2"));
+ }
+
@Test
@Order(1)
@DisplayName("setup: create bucket and tag it via the standard S3 tagging API")
@@ -131,33 +157,27 @@ class S3ControlUrlEncodedArnIntegrationTest {
@Test
@Order(6)
- @DisplayName("Malformed ARN returns valid S3-style XML error body (#435)")
+ @DisplayName("Malformed ARN returns S3 Control ErrorResponse wrapper (#435, #557)")
void malformedArnReturnsXmlError() {
// Path param must not contain a literal ':bucket/' segment after decoding.
- given()
+ Response response = given()
.header("x-amz-account-id", ACCOUNT)
.when()
- .get("/v20180820/tags/arn%3Aaws%3As3%3A%3A%3Abogus%2F" + BUCKET)
- .then()
- .statusCode(400)
- .contentType(containsString("xml"))
- .body(containsString("<Error>"))
- .body(containsString("<Code>InvalidRequest</Code>"));
+ .get("/v20180820/tags/arn%3Aaws%3As3%3A%3A%3Abogus%2F" + BUCKET);
+
+ assertS3ControlErrorResponse(response);
}
@Test
@Order(7)
- @DisplayName("Malformed percent-encoding returns XML error, not JSON 500 (#435)")
+ @DisplayName("Malformed percent-encoding returns S3 Control ErrorResponse wrapper (#435, #557)")
void malformedPercentEncodingReturnsXmlError() {
// %ZZ is not a valid percent-encoding sequence; URLDecoder throws IAE.
- given()
+ Response response = given()
.header("x-amz-account-id", ACCOUNT)
.when()
- .get("/v20180820/tags/arn%3Aaws%ZZbucket%2F" + BUCKET)
- .then()
- .statusCode(400)
- .contentType(containsString("xml"))
- .body(containsString("<Error>"))
- .body(containsString("<Code>InvalidRequest</Code>"));
+ .get("/v20180820/tags/arn%3Aaws%ZZbucket%2F" + BUCKET);
+
+ assertS3ControlErrorResponse(response);
}
}