adutra commented on code in PR #623:
URL: https://github.com/apache/polaris/pull/623#discussion_r1907579274


##########
dropwizard/service/src/main/java/org/apache/polaris/service/dropwizard/auth/PolarisPrincipalRoleSecurityContextProvider.java:
##########
@@ -0,0 +1,144 @@
+/*
+ * 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.dropwizard.auth;
+
+import jakarta.annotation.Priority;
+import jakarta.inject.Inject;
+import jakarta.inject.Provider;
+import jakarta.ws.rs.Priorities;
+import jakarta.ws.rs.container.ContainerRequestContext;
+import jakarta.ws.rs.container.ContainerRequestFilter;
+import jakarta.ws.rs.core.SecurityContext;
+import java.io.IOException;
+import java.security.Principal;
+import java.util.List;
+import java.util.Set;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+import org.apache.iceberg.exceptions.NotAuthorizedException;
+import org.apache.polaris.core.PolarisCallContext;
+import org.apache.polaris.core.auth.AuthenticatedPolarisPrincipal;
+import org.apache.polaris.core.auth.PolarisGrantManager;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+import org.apache.polaris.core.entity.PolarisEntity;
+import org.apache.polaris.core.entity.PrincipalRoleEntity;
+import org.apache.polaris.core.persistence.MetaStoreManagerFactory;
+import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
+import org.apache.polaris.service.auth.BasePolarisAuthenticator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Priority(Priorities.AUTHENTICATION + 1)
+public class PolarisPrincipalRoleSecurityContextProvider implements 
ContainerRequestFilter {
+  private static final Logger LOGGER =
+      
LoggerFactory.getLogger(PolarisPrincipalRoleSecurityContextProvider.class);
+  @Inject Provider<SecurityContext> securityContextProvider;
+  @Inject MetaStoreManagerFactory metaStoreManager;
+  @Inject Provider<PolarisGrantManager> polarisGrantManagerProvider;
+
+  @Override
+  public void filter(ContainerRequestContext requestContext) throws 
IOException {
+    AuthenticatedPolarisPrincipal polarisPrincipal =
+        (AuthenticatedPolarisPrincipal) 
securityContextProvider.get().getUserPrincipal();
+    if (polarisPrincipal == null) {
+      return;
+    }
+    SecurityContext securityContext =
+        createSecurityContext(requestContext.getSecurityContext(), 
polarisPrincipal);
+    requestContext.setSecurityContext(securityContext);

Review Comment:
   It's not that uncommon to augment the security context, it's already being 
done (have a look at `io.dropwizard.auth.AuthFilter`). Even in Quarkus it's 
common practice:
   
   https://quarkus.io/guides/security-customization#jaxrs-security-context
   
   That said, I agree that there is a lot of overlap between 
`AuthenticatedPolarisPrincipal` and `SecurityContext`. I'd note that 
`SecurityContext` is already being passed as a method argument to all API and 
service methods, and from the security context you can get the principal (but 
not the other way around). 



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to