Skip to content

Commit

Permalink
Step to java-driver 4.16.0 (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommystendahl authored Jun 22, 2023
1 parent 531d45f commit 8354da0
Show file tree
Hide file tree
Showing 17 changed files with 370 additions and 566 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.exceptions.ServerError;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.servererrors.ServerError;
import com.ericsson.bss.cassandra.ecaudit.logger.Slf4jAuditLogger;
import com.ericsson.bss.cassandra.ecaudit.test.daemon.CassandraDaemonForAuditTest;
import org.mockito.ArgumentCaptor;
Expand All @@ -50,8 +48,7 @@
@RunWith(MockitoJUnitRunner.class)
public class ITAllowAllAuthorizer
{
private static Cluster cluster;
private static Session session;
private static CqlSession session;

@Captor
private ArgumentCaptor<ILoggingEvent> loggingEventCaptor;
Expand All @@ -63,8 +60,7 @@ public class ITAllowAllAuthorizer
public static void beforeClass() throws Exception
{
CassandraDaemonForAuditTest cdt = CassandraDaemonForAuditTest.getInstance();
cluster = cdt.createCluster();
session = cluster.connect();
session = cdt.createSession();
}

@Before
Expand All @@ -86,7 +82,6 @@ public void after()
public static void afterClass()
{
session.close();
cluster.close();
}

@Test
Expand Down Expand Up @@ -116,10 +111,8 @@ public void testGrantFails()

private void givenTable(String keyspace, String table)
{
session.execute(new SimpleStatement(
"CREATE KEYSPACE IF NOT EXISTS " + keyspace + " WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1} AND DURABLE_WRITES = false"));
session.execute(new SimpleStatement(
"CREATE TABLE IF NOT EXISTS " + keyspace + "." + table + " (key int PRIMARY KEY, value text)"));
session.execute("CREATE KEYSPACE IF NOT EXISTS " + keyspace + " WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1} AND DURABLE_WRITES = false");
session.execute("CREATE TABLE IF NOT EXISTS " + keyspace + "." + table + " (key int PRIMARY KEY, value text)");
}

private List<String> getLogEntries()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.exceptions.UnauthorizedException;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.servererrors.UnauthorizedException;
import com.ericsson.bss.cassandra.ecaudit.logger.Slf4jAuditLogger;
import com.ericsson.bss.cassandra.ecaudit.test.daemon.CassandraDaemonForAuditTest;
import org.mockito.ArgumentCaptor;
Expand All @@ -50,8 +48,7 @@
@RunWith(MockitoJUnitRunner.class)
public class ITQueryLogger
{
private static Cluster cluster;
private static Session session;
private static CqlSession session;

@Captor
private ArgumentCaptor<ILoggingEvent> loggingEventCaptor;
Expand All @@ -63,8 +60,7 @@ public class ITQueryLogger
public static void beforeClass() throws Exception
{
CassandraDaemonForAuditTest cdt = CassandraDaemonForAuditTest.getInstance();
cluster = cdt.createCluster();
session = cluster.connect();
session = cdt.createSession();
}

@Before
Expand All @@ -86,7 +82,6 @@ public void after()
public static void afterClass()
{
session.close();
cluster.close();
}

@Test
Expand Down Expand Up @@ -116,10 +111,8 @@ public void testGrantFails()

private void givenTable(String keyspace, String table)
{
session.execute(new SimpleStatement(
"CREATE KEYSPACE IF NOT EXISTS " + keyspace + " WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1} AND DURABLE_WRITES = false"));
session.execute(new SimpleStatement(
"CREATE TABLE IF NOT EXISTS " + keyspace + "." + table + " (key int PRIMARY KEY, value text)"));
session.execute("CREATE KEYSPACE IF NOT EXISTS " + keyspace + " WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1} AND DURABLE_WRITES = false");
session.execute("CREATE TABLE IF NOT EXISTS " + keyspace + "." + table + " (key int PRIMARY KEY, value text)");
}

private List<String> getLogEntries()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.exceptions.AuthenticationException;
import com.datastax.oss.driver.api.core.AllNodesFailedException;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
import com.ericsson.bss.cassandra.ecaudit.AuditAdapter;
import com.ericsson.bss.cassandra.ecaudit.common.record.Status;
import com.ericsson.bss.cassandra.ecaudit.common.record.StoredAuditRecord;
Expand Down Expand Up @@ -66,12 +64,10 @@ public class ITVerifyChronicleBackend
private static final AtomicInteger usernameNumber = new AtomicInteger();

private static CassandraDaemonForAuditTest cdt;
private static Cluster superCluster;
private static Session superSession;
private static CqlSession superSession;

private static String testUsername;
private static Cluster testCluster;
private static Session testSession;
private static CqlSession testSession;

private static QueueReader reader;
private static AuditLogger customLogger;
Expand All @@ -81,37 +77,24 @@ public static void beforeClass() throws Exception
{
cdt = CassandraDaemonForAuditTest.getInstance();

try (Cluster cassandraCluster = cdt.createCluster();
Session cassandraSession = cassandraCluster.connect())
try (CqlSession cassandraSession = cdt.createSession())
{
cassandraSession.execute(new SimpleStatement(
"CREATE ROLE " + SUITE_SUPER_USER + " WITH PASSWORD = 'secret' AND LOGIN = true AND SUPERUSER = true"));
cassandraSession.execute(new SimpleStatement(
"ALTER ROLE " + SUITE_SUPER_USER + " WITH OPTIONS = { 'grant_audit_whitelist_for_execute' : 'connections' }"));
cassandraSession.execute(new SimpleStatement(
"ALTER ROLE " + SUITE_SUPER_USER + " WITH OPTIONS = { 'grant_audit_whitelist_for_create' : 'roles'}"));
cassandraSession.execute(new SimpleStatement(
"ALTER ROLE " + SUITE_SUPER_USER + " WITH OPTIONS = { 'grant_audit_whitelist_for_alter' : 'roles'}"));
cassandraSession.execute(new SimpleStatement(
"ALTER ROLE " + SUITE_SUPER_USER + " WITH OPTIONS = { 'grant_audit_whitelist_for_drop' : 'roles'}"));
cassandraSession.execute(new SimpleStatement(
"ALTER ROLE " + SUITE_SUPER_USER + " WITH OPTIONS = { 'grant_audit_whitelist_for_create' : 'data'}"));
cassandraSession.execute(new SimpleStatement(
"ALTER ROLE " + SUITE_SUPER_USER + " WITH OPTIONS = { 'grant_audit_whitelist_for_alter' : 'data'}"));
cassandraSession.execute(new SimpleStatement(
"ALTER ROLE " + SUITE_SUPER_USER + " WITH OPTIONS = { 'grant_audit_whitelist_for_drop' : 'data'}"));
cassandraSession.execute(new SimpleStatement(
"ALTER ROLE " + SUITE_SUPER_USER + " WITH OPTIONS = { 'grant_audit_whitelist_for_select' : 'data'}"));
cassandraSession.execute(new SimpleStatement(
"ALTER ROLE " + SUITE_SUPER_USER + " WITH OPTIONS = { 'grant_audit_whitelist_for_modify' : 'data'}"));
cassandraSession.execute("CREATE ROLE " + SUITE_SUPER_USER + " WITH PASSWORD = 'secret' AND LOGIN = true AND SUPERUSER = true");
cassandraSession.execute("ALTER ROLE " + SUITE_SUPER_USER + " WITH OPTIONS = { 'grant_audit_whitelist_for_execute' : 'connections' }");
cassandraSession.execute("ALTER ROLE " + SUITE_SUPER_USER + " WITH OPTIONS = { 'grant_audit_whitelist_for_create' : 'roles'}");
cassandraSession.execute("ALTER ROLE " + SUITE_SUPER_USER + " WITH OPTIONS = { 'grant_audit_whitelist_for_alter' : 'roles'}");
cassandraSession.execute("ALTER ROLE " + SUITE_SUPER_USER + " WITH OPTIONS = { 'grant_audit_whitelist_for_drop' : 'roles'}");
cassandraSession.execute("ALTER ROLE " + SUITE_SUPER_USER + " WITH OPTIONS = { 'grant_audit_whitelist_for_create' : 'data'}");
cassandraSession.execute("ALTER ROLE " + SUITE_SUPER_USER + " WITH OPTIONS = { 'grant_audit_whitelist_for_alter' : 'data'}");
cassandraSession.execute("ALTER ROLE " + SUITE_SUPER_USER + " WITH OPTIONS = { 'grant_audit_whitelist_for_drop' : 'data'}");
cassandraSession.execute("ALTER ROLE " + SUITE_SUPER_USER + " WITH OPTIONS = { 'grant_audit_whitelist_for_select' : 'data'}");
cassandraSession.execute("ALTER ROLE " + SUITE_SUPER_USER + " WITH OPTIONS = { 'grant_audit_whitelist_for_modify' : 'data'}");
}

superCluster = cdt.createCluster(SUITE_SUPER_USER, "secret");
superSession = superCluster.connect();
superSession = cdt.createSession(SUITE_SUPER_USER, "secret");

testUsername = givenUniqueSuperuserWithMinimalWhitelist();
testCluster = cdt.createCluster(testUsername, "secret");
testSession = testCluster.connect();
testSession = cdt.createSession(testUsername, "secret");

Path auditDirectory = CassandraDaemonForAuditTest.getInstance().getAuditDirectory();
ToolOptions options = ToolOptions
Expand Down Expand Up @@ -150,28 +133,25 @@ public static void afterClass()
AuditAdapter.getInstance().getAuditor().removeLogger(customLogger);

testSession.close();
testCluster.close();

for (int i = 0; i < usernameNumber.get(); i++)
{
superSession.execute(new SimpleStatement("DROP ROLE IF EXISTS " + SUITE_TEST_USER_PREFIX + i));
superSession.execute("DROP ROLE IF EXISTS " + SUITE_TEST_USER_PREFIX + i);
}
superSession.close();
superCluster.close();

try (Cluster cassandraCluster = cdt.createCluster();
Session cassandraSession = cassandraCluster.connect())
try (CqlSession cassandraSession = cdt.createSession())
{
cassandraSession.execute(new SimpleStatement("DROP ROLE IF EXISTS " + SUITE_SUPER_USER));
cassandraSession.execute("DROP ROLE IF EXISTS " + SUITE_SUPER_USER);
}
}

@Test
public void testFailedAuthenticationRequest()
{
assertThatExceptionOfType(AuthenticationException.class)
.isThrownBy(() -> cdt.createCluster("user", "password").connect())
.withMessageContaining("Authentication error");
assertThatExceptionOfType(AllNodesFailedException.class)
.isThrownBy(() -> cdt.createSession("user", "password"))
.withMessageContaining("AuthenticationException: Authentication error");

List<StoredAuditRecord> records = waitAndGetRecords();
assertThat(records).hasSize(2);
Expand Down Expand Up @@ -286,15 +266,13 @@ private List<StoredAuditRecord> getRecords()

private void givenKeyspace(String keyspace)
{
superSession.execute(new SimpleStatement(
"CREATE KEYSPACE IF NOT EXISTS " + keyspace + " WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1} AND DURABLE_WRITES = false"));
superSession.execute("CREATE KEYSPACE IF NOT EXISTS " + keyspace + " WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1} AND DURABLE_WRITES = false");
}

private void givenTable(String keyspace, String table)
{
givenKeyspace(keyspace);
superSession.execute(new SimpleStatement(
"CREATE TABLE IF NOT EXISTS " + keyspace + "." + table + " (key int PRIMARY KEY, value text)"));
superSession.execute("CREATE TABLE IF NOT EXISTS " + keyspace + "." + table + " (key int PRIMARY KEY, value text)");
}

private static String getSuperuserWithMinimalWhitelist()
Expand All @@ -305,26 +283,20 @@ private static String getSuperuserWithMinimalWhitelist()
private static String givenUniqueSuperuserWithMinimalWhitelist()
{
String username = SUITE_TEST_USER_PREFIX + usernameNumber.getAndIncrement();
superSession.execute(new SimpleStatement(
"CREATE ROLE " + username + " WITH PASSWORD = 'secret' AND LOGIN = true AND SUPERUSER = true"));
superSession.execute(new SimpleStatement(
"ALTER ROLE " + username + " WITH OPTIONS = { 'grant_audit_whitelist_for_execute' : 'connections' }"));
superSession.execute(new SimpleStatement(
"ALTER ROLE " + username + " WITH OPTIONS = { 'grant_audit_whitelist_for_select' : 'data/system' }"));
superSession.execute(new SimpleStatement(
"ALTER ROLE " + username + " WITH OPTIONS = { 'grant_audit_whitelist_for_select' : 'data/system_schema' }"));
superSession.execute("CREATE ROLE " + username + " WITH PASSWORD = 'secret' AND LOGIN = true AND SUPERUSER = true");
superSession.execute("ALTER ROLE " + username + " WITH OPTIONS = { 'grant_audit_whitelist_for_execute' : 'connections' }");
superSession.execute("ALTER ROLE " + username + " WITH OPTIONS = { 'grant_audit_whitelist_for_select' : 'data/system' }");
superSession.execute("ALTER ROLE " + username + " WITH OPTIONS = { 'grant_audit_whitelist_for_select' : 'data/system_schema' }");
superSession.execute("ALTER ROLE " + username + " WITH OPTIONS = { 'grant_audit_whitelist_for_select' : 'data/system_virtual_schema' }");
return username;
}

private void resetTestUserWithMinimalWhitelist(String username)
{
superSession.execute(new SimpleStatement(
"DELETE FROM system_auth.role_audit_whitelists_v2 WHERE role = '" + username + "'"));
superSession.execute(new SimpleStatement(
"ALTER ROLE " + username + " WITH OPTIONS = { 'grant_audit_whitelist_for_execute' : 'connections' }"));
superSession.execute(new SimpleStatement(
"ALTER ROLE " + username + " WITH OPTIONS = { 'grant_audit_whitelist_for_select' : 'data/system' }"));
superSession.execute(new SimpleStatement(
"ALTER ROLE " + username + " WITH OPTIONS = { 'grant_audit_whitelist_for_select' : 'data/system_schema' }"));
superSession.execute("DELETE FROM system_auth.role_audit_whitelists_v2 WHERE role = '" + username + "'");
superSession.execute("ALTER ROLE " + username + " WITH OPTIONS = { 'grant_audit_whitelist_for_execute' : 'connections' }");
superSession.execute("ALTER ROLE " + username + " WITH OPTIONS = { 'grant_audit_whitelist_for_select' : 'data/system' }");
superSession.execute("ALTER ROLE " + username + " WITH OPTIONS = { 'grant_audit_whitelist_for_select' : 'data/system_schema' }");
superSession.execute("ALTER ROLE " + username + " WITH OPTIONS = { 'grant_audit_whitelist_for_select' : 'data/system_virtual_schema' }");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.CodecRegistry;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ProtocolVersion;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.TupleType;
import com.datastax.driver.core.UserType;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
import com.datastax.oss.driver.api.core.type.DataTypes;
import com.datastax.oss.driver.api.core.type.TupleType;
import com.datastax.oss.driver.api.core.type.UserDefinedType;
import com.ericsson.bss.cassandra.ecaudit.AuditAdapter;
import com.ericsson.bss.cassandra.ecaudit.entry.suppressor.BoundValueSuppressor;
import com.ericsson.bss.cassandra.ecaudit.entry.suppressor.SuppressBlobs;
Expand Down Expand Up @@ -81,8 +77,7 @@ public class ITVerifyCustomBoundValueSuppressor
private static final String TABLE = "CREATE TABLE ks1.t1 (key1 text, key2 int, key3 text, val1 blob, val2 list<blob>, val3 map<int, frozen<list<blob>>>, val4 int, val5 tuple<text, blob>, val6 frozen<ks1.mytype>, PRIMARY KEY((key1, key2), key3))";
private static final String INSERT = "INSERT INTO ks1.t1 (key1, key2, key3, val1, val2, val3, val4, val5, val6) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";

private static Cluster cluster;
private static Session session;
private static CqlSession session;
private static AuditLogger customLogger;

private static BoundValueSuppressor defaultSuppressor;
Expand All @@ -97,12 +92,11 @@ public class ITVerifyCustomBoundValueSuppressor
public static void beforeClass() throws Exception
{
CassandraDaemonForAuditTest cdt = CassandraDaemonForAuditTest.getInstance();
cluster = cdt.createCluster();
session = cluster.connect();
session = cdt.createSession();

session.execute(new SimpleStatement(KEYSPACE));
session.execute(new SimpleStatement(UDT));
session.execute(new SimpleStatement(TABLE));
session.execute(KEYSPACE);
session.execute(UDT);
session.execute(TABLE);

// Configure logger with custom format with only operation
customLogger = new Slf4jAuditLogger(Collections.singletonMap("log_format", "operation=${OPERATION}"), CUSTOM_LOGGER_NAME);
Expand Down Expand Up @@ -130,7 +124,6 @@ public static void afterClass()
{
AuditAdapter.getInstance().getAuditor().removeLogger(customLogger);
session.close();
cluster.close();
setBoundValueSuppressor(defaultSuppressor);
}

Expand Down Expand Up @@ -206,8 +199,12 @@ private String getLogEntry()

private void executePreparedStatement()
{
TupleType tupleType = TupleType.of(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE, DataType.text(), DataType.blob());
UserType udt = session.getCluster().getMetadata().getKeyspace("ks1").getUserType("mytype");
TupleType tupleType = DataTypes.tupleOf(DataTypes.TEXT, DataTypes.BLOB);
UserDefinedType udt = session.getMetadata()
.getKeyspace("ks1")
.flatMap(ks -> ks.getUserDefinedType("mytype"))
.orElseThrow(() -> new IllegalArgumentException("Missing UDT definition"));


PreparedStatement preparedInsert = session.prepare(INSERT);
session.execute(preparedInsert.bind("PartKey1",
Expand All @@ -218,7 +215,7 @@ private void executePreparedStatement()
Collections.singletonMap(99, Arrays.asList(createBlob(4), createBlob(4))),
43,
tupleType.newValue("Hello", createBlob(4)),
udt.newValue().setString("mykey", "Kalle").setBytes("myval", (ByteBuffer)createBlob(16))
udt.newValue().setString("mykey", "Kalle").setByteBuffer("myval", (ByteBuffer)createBlob(16))
));
}

Expand Down
Loading

0 comments on commit 8354da0

Please sign in to comment.