-
Notifications
You must be signed in to change notification settings - Fork 330
Allow realm context resolution to execute blocking calls #1591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
74081be
a2657ec
dc96274
0508b4f
52b1ff8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.polaris.service.quarkus.context; | ||
|
|
||
| import io.smallrye.common.vertx.ContextLocals; | ||
| import io.smallrye.mutiny.Uni; | ||
| import jakarta.inject.Inject; | ||
| import jakarta.ws.rs.container.ContainerRequestContext; | ||
| import jakarta.ws.rs.core.MediaType; | ||
| import jakarta.ws.rs.core.Response; | ||
| import org.apache.iceberg.rest.responses.ErrorResponse; | ||
| import org.apache.polaris.service.config.PolarisFilterPriorities; | ||
| import org.apache.polaris.service.context.RealmContextResolver; | ||
| import org.jboss.resteasy.reactive.server.ServerRequestFilter; | ||
|
|
||
| public class RealmContextFilter { | ||
|
|
||
| public static final String REALM_CONTEXT_KEY = "realmContext"; | ||
|
|
||
| @Inject RealmContextResolver realmContextResolver; | ||
|
|
||
| @ServerRequestFilter(preMatching = true, priority = PolarisFilterPriorities.REALM_CONTEXT_FILTER) | ||
| public Uni<Response> resolveRealmContext(ContainerRequestContext rc) { | ||
| return Uni.createFrom() | ||
| .completionStage( | ||
| () -> | ||
| realmContextResolver.resolveRealmContext( | ||
| rc.getUriInfo().getRequestUri().toString(), | ||
| rc.getMethod(), | ||
| rc.getUriInfo().getPath(), | ||
| rc.getHeaders()::getFirst)) | ||
| .onItem() | ||
| .invoke(realmContext -> rc.setProperty(REALM_CONTEXT_KEY, realmContext)) | ||
| .invoke(realmContext -> ContextLocals.put(REALM_CONTEXT_KEY, realmContext)) | ||
| .onItemOrFailure() | ||
| .transform((realmContext, error) -> error == null ? null : errorResponse(error)); | ||
| } | ||
|
|
||
| private static Response errorResponse(Throwable error) { | ||
| return Response.status(Response.Status.NOT_FOUND) | ||
| .type(MediaType.APPLICATION_JSON_TYPE) | ||
| .entity( | ||
| ErrorResponse.builder() | ||
| .responseCode(Response.Status.NOT_FOUND.getStatusCode()) | ||
| .withMessage( | ||
| error.getMessage() != null ? error.getMessage() : "Missing or invalid realm") | ||
| .withType("MissingOrInvalidRealm") | ||
| .build()) | ||
| .build(); | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,22 +19,45 @@ | |
| package org.apache.polaris.service.context; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.CompletionStage; | ||
| import java.util.function.Function; | ||
| import org.apache.commons.collections.map.CaseInsensitiveMap; | ||
| import org.apache.polaris.core.context.RealmContext; | ||
|
|
||
| /** | ||
| * An interface for resolving the realm context for a given request. | ||
| * | ||
| * <p>General implementation guidance: | ||
| * | ||
| * <p>Methods in this class should not block the calling thread. If the realm resolution process is | ||
| * blocking, it should be done in a separate thread. | ||
| * | ||
| * <p>In the case of an error during realm resolution, the {@link CompletionStage} should complete | ||
| * exceptionally; methods should not throw exceptions directly. | ||
|
Comment on lines
+35
to
+36
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [doubt] can you please elaborate on, who throws the exception ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @singhpk234 as usual for asynchronous code, invoking |
||
| * | ||
| * <p>The realm resolution takes place in the very early stages of the request processing pipeline, | ||
| * and before any authentication or authorization checks are performed. Implementations should be | ||
| * careful with the use of any blocking operations, as they may lead to performance issues or | ||
| * deadlocks, especially in public environments. | ||
| */ | ||
| public interface RealmContextResolver { | ||
|
|
||
| /** | ||
| * Resolves the realm context for the given request. | ||
| * Resolves the realm context for the given request, and returns a {@link CompletionStage} that | ||
| * completes with the resolved realm context. | ||
| * | ||
| * @return the resolved realm context | ||
| * @throws UnresolvableRealmContextException if the realm context cannot be resolved | ||
| * @return a {@link CompletionStage} that completes with the resolved realm context | ||
| */ | ||
| RealmContext resolveRealmContext( | ||
| CompletionStage<RealmContext> resolveRealmContext( | ||
| String requestURL, String method, String path, Function<String, String> headers); | ||
|
|
||
| default RealmContext resolveRealmContext( | ||
| /** | ||
| * Resolves the realm context for the given request, and returns a {@link CompletionStage} that | ||
| * completes with the resolved realm context. | ||
| * | ||
| * @return a {@link CompletionStage} that completes with the resolved realm context | ||
| */ | ||
| default CompletionStage<RealmContext> resolveRealmContext( | ||
| String requestURL, String method, String path, Map<String, String> headers) { | ||
| CaseInsensitiveMap caseInsensitiveMap = new CaseInsensitiveMap(headers); | ||
| return resolveRealmContext( | ||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this changes HTTP responses for unknown realms from 404 to 400 🤔 As for me, 404 seems to be more applicable. Did you have any reason in mind for changing the status code in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really, the HTTP response for failed realm resolution is now crafted by the
RealmContextFilterwhich is mapping all errors to 404 as before.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I missed that 🤦