You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@polaris.apache.org by "adutra (via GitHub)" <gi...@apache.org> on 2025/01/22 17:14:13 UTC

[PR] [WIP] Reorder filters and let RealmIdResolver throw NotAuthorized [polaris]

adutra opened a new pull request, #848:
URL: https://github.com/apache/polaris/pull/848

   ⚠️ Not ready for review. Requires #786.
   
   Fixes #819. 
   
   Summary of changes:
   
   * Vert.x route filters were converted to JAX-RS `ContainerRequestFilter`
   * New `RealmIdFilter` introduced
   * `RealmIdResolver` now can throw `NotAuthorizedException`
   * Added tests in `PolarisApplicationIntegrationTest`
   * Fixed default realm name in tests
   * Priorities were revisited, new order of execution is shown below:
   
   | Filter | Order | Priority |
   | ------|-----| --------|
   | `RealmIdFilter` | 1 | 900  |
   | `QuarkusLoggingMDCFilter` | 2 | 901 |
   | `QuarkusTracingFilter` | 3 | 902 |
   | `PolarisPrincipalAuthenticatorFilter` | 4 | 1000 |
   | `PolarisPrincipalRolesProviderFilter`  | 5 | 1001 |
   | `RateLimiterFilter` | 6 | 5000 |
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Reorder filters and let RealmIdResolver throw UnresolvableRealmException [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #848:
URL: https://github.com/apache/polaris/pull/848#discussion_r1930612831


##########
quarkus/service/src/main/java/org/apache/polaris/service/quarkus/config/QuarkusProducers.java:
##########
@@ -100,13 +100,8 @@ public PolarisDiagnostics polarisDiagnostics() {
 
   @Produces
   @RequestScoped
-  public RealmId realmId(@Context HttpServerRequest request, RealmIdResolver realmIdResolver) {
-    return realmIdResolver.resolveRealmContext(
-        request.absoluteURI(),
-        request.method().name(),
-        request.path(),
-        request.headers().entries().stream()
-            .collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), HashMap::putAll));
+  public RealmId realmId(@Context ContainerRequestContext request) {
+    return (RealmId) request.getProperty(RealmIdFilter.REALM_ID_KEY);

Review Comment:
   TIL - thx :sweat_smile: 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Reorder filters and let RealmContextResolver throw UnresolvableRealmContextException [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #848:
URL: https://github.com/apache/polaris/pull/848#discussion_r1941991644


##########
quarkus/service/src/main/java/org/apache/polaris/service/quarkus/tracing/QuarkusTracingFilter.java:
##########
@@ -19,32 +19,41 @@
 package org.apache.polaris.service.quarkus.tracing;
 
 import io.opentelemetry.api.trace.Span;
-import io.quarkus.vertx.web.RouteFilter;
-import io.vertx.ext.web.RoutingContext;
+import jakarta.annotation.Priority;
 import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.ws.rs.container.ContainerRequestContext;
+import jakarta.ws.rs.container.ContainerRequestFilter;
+import jakarta.ws.rs.container.PreMatching;
+import jakarta.ws.rs.ext.Provider;
+import org.apache.polaris.core.context.RealmContext;
+import org.apache.polaris.service.context.RealmContextFilter;
+import org.apache.polaris.service.quarkus.config.QuarkusFilterPriorities;
 import org.apache.polaris.service.quarkus.logging.QuarkusLoggingMDCFilter;
 import org.eclipse.microprofile.config.inject.ConfigProperty;
 
+@PreMatching
 @ApplicationScoped
-public class QuarkusTracingFilter {
+@Priority(QuarkusFilterPriorities.TRACING_FILTER)
+@Provider
+public class QuarkusTracingFilter implements ContainerRequestFilter {
 
   public static final String REQUEST_ID_ATTRIBUTE = "polaris.request.id";
-  public static final String REALM_ID_ATTRIBUTE = "polaris.realm";
+  public static final String REALM_ID_ATTRIBUTE = "polaris.realm.id";
 
   @ConfigProperty(name = "quarkus.otel.sdk.disabled")
   boolean sdkDisabled;
 
-  @RouteFilter(QuarkusLoggingMDCFilter.PRIORITY - 1)
-  public void applySpanAttributes(RoutingContext rc) {
+  @Override
+  public void filter(ContainerRequestContext rc) {
     if (!sdkDisabled) {

Review Comment:
   tangential: When OTel is disabled, it's still ok to operate on `Span.current()`, we'd simply get a "no-op" `Span`. However, IMHO it is preferable to exercise the code that produces span values even when tracing is disabled. This is to avoid bugs that only pop up when tracing is on.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Reorder filters and let RealmIdResolver throw NotAuthorized [polaris]

Posted by "adutra (via GitHub)" <gi...@apache.org>.
adutra commented on code in PR #848:
URL: https://github.com/apache/polaris/pull/848#discussion_r1930508156


##########
quarkus/service/src/main/java/org/apache/polaris/service/quarkus/config/QuarkusProducers.java:
##########
@@ -100,13 +100,8 @@ public PolarisDiagnostics polarisDiagnostics() {
 
   @Produces
   @RequestScoped
-  public RealmId realmId(@Context HttpServerRequest request, RealmIdResolver realmIdResolver) {
-    return realmIdResolver.resolveRealmContext(
-        request.absoluteURI(),
-        request.method().name(),
-        request.path(),
-        request.headers().entries().stream()
-            .collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), HashMap::putAll));
+  public RealmId realmId(@Context ContainerRequestContext request) {
+    return (RealmId) request.getProperty(RealmIdFilter.REALM_ID_KEY);

Review Comment:
   This is implicitly the case already: if the property is absent, this method returns null – and returning null from a producer method is forbidden:
   
   https://docs.jboss.org/cdi/spec/2.0/cdi-spec.html#producer_method



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Reorder filters and let RealmIdResolver throw UnresolvableRealmException [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on PR #848:
URL: https://github.com/apache/polaris/pull/848#issuecomment-2617791709

   @collado-mike : any concerns from your POV?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Reorder filters and let RealmIdResolver throw UnresolvableRealmException [polaris]

Posted by "adutra (via GitHub)" <gi...@apache.org>.
adutra commented on code in PR #848:
URL: https://github.com/apache/polaris/pull/848#discussion_r1930995542


##########
service/common/src/main/java/org/apache/polaris/service/context/RealmContextConfiguration.java:
##########
@@ -33,6 +33,16 @@ public interface RealmContextConfiguration {
   /** The header name that contains the realm identifier. */
   String headerName();
 
+  /**
+   * Whether to require the realm header to be present in the request. If this is true and the realm
+   * header is not present, the request will be rejected. If this is false and the realm header is
+   * not present, the default realm will be used.
+   *
+   * <p>Note: this is actually only enforced when using {@link DefaultRealmIdResolver}. When using
+   * {@link TestRealmIdResolver}, this setting is ignored.
+   */
+  boolean requireHeader();

Review Comment:
   Yeah changing this to true would break downstreams. But we can mention this in the "Configuring for production" docs.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Reorder filters and let RealmContextResolver throw UnresolvableRealmContextException [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b merged PR #848:
URL: https://github.com/apache/polaris/pull/848


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Reorder filters and let RealmIdResolver throw NotAuthorized [polaris]

Posted by "adutra (via GitHub)" <gi...@apache.org>.
adutra commented on code in PR #848:
URL: https://github.com/apache/polaris/pull/848#discussion_r1930535591


##########
service/common/src/main/java/org/apache/polaris/service/context/RealmIdFilter.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.context;
+
+import jakarta.annotation.Priority;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import jakarta.ws.rs.NotAuthorizedException;
+import jakarta.ws.rs.container.ContainerRequestContext;
+import jakarta.ws.rs.container.ContainerRequestFilter;
+import jakarta.ws.rs.container.PreMatching;
+import jakarta.ws.rs.core.Response;
+import jakarta.ws.rs.core.Response.Status;
+import jakarta.ws.rs.ext.Provider;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.polaris.core.context.RealmId;
+import org.apache.polaris.service.config.PolarisFilterPriorities;
+
+@PreMatching
+@ApplicationScoped
+@Priority(PolarisFilterPriorities.REALM_ID_FILTER)
+@Provider
+public class RealmIdFilter implements ContainerRequestFilter {
+
+  public static final String REALM_ID_KEY = "realmId";
+
+  @Inject RealmIdResolver realmIdResolver;
+
+  @Override
+  public void filter(ContainerRequestContext rc) {
+    RealmId realmId = null;
+    try {
+      realmId = resolveRealmContext(rc);
+    } catch (NotAuthorizedException e) {
+      rc.abortWith(Response.status(Status.UNAUTHORIZED).build());

Review Comment:
   Makes sense, i will update the PR title and description accordingly.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Reorder filters and let RealmIdResolver throw NotAuthorized [polaris]

Posted by "adutra (via GitHub)" <gi...@apache.org>.
adutra commented on PR #848:
URL: https://github.com/apache/polaris/pull/848#issuecomment-2612194366

   This is now ready for review.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Reorder filters and let RealmIdResolver throw UnresolvableRealmException [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #848:
URL: https://github.com/apache/polaris/pull/848#discussion_r1930969924


##########
service/common/src/main/java/org/apache/polaris/service/context/RealmContextConfiguration.java:
##########
@@ -33,6 +33,16 @@ public interface RealmContextConfiguration {
   /** The header name that contains the realm identifier. */
   String headerName();
 
+  /**
+   * Whether to require the realm header to be present in the request. If this is true and the realm
+   * header is not present, the request will be rejected. If this is false and the realm header is
+   * not present, the default realm will be used.
+   *
+   * <p>Note: this is actually only enforced when using {@link DefaultRealmIdResolver}. When using
+   * {@link TestRealmIdResolver}, this setting is ignored.
+   */
+  boolean requireHeader();

Review Comment:
   Defaulting to `true` would be a behaviour change at the REST/HTTP level. I'd prefer to keep old behaviour in this PR and change defaults later.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Reorder filters and let RealmIdResolver throw UnresolvableRealmException [polaris]

Posted by "snazy (via GitHub)" <gi...@apache.org>.
snazy commented on code in PR #848:
URL: https://github.com/apache/polaris/pull/848#discussion_r1930970984


##########
service/common/src/main/java/org/apache/polaris/service/context/RealmContextConfiguration.java:
##########
@@ -33,6 +33,16 @@ public interface RealmContextConfiguration {
   /** The header name that contains the realm identifier. */
   String headerName();
 
+  /**
+   * Whether to require the realm header to be present in the request. If this is true and the realm
+   * header is not present, the request will be rejected. If this is false and the realm header is
+   * not present, the default realm will be used.
+   *
+   * <p>Note: this is actually only enforced when using {@link DefaultRealmIdResolver}. When using
+   * {@link TestRealmIdResolver}, this setting is ignored.
+   */
+  boolean requireHeader();

Review Comment:
   WFM



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Reorder filters and let RealmContextResolver throw UnresolvableRealmContextException [polaris]

Posted by "collado-mike (via GitHub)" <gi...@apache.org>.
collado-mike commented on code in PR #848:
URL: https://github.com/apache/polaris/pull/848#discussion_r1943792695


##########
quarkus/service/src/main/java/org/apache/polaris/service/quarkus/tracing/QuarkusTracingFilter.java:
##########
@@ -19,32 +19,41 @@
 package org.apache.polaris.service.quarkus.tracing;
 
 import io.opentelemetry.api.trace.Span;
-import io.quarkus.vertx.web.RouteFilter;
-import io.vertx.ext.web.RoutingContext;
+import jakarta.annotation.Priority;
 import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.ws.rs.container.ContainerRequestContext;
+import jakarta.ws.rs.container.ContainerRequestFilter;
+import jakarta.ws.rs.container.PreMatching;
+import jakarta.ws.rs.ext.Provider;
+import org.apache.polaris.core.context.RealmContext;
+import org.apache.polaris.service.context.RealmContextFilter;
+import org.apache.polaris.service.quarkus.config.QuarkusFilterPriorities;
 import org.apache.polaris.service.quarkus.logging.QuarkusLoggingMDCFilter;
 import org.eclipse.microprofile.config.inject.ConfigProperty;
 
+@PreMatching
 @ApplicationScoped
-public class QuarkusTracingFilter {
+@Priority(QuarkusFilterPriorities.TRACING_FILTER)
+@Provider
+public class QuarkusTracingFilter implements ContainerRequestFilter {
 
   public static final String REQUEST_ID_ATTRIBUTE = "polaris.request.id";
-  public static final String REALM_ID_ATTRIBUTE = "polaris.realm";
+  public static final String REALM_ID_ATTRIBUTE = "polaris.realm.id";
 
   @ConfigProperty(name = "quarkus.otel.sdk.disabled")
   boolean sdkDisabled;
 
-  @RouteFilter(QuarkusLoggingMDCFilter.PRIORITY - 1)
-  public void applySpanAttributes(RoutingContext rc) {
+  @Override
+  public void filter(ContainerRequestContext rc) {
     if (!sdkDisabled) {

Review Comment:
   I get the sentiment, but at the same time, it's nice to be able to turn off the functionality entirely, including any bugs that may be present that could impact the running service even though you don't want/care about tracing. 



##########
service/common/src/main/java/org/apache/polaris/service/context/DefaultRealmContextResolver.java:
##########
@@ -37,19 +37,24 @@ public DefaultRealmContextResolver(RealmContextConfiguration configuration) {
 
   @Override
   public RealmContext resolveRealmContext(
-      String requestURL, String method, String path, Map<String, String> headers) {
-
-    String realm;
+      String requestURL, String method, String path, Function<String, String> headers) {
+    String realm = resolveRealmIdentifier(headers);
+    return () -> realm;
+  }
 
-    if (headers.containsKey(configuration.headerName())) {
-      realm = headers.get(configuration.headerName());
+  private String resolveRealmIdentifier(Function<String, String> headers) {
+    String realm = headers.apply(configuration.headerName());
+    if (realm != null) {
       if (!configuration.realms().contains(realm)) {
-        throw new IllegalArgumentException("Unknown realm: " + realm);
+        throw new UnresolvableRealmContextException("Unknown realm: " + realm);
       }

Review Comment:
   Personally, i would push the check down into the `RealmContextConfiguration` rather than exposing a set of valid realms and doing the check here. But for this change, I think the `UnresolvableRealmContextExcpetion` is the right change to make



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Reorder filters and let RealmContextResolver throw UnresolvableRealmContextException [polaris]

Posted by "adutra (via GitHub)" <gi...@apache.org>.
adutra commented on PR #848:
URL: https://github.com/apache/polaris/pull/848#issuecomment-2637697679

   This has been in review for 2 weeks. Is it OK to merge now? @collado-mike 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Reorder filters and let RealmIdResolver throw UnresolvableRealmException [polaris]

Posted by "snazy (via GitHub)" <gi...@apache.org>.
snazy commented on code in PR #848:
URL: https://github.com/apache/polaris/pull/848#discussion_r1930961344


##########
service/common/src/main/java/org/apache/polaris/service/context/RealmContextConfiguration.java:
##########
@@ -33,6 +33,16 @@ public interface RealmContextConfiguration {
   /** The header name that contains the realm identifier. */
   String headerName();
 
+  /**
+   * Whether to require the realm header to be present in the request. If this is true and the realm
+   * header is not present, the request will be rejected. If this is false and the realm header is
+   * not present, the default realm will be used.
+   *
+   * <p>Note: this is actually only enforced when using {@link DefaultRealmIdResolver}. When using
+   * {@link TestRealmIdResolver}, this setting is ignored.
+   */
+  boolean requireHeader();

Review Comment:
   Should this be true by default?



##########
integration-tests/src/main/java/org/apache/polaris/service/it/env/PolarisApiEndpoints.java:
##########
@@ -49,4 +49,8 @@ public URI managementApiEndpoint() {
   public String realm() {
     return realm;
   }
+
+  public String realmHeader() {

Review Comment:
   Maybe rename to `realmHeaderName()` + `realmId()` (latter for consistency w/ `Server`)?



##########
service/common/src/main/java/org/apache/polaris/service/context/RealmContextConfiguration.java:
##########
@@ -33,6 +33,16 @@ public interface RealmContextConfiguration {
   /** The header name that contains the realm identifier. */
   String headerName();
 
+  /**
+   * Whether to require the realm header to be present in the request. If this is true and the realm
+   * header is not present, the request will be rejected. If this is false and the realm header is
+   * not present, the default realm will be used.
+   *
+   * <p>Note: this is actually only enforced when using {@link DefaultRealmIdResolver}. When using
+   * {@link TestRealmIdResolver}, this setting is ignored.

Review Comment:
   ```suggestion
      * <p>Note: this setting is only enforced in production setups. 
   ```



##########
service/common/src/main/java/org/apache/polaris/service/context/RealmIdFilter.java:
##########
@@ -0,0 +1,57 @@
+/*
+ * 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.context;
+
+import jakarta.annotation.Priority;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import jakarta.ws.rs.container.ContainerRequestContext;
+import jakarta.ws.rs.container.ContainerRequestFilter;
+import jakarta.ws.rs.container.PreMatching;
+import jakarta.ws.rs.ext.Provider;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.polaris.core.context.RealmId;
+import org.apache.polaris.service.config.PolarisFilterPriorities;
+
+@PreMatching
+@ApplicationScoped
+@Priority(PolarisFilterPriorities.REALM_ID_FILTER)
+@Provider
+public class RealmIdFilter implements ContainerRequestFilter {
+
+  public static final String REALM_ID_KEY = "realmId";
+
+  @Inject RealmIdResolver realmIdResolver;
+
+  @Override
+  public void filter(ContainerRequestContext rc) {
+    RealmId realmId =
+        realmIdResolver.resolveRealmId(
+            rc.getUriInfo().getRequestUri().toString(),
+            rc.getMethod(),
+            rc.getUriInfo().getPath(),
+            rc.getHeaders().entrySet().stream()
+                .collect(
+                    HashMap::new,
+                    (m, e) -> m.put(e.getKey(), e.getValue().getFirst()),
+                    Map::putAll));

Review Comment:
   Wonder if it makes more sense to change `RealmIdResolver` to expect a `Function<String, String>` rather than a "full blown map" (and use `request::getHeader` here).



##########
integration-tests/src/main/java/org/apache/polaris/service/it/env/Server.java:
##########
@@ -36,4 +39,8 @@ public interface Server extends AutoCloseable {
   URI baseUri();
 
   ClientPrincipal adminCredentials();
+
+  default String realmHeader() {

Review Comment:
   ```suggestion
     default String realmHeaderName() {
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Reorder filters and let RealmIdResolver throw NotAuthorized [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #848:
URL: https://github.com/apache/polaris/pull/848#discussion_r1929243872


##########
integration-tests/src/main/java/org/apache/polaris/service/it/env/PolarisApiEndpoints.java:
##########
@@ -28,7 +28,11 @@
  */
 public final class PolarisApiEndpoints implements Serializable {
 
-  public static String REALM_HEADER = "realm";
+  /**
+   * The header name for the realm ID. Tests must make sure that Polaris is configured with this
+   * header name.
+   */
+  public static String REALM_HEADER = "Polaris-Realm";

Review Comment:
   Would you mind moving this to a (new) method on `PolarisServerManager` or `Server` so that it is possible to customize?



##########
service/common/src/main/java/org/apache/polaris/service/context/DefaultRealmIdResolver.java:
##########
@@ -36,15 +37,15 @@ public DefaultRealmIdResolver(RealmContextConfiguration configuration) {
   }
 
   @Override
-  public RealmId resolveRealmContext(
+  public RealmId resolveRealmId(
       String requestURL, String method, String path, Map<String, String> headers) {
 
     String realm;
 
     if (headers.containsKey(configuration.headerName())) {
       realm = headers.get(configuration.headerName());
       if (!configuration.realms().contains(realm)) {
-        throw new IllegalArgumentException("Unknown realm: " + realm);
+        throw new NotAuthorizedException("Unknown realm: " + realm);
       }
     } else {
       realm = configuration.defaultRealm();

Review Comment:
   I think it could be valuable to cover this with an explicit config flag. If realms are used in a particular deployment, it is probably not wise to allow potentially malicious clients to try sending requests at the default realm without knowing its ID.



##########
quarkus/service/src/main/java/org/apache/polaris/service/quarkus/config/QuarkusProducers.java:
##########
@@ -100,13 +100,8 @@ public PolarisDiagnostics polarisDiagnostics() {
 
   @Produces
   @RequestScoped
-  public RealmId realmId(@Context HttpServerRequest request, RealmIdResolver realmIdResolver) {
-    return realmIdResolver.resolveRealmContext(
-        request.absoluteURI(),
-        request.method().name(),
-        request.path(),
-        request.headers().entries().stream()
-            .collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), HashMap::putAll));
+  public RealmId realmId(@Context ContainerRequestContext request) {
+    return (RealmId) request.getProperty(RealmIdFilter.REALM_ID_KEY);

Review Comment:
   Should we check that the property is present (as it should at this stage)?... just for extra safety.



##########
service/common/src/main/java/org/apache/polaris/service/context/RealmIdFilter.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.context;
+
+import jakarta.annotation.Priority;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import jakarta.ws.rs.NotAuthorizedException;
+import jakarta.ws.rs.container.ContainerRequestContext;
+import jakarta.ws.rs.container.ContainerRequestFilter;
+import jakarta.ws.rs.container.PreMatching;
+import jakarta.ws.rs.core.Response;
+import jakarta.ws.rs.core.Response.Status;
+import jakarta.ws.rs.ext.Provider;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.polaris.core.context.RealmId;
+import org.apache.polaris.service.config.PolarisFilterPriorities;
+
+@PreMatching
+@ApplicationScoped
+@Priority(PolarisFilterPriorities.REALM_ID_FILTER)
+@Provider
+public class RealmIdFilter implements ContainerRequestFilter {
+
+  public static final String REALM_ID_KEY = "realmId";
+
+  @Inject RealmIdResolver realmIdResolver;
+
+  @Override
+  public void filter(ContainerRequestContext rc) {
+    RealmId realmId = null;
+    try {
+      realmId = resolveRealmContext(rc);
+    } catch (NotAuthorizedException e) {
+      rc.abortWith(Response.status(Status.UNAUTHORIZED).build());

Review Comment:
   I'm not sure about 401 in this context. I believe it generally means your credentials are not valid, which is not applicable to failures in realm resolution.
   
   I'm thinking that a 404 may fit better. For example, GH gives 404 on accessing repositories (i.e. realms) to which the user has no access.



##########
integration-tests/src/main/java/org/apache/polaris/service/it/test/PolarisApplicationIntegrationTest.java:
##########
@@ -657,4 +663,52 @@ public void testRequestBodyTooLarge() throws Exception {
               });
     }
   }
+
+  @Test
+  public void testNoRealmHeader() {
+    try (Response response =
+        managementApi
+            .request(
+                "v1/catalogs", Map.of(), Map.of(), Map.of("Authorization", "Bearer " + authToken))
+            .get()) {
+      assertThat(response.getStatus()).isEqualTo(Status.OK.getStatusCode());
+      Catalogs roles = response.readEntity(Catalogs.class);
+      assertThat(roles.getCatalogs()).extracting(Catalog::getName).contains(internalCatalogName);
+    }
+  }
+
+  @ParameterizedTest
+  @ValueSource(strings = {"POLARIS", "OTHER"})
+  public void testRealmHeaderValid(String realmId) {
+    try (Response response =
+        managementApi
+            .request(
+                "v1/catalogs",
+                Map.of(),
+                Map.of(),
+                Map.of("Authorization", "Bearer " + authToken, REALM_HEADER, realmId))
+            .get()) {
+      assertThat(response.getStatus()).isEqualTo(Status.OK.getStatusCode());
+      Catalogs roles = response.readEntity(Catalogs.class);
+      if ("POLARIS".equals(realmId)) {
+        assertThat(roles.getCatalogs()).extracting(Catalog::getName).contains(internalCatalogName);

Review Comment:
   nit: maybe make `internalCatalogName` a parameter to this test?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Reorder filters and let RealmIdResolver throw NotAuthorized [polaris]

Posted by "adutra (via GitHub)" <gi...@apache.org>.
adutra commented on code in PR #848:
URL: https://github.com/apache/polaris/pull/848#discussion_r1930509114


##########
service/common/src/main/java/org/apache/polaris/service/context/DefaultRealmIdResolver.java:
##########
@@ -36,15 +37,15 @@ public DefaultRealmIdResolver(RealmContextConfiguration configuration) {
   }
 
   @Override
-  public RealmId resolveRealmContext(
+  public RealmId resolveRealmId(
       String requestURL, String method, String path, Map<String, String> headers) {
 
     String realm;
 
     if (headers.containsKey(configuration.headerName())) {
       realm = headers.get(configuration.headerName());
       if (!configuration.realms().contains(realm)) {
-        throw new IllegalArgumentException("Unknown realm: " + realm);
+        throw new NotAuthorizedException("Unknown realm: " + realm);
       }
     } else {
       realm = configuration.defaultRealm();

Review Comment:
   I completely agree, I think that having a default realm is a potential security issue.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Reorder filters and let RealmContextResolver throw UnresolvableRealmContextException [polaris]

Posted by "adutra (via GitHub)" <gi...@apache.org>.
adutra commented on PR #848:
URL: https://github.com/apache/polaris/pull/848#issuecomment-2633504267

   PR Rebased. @dimas-b @snazy @collado-mike can I get another review please?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org