adelapena commented on code in PR #2156: URL: https://github.com/apache/cassandra/pull/2156#discussion_r1109803716
########## test/distributed/org/apache/cassandra/distributed/test/ColumnMaskTest.java: ########## @@ -0,0 +1,107 @@ +/* + * 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.distributed.test; + +import java.util.function.Consumer; + +import org.junit.Test; + +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Session; +import org.apache.cassandra.auth.CassandraRoleManager; +import org.apache.cassandra.distributed.Cluster; +import org.apache.cassandra.distributed.api.IInvokableInstance; +import org.apache.cassandra.distributed.impl.RowUtil; + +import static java.util.concurrent.TimeUnit.MINUTES; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.apache.cassandra.auth.CassandraRoleManager.DEFAULT_SUPERUSER_NAME; +import static org.apache.cassandra.auth.CassandraRoleManager.DEFAULT_SUPERUSER_PASSWORD; +import static org.apache.cassandra.distributed.api.Feature.GOSSIP; +import static org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL; +import static org.apache.cassandra.distributed.shared.AssertUtils.assertRows; +import static org.apache.cassandra.distributed.shared.AssertUtils.row; +import static org.awaitility.Awaitility.await; + +/** + * Tests for dynamic data masking. + */ +public class ColumnMaskTest extends TestBaseImpl +{ + /** + * Tests that column masks using UDFs are correctly loaded on startup. + * The UDF should be loaded before it's referenced by the mask. + */ + @Test + public void testUDFMaskedColumnsOnStartup() throws Throwable + { + try (Cluster cluster = init(Cluster.build() + .withNodes(1) + .withConfig(conf -> conf.with(GOSSIP, NATIVE_PROTOCOL) + .set("user_defined_functions_enabled", "true") + .set("authenticator", "PasswordAuthenticator") + .set("authorizer", "CassandraAuthorizer")) + .start())) + { + IInvokableInstance node = cluster.get(1); + + // create a table with a column masked with a UDF + cluster.schemaChange(withKeyspace("CREATE FUNCTION %s.f(column text, replacement text) " + + "RETURNS NULL ON NULL INPUT " + + "RETURNS text " + + "LANGUAGE java " + + "AS 'return replacement;'")); + cluster.schemaChange(withKeyspace("CREATE TABLE %s.t (k int PRIMARY KEY, v text MASKED WITH %<s.f('redacted'))")); + node.executeInternal(withKeyspace("INSERT INTO %s.t(k, v) VALUES (0, 'secret')")); + + // create a user without UNMASK permission + withAuthenticatedSession(node, DEFAULT_SUPERUSER_NAME, DEFAULT_SUPERUSER_PASSWORD, session -> { + session.execute("CREATE USER test WITH PASSWORD 'test'"); + session.execute(withKeyspace("GRANT ALL ON KEYSPACE %s TO test")); + session.execute(withKeyspace("REVOKE UNMASK ON KEYSPACE %s FROM test")); + }); + + // restart the node, so the schema elements (the UDF and the mask) have to be loaded + node.shutdown().get(); + node.startup(); + + // verify that the user without UNMASK permission can't see the clear data + withAuthenticatedSession(node, "test", "test", session -> { + ResultSet resultSet = session.execute(withKeyspace("SELECT * FROM %s.t")); + assertRows(RowUtil.toObjects(resultSet), row(0, "redacted")); + }); + } + } + + private static void withAuthenticatedSession(IInvokableInstance instance, String username, String password, Consumer<Session> consumer) + { Review Comment: Not only. We would need something similar for authentication, but we can't use `callOnInstance` on upgrade tests. Also, as it's mentioned on that conversation we would also need a custom load balancing policy since the upgrade test involves multiple nodes. And, since upgrade tests are more sensitive to OOMs and timeouts, we would need to split the test. I have given it a go on top of 18069, [on this commit](https://github.com/apache/cassandra/pull/2125/commits/58e4cb9f98c462baa80ca1b6be43ccc376b34f7f). Some repeated runs can be found [here](https://app.circleci.com/pipelines/github/adelapena/cassandra/2650/workflows/a75e1832-bd9e-4eba-947b-e7782d6457d2). -- 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]

