mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(emr-containers): list only the kernels a managed endpoint's session token started (#9251)
A managed endpoint's gateway listed every kernel it ran to any session token, where ListKernels is "Request that the interactive endpoint list the kernels that have been previously started using the same session token" (EMR on EKS Development Guide, "Monitoring interactive endpoints"). - The proxy passes the gateway the SHA-256 of the token a request carries, in a header of Floci's own, in place of the token, and drops one the client sent. - The gateway program's kernel manager records the digest a kernel was started with, whether the kernels API or the sessions API started it, and drops the record when the kernel goes, whether a request, the culler or the kernel's death removes it. - GET /api/kernels answers with the kernels started with the request's digest alone, and GET /api/sessions with the sessions holding them. Any of the endpoint's tokens still reaches a kernel or a session by its id, as the guide scopes only the listing. - The nightly check starts a session beside its PySpark kernel, lists both kernels and the session with the token that started them, and nothing with another token of the endpoint.
This commit is contained in:
1 parent
711d9ea887
commit
3f2cd7198e
5 files changed
+159
-25
No files matched your search
+43
-7
@@ -35,6 +35,8 @@ import java.net.http.HttpResponse;
|
||||
import java.net.http.WebSocket;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
@@ -51,7 +53,8 @@ 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 runs code on Floci's S3 over the kernel's
|
||||
* {@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}.
|
||||
*
|
||||
* <p>A nightly check: the release's notebook image is about 6 GB.
|
||||
@@ -128,12 +131,7 @@ class EmrContainersGatewayNightlyIntegrationTest {
|
||||
String path = URI.create(serverUrl).getPath();
|
||||
assertEquals(EmrContainersService.GATEWAY_PATH + endpointId, path);
|
||||
String gateway = "http://localhost:" + RestAssured.port + path;
|
||||
String token = emr().body("""
|
||||
{"executionRoleArn": "%s", "credentialType": "TOKEN"}
|
||||
""".formatted(ROLE))
|
||||
.post("/virtualclusters/" + virtualClusterId + "/endpoints/" + endpointId + "/credentials")
|
||||
.then().statusCode(200)
|
||||
.extract().path("credentials.token");
|
||||
String token = sessionToken(virtualClusterId, endpointId);
|
||||
|
||||
HttpClient http = HttpClient.newHttpClient();
|
||||
assertEquals(401, http.send(HttpRequest.newBuilder(URI.create(gateway + "/api/kernelspecs")).build(),
|
||||
@@ -152,6 +150,25 @@ class EmrContainersGatewayNightlyIntegrationTest {
|
||||
""")).build(), HttpResponse.BodyHandlers.ofString());
|
||||
assertEquals(201, started.statusCode(), started.body());
|
||||
String kernelId = JSON.readTree(started.body()).path("id").textValue();
|
||||
// ListKernels lists the kernels started with the same session token, through the sessions API too, and the
|
||||
// sessions listing the sessions holding them.
|
||||
HttpResponse<String> created = http.send(HttpRequest.newBuilder(URI.create(gateway + "/api/sessions"))
|
||||
.header("Authorization", "token " + token)
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString("""
|
||||
{"path": "nightly.ipynb", "type": "notebook", "kernel": {"name": "python3"}}
|
||||
""")).build(), HttpResponse.BodyHandlers.ofString());
|
||||
assertEquals(201, created.statusCode(), created.body());
|
||||
JsonNode notebook = JSON.readTree(created.body());
|
||||
assertEquals(List.of(kernelId, notebook.at("/kernel/id").textValue()), ids(http, gateway + "/api/kernels",
|
||||
token));
|
||||
assertEquals(List.of(notebook.path("id").textValue()), ids(http, gateway + "/api/sessions", token));
|
||||
String another = sessionToken(virtualClusterId, endpointId);
|
||||
assertEquals(List.of(), ids(http, gateway + "/api/kernels", another), "another session token of the endpoint");
|
||||
assertEquals(List.of(), ids(http, gateway + "/api/sessions", another), "another session token of the endpoint");
|
||||
assertEquals(204, http.send(HttpRequest.newBuilder(URI.create(gateway + "/api/sessions/"
|
||||
+ notebook.path("id").textValue())).header("Authorization", "token " + token).DELETE().build(),
|
||||
HttpResponse.BodyHandlers.discarding()).statusCode());
|
||||
|
||||
Messages messages = new Messages();
|
||||
WebSocket socket = http.newWebSocketBuilder().header("Authorization", "token " + token)
|
||||
@@ -203,6 +220,25 @@ class EmrContainersGatewayNightlyIntegrationTest {
|
||||
return null;
|
||||
}
|
||||
|
||||
// The ids the listing at url answers a request carrying token with.
|
||||
private static List<String> ids(HttpClient http, String url, String token) throws Exception {
|
||||
HttpResponse<String> listed = http.send(HttpRequest.newBuilder(URI.create(url))
|
||||
.header("Authorization", "token " + token).build(), HttpResponse.BodyHandlers.ofString());
|
||||
assertEquals(200, listed.statusCode(), listed.body());
|
||||
List<String> ids = new ArrayList<>();
|
||||
JSON.readTree(listed.body()).forEach(kernel -> ids.add(kernel.path("id").textValue()));
|
||||
return ids;
|
||||
}
|
||||
|
||||
private static String sessionToken(String virtualClusterId, String endpointId) {
|
||||
return emr().body("""
|
||||
{"executionRoleArn": "%s", "credentialType": "TOKEN"}
|
||||
""".formatted(ROLE))
|
||||
.post("/virtualclusters/" + virtualClusterId + "/endpoints/" + endpointId + "/credentials")
|
||||
.then().statusCode(200)
|
||||
.extract().path("credentials.token");
|
||||
}
|
||||
|
||||
private String virtualCluster() {
|
||||
String eksCluster = "emr-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
EksRequests.eks(ACCOUNT, REGION)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.floci.aws.services.emrcontainers;
|
||||
|
||||
import io.floci.aws.core.common.Digests;
|
||||
import io.floci.aws.core.common.ServiceConfigAccess;
|
||||
import io.floci.aws.core.common.SparkConnectTargets;
|
||||
import io.floci.aws.core.common.WebSocketUpgrades;
|
||||
@@ -73,6 +74,7 @@ class EmrContainersGatewayProxyTest {
|
||||
.putHeader("content-type", "application/json")
|
||||
.end(new JsonObject().put("method", request.method().name()).put("uri", request.uri())
|
||||
.put("authorization", request.getHeader("Authorization"))
|
||||
.put("session", request.getHeader(EmrContainersGateways.SESSION_HEADER))
|
||||
.put("body", body.toString()).encode()));
|
||||
}).listen(0, "127.0.0.1"));
|
||||
service = mock(EmrContainersService.class);
|
||||
@@ -112,6 +114,17 @@ class EmrContainersGatewayProxyTest {
|
||||
awaitCount(requestsEnded, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void gatewayKnowsTheSessionTokenByItsDigestAlone() throws Exception {
|
||||
// Floci's digest replaces one the client sends, which would let it list another token's kernels.
|
||||
Answer answer = request(HttpMethod.GET, BASE + "/api/kernels", "token " + TOKEN, null,
|
||||
Digests.sha256Hex("another-session-token"));
|
||||
|
||||
assertEquals(201, answer.status);
|
||||
assertEquals(Digests.sha256Hex(TOKEN), new JsonObject(answer.body).getString("session"));
|
||||
awaitCount(requestsEnded, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryIsReadAsTheGatewayReadsIt() throws Exception {
|
||||
// Its last token parameter is the one checked, and a malformed escape stays as it came.
|
||||
@@ -189,11 +202,19 @@ class EmrContainersGatewayProxyTest {
|
||||
}
|
||||
|
||||
private Answer request(HttpMethod method, String uri, String authorization, String body) throws Exception {
|
||||
return request(method, uri, authorization, body, null);
|
||||
}
|
||||
|
||||
private Answer request(HttpMethod method, String uri, String authorization, String body, String session)
|
||||
throws Exception {
|
||||
RequestOptions options = new RequestOptions().setMethod(method).setHost("127.0.0.1")
|
||||
.setPort(floci.actualPort()).setURI(uri);
|
||||
if (authorization != null) {
|
||||
options.addHeader("Authorization", authorization);
|
||||
}
|
||||
if (session != null) {
|
||||
options.addHeader(EmrContainersGateways.SESSION_HEADER, session);
|
||||
}
|
||||
// The answer's body is read as its head arrives, on the event loop: asked for later, it may have gone by.
|
||||
return await(client.request(options)
|
||||
.compose(request -> body == null ? request.send() : request.send(Buffer.buffer(body)))
|
||||
|
||||
Reference in new issue
Block a user