nvharikrishna commented on code in PR #165:
URL: https://github.com/apache/cassandra-sidecar/pull/165#discussion_r1894266085


##########
server/src/main/java/org/apache/cassandra/sidecar/acl/authorization/CassandraActions.java:
##########
@@ -0,0 +1,36 @@
+/*
+ * 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;
+
+/**
+ * Cassandra actions allowed.
+ */
+public class CassandraActions

Review Comment:
   Cassandra calls these things as permissions. 
https://cassandra.apache.org/doc/5.0/cassandra/developing/cql/security.html#cql-permissions.
 Can it be renamed as CassandraPermissions?



##########
server/src/main/java/org/apache/cassandra/sidecar/acl/authorization/StandardAction.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * 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 io.vertx.ext.auth.authorization.Authorization;
+import io.vertx.ext.auth.authorization.PermissionBasedAuthorization;
+import io.vertx.ext.auth.authorization.impl.PermissionBasedAuthorizationImpl;
+import org.apache.cassandra.sidecar.exceptions.ConfigurationException;
+
+import static 
org.apache.cassandra.sidecar.acl.authorization.WildcardAction.WILDCARD_TOKEN;

Review Comment:
   WildcardAction is a child class/type of this class. Parent class should not 
have knowledge about the child class.



##########
server/src/main/java/org/apache/cassandra/sidecar/acl/authorization/Action.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.cassandra.sidecar.acl.authorization;
+
+import io.vertx.ext.auth.authorization.Authorization;
+import org.jetbrains.annotations.NotNull;
+
+import static 
org.apache.cassandra.sidecar.acl.authorization.WildcardAction.WILDCARD_PART_DIVIDER_TOKEN;
+import static 
org.apache.cassandra.sidecar.acl.authorization.WildcardAction.WILDCARD_TOKEN;
+
+/**
+ * Represents an action that can be granted to a user on a resource or across 
resources.

Review Comment:
   Generally it is the permissions granted on a resource but not action. 
Actions that can be performed on a resource are defined by the resource itself. 
Granting permission is the authorization to do something. I think you mean it 
to be a Permission rather than an Action. If yes, can this class be renamed as 
Permission?



##########
server/src/main/java/org/apache/cassandra/sidecar/acl/authorization/RoleAuthorizationsCache.java:
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.Collections;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import io.vertx.core.Vertx;
+import io.vertx.ext.auth.authorization.Authorization;
+import org.apache.cassandra.sidecar.acl.AuthCache;
+import org.apache.cassandra.sidecar.concurrent.ExecutorPools;
+import org.apache.cassandra.sidecar.config.SidecarConfiguration;
+import org.apache.cassandra.sidecar.db.SidecarPermissionsDatabaseAccessor;
+import org.apache.cassandra.sidecar.db.SystemAuthDatabaseAccessor;
+
+/**
+ * Caches role and authorizations held by it. Entries from 
system_auth.role_permissions table in Cassandra and
+ * sidecar_internal.role_permissions_v1 table are processed into 
authorizations and cached here. All table entries are
+ * stored against a unique cache key. Caching against UNIQUE_CACHE_ENTRY is 
done to make sure new entries in the table
+ * are picked up during cache refreshes.
+ */
+@Singleton
+public class RoleAuthorizationsCache extends AuthCache<String, Map<String, 
Set<Authorization>>>
+{
+    private static final String NAME = "role_permissions_cache";
+    protected static final String UNIQUE_CACHE_ENTRY = 
"unique_cache_entry_key";
+
+    @Inject
+    public RoleAuthorizationsCache(Vertx vertx,
+                                   ExecutorPools executorPools,
+                                   SidecarConfiguration sidecarConfiguration,
+                                   SystemAuthDatabaseAccessor 
systemAuthDatabaseAccessor,
+                                   SidecarPermissionsDatabaseAccessor 
sidecarPermissionsDatabaseAccessor)
+    {
+        super(NAME,
+              vertx,
+              executorPools,
+              k -> loadAuthorizations(systemAuthDatabaseAccessor,
+                                      
sidecarConfiguration.serviceConfiguration().schemaKeyspaceConfiguration().isEnabled(),
+                                      sidecarPermissionsDatabaseAccessor),
+              () -> Collections.singletonMap(UNIQUE_CACHE_ENTRY,
+                                             
loadAuthorizations(systemAuthDatabaseAccessor,
+                                                                
sidecarConfiguration.serviceConfiguration().schemaKeyspaceConfiguration().isEnabled(),
+                                                                
sidecarPermissionsDatabaseAccessor)),
+              
sidecarConfiguration.accessControlConfiguration().permissionCacheConfiguration());
+    }
+
+    public Set<Authorization> getAuthorizations(String role)
+    {
+        return get(UNIQUE_CACHE_ENTRY).get(role);
+    }
+
+    private static Map<String, Set<Authorization>> 
loadAuthorizations(SystemAuthDatabaseAccessor systemAuthDatabaseAccessor,
+                                                                      boolean 
isSidecarSchemaEnabled,
+                                                                      
SidecarPermissionsDatabaseAccessor sidecarPermissionsDatabaseAccessor)
+    {
+
+        // when entries in cache are not found, null is returned. We can not 
add null in Map
+        Map<String, Set<Authorization>>  roleAuthorizations
+        = 
Optional.ofNullable(systemAuthDatabaseAccessor.getAllRolesAndPermissions()).orElse(Collections.emptyMap());
+
+        if (isSidecarSchemaEnabled)
+        {
+            Map<String, Set<Authorization>> sidecarAuthorizations
+            = 
Optional.ofNullable(sidecarPermissionsDatabaseAccessor.getAllRolesAndPermissions()).orElse(Collections.emptyMap());
+
+            // merge authorizations from Cassandra and Sidecar tables
+            sidecarAuthorizations.forEach((role, permissions) -> {
+                roleAuthorizations.merge(role, permissions, 
(existingPermissions, newPermissions) -> {
+                    existingPermissions.addAll(newPermissions);
+                    return existingPermissions;
+                });

Review Comment:
   Can `authorizations` be used  for Set<Authorization> instead of using 
`permissions`?



##########
server/src/main/java/org/apache/cassandra/sidecar/acl/authorization/StandardAction.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * 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 io.vertx.ext.auth.authorization.Authorization;
+import io.vertx.ext.auth.authorization.PermissionBasedAuthorization;
+import io.vertx.ext.auth.authorization.impl.PermissionBasedAuthorizationImpl;
+import org.apache.cassandra.sidecar.exceptions.ConfigurationException;
+
+import static 
org.apache.cassandra.sidecar.acl.authorization.WildcardAction.WILDCARD_TOKEN;
+
+/**
+ * Standard actions need an exact match between allowed actions.
+ */
+public class StandardAction implements Action
+{
+    protected final String name;
+
+    public StandardAction(String name)
+    {
+        if (name == null || name.isEmpty())
+        {
+            throw new ConfigurationException("Action name can not be null or 
empty. To allow wildcard action across " +

Review Comment:
   Any reason to use ConfigurationException here? Doesn't 
IllegalArgumentException sound better here? 



##########
server/src/main/java/org/apache/cassandra/sidecar/acl/authorization/RoleAuthorizationsCache.java:
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.Collections;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import io.vertx.core.Vertx;
+import io.vertx.ext.auth.authorization.Authorization;
+import org.apache.cassandra.sidecar.acl.AuthCache;
+import org.apache.cassandra.sidecar.concurrent.ExecutorPools;
+import org.apache.cassandra.sidecar.config.SidecarConfiguration;
+import org.apache.cassandra.sidecar.db.SidecarPermissionsDatabaseAccessor;
+import org.apache.cassandra.sidecar.db.SystemAuthDatabaseAccessor;
+
+/**
+ * Caches role and authorizations held by it. Entries from 
system_auth.role_permissions table in Cassandra and
+ * sidecar_internal.role_permissions_v1 table are processed into 
authorizations and cached here. All table entries are
+ * stored against a unique cache key. Caching against UNIQUE_CACHE_ENTRY is 
done to make sure new entries in the table
+ * are picked up during cache refreshes.
+ */
+@Singleton
+public class RoleAuthorizationsCache extends AuthCache<String, Map<String, 
Set<Authorization>>>
+{
+    private static final String NAME = "role_permissions_cache";
+    protected static final String UNIQUE_CACHE_ENTRY = 
"unique_cache_entry_key";
+
+    @Inject
+    public RoleAuthorizationsCache(Vertx vertx,
+                                   ExecutorPools executorPools,
+                                   SidecarConfiguration sidecarConfiguration,
+                                   SystemAuthDatabaseAccessor 
systemAuthDatabaseAccessor,
+                                   SidecarPermissionsDatabaseAccessor 
sidecarPermissionsDatabaseAccessor)
+    {
+        super(NAME,
+              vertx,
+              executorPools,
+              k -> loadAuthorizations(systemAuthDatabaseAccessor,
+                                      
sidecarConfiguration.serviceConfiguration().schemaKeyspaceConfiguration().isEnabled(),
+                                      sidecarPermissionsDatabaseAccessor),
+              () -> Collections.singletonMap(UNIQUE_CACHE_ENTRY,
+                                             
loadAuthorizations(systemAuthDatabaseAccessor,
+                                                                
sidecarConfiguration.serviceConfiguration().schemaKeyspaceConfiguration().isEnabled(),
+                                                                
sidecarPermissionsDatabaseAccessor)),
+              
sidecarConfiguration.accessControlConfiguration().permissionCacheConfiguration());
+    }
+
+    public Set<Authorization> getAuthorizations(String role)
+    {
+        return get(UNIQUE_CACHE_ENTRY).get(role);

Review Comment:
   get(UNIQUE_CACHE_ENTRY) - trying to fetch entry from cache. I think we need 
a null check here (in case the cache fails to load entries).



-- 
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: pr-unsubscr...@cassandra.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org

Reply via email to