diff --git a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigConfigurationRouteFilter.java b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigConfigurationRouteFilter.java new file mode 100644 index 0000000000..09bf52ee24 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigConfigurationRouteFilter.java @@ -0,0 +1,57 @@ +package io.github.hectorvent.floci.services.appconfig; + +import jakarta.ws.rs.container.ContainerRequestContext; +import jakarta.ws.rs.container.ContainerRequestFilter; +import jakarta.ws.rs.container.PreMatching; +import jakarta.ws.rs.core.UriInfo; +import jakarta.ws.rs.ext.Provider; + +import java.net.URI; + +/** + * Pre-matching filter that disambiguates {@code GET /configuration} between the + * AppConfigData runtime API (GetLatestConfiguration) and an S3 bucket literally + * named {@code configuration} (issue #1294). + * + *

A genuine AppConfigData GetLatestConfiguration request always carries a + * {@code configuration_token} query parameter, while a path-style S3 + * ListObjectsV2 request does not. When the token is present the request is + * rewritten onto the internal AppConfigData path so {@link AppConfigDataController} + * handles it; otherwise the bare {@code /configuration} path is left untouched + * and falls through to S3's {@code /{bucket}} route. + * + *

The internal path contains an underscore, which S3 bucket names forbid, so + * it can never collide with a real bucket. This mirrors the existing routing + * filters ({@code SqsQueueUrlRouterFilter}, {@code LambdaUrlRoutingFilter}) that + * resolve path collisions before JAX-RS resource matching. + */ +@Provider +@PreMatching +public class AppConfigConfigurationRouteFilter implements ContainerRequestFilter { + + /** Internal-only path that {@link AppConfigDataController} binds for GetLatestConfiguration. */ + static final String INTERNAL_PATH = "/_appconfigdata_configuration"; + + @Override + public void filter(ContainerRequestContext ctx) { + if (!"GET".equals(ctx.getMethod())) { + return; + } + UriInfo uriInfo = ctx.getUriInfo(); + String path = uriInfo.getPath(); + if (path.startsWith("/")) { + path = path.substring(1); + } + if (!"configuration".equals(path)) { + return; + } + String token = uriInfo.getQueryParameters().getFirst("configuration_token"); + if (token == null || token.isBlank()) { + // No token: this is an S3 path-style request to a bucket named + // "configuration". Leave the path so it matches S3's /{bucket} route. + return; + } + URI rewritten = uriInfo.getRequestUriBuilder().replacePath(INTERNAL_PATH).build(); + ctx.setRequestUri(rewritten); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataController.java b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataController.java index 51e8526bf0..d2e216357a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataController.java +++ b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataController.java @@ -35,8 +35,13 @@ public class AppConfigDataController { return Response.status(201).entity(Map.of("InitialConfigurationToken", token)).build(); } + // Bound to an internal path rather than the public "/configuration" so that + // an S3 bucket named "configuration" is not shadowed by this route (issue + // #1294). AppConfigConfigurationRouteFilter rewrites genuine GetLatestConfiguration + // requests (those carrying a configuration_token) onto this path; requests + // without a token fall through to S3's /{bucket} handler. @GET - @Path("/configuration") + @Path(AppConfigConfigurationRouteFilter.INTERNAL_PATH) public Response getLatestConfiguration(@QueryParam("configuration_token") String token) { ConfigurationData data = service.getLatestConfiguration(token); return Response.ok(data.content()) diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ConfigurationBucketIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ConfigurationBucketIntegrationTest.java new file mode 100644 index 0000000000..0e6ae7f144 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ConfigurationBucketIntegrationTest.java @@ -0,0 +1,89 @@ +package io.github.hectorvent.floci.services.s3; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; + +/** + * An S3 bucket named exactly configuration must NOT collide with + * the AppConfigData runtime route (GET /configuration, issue #1294). + * + *

Discriminator: a genuine AppConfigData GetLatestConfiguration request always + * carries a configuration_token query parameter. An S3 path-style + * request to a bucket named configuration does not, so it must be + * routed to S3 instead of NPE-ing inside the AppConfig handler. + */ +@QuarkusTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class S3ConfigurationBucketIntegrationTest { + + private static final String BUCKET = "configuration"; + private static final String KEY = "settings.json"; + private static final String BODY = "{\"feature\":\"on\"}"; + + @Test + @Order(0) + void createConfigurationBucket() { + given() + .when() + .put("/" + BUCKET) + .then() + .statusCode(200); + } + + @Test + @Order(1) + void listObjectsV2_mustNotBeRoutedToAppConfig() { + String body = given() + .queryParam("list-type", "2") + .when() + .get("/" + BUCKET) + .then() + .statusCode(200) + .body(containsString("ListBucketResult")) + .body(containsString("configuration")) + .extract().asString(); + + if (body.contains("BadRequestException") || body.contains("Invalid configuration token")) { + throw new AssertionError( + "GET /" + BUCKET + "?list-type=2 was routed to AppConfig instead of S3. Response: " + body); + } + } + + @Test + @Order(2) + void putAndGetObjectInConfigurationBucket() { + given() + .header("Content-Type", "application/json") + .body(BODY) + .when() + .put("/" + BUCKET + "/" + KEY) + .then() + .statusCode(200); + + given() + .when() + .get("/" + BUCKET + "/" + KEY) + .then() + .statusCode(200) + .body(equalTo(BODY)); + } + + @Test + @Order(3) + void listObjectsV2_returnsStoredObject() { + given() + .queryParam("list-type", "2") + .when() + .get("/" + BUCKET) + .then() + .statusCode(200) + .body(containsString("" + KEY + "")); + } +}