mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
20e0ca6946
commit
ac844769fb
8 files changed
+108
-16
No files matched your search
@@ -44,7 +44,9 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
* {@code token} parameter, or an {@code Authorization: token <token>} header. One without such a token gets that
|
||||
* gateway's answer, 401 with {@code {"reason": "Unauthorized", "message": ""}}; one whose endpoint is not ACTIVE,
|
||||
* 503, its message saying why. The gateway gets the token's SHA-256 in place of the token, in
|
||||
* {@link EmrContainersGateways#SESSION_HEADER}, to list a session token's kernels by.
|
||||
* {@link EmrContainersGateways#SESSION_HEADER}, to list a session token's kernels by. A CORS preflight
|
||||
* ({@code OPTIONS}), which a browser sends without the token, goes on without one, as that gateway's
|
||||
* TokenAuthorizationMixin checks none for it.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class EmrContainersGatewayProxy {
|
||||
@@ -94,11 +96,13 @@ public class EmrContainersGatewayProxy {
|
||||
int slash = rest.indexOf('/');
|
||||
String endpointId = slash < 0 ? rest : rest.substring(0, slash);
|
||||
String path = slash < 0 ? "/" : rest.substring(slash);
|
||||
String token = token(request);
|
||||
boolean preflight = request.method() == HttpMethod.OPTIONS;
|
||||
String token = preflight ? null : token(request);
|
||||
request.pause();
|
||||
// A service reads its storage to route a request, so off the event loop.
|
||||
vertx.<Optional<SparkConnectTargets.Route>>executeBlocking(() -> service.gatewayRoute(endpointId, token),
|
||||
false).onComplete(routed -> {
|
||||
vertx.<Optional<SparkConnectTargets.Route>>executeBlocking(() -> preflight
|
||||
? service.gatewayPreflightRoute(endpointId) : service.gatewayRoute(endpointId, token), false)
|
||||
.onComplete(routed -> {
|
||||
if (routed.failed()) {
|
||||
LOG.warnv(routed.cause(), "A request to endpoint {0}'s gateway could not be routed",
|
||||
endpointId);
|
||||
@@ -113,7 +117,8 @@ public class EmrContainersGatewayProxy {
|
||||
refuse(request, 503, "Service Unavailable", refused.refusal());
|
||||
}
|
||||
} else {
|
||||
forward(request, path + query(request), Digests.sha256Hex(token), routed.result().get());
|
||||
forward(request, path + query(request), preflight ? null : Digests.sha256Hex(token),
|
||||
routed.result().get());
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -179,7 +184,9 @@ public class EmrContainersGatewayProxy {
|
||||
options.addHeader(header.getKey(), header.getValue());
|
||||
}
|
||||
}
|
||||
options.addHeader(EmrContainersGateways.SESSION_HEADER, session);
|
||||
if (session != null) {
|
||||
options.addHeader(EmrContainersGateways.SESSION_HEADER, session);
|
||||
}
|
||||
client.request(options).onComplete(opened -> {
|
||||
if (opened.failed()) {
|
||||
refuse(request, 502, "Bad Gateway",
|
||||
|
||||
@@ -769,6 +769,15 @@ public class EmrContainersService implements Resettable, StoredResourceArns {
|
||||
: gateways.servers().route(token, endpointId, gatewayOwner);
|
||||
}
|
||||
|
||||
/**
|
||||
* Where a CORS preflight to endpoint {@code endpointId}'s gateway goes, which Jupyter Enterprise Gateway 2.6.0's
|
||||
* TokenAuthorizationMixin takes without a token: empty where no gateway of this process runs, refused while the
|
||||
* endpoint is not ACTIVE, else its gateway. Called off the event loop, as the account and Region of none.
|
||||
*/
|
||||
public Optional<SparkConnectTargets.Route> gatewayPreflightRoute(String endpointId) {
|
||||
return gateways == null ? Optional.empty() : gateways.servers().routeWithoutToken(endpointId, gatewayOwner);
|
||||
}
|
||||
|
||||
/**
|
||||
* The endpoint as it stands, stored: in the docker runner mode, one whose gateway no thread of this process runs,
|
||||
* as after a restart, has ended, TERMINATED_WITH_ERRORS, or TERMINATED where it was being deleted.
|
||||
|
||||
@@ -246,6 +246,23 @@ public final class SessionServers {
|
||||
return Optional.of(RequestScopes.callAs(grant.accountId(), grant.region(), () -> callStarted(grant, owner)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Where a call to session {@code sessionId}'s server that carries no token goes, as a server takes a CORS
|
||||
* preflight without one: empty where no thread of this process runs that server; refused while {@code owner} says
|
||||
* the session takes no calls; else its server, the call counting as one the session takes until it ends.
|
||||
*/
|
||||
public Optional<Route> routeWithoutToken(String sessionId, Owner owner) {
|
||||
Server server;
|
||||
synchronized (this) {
|
||||
server = servers.get(sessionId);
|
||||
}
|
||||
if (server == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
Grant grant = new Grant(server.accountId, server.region, sessionId, Instant.MAX);
|
||||
return Optional.of(RequestScopes.callAs(server.accountId, server.region, () -> callStarted(grant, owner)));
|
||||
}
|
||||
|
||||
/** Checks the session at {@code at} by {@code check}, run as its account and Region, in place of any it had. */
|
||||
public void checkAt(String sessionId, Instant at, Runnable check) {
|
||||
if (checks == null) {
|
||||
|
||||
Reference in new issue
Block a user