frankgh commented on code in PR #270:
URL: https://github.com/apache/cassandra-sidecar/pull/270#discussion_r2450035571


##########
server/src/main/java/org/apache/cassandra/sidecar/acl/authorization/CachedAuthorizationHandler.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * 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.acl.authorization;
+
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.BiConsumer;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.github.benmanes.caffeine.cache.AsyncCache;
+import io.vertx.ext.auth.User;
+import io.vertx.ext.auth.authorization.Authorization;
+import io.vertx.ext.auth.authorization.AuthorizationContext;
+import io.vertx.ext.web.RoutingContext;
+import io.vertx.ext.web.handler.AuthorizationHandler;
+import io.vertx.ext.web.handler.HttpException;
+import io.vertx.ext.web.handler.impl.AuthorizationHandlerImpl;
+import org.apache.cassandra.sidecar.acl.AdminIdentityResolver;
+import org.apache.cassandra.sidecar.config.AccessControlConfiguration;
+import org.apache.cassandra.sidecar.metrics.SidecarMetrics;
+import org.apache.cassandra.sidecar.metrics.server.AuthMetrics;
+
+import static io.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN;
+import static org.apache.cassandra.sidecar.utils.AuthUtils.extractIdentities;
+
+/**
+ * {@link CachedAuthorizationHandler} caches all authorization requests using 
{@link AuthorizationCacheKey}.
+ */
+public class CachedAuthorizationHandler extends AuthorizationHandlerImpl
+{
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(CachedAuthorizationHandler.class);
+
+    // uniquely identities CachedAuthorizationHandler across different routes. 
Having same handlerId can lead
+    // to permission bypass across routes.
+    private static final AtomicInteger HANDLER_ID_GEN = new AtomicInteger(0);
+    private static final HttpException FORBIDDEN_EXCEPTION = new 
HttpException(403);
+    private final int handlerId;
+    private final AccessControlConfiguration accessControlConfiguration;
+    private final AuthorizationParameterValidateHandler 
authZParameterValidateHandler;
+    private final AdminIdentityResolver adminIdentityResolver;
+    private final AuthMetrics authMetrics;
+    private final AsyncCache<AuthorizationCacheKey, Boolean> 
authorizationCache;
+
+    // This is overridden since Vert.x does not expose this
+    private BiConsumer<RoutingContext, AuthorizationContext> variableHandler;
+
+    public CachedAuthorizationHandler(AccessControlConfiguration 
accessControlConfiguration,
+                                      AuthorizationParameterValidateHandler 
authZParameterValidateHandler,
+                                      AdminIdentityResolver 
adminIdentityResolver,
+                                      Authorization authorization,
+                                      SidecarMetrics sidecarMetrics,
+                                      AsyncCache<AuthorizationCacheKey, 
Boolean> authorizationCache)
+    {
+        this(HANDLER_ID_GEN.getAndIncrement(), accessControlConfiguration, 
authZParameterValidateHandler,
+             adminIdentityResolver, authorization, sidecarMetrics, 
authorizationCache);
+    }
+
+    @VisibleForTesting
+    public CachedAuthorizationHandler(int handlerId,
+                                      AccessControlConfiguration 
accessControlConfiguration,
+                                      AuthorizationParameterValidateHandler 
authZParameterValidateHandler,
+                                      AdminIdentityResolver 
adminIdentityResolver,
+                                      Authorization authorization,
+                                      SidecarMetrics sidecarMetrics,
+                                      AsyncCache<AuthorizationCacheKey, 
Boolean> authorizationCache)
+    {
+        super(authorization);
+        this.handlerId = handlerId;
+        this.accessControlConfiguration = accessControlConfiguration;
+        this.authZParameterValidateHandler = authZParameterValidateHandler;
+        this.adminIdentityResolver = adminIdentityResolver;
+        this.authMetrics = sidecarMetrics.server().auth();
+        this.authorizationCache = authorizationCache;
+    }
+
+    @Override
+    public void handle(RoutingContext ctx)
+    {
+        long startTimeNanos = System.nanoTime();
+        authZParameterValidateHandler.handle(ctx);
+        if (ctx.failed()) // failed due to validation
+        {
+            return;
+        }
+
+        User user = ctx.user();
+        AuthorizationContext authorizationContext = 
AuthorizationContext.create(user);
+        if (this.variableHandler != null)
+        {
+            this.variableHandler.accept(ctx, authorizationContext);
+        }
+
+        AtomicBoolean ctxNextCalled = new AtomicBoolean(false);
+        AuthorizationCacheKey key = AuthorizationCacheKey.create(handlerId, 
authorizationContext);
+        CompletableFuture<Boolean> authorizationFuture = 
checkAuthorization(key, ctx, ctxNextCalled, startTimeNanos);

Review Comment:
   the reason for the client to use CompletableFuture is because the core 
project client project does not have any vertx dependencies 



-- 
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]

Reply via email to