You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by GitBox <gi...@apache.org> on 2021/11/08 20:45:42 UTC

[GitHub] [cassandra] azotcsit commented on a change in pull request #1208: CASSANDRA-16914. Implement Virtual Tables for Auth Caches.

azotcsit commented on a change in pull request #1208:
URL: https://github.com/apache/cassandra/pull/1208#discussion_r736630471



##########
File path: src/java/org/apache/cassandra/auth/PasswordAuthenticator.java
##########
@@ -78,6 +78,10 @@ public boolean requireAuthentication()
         return true;
     }
 
+    public CredentialsCache getCredentialsCache() {

Review comment:
       Fixed.

##########
File path: test/unit/org/apache/cassandra/service/ClientStateTest.java
##########
@@ -64,7 +62,7 @@ public static void afterClass()
     }
 
     @Test
-    public void permissionsCheckStartsAtHeadOfResourceChain() throws Exception {
+    public void permissionsCheckStartsAtHeadOfResourceChain() {

Review comment:
       Fixed.

##########
File path: test/unit/org/apache/cassandra/db/virtual/PermissionsCacheKeysTableTest.java
##########
@@ -0,0 +1,196 @@
+/*
+ * 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.db.virtual;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
+
+import com.google.common.collect.ImmutableList;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import org.apache.cassandra.SchemaLoader;
+import org.apache.cassandra.auth.AuthTestUtils;
+import org.apache.cassandra.auth.AuthenticatedUser;
+import org.apache.cassandra.auth.DataResource;
+import org.apache.cassandra.auth.IResource;
+import org.apache.cassandra.auth.Permission;
+import org.apache.cassandra.auth.RoleResource;
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.cql3.UntypedResultSet;
+
+import static org.apache.cassandra.auth.AuthTestUtils.ROLE_A;
+import static org.apache.cassandra.auth.AuthTestUtils.ROLE_B;
+
+public class PermissionsCacheKeysTableTest extends CQLTester
+{
+    private static final String KS_NAME = "vts";
+
+    @SuppressWarnings("FieldCanBeLocal")
+    private PermissionsCacheKeysTable table;
+
+    @BeforeClass
+    public static void setUpClass()
+    {
+        // high value is used for convenient debugging
+        DatabaseDescriptor.setPermissionsValidity(20_000);
+
+        CQLTester.setUpClass();
+        AuthTestUtils.LocalCassandraRoleManager roleManager = new AuthTestUtils.LocalCassandraRoleManager();

Review comment:
       I changed tests to use `CQLTester.requireAuthentication` for local auth setup. However, I did not migrate them to use `useSuperUser`/`useUser` because these methods cannot be referred from static `@BeforeClass`. Theoretically I could put them into `@Before/@After` but I felt it is an overhead (not a problem though) to re-create resources for every test while it could be done in static `@BeforeClass` once and for all.
   
   Another advantage in the current implementation is that it is possible to easily use `IResource.applicablePermissions`. For CQL version it would require more code to construct statements and look cumbersome.
   
   Please, let me know your thoughts. If you believe that CQL is a better way, I'd like to understand the advantages. The only I can see is less dependencies on internal Java implementation (no need to changes tests if some Java classes are changed).

##########
File path: test/unit/org/apache/cassandra/db/virtual/CredentialsCacheKeysTableTest.java
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.db.virtual;
+
+import com.google.common.collect.ImmutableList;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.datastax.driver.core.EndPoint;
+import com.datastax.driver.core.PlainTextAuthProvider;
+import org.apache.cassandra.SchemaLoader;
+import org.apache.cassandra.auth.AuthTestUtils;
+import org.apache.cassandra.auth.AuthenticatedUser;
+import org.apache.cassandra.auth.IAuthenticator;
+import org.apache.cassandra.auth.RoleResource;
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.cql3.UntypedResultSet;
+
+import static org.apache.cassandra.auth.AuthTestUtils.ROLE_A;
+import static org.apache.cassandra.auth.AuthTestUtils.ROLE_B;
+
+public class CredentialsCacheKeysTableTest extends CQLTester
+{
+    private static final String KS_NAME = "vts";
+    private static AuthTestUtils.LocalPasswordAuthenticator passwordAuthenticator;
+
+    @SuppressWarnings("FieldCanBeLocal")
+    private CredentialsCacheKeysTable table;
+
+    @BeforeClass
+    public static void setUpClass()
+    {
+        // high value is used for convenient debugging
+        DatabaseDescriptor.setPermissionsValidity(20_000);
+
+        CQLTester.setUpClass();
+        AuthTestUtils.LocalCassandraRoleManager roleManager = new AuthTestUtils.LocalCassandraRoleManager();
+        passwordAuthenticator = new AuthTestUtils.LocalPasswordAuthenticator();
+        SchemaLoader.setupAuth(roleManager,
+                               passwordAuthenticator,
+                               new AuthTestUtils.LocalCassandraAuthorizer(),
+                               new AuthTestUtils.LocalCassandraNetworkAuthorizer());
+
+        roleManager.createRole(AuthenticatedUser.SYSTEM_USER, ROLE_A, AuthTestUtils.getLoginRoleOptions());
+        roleManager.createRole(AuthenticatedUser.SYSTEM_USER, ROLE_B, AuthTestUtils.getLoginRoleOptions());
+    }
+
+    @Before
+    public void config()
+    {
+        table = new CredentialsCacheKeysTable(KS_NAME);
+        VirtualKeyspaceRegistry.instance.register(new VirtualKeyspace(KS_NAME, ImmutableList.of(table)));
+
+        // ensure nothing keeps cached between tests
+        passwordAuthenticator.getCredentialsCache().invalidate();
+    }
+
+    @AfterClass
+    public static void tearDownClass()
+    {
+        DatabaseDescriptor.setPermissionsValidity(DatabaseDescriptor.getRawConfig().permissions_validity_in_ms);
+    }
+
+    @Test
+    public void testSelectAllWhenPermissionsAreNotCached() throws Throwable
+    {
+        UntypedResultSet result = execute("SELECT * FROM vts.credentials_cache_keys");

Review comment:
       Done for all tests.

##########
File path: src/java/org/apache/cassandra/db/virtual/CredentialsCacheKeysTable.java
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.db.virtual;
+
+import org.apache.cassandra.auth.IAuthenticator;
+import org.apache.cassandra.auth.PasswordAuthenticator;
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.dht.LocalPartitioner;
+import org.apache.cassandra.schema.TableMetadata;
+
+final class CredentialsCacheKeysTable extends AbstractMutableVirtualTable
+{
+    private static final String ROLE = "role";
+
+    CredentialsCacheKeysTable(String keyspace)
+    {
+        super(TableMetadata.builder(keyspace, "credentials_cache_keys")
+                .comment("keys in the credentials cache")
+                .kind(TableMetadata.Kind.VIRTUAL)
+                .partitioner(new LocalPartitioner(UTF8Type.instance))
+                .addPartitionKeyColumn(ROLE, UTF8Type.instance)
+                .build());
+    }
+
+    public DataSet data()
+    {
+        SimpleDataSet result = new SimpleDataSet(metadata());
+
+        IAuthenticator authenticator = DatabaseDescriptor.getAuthenticator();
+        if (authenticator instanceof PasswordAuthenticator)
+            ((PasswordAuthenticator) authenticator).getCredentialsCache().getAll()
+                .forEach((roleName, ignored) -> result.row(roleName));

Review comment:
       I double checked whether `IAuthenticator` is not supposed to be changed in runtime - that's correct. So I started setting up it in constructor.




-- 
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-unsubscribe@cassandra.apache.org

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



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