From 2effefbbf05818528c726aa1ab4320527549d465 Mon Sep 17 00:00:00 2001 From: Kenshin Okinaka Date: Sat, 28 Nov 2026 21:16:03 +0900 Subject: [PATCH] fix(s3tables): route requests by the service they are signed for (#9603) S3 Tables' routes begin with literal segments (/buckets, /get-table, /namespaces, /tables) that shadowed S3's path-style routes, so an S3 call for a bucket with one of those names reached S3 Tables. The routes now sit under an internal prefix that only a request signed for s3tables is rewritten onto, as Signer's and DLM's are, so a request signed for S3 keeps its path and reaches S3. The shared SignedServiceRoute rewrite now keeps the path's percent-encoding, so an encoded table bucket ARN stays one path segment, and IAM matches a moved request on that percent-encoded path. --- .../aws/core/common/SignedServiceRoute.java | 7 ++- .../aws/services/iam/IamActionRegistry.java | 9 ++- .../floci/aws/services/iam/IamRestRoutes.java | 15 ++--- .../services/s3tables/S3TablesController.java | 2 +- .../s3tables/S3TablesRouteFilter.java | 43 ++++++++++++++ .../s3tables/S3TablesIntegrationTest.java | 24 ++++---- .../S3TablesRouteIntegrationTest.java | 58 +++++++++++++++++++ .../S3TablesWarehouseIntegrationTest.java | 16 ++--- .../floci/aws/testing/S3TablesRequests.java | 16 +++++ 9 files changed, 158 insertions(+), 32 deletions(-) create mode 100644 src/main/java/io/floci/aws/services/s3tables/S3TablesRouteFilter.java create mode 100644 src/test/java/io/floci/aws/services/s3tables/S3TablesRouteIntegrationTest.java create mode 100644 src/test/java/io/floci/aws/testing/S3TablesRequests.java diff --git a/src/main/java/io/floci/aws/core/common/SignedServiceRoute.java b/src/main/java/io/floci/aws/core/common/SignedServiceRoute.java index 79cc0ee7b1..7569a69755 100644 --- a/src/main/java/io/floci/aws/core/common/SignedServiceRoute.java +++ b/src/main/java/io/floci/aws/core/common/SignedServiceRoute.java @@ -3,7 +3,9 @@ package io.floci.aws.core.common; import jakarta.ws.rs.container.ContainerRequestContext; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.core.UriBuilder; +import java.net.URI; import java.util.Optional; import java.util.Set; import java.util.function.Predicate; @@ -111,8 +113,9 @@ public final class SignedServiceRoute { if (!publicPaths.test(normalizedPath) || !signedForService(ctx)) { return; } - ctx.setRequestUri(ctx.getUriInfo().getRequestUriBuilder() - .replacePath(internalPrefix + normalizedPath) + URI requestUri = ctx.getUriInfo().getRequestUri(); + RequestUriRewrite.apply(ctx, UriBuilder.fromUri(requestUri) + .replacePath(internalPrefix + requestUri.getRawPath()) .build()); ctx.setProperty(INTERNAL_PREFIX_PROPERTY, internalPrefix); } diff --git a/src/main/java/io/floci/aws/services/iam/IamActionRegistry.java b/src/main/java/io/floci/aws/services/iam/IamActionRegistry.java index b5dbc96a53..ed73d8dc56 100644 --- a/src/main/java/io/floci/aws/services/iam/IamActionRegistry.java +++ b/src/main/java/io/floci/aws/services/iam/IamActionRegistry.java @@ -299,7 +299,9 @@ public class IamActionRegistry { * The path the router matched, relative to the application root. A request no filter rewrote is matched * on its normalized path, still percent-encoded but with encoded unreserved characters such as {@code %2E} * decoded and dot segments removed, so {@code /functions/x/%2E%2E/fn} is {@code /functions/fn}; a request - * a filter rewrote is matched on its new path, decoded. {@link #sentPath} for a synthetic context. + * a filter rewrote is matched on its new path decoded once, which stays percent-encoded when the filter escaped + * it first ({@code RequestUriRewrite}, as {@link SignedServiceRoute} does). {@link #sentPath} for a synthetic + * context. */ static String rawPath(ContainerRequestContext ctx) { ResteasyReactiveRequestContext request = ResteasyRequests.of(ctx); @@ -358,7 +360,10 @@ public class IamActionRegistry { .orElse(path); } - /** Whether a route filter moved the request under an internal prefix, so the router matched its path decoded. */ + /** + * Whether a route filter moved the request under an internal prefix, so its path is matched on the model's routes + * without the prefix and a label may span the slashes it was sent with unencoded. + */ private static boolean movedUnderPrefix(ContainerRequestContext ctx) { return SignedServiceRoute.internalPrefix(ctx).isPresent(); } diff --git a/src/main/java/io/floci/aws/services/iam/IamRestRoutes.java b/src/main/java/io/floci/aws/services/iam/IamRestRoutes.java index 97fe04d83c..126c7a5ab6 100644 --- a/src/main/java/io/floci/aws/services/iam/IamRestRoutes.java +++ b/src/main/java/io/floci/aws/services/iam/IamRestRoutes.java @@ -38,12 +38,13 @@ import java.util.regex.Pattern; * a literal and a label, before a label, before a greedy label, then the route naming more of the query. * *

A request a route filter moved under an internal prefix ({@code SignedServiceRoute}) is matched on its path - * without the prefix, which is the path the model gives its operation. The router matched it decoded, so a label's - * value holds the slashes its encoding hid (a key ARN on the Payment Cryptography data plane), and the controller's - * route takes them: a path that matches no route with each label taking one segment is matched again with each - * label taking as many as its value spans. So is a request whose controller takes a label's value with the slashes - * it was sent with unencoded (a durable execution's ARN, an EKS access entry's principal), the route whose labels - * the path then gives the values the router gave the path parameters of the method it dispatched to being its own. + * without the prefix, which is the path the model gives its operation. The router matches it percent-encoded, as it + * was sent, so a label whose slashes are encoded stays one segment. A label sent with its slashes unencoded (a key + * ARN on the Payment Cryptography data plane) still reaches a controller route that takes them: a path that matches + * no route with each label taking one segment is matched again with each label taking as many as its value spans. + * So is a request whose controller takes a label's value with the slashes it was sent with unencoded (a durable + * execution's ARN, an EKS access entry's principal), the route whose labels the path then gives the values the + * router gave the path parameters of the method it dispatched to being its own. * *

An operation may have alternative routes, the bare paths a shared controller also serves it on, with where * the request carries the members the path no longer holds, and the alternative's own method when it is not the @@ -272,7 +273,7 @@ final class IamRestRoutes { /** * As {@link #operation}, for a path a route filter moved under an internal prefix, given without it: when it - * matches no route, its labels may span the slashes their decoded values hold. + * matches no route, its labels may span the slashes they were sent with unencoded. */ Optional operation(String scope, String method, String path, MultivaluedMap query, boolean movedUnderPrefix) { diff --git a/src/main/java/io/floci/aws/services/s3tables/S3TablesController.java b/src/main/java/io/floci/aws/services/s3tables/S3TablesController.java index 4ba7512b20..943453f442 100644 --- a/src/main/java/io/floci/aws/services/s3tables/S3TablesController.java +++ b/src/main/java/io/floci/aws/services/s3tables/S3TablesController.java @@ -26,7 +26,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -@Path("/") +@Path(S3TablesRouteFilter.INTERNAL_PREFIX) @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public class S3TablesController { diff --git a/src/main/java/io/floci/aws/services/s3tables/S3TablesRouteFilter.java b/src/main/java/io/floci/aws/services/s3tables/S3TablesRouteFilter.java new file mode 100644 index 0000000000..d201dab0d1 --- /dev/null +++ b/src/main/java/io/floci/aws/services/s3tables/S3TablesRouteFilter.java @@ -0,0 +1,43 @@ +package io.floci.aws.services.s3tables; + +import io.floci.aws.core.common.ResolvedServiceCatalog; +import io.floci.aws.core.common.SignedServiceRoute; +import jakarta.inject.Inject; +import jakarta.ws.rs.container.ContainerRequestContext; +import jakarta.ws.rs.container.ContainerRequestFilter; +import jakarta.ws.rs.container.PreMatching; +import jakarta.ws.rs.ext.Provider; + +import java.util.function.Predicate; + +/** + * Rewrites S3 Tables requests to an internal prefix before JAX-RS route matching. + * + *

S3 Tables' paths begin with a literal segment, and S3's path-style routes claim any first + * segment as a bucket, so a request for object {@code k} in bucket {@code tables} would otherwise + * reach ListTables. The rewrite is keyed on the SigV4 credential scope, so a request signed for + * S3 keeps its path and reaches S3: only a request signed for {@code s3tables} is moved. + */ +@Provider +@PreMatching +public class S3TablesRouteFilter implements ContainerRequestFilter { + + static final String INTERNAL_PREFIX = "/_s3tables"; + + private static final Predicate PUBLIC_PATHS = SignedServiceRoute.under("/buckets") + .or(SignedServiceRoute.under("/get-table")) + .or(SignedServiceRoute.under("/namespaces")) + .or(SignedServiceRoute.under("/tables")); + + private final SignedServiceRoute route; + + @Inject + public S3TablesRouteFilter(ResolvedServiceCatalog catalog) { + this.route = new SignedServiceRoute(catalog, "s3tables", INTERNAL_PREFIX, PUBLIC_PATHS); + } + + @Override + public void filter(ContainerRequestContext ctx) { + route.apply(ctx); + } +} diff --git a/src/test/java/io/floci/aws/services/s3tables/S3TablesIntegrationTest.java b/src/test/java/io/floci/aws/services/s3tables/S3TablesIntegrationTest.java index 9ce57fcced..750f794093 100644 --- a/src/test/java/io/floci/aws/services/s3tables/S3TablesIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/s3tables/S3TablesIntegrationTest.java @@ -13,7 +13,7 @@ import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.regex.Pattern; -import static io.restassured.RestAssured.given; +import static io.floci.aws.testing.S3TablesRequests.s3tables; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -46,7 +46,7 @@ class S3TablesIntegrationTest { @Test @Order(1) void createsTableBucketThroughTheS3TablesRoute() { - given() + s3tables() .urlEncodingEnabled(false) .contentType(JSON_CONTENT_TYPE) .body(""" @@ -63,7 +63,7 @@ class S3TablesIntegrationTest { @Test @Order(2) void managesNamespaceAndTableThroughEncodedBucketArnRoutes() { - given() + s3tables() .urlEncodingEnabled(false) .contentType(JSON_CONTENT_TYPE) .body(""" @@ -77,7 +77,7 @@ class S3TablesIntegrationTest { .body("namespace", contains(NAMESPACE)) .body("tableBucketARN", equalTo(BUCKET_ARN)); - versionToken = given() + versionToken = s3tables() .urlEncodingEnabled(false) .contentType(JSON_CONTENT_TYPE) .body(""" @@ -101,7 +101,7 @@ class S3TablesIntegrationTest { @Test @Order(3) void updatesMetadataLocationUsingOptimisticVersionTokens() { - String replacementToken = given() + String replacementToken = s3tables() .urlEncodingEnabled(false) .contentType(JSON_CONTENT_TYPE) .body(""" @@ -119,7 +119,7 @@ class S3TablesIntegrationTest { .extract() .path("versionToken"); - given() + s3tables() .urlEncodingEnabled(false) .contentType(JSON_CONTENT_TYPE) .body(""" @@ -140,7 +140,7 @@ class S3TablesIntegrationTest { @Test @Order(4) void roundTripsTableBucketPolicy() { - given() + s3tables() .urlEncodingEnabled(false) .contentType(JSON_CONTENT_TYPE) .body(""" @@ -151,7 +151,7 @@ class S3TablesIntegrationTest { .then() .statusCode(200); - given() + s3tables() .urlEncodingEnabled(false) .when() .get("/buckets/" + ENCODED_BUCKET_ARN + "/policy") @@ -164,7 +164,7 @@ class S3TablesIntegrationTest { @Test @Order(5) void roundTripsTypedMaintenanceConfigurationMaps() { - given() + s3tables() .urlEncodingEnabled(false) .contentType(JSON_CONTENT_TYPE) .body("{ \"value\": { \"status\": \"enabled\" } }") @@ -173,7 +173,7 @@ class S3TablesIntegrationTest { .then() .statusCode(200); - given() + s3tables() .urlEncodingEnabled(false) .when() .get("/buckets/" + ENCODED_BUCKET_ARN + "/maintenance") @@ -181,7 +181,7 @@ class S3TablesIntegrationTest { .statusCode(200) .body("configuration.icebergUnreferencedFileRemoval.status", equalTo("enabled")); - given() + s3tables() .urlEncodingEnabled(false) .contentType(JSON_CONTENT_TYPE) .body("{ \"value\": { \"status\": \"enabled\" } }") @@ -191,7 +191,7 @@ class S3TablesIntegrationTest { .then() .statusCode(200); - given() + s3tables() .urlEncodingEnabled(false) .when() .get("/tables/" + ENCODED_BUCKET_ARN + "/" + NAMESPACE + "/" + TABLE_NAME + "/maintenance") diff --git a/src/test/java/io/floci/aws/services/s3tables/S3TablesRouteIntegrationTest.java b/src/test/java/io/floci/aws/services/s3tables/S3TablesRouteIntegrationTest.java new file mode 100644 index 0000000000..80b7e5203e --- /dev/null +++ b/src/test/java/io/floci/aws/services/s3tables/S3TablesRouteIntegrationTest.java @@ -0,0 +1,58 @@ +package io.floci.aws.services.s3tables; + +import io.floci.aws.testing.AwsTestClients; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import software.amazon.awssdk.core.sync.RequestBody; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.model.NoSuchKeyException; +import software.amazon.awssdk.services.s3tables.S3TablesClient; +import software.amazon.awssdk.services.s3tables.model.CreateTableBucketResponse; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +class S3TablesRouteIntegrationTest { + + @ParameterizedTest + @ValueSource(strings = {"buckets", "get-table", "namespaces", "tables"}) + void pathStyleS3RequestsForABucketNamedAfterAnS3TablesRouteReachS3(String bucket) { + try (S3Client s3 = AwsTestClients.create(S3Client.builder().forcePathStyle(true), + "000000000000", Region.US_EAST_1)) { + s3.createBucket(r -> r.bucket(bucket)); + s3.headBucket(r -> r.bucket(bucket)); + s3.putObject(r -> r.bucket(bucket).key("k"), RequestBody.fromString("hello")); + + assertEquals("hello", s3.getObjectAsBytes(r -> r.bucket(bucket).key("k")).asUtf8String()); + assertEquals(1, s3.listObjectsV2(r -> r.bucket(bucket)).keyCount()); + s3.deleteObjects(r -> r.bucket(bucket).delete(d -> d.objects(o -> o.key("k")))); + assertThrows(NoSuchKeyException.class, () -> s3.getObject(r -> r.bucket(bucket).key("k"))); + s3.deleteBucket(r -> r.bucket(bucket)); + } + } + + @ParameterizedTest + @ValueSource(strings = {"buckets", "tables"}) + void s3TablesRequestsStillReachS3TablesWhileAnS3BucketHasTheSameName(String name) { + try (S3Client s3 = AwsTestClients.create(S3Client.builder().forcePathStyle(true), + "000000000000", Region.US_EAST_1); + S3TablesClient tables = AwsTestClients.create(S3TablesClient.builder(), + "000000000000", Region.US_EAST_1)) { + s3.createBucket(r -> r.bucket(name)); + CreateTableBucketResponse created = tables.createTableBucket(r -> r.name("route-" + name)); + try { + assertEquals(created.arn(), tables.getTableBucket(r -> r.tableBucketARN(created.arn())).arn()); + assertTrue(tables.listTableBuckets(r -> { }).tableBuckets().stream() + .anyMatch(b -> b.arn().equals(created.arn()))); + s3.headBucket(r -> r.bucket(name)); + } finally { + tables.deleteTableBucket(r -> r.tableBucketARN(created.arn())); + s3.deleteBucket(r -> r.bucket(name)); + } + } + } +} diff --git a/src/test/java/io/floci/aws/services/s3tables/S3TablesWarehouseIntegrationTest.java b/src/test/java/io/floci/aws/services/s3tables/S3TablesWarehouseIntegrationTest.java index b2079faa52..8df2b88a65 100644 --- a/src/test/java/io/floci/aws/services/s3tables/S3TablesWarehouseIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/s3tables/S3TablesWarehouseIntegrationTest.java @@ -16,7 +16,7 @@ import software.amazon.awssdk.services.s3.model.NoSuchBucketException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import static io.restassured.RestAssured.given; +import static io.floci.aws.testing.S3TablesRequests.s3tables; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.matchesPattern; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -44,12 +44,12 @@ class S3TablesWarehouseIntegrationTest { @BeforeEach void createTable() { String bucketName = "warehouse-" + System.nanoTime(); - String bucketArn = given().contentType(JSON).body("{\"name\": \"" + bucketName + "\"}") + String bucketArn = s3tables().contentType(JSON).body("{\"name\": \"" + bucketName + "\"}") .when().put("/buckets").then().statusCode(200).extract().path("arn"); encodedBucketArn = URLEncoder.encode(bucketArn, StandardCharsets.UTF_8); - given().urlEncodingEnabled(false).contentType(JSON).body("{\"namespace\": [\"analytics\"]}") + s3tables().urlEncodingEnabled(false).contentType(JSON).body("{\"namespace\": [\"analytics\"]}") .when().put("/namespaces/" + encodedBucketArn).then().statusCode(200); - given().urlEncodingEnabled(false).contentType(JSON).body("{\"name\": \"events\", \"format\": \"ICEBERG\"}") + s3tables().urlEncodingEnabled(false).contentType(JSON).body("{\"name\": \"events\", \"format\": \"ICEBERG\"}") .when().put("/tables/" + encodedBucketArn + "/analytics").then().statusCode(200); s3 = AwsTestClients.create(S3Client.builder().forcePathStyle(true), "000000000000", Region.US_EAST_1); } @@ -60,7 +60,7 @@ class S3TablesWarehouseIntegrationTest { } private String warehouseLocation() { - return given().urlEncodingEnabled(false) + return s3tables().urlEncodingEnabled(false) .when().get("/get-table?tableBucketARN=" + encodedBucketArn + "&namespace=analytics&name=events") .then().statusCode(200) .body("warehouseLocation", matchesPattern("s3://[0-9a-f-]{36}--table-s3")) @@ -76,7 +76,7 @@ class S3TablesWarehouseIntegrationTest { s3.putObject(r -> r.bucket(bucket).key("metadata/00000-a.metadata.json"), RequestBody.fromString("{}")); - given().urlEncodingEnabled(false) + s3tables().urlEncodingEnabled(false) .when().get("/tables/" + encodedBucketArn + "/analytics/events/metadata-location") .then().statusCode(200) .body("warehouseLocation", equalTo(warehouse)); @@ -99,7 +99,7 @@ class S3TablesWarehouseIntegrationTest { s3.putObject(r -> r.bucket(bucket).key("data/part-0.parquet"), RequestBody.fromString("v2")); s3.deleteObject(r -> r.bucket(bucket).key("data/part-0.parquet")); - given().urlEncodingEnabled(false) + s3tables().urlEncodingEnabled(false) .when().delete("/tables/" + encodedBucketArn + "/analytics/events") .then().statusCode(204); @@ -111,7 +111,7 @@ class S3TablesWarehouseIntegrationTest { String bucket = warehouseLocation().substring("s3://".length()); s3.putObject(r -> r.bucket(bucket).key("data/part-0.parquet"), RequestBody.fromString("x")); - given().urlEncodingEnabled(false) + s3tables().urlEncodingEnabled(false) .when().delete("/tables/" + encodedBucketArn + "/analytics/events") .then().statusCode(204); diff --git a/src/test/java/io/floci/aws/testing/S3TablesRequests.java b/src/test/java/io/floci/aws/testing/S3TablesRequests.java new file mode 100644 index 0000000000..d1be7211d5 --- /dev/null +++ b/src/test/java/io/floci/aws/testing/S3TablesRequests.java @@ -0,0 +1,16 @@ +package io.floci.aws.testing; + +import io.restassured.specification.RequestSpecification; + +import static io.restassured.RestAssured.given; + +/** One S3 Tables REST JSON request, signed for s3tables so it reaches S3 Tables rather than S3. */ +public final class S3TablesRequests { + + private S3TablesRequests() {} + + public static RequestSpecification s3tables() { + return given().header("Authorization", + "AWS4-HMAC-SHA256 Credential=000000000000/20260101/us-east-1/s3tables/aws4_request"); + } +}