feat(emr-containers): let a browser's CORS preflight reach a managed endpoint's gateway (#9283)

A browser sends its CORS preflight (OPTIONS) without the session token,
and Jupyter Enterprise Gateway 2.6.0's TokenAuthorizationMixin checks
none for it ("if server_token and not self.request.method ==
'OPTIONS'"). Floci's proxy asked one of every request, so a preflight
to a managed endpoint's serverUrl got 401.

- SessionServers.routeWithoutToken routes a call that carries no token
  to a running server, in that server's own account and Region, as a
  call of its session: empty where no server runs, refused while the
  owner refuses calls.
- EmrContainersService.gatewayPreflightRoute is that route for an
  endpoint's gateway, and the proxy takes it for an OPTIONS request,
  passing the gateway no session header. An endpoint with no gateway
  running still answers 401, one that is not ACTIVE 503.
- The nightly check sends a preflight without a token and gets the
  gateway's 200 with its Access-Control-Allow-Methods.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-23 12:25:11 -04:00
1 parent 20e0ca6946
commit ac844769fb
8 files changed
+108 -16

No files matched your search

@@ -53,12 +53,13 @@ import static org.junit.jupiter.api.Assertions.fail;
/**
* An EMR on EKS managed endpoint, run for real in the {@code docker} runner mode: it runs a Jupyter Enterprise Gateway
* in its release's {@code notebook-spark} image from Amazon ECR Public, and a Jupyter client reaches it at its
* {@code serverUrl} with a session token, starts a PySpark kernel there, and a session with a kernel of its own, which
* the kernels and sessions listings show that token alone, and runs code on Floci's S3 over the PySpark kernel's
* WebSocket, as the guide's "Example: Deploy a JupyterLab notebook" does through {@code JUPYTER_GATEWAY_URL}, then
* changes its Spark session's settings with {@code %%configure}, as "Modifying PySpark session parameters" does,
* and runs code on Floci's S3 in the "Apache Spark with Scala" kernel too. An
* endpoint whose {@code jeg-config} sets a short idle timeout culls a kernel left idle.
* {@code serverUrl} with a session token, as a browser's CORS preflight does without one. The client starts a
* PySpark kernel there, and a session with a kernel of its own, which the kernels and sessions listings show that
* token alone, and runs code on Floci's S3 over the PySpark kernel's WebSocket, as the guide's "Example: Deploy a
* JupyterLab notebook" does through {@code JUPYTER_GATEWAY_URL}, then changes its Spark session's settings with
* {@code %%configure}, as "Modifying PySpark session parameters" does, and runs code on Floci's S3 in the "Apache
* Spark with Scala" kernel too. An endpoint whose {@code jeg-config} sets a short idle timeout culls a kernel left
* idle.
*
* <p>A nightly check: the release's notebook image is about 6 GB.
*/
@@ -122,6 +123,14 @@ class EmrContainersGatewayNightlyIntegrationTest {
HttpClient http = HttpClient.newHttpClient();
assertEquals(401, http.send(HttpRequest.newBuilder(URI.create(gateway + "/api/kernelspecs")).build(),
HttpResponse.BodyHandlers.discarding()).statusCode(), "a request without the session token");
// A browser's CORS preflight carries no token, and the gateway answers it.
HttpResponse<Void> preflight = http.send(HttpRequest.newBuilder(URI.create(gateway + "/api/kernels"))
.method("OPTIONS", HttpRequest.BodyPublishers.noBody())
.header("Origin", "http://notebook.test").header("Access-Control-Request-Method", "POST").build(),
HttpResponse.BodyHandlers.discarding());
assertEquals(200, preflight.statusCode());
assertTrue(preflight.headers().firstValue("Access-Control-Allow-Methods").orElse("").contains("POST"),
preflight.headers().toString());
HttpResponse<String> specs = http.send(HttpRequest.newBuilder(URI.create(gateway + "/api/kernelspecs"))
.header("Authorization", "token " + token).build(), HttpResponse.BodyHandlers.ofString());
assertEquals(200, specs.statusCode(), specs.body());
@@ -81,6 +81,9 @@ class EmrContainersGatewayProxyTest {
when(service.gatewayRoute(anyString(), any())).thenReturn(Optional.empty());
when(service.gatewayRoute(ENDPOINT, TOKEN)).thenAnswer(invocation -> Optional.of(
SparkConnectTargets.Route.to("127.0.0.1", gateway.actualPort(), requestsEnded::incrementAndGet)));
when(service.gatewayPreflightRoute(anyString())).thenReturn(Optional.empty());
when(service.gatewayPreflightRoute(ENDPOINT)).thenAnswer(invocation -> Optional.of(
SparkConnectTargets.Route.to("127.0.0.1", gateway.actualPort(), requestsEnded::incrementAndGet)));
ServiceConfigAccess serviceConfigAccess = mock(ServiceConfigAccess.class);
when(serviceConfigAccess.isEnabled(eq(EmrContainersService.SERVICE))).thenReturn(true);
EmrContainersGatewayProxy proxy = new EmrContainersGatewayProxy(service, serviceConfigAccess, vertx);
@@ -125,6 +128,22 @@ class EmrContainersGatewayProxyTest {
awaitCount(requestsEnded, 1);
}
@Test
void corsPreflightGoesToItsGatewayWithoutAToken() throws Exception {
// A browser's preflight carries no token, and Jupyter Enterprise Gateway checks none for one.
Answer preflight = request(HttpMethod.OPTIONS, BASE + "/api/kernels", null, null);
Answer elsewhere = request(HttpMethod.OPTIONS, EmrContainersService.GATEWAY_PATH + "ep0000000000000/api",
null, null);
assertEquals(201, preflight.status);
JsonObject got = new JsonObject(preflight.body);
assertEquals("OPTIONS", got.getString("method"));
assertEquals("/api/kernels", got.getString("uri"));
assertNull(got.getString("session"), "a preflight is no session token's");
assertEquals(401, elsewhere.status, "an endpoint with no gateway running");
awaitCount(requestsEnded, 1);
}
@Test
void queryIsReadAsTheGatewayReadsIt() throws Exception {
// Its last token parameter is the one checked, and a malformed escape stays as it came.
@@ -1403,6 +1403,22 @@ class EmrContainersServiceTest {
runner.stop(otherId);
}
@Test
void corsPreflightReachesAnActiveEndpointsGatewayWithoutAToken() {
GatewayRunner runner = new GatewayRunner();
EmrContainersService service = gatewayService(runner, stores());
String clusterId = virtualCluster(service, EKS_CLUSTER);
String endpointId = gatewayEndpoint(service, clusterId);
awaitEndpoint(service, clusterId, endpointId, "ACTIVE");
SparkConnectTargets.Route route = service.gatewayPreflightRoute(endpointId).orElseThrow();
assertEquals("127.0.0.1", route.host());
assertEquals(GatewayRunner.PORT, route.port());
route.ended().run();
assertTrue(service.gatewayPreflightRoute("ep0000000000000").isEmpty(), "no gateway runs for it");
runner.stop(endpointId);
}
@Test
void deletedDockerEndpointStopsItsGatewayAndItsTokensGo() {
GatewayRunner runner = new GatewayRunner();
@@ -83,6 +83,21 @@ class SessionServersTest {
assertEquals(List.of("answering", "busy", "idle", "busy"), owner.events);
}
@Test
void aCallWithoutATokenReachesARunningServerAsACallOfItsSession() {
answeringServer();
Route route = servers.routeWithoutToken(SESSION, owner).orElseThrow();
route.ended().run();
assertEquals(List.of("answering", "busy", "idle"), owner.events);
assertTrue(servers.routeWithoutToken("session-2", owner).isEmpty(), "no server runs for it");
owner.refusal = () -> "Session " + SESSION + " is TERMINATING.";
Route refused = servers.routeWithoutToken(SESSION, owner).orElseThrow();
assertEquals(Route.UNAVAILABLE, refused.status());
assertEquals("Session " + SESSION + " is TERMINATING.", refused.refusal());
}
@Test
void aServersEntryGoesWhenItStopsEvenWhereItsOwnerFailsToHearIt() {
answeringServer();