blerer commented on a change in pull request #1208: URL: https://github.com/apache/cassandra/pull/1208#discussion_r736583736
########## 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: The practice within the C* unit test is to inline the execute within the assertion. It is often easier to read to see the query next to the results. ########## File path: src/java/org/apache/cassandra/auth/PasswordAuthenticator.java ########## @@ -78,6 +78,10 @@ public boolean requireAuthentication() return true; } + public CredentialsCache getCredentialsCache() { Review comment: nit: The brace should be on the next line ########## 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: `CQLTester.requireAuthentication` can be used to setup the authentication and automatically create the super user. After that it is possible to use CQL queries to create roles and grant/revoke permissions ########## 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: The logic to retrieve the credential cache could be extracted. Moreover, if I am not mistaken the IAuthenticator cannot be changed without restarting the server so we could retrieve the Credential cache only once in the constructor. ########## 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: nit: The brace should be on the next line. -- 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]

