dimas-b commented on code in PR #1373: URL: https://github.com/apache/polaris/pull/1373#discussion_r2045473490
########## quarkus/service/src/main/java/org/apache/polaris/service/quarkus/auth/ActiveRolesAugmentor.java: ########## @@ -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.auth; + +import io.quarkus.security.AuthenticationFailedException; +import io.quarkus.security.identity.AuthenticationRequestContext; +import io.quarkus.security.identity.SecurityIdentity; +import io.quarkus.security.identity.SecurityIdentityAugmentor; +import io.quarkus.security.runtime.QuarkusSecurityIdentity; +import io.smallrye.mutiny.Uni; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import java.util.Set; +import org.apache.polaris.core.auth.AuthenticatedPolarisPrincipal; +import org.apache.polaris.service.auth.ActiveRolesProvider; + +/** + * A custom {@link SecurityIdentityAugmentor} that adds active roles to the {@link + * SecurityIdentity}. This is used to augment the identity with active roles after authentication. + */ +@ApplicationScoped +public class ActiveRolesAugmentor implements SecurityIdentityAugmentor { + + @Inject ActiveRolesProvider activeRolesProvider; + + @Override + public Uni<SecurityIdentity> augment( + SecurityIdentity identity, AuthenticationRequestContext context) { + if (identity.isAnonymous()) { + return Uni.createFrom().item(identity); + } + return context.runBlocking(() -> augmentWithActiveRoles(identity)); + } + + private SecurityIdentity augmentWithActiveRoles(SecurityIdentity identity) { + AuthenticatedPolarisPrincipal polarisPrincipal = + identity.getPrincipal(AuthenticatedPolarisPrincipal.class); + if (polarisPrincipal == null) { + throw new AuthenticationFailedException("No Polaris principal found"); + } + Set<String> validRoleNames = activeRolesProvider.getActiveRoles(polarisPrincipal); + return QuarkusSecurityIdentity.builder() + .setAnonymous(false) + .setPrincipal(polarisPrincipal) + .addRoles(validRoleNames) + .addCredentials(identity.getCredentials()) Review Comment: nit: do we really need these credentials in Polaris? Just trying to avoid exposing unnecessary information :) ########## server-templates/api.mustache: ########## @@ -105,7 +105,8 @@ public class {{classname}} { @{{httpMethod}}{{#subresourceOperation}} @Path("{{{path}}}"){{/subresourceOperation}}{{#hasConsumes}} @Consumes({ {{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}} }){{/hasConsumes}}{{#hasProduces}} - @Produces({ {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} }){{/hasProduces}} + @Produces({ {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} }){{/hasProduces}}{{#hasAuthMethods}} + {{#authMethods}}{{#isOAuth}}@RolesAllowed("**"){{/isOAuth}}{{/authMethods}}{{/hasAuthMethods}} Review Comment: `@RolesAllowed("**")` LGTM, however, just want to mention that we could take full control of authentication and [deny all requests in the auth Mechanism](https://github.com/projectnessie/nessie/blob/main/servers/quarkus-authn/src/main/java/org/projectnessie/server/authn/NessieAuthMechanism.java#L134) (if we wanted). That would complicate the mechanisms a bit, but would provide extra certainly that no holes are left in the API. ########## quarkus/service/src/main/java/org/apache/polaris/service/quarkus/auth/PolarisAuthenticationMechanism.java: ########## @@ -0,0 +1,92 @@ +/* + * 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.auth; + +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.quarkus.security.credential.TokenCredential; +import io.quarkus.security.identity.IdentityProviderManager; +import io.quarkus.security.identity.SecurityIdentity; +import io.quarkus.security.identity.request.AuthenticationRequest; +import io.quarkus.security.identity.request.TokenAuthenticationRequest; +import io.quarkus.vertx.http.runtime.security.ChallengeData; +import io.quarkus.vertx.http.runtime.security.HttpAuthenticationMechanism; +import io.quarkus.vertx.http.runtime.security.HttpCredentialTransport; +import io.quarkus.vertx.http.runtime.security.HttpSecurityUtils; +import io.smallrye.mutiny.Uni; +import io.vertx.ext.web.RoutingContext; +import jakarta.annotation.Priority; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Alternative; +import java.util.Collections; +import java.util.Set; + +/** A custom {@link HttpAuthenticationMechanism} that handles Polaris token authentication. */ +@ApplicationScoped +@Alternative Review Comment: Why `@Alternative`? Will this replace _all_ other mechanisms? ########## quarkus/service/src/main/java/org/apache/polaris/service/quarkus/auth/PolarisAuthenticationMechanism.java: ########## @@ -0,0 +1,92 @@ +/* + * 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.auth; + +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.quarkus.security.credential.TokenCredential; +import io.quarkus.security.identity.IdentityProviderManager; +import io.quarkus.security.identity.SecurityIdentity; +import io.quarkus.security.identity.request.AuthenticationRequest; +import io.quarkus.security.identity.request.TokenAuthenticationRequest; +import io.quarkus.vertx.http.runtime.security.ChallengeData; +import io.quarkus.vertx.http.runtime.security.HttpAuthenticationMechanism; +import io.quarkus.vertx.http.runtime.security.HttpCredentialTransport; +import io.quarkus.vertx.http.runtime.security.HttpSecurityUtils; +import io.smallrye.mutiny.Uni; +import io.vertx.ext.web.RoutingContext; +import jakarta.annotation.Priority; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Alternative; +import java.util.Collections; +import java.util.Set; + +/** A custom {@link HttpAuthenticationMechanism} that handles Polaris token authentication. */ +@ApplicationScoped +@Alternative +@Priority(1) Review Comment: Maybe a slightly higher priority to allow further override in downstream projects? ########## quarkus/service/src/main/java/org/apache/polaris/service/quarkus/auth/PolarisAuthenticationMechanism.java: ########## @@ -0,0 +1,92 @@ +/* + * 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.auth; + +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.quarkus.security.credential.TokenCredential; +import io.quarkus.security.identity.IdentityProviderManager; +import io.quarkus.security.identity.SecurityIdentity; +import io.quarkus.security.identity.request.AuthenticationRequest; +import io.quarkus.security.identity.request.TokenAuthenticationRequest; +import io.quarkus.vertx.http.runtime.security.ChallengeData; +import io.quarkus.vertx.http.runtime.security.HttpAuthenticationMechanism; +import io.quarkus.vertx.http.runtime.security.HttpCredentialTransport; +import io.quarkus.vertx.http.runtime.security.HttpSecurityUtils; +import io.smallrye.mutiny.Uni; +import io.vertx.ext.web.RoutingContext; +import jakarta.annotation.Priority; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Alternative; +import java.util.Collections; +import java.util.Set; + +/** A custom {@link HttpAuthenticationMechanism} that handles Polaris token authentication. */ +@ApplicationScoped +@Alternative +@Priority(1) +public class PolarisAuthenticationMechanism implements HttpAuthenticationMechanism { Review Comment: Should this be under `quarkus/server` perhaps (and the other new auth classes)? It might be preferable to isolate service code and auth code. Plus, downsteam projects may want to adjust how authentication is down, and adding auth-related beans to the services jar may complicate reuse :thinking: -- 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]
