diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoWellKnownController.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoWellKnownController.java index 9f60676b02..a56352abea 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoWellKnownController.java +++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoWellKnownController.java @@ -16,11 +16,21 @@ import java.util.Base64; * Exposes Cognito well-known endpoints. * The JWKS endpoint allows downstream services to verify JWTs issued by Floci Cognito pools. * Path mirrors real AWS: /{userPoolId}/.well-known/jwks.json + * + *
The {@code poolId} path parameter is constrained to {@link #POOL_ID_PATTERN}
+ * so that S3 object keys like {@code / Discriminator: real Cognito UserPool IDs always contain an underscore
+ * (.well-known/ must NOT collide with the Cognito
+ * well-known OIDC endpoints (/{poolId}/.well-known/jwks.json and
+ * /{poolId}/.well-known/openid-configuration).
+ *
+ * <region>_<random>, e.g. us-east-1_xxx),
+ * while AWS S3 bucket names forbid underscores. Any first path segment without
+ * an underscore therefore cannot be a Cognito pool id and must be routed to
+ * S3.
+ */
+@QuarkusTest
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+class S3WellKnownKeyIntegrationTest {
+
+ private static final String BUCKET = "test-bucket-jwks-967";
+ private static final String JWKS_KEY = ".well-known/jwks.json";
+ private static final String SIBLING_KEY = ".well-known/test.json";
+ private static final String JWKS_BODY = "{\"keys\":[{\"kty\":\"RSA\"}]}";
+ private static final String SIBLING_BODY = "{\"hello\":\"world\"}";
+
+ @Test
+ @Order(0)
+ void createBucket() {
+ given()
+ .when()
+ .put("/" + BUCKET)
+ .then()
+ .statusCode(200);
+ }
+
+ @Test
+ @Order(1)
+ void putAndGetSiblingWellKnownObject() {
+ given()
+ .header("Content-Type", "application/json")
+ .body(SIBLING_BODY)
+ .when()
+ .put("/" + BUCKET + "/" + SIBLING_KEY)
+ .then()
+ .statusCode(200);
+
+ given()
+ .when()
+ .get("/" + BUCKET + "/" + SIBLING_KEY)
+ .then()
+ .statusCode(200)
+ .body(equalTo(SIBLING_BODY));
+ }
+
+ @Test
+ @Order(2)
+ void putAndGetJwksObject_mustNotBeRoutedToCognito() {
+ given()
+ .header("Content-Type", "application/json")
+ .body(JWKS_BODY)
+ .when()
+ .put("/" + BUCKET + "/" + JWKS_KEY)
+ .then()
+ .statusCode(200);
+
+ given()
+ .when()
+ .get("/" + BUCKET + "/" + JWKS_KEY)
+ .then()
+ .statusCode(200)
+ .body(equalTo(JWKS_BODY));
+ }
+
+ @Test
+ @Order(3)
+ void getJwksObject_mustNotReturnCognitoErrorBody() {
+ String body = given()
+ .when()
+ .get("/" + BUCKET + "/" + JWKS_KEY)
+ .then()
+ .extract().asString();
+
+ if (body.contains("ResourceNotFoundException") || body.contains("User pool not found")) {
+ throw new AssertionError(
+ "GET /" + BUCKET + "/" + JWKS_KEY
+ + " was routed to Cognito instead of S3. Response: " + body);
+ }
+ }
+
+ @Test
+ @Order(4)
+ void getOpenIdConfigurationObject_mustNotBeRoutedToCognito() {
+ String openIdKey = ".well-known/openid-configuration";
+ String openIdBody = "{\"issuer\":\"custom\"}";
+
+ given()
+ .header("Content-Type", "application/json")
+ .body(openIdBody)
+ .when()
+ .put("/" + BUCKET + "/" + openIdKey)
+ .then()
+ .statusCode(200);
+
+ given()
+ .when()
+ .get("/" + BUCKET + "/" + openIdKey)
+ .then()
+ .statusCode(200)
+ .body(containsString("custom"));
+ }
+}