sarankk commented on code in PR #145: URL: https://github.com/apache/cassandra-sidecar/pull/145#discussion_r1830372066
########## server/src/main/java/org/apache/cassandra/sidecar/accesscontrol/authentication/MutualTlsAuthenticationHandler.java: ########## @@ -0,0 +1,85 @@ +/* + * 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.cassandra.sidecar.accesscontrol.authentication; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import io.netty.handler.codec.http.HttpResponseStatus; +import io.vertx.core.AsyncResult; +import io.vertx.core.Future; +import io.vertx.core.Handler; +import io.vertx.ext.auth.User; +import io.vertx.ext.auth.authentication.CertificateCredentials; +import io.vertx.ext.auth.authentication.CredentialValidationException; +import io.vertx.ext.auth.mtls.MutualTlsAuthentication; +import io.vertx.ext.web.RoutingContext; +import io.vertx.ext.web.handler.HttpException; +import io.vertx.ext.web.handler.impl.AuthenticationHandlerImpl; +import org.apache.cassandra.sidecar.concurrent.ExecutorPools; + +/** + * Handler for verifying user certificates for Mutual TLS authentication. {@link MutualTlsAuthenticationHandler} can be + * chained with other {@link io.vertx.ext.web.handler.AuthenticationHandler} implementations. + */ +public class MutualTlsAuthenticationHandler extends AuthenticationHandlerImpl<MutualTlsAuthentication> +{ + private static final Logger LOGGER = LoggerFactory.getLogger(MutualTlsAuthenticationHandler.class); + private final ExecutorPools executorPools; + + public MutualTlsAuthenticationHandler(MutualTlsAuthentication authProvider, + ExecutorPools executorPools) + { + super(authProvider); + this.executorPools = executorPools; + } + + @Override + public void authenticate(RoutingContext ctx, Handler<AsyncResult<User>> handler) + { + if (!ctx.request().isSSL()) + { + ctx.response().setStatusCode(HttpResponseStatus.BAD_REQUEST.code()).end(); + return; + } + + CertificateCredentials certificateCredentials = CertificateCredentials.fromHttpRequest(ctx.request()); + executorPools + .service() + .executeBlocking(promise -> { + authProvider.authenticate(certificateCredentials) + .onSuccess(promise::complete) + .onFailure(cause -> { + if (cause instanceof CredentialValidationException) + { + // If credentials are invalid, reject the promise with a BAD_REQUEST error + promise.fail(new HttpException(HttpResponseStatus.BAD_REQUEST.code(), + "Error validating credentials passed")); + } + else + { + // In case of other failures, reject the promise with an UNAUTHORIZED error + promise.fail(new HttpException(HttpResponseStatus.UNAUTHORIZED.code())); + } + }); + }) + .onSuccess(result -> handler.handle(Future.succeededFuture((User) result))) + .onFailure(cause -> handler.handle(Future.failedFuture(cause))); Review Comment: The reason we hand off to executor pool, is because, we use `identityRoleCache`, during loads, since database calls are involved, it blocks thread. I can move hand off to executor pool inside `CassandraIdentityExtractor` -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

