Skip to content

Commit

Permalink
Merge branch 'release/c3.0' into release/c3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
tommystendahl committed Oct 5, 2023
2 parents b50d649 + 9594a53 commit df8ecfa
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ public static void beforeClass() throws Exception
"ALTER ROLE cassandra WITH OPTIONS = { 'grant_audit_whitelist_for_all' : 'data/system_schema' }"));

session.execute(new SimpleStatement(
"CREATE KEYSPACE ecks WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1} AND DURABLE_WRITES = false"));
session.execute(new SimpleStatement("CREATE TABLE ecks.ectbl (partk int PRIMARY KEY, clustk text, value text)"));
"CREATE KEYSPACE ecks_itva WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1} AND DURABLE_WRITES = false"));
session.execute(new SimpleStatement("CREATE TABLE ecks_itva.ectbl (partk int PRIMARY KEY, clustk text, value text)"));
session.execute(new SimpleStatement(
"CREATE TABLE ecks.ectypetbl (partk int PRIMARY KEY, v0 text, v1 ascii, v2 bigint, v3 blob, v4 boolean, "
"CREATE TABLE ecks_itva.ectypetbl (partk int PRIMARY KEY, v0 text, v1 ascii, v2 bigint, v3 blob, v4 boolean, "
+ "v5 date, v6 decimal, v7 double, v8 float, v9 inet, v10 int, v11 smallint, v12 time, v13 timestamp, "
+ "v14 uuid, v15 varchar, v16 varint)"));

Expand All @@ -115,17 +115,17 @@ public static void beforeClass() throws Exception
session.execute(new SimpleStatement(
"ALTER ROLE sam WITH OPTIONS = { 'grant_audit_whitelist_for_all' : 'data/system_schema'}"));
session.execute(new SimpleStatement(
"ALTER ROLE sam WITH OPTIONS = { 'grant_audit_whitelist_for_all' : 'data/ecks/ectbl'}"));
"ALTER ROLE sam WITH OPTIONS = { 'grant_audit_whitelist_for_all' : 'data/ecks_itva/ectbl'}"));
session.execute(new SimpleStatement(
"ALTER ROLE sam WITH OPTIONS = { 'grant_audit_whitelist_for_all' : 'data/nonexistingks'}"));
session.execute(new SimpleStatement(
"ALTER ROLE sam WITH OPTIONS = { 'grant_audit_whitelist_for_all' : 'data/ecks/nonexistingtbl'}"));
"ALTER ROLE sam WITH OPTIONS = { 'grant_audit_whitelist_for_all' : 'data/ecks_itva/nonexistingtbl'}"));
session.execute(new SimpleStatement(
"ALTER ROLE sam WITH OPTIONS = { 'grant_audit_whitelist_for_all' : 'connections'}"));
session.execute(new SimpleStatement(
"GRANT MODIFY ON ecks.ectbl TO sam"));
"GRANT MODIFY ON ecks_itva.ectbl TO sam"));
session.execute(new SimpleStatement(
"GRANT SELECT ON ecks.ectbl TO sam"));
"GRANT SELECT ON ecks_itva.ectbl TO sam"));

session.execute(new SimpleStatement(
"CREATE ROLE foo WITH PASSWORD = 'secret' AND LOGIN = true AND SUPERUSER = true"));
Expand Down Expand Up @@ -177,14 +177,20 @@ public void after()
@AfterClass
public static void afterClass()
{
session.execute(new SimpleStatement("DROP KEYSPACE IF EXISTS ecks"));
session.execute(new SimpleStatement("DROP ROLE IF EXISTS ecuser"));
session.execute(new SimpleStatement("DROP ROLE IF EXISTS foo"));
session.execute(new SimpleStatement("DROP ROLE IF EXISTS bar"));
session.execute(new SimpleStatement("DROP ROLE IF EXISTS mute"));

session.close();
cluster.close();
if(session != null)
{
session.execute(new SimpleStatement("DROP KEYSPACE IF EXISTS ecks_itva"));
session.execute(new SimpleStatement("DROP ROLE IF EXISTS ecuser"));
session.execute(new SimpleStatement("DROP ROLE IF EXISTS foo"));
session.execute(new SimpleStatement("DROP ROLE IF EXISTS bar"));
session.execute(new SimpleStatement("DROP ROLE IF EXISTS mute"));

session.close();
}
if(cluster != null)
{
cluster.close();
}
}

@Test
Expand Down Expand Up @@ -292,18 +298,18 @@ public void testValidSimpleStatementsAreLogged()
public void testValidPreparedStatementsAreLogged()
{
PreparedStatement preparedInsertStatement = session
.prepare("INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (?, ?, ?)");
.prepare("INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (?, ?, ?)");
PreparedStatement preparedSelectStatement = session
.prepare("SELECT * FROM ecks.ectbl WHERE partk = ?");
PreparedStatement preparedDeleteStatement = session.prepare("DELETE FROM ecks.ectbl WHERE partk = ?");
.prepare("SELECT * FROM ecks_itva.ectbl WHERE partk = ?");
PreparedStatement preparedDeleteStatement = session.prepare("DELETE FROM ecks_itva.ectbl WHERE partk = ?");

List<String> expectedStatements = Arrays.asList(
"INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (?, ?, ?)[1, '1', 'valid']",
"SELECT * FROM ecks.ectbl WHERE partk = ?[1]",
"DELETE FROM ecks.ectbl WHERE partk = ?[1]",
"INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (?, ?, ?)[2, '2', 'valid']",
"SELECT * FROM ecks.ectbl WHERE partk = ?[2]",
"DELETE FROM ecks.ectbl WHERE partk = ?[2]");
"INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (?, ?, ?)[1, '1', 'valid']",
"SELECT * FROM ecks_itva.ectbl WHERE partk = ?[1]",
"DELETE FROM ecks_itva.ectbl WHERE partk = ?[1]",
"INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (?, ?, ?)[2, '2', 'valid']",
"SELECT * FROM ecks_itva.ectbl WHERE partk = ?[2]",
"DELETE FROM ecks_itva.ectbl WHERE partk = ?[2]");

for (int i = 1; i <= 2; i++)
{
Expand All @@ -326,11 +332,11 @@ public void testValidPreparedStatementsAreLogged()
@Test
public void testPreparedStatementsWithoutValuesAreLogged()
{
PreparedStatement preparedInsertStatement = session.prepare("SELECT * FROM ecks.ectbl");
PreparedStatement preparedInsertStatement = session.prepare("SELECT * FROM ecks_itva.ectbl");

List<String> expectedStatements = Arrays.asList(
"SELECT * FROM ecks.ectbl[]",
"SELECT * FROM ecks.ectbl[]");
"SELECT * FROM ecks_itva.ectbl[]",
"SELECT * FROM ecks_itva.ectbl[]");

for (int i = 1; i <= 2; i++)
{
Expand All @@ -351,11 +357,11 @@ public void testPreparedStatementsWithoutValuesAreLogged()
@Test
public void testPreloadedPreparedStatementsAreLogged()
{
PreparedStatement preparedSelectStatement = session.prepare("SELECT * FROM ecks.ectbl");
PreparedStatement preparedSelectStatement = session.prepare("SELECT * FROM ecks_itva.ectbl");

List<String> expectedStatements = Arrays.asList(
"SELECT * FROM ecks.ectbl[]",
"SELECT * FROM ecks.ectbl[]");
"SELECT * FROM ecks_itva.ectbl[]",
"SELECT * FROM ecks_itva.ectbl[]");

// Simulate caching behavior during a restart
QueryProcessor.clearPreparedStatements(true);
Expand All @@ -381,22 +387,22 @@ public void testPreloadedPreparedStatementsAreLogged()
public void testValidBatchStatementsAreLogged()
{
PreparedStatement preparedInsertStatement1 = session
.prepare("INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (?, ?, ?)");
.prepare("INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (?, ?, ?)");
PreparedStatement preparedInsertStatement2 = session
.prepare("INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (?, ?, 'valid')");
.prepare("INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (?, ?, 'valid')");

List<String> expectedStatements = Arrays.asList(
"INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (?, ?, ?)[1, '1', 'valid']",
"INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (?, ?, ?)[2, '2', 'valid']",
"INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (?, ?, 'valid')[3, '3']",
"INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (4, '4', 'valid')",
"INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (?, ?, 'valid')[5, '5']");
"INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (?, ?, ?)[1, '1', 'valid']",
"INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (?, ?, ?)[2, '2', 'valid']",
"INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (?, ?, 'valid')[3, '3']",
"INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (4, '4', 'valid')",
"INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (?, ?, 'valid')[5, '5']");

BatchStatement batch = new BatchStatement(BatchStatement.Type.UNLOGGED);
batch.add(preparedInsertStatement1.bind(1, "1", "valid"));
batch.add(preparedInsertStatement1.bind(2, "2", "valid"));
batch.add(preparedInsertStatement2.bind(3, "3"));
batch.add(new SimpleStatement("INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (4, '4', 'valid')"));
batch.add(new SimpleStatement("INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (4, '4', 'valid')"));
batch.add(preparedInsertStatement2.bind(5, "5"));
session.execute(batch);

Expand All @@ -416,11 +422,11 @@ public void testValidBatchStatementsAreLogged()
public void testValidNonPreparedBatchStatementsAreLogged()
{
List<String> allStatements = Arrays.asList(
"INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (1, '1', 'valid')",
"INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (2, '2', 'valid')",
"INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (3, '3', 'valid')",
"INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (4, '4', 'valid')",
"INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (5, '5', 'valid')");
"INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (1, '1', 'valid')",
"INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (2, '2', 'valid')",
"INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (3, '3', 'valid')",
"INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (4, '4', 'valid')",
"INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (5, '5', 'valid')");

StringBuilder batchStatementBuilder = new StringBuilder("BEGIN UNLOGGED BATCH ");
for (String statement : allStatements)
Expand Down Expand Up @@ -448,8 +454,8 @@ public void testValidNonPreparedBatchStatementsAreLogged()
public void testValidPreparedBatchStatementsAreLogged()
{
String statement = "BEGIN UNLOGGED BATCH USING TIMESTAMP ? " +
"INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (?, ?, ?); " +
"INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (?, ?, 'valid'); " +
"INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (?, ?, ?); " +
"INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (?, ?, 'valid'); " +
"APPLY BATCH;";

List<String> expectedStatements = Collections.singletonList(statement + "[1234, 1, '1', 'valid', 3, '3']");
Expand All @@ -472,12 +478,12 @@ public void testValidPreparedBatchStatementsAreLogged()
public void testValidPreparedStatementTypesAreLogged() throws Exception
{
PreparedStatement preparedStatement = session
.prepare("INSERT INTO ecks.ectypetbl "
.prepare("INSERT INTO ecks_itva.ectypetbl "
+ "(partk, v0, v1, v2, v4, v5, v9, v13, v15)"
+ " VALUES "
+ "(?, ?, ?, ?, ?, ?, ?, ?, ?)");

String expectedStatement = "INSERT INTO ecks.ectypetbl "
String expectedStatement = "INSERT INTO ecks_itva.ectypetbl "
+ "(partk, v0, v1, v2, v4, v5, v9, v13, v15)"
+ " VALUES "
+ "(?, ?, ?, ?, ?, ?, ?, ?, ?)[1, 'text', 'ascii', 123123123123123123, true, 1976-02-25, 8.8.8.8, 2004-05-29T14:29:00.000Z, 'varchar']";
Expand All @@ -504,7 +510,7 @@ public void testValidPreparedStatementTypesAreLogged() throws Exception
@Test
public void testValidSimpleStatementTypesAreLogged()
{
String statement = "INSERT INTO ecks.ectypetbl "
String statement = "INSERT INTO ecks_itva.ectypetbl "
+ "(partk, v0, v1, v2, v4, v5, v9, v13, v15)"
+ " VALUES "
+ "(1, 'text', 'ascii', 123123123123123123, true, '1976-02-25', '8.8.8.8', '2004-05-29T14:29:00.000Z', 'varchar')";
Expand Down Expand Up @@ -556,9 +562,9 @@ public void testWhitelistedUserValidStatementsWithUseAreNotLogged()
// Driver or Cassandra will add double-quotes to ks on one of the connections if statemens doesn't have it here.
// TODO: Research if this is "bug" in Cassandra, driver or ecAudit?
List<String> statements = Arrays.asList(
"INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (1, 'one', 'valid')",
"SELECT * FROM ecks.ectbl",
"USE \"ecks\"",
"INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (1, 'one', 'valid')",
"SELECT * FROM ecks_itva.ectbl",
"USE \"ecks_itva\"",
"INSERT INTO ectbl (partk, clustk, value) VALUES (1, 'one', 'valid')",
"SELECT * FROM ectbl");

Expand All @@ -582,7 +588,7 @@ public void testWhitelistedUserValidStatementsWithUseAreNotLogged()
.stream()
.map(ILoggingEvent::getFormattedMessage)
.collect(Collectors.toList()))
.containsOnlyElementsOf(expectedAttemptsAsUser(Arrays.asList("USE \"ecks\""), user));
.containsOnlyElementsOf(expectedAttemptsAsUser(Arrays.asList("USE \"ecks_itva\""), user));
}

/**
Expand All @@ -599,7 +605,7 @@ public void testMultipleUseStatementsPreserveOrder()
try (Cluster privateCluster = cdt.createCluster(user, "secret");
Session privateSession = privateCluster.connect())
{
executeOneUseWithFollowingSelect(user, privateSession, "USE \"ecks\"");
executeOneUseWithFollowingSelect(user, privateSession, "USE \"ecks_itva\"");
executeOneUseWithFollowingSelect(user, privateSession, "USE \"ecks2\"");
executeOneUseWithFollowingSelect(user, privateSession, "USE \"ecks3\"");
}
Expand All @@ -608,14 +614,14 @@ public void testMultipleUseStatementsPreserveOrder()
private void executeOneUseWithFollowingSelect(String user, Session privateSession, String useStatement) {
ArgumentCaptor<ILoggingEvent> loggingEventCaptor1 = ArgumentCaptor.forClass(ILoggingEvent.class);
privateSession.execute(new SimpleStatement(useStatement));
privateSession.execute(new SimpleStatement("SELECT * FROM ecks.ectypetbl"));
privateSession.execute(new SimpleStatement("SELECT * FROM ecks_itva.ectypetbl"));
verify(mockAuditAppender, atLeast(2)).doAppend(loggingEventCaptor1.capture());
List<ILoggingEvent> loggingEvents1 = loggingEventCaptor1.getAllValues();
assertThat(loggingEvents1
.stream()
.map(ILoggingEvent::getFormattedMessage)
.collect(Collectors.toList()))
.containsOnlyElementsOf(expectedAttemptsAsUser(Arrays.asList(useStatement, "SELECT * FROM ecks.ectypetbl"), user));
.containsOnlyElementsOf(expectedAttemptsAsUser(Arrays.asList(useStatement, "SELECT * FROM ecks_itva.ectypetbl"), user));
reset(mockAuditAppender);
}

Expand All @@ -624,11 +630,11 @@ public void testFailedStatementsAreLogged()
{
List<String> statements = Arrays.asList(
"CREATE ROLE ecuser WITH PASSWORD = 'secret' AND LOGIN = true",
"CREATE KEYSPACE ecks WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1} AND DURABLE_WRITES = false",
"CREATE TABLE ecks.ectbl (partk int PRIMARY KEY, clustk text, value text)",
"CREATE KEYSPACE ecks_itva WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1} AND DURABLE_WRITES = false",
"CREATE TABLE ecks_itva.ectbl (partk int PRIMARY KEY, clustk text, value text)",
"INSERT INTO invalidks.invalidtbl (partk, clustk, value) VALUES (1, 'one', 'valid')",
"SELECT * FROM invalidks.invalidtbl",
"SELECT * FROM ecks.invalidtbl",
"SELECT * FROM ecks_itva.invalidtbl",
"DELETE FROM invalidks.invalidtbl WHERE partk = 2",
"DROP KEYSPACE invalidks",
"DROP ROLE invaliduser",
Expand All @@ -655,9 +661,9 @@ public void testFailedWhitelistedStatementsAreNotLogged()
{
List<String> statements = Arrays.asList(
"SELECT * FROM nonexistingks.nonexistingtbl",
"SELECT * FROM ecks.nonexistingtbl",
"SELECT * FROM ecks_itva.nonexistingtbl",
"INSERT INTO nonexistingks.nonexistingtbl (partk, clustk, value) VALUES (1, 'one', 'valid')",
"INSERT INTO ecks.nonexistingtbl (partk, clustk, value) VALUES (1, 'one', 'valid')");
"INSERT INTO ecks_itva.nonexistingtbl (partk, clustk, value) VALUES (1, 'one', 'valid')");

try (Cluster privateCluster = cdt.createCluster("sam", "secret");
Session privateSession = privateCluster.connect())
Expand All @@ -682,7 +688,7 @@ public void testFailedWhitelistedBatchStatementIsNotLogged()
for (String statement : statements)
{
BatchStatement batch = new BatchStatement(BatchStatement.Type.UNLOGGED);
batch.add(new SimpleStatement("INSERT INTO ecks.ectbl (partk, clustk, value) VALUES (4, '4', 'valid')"));
batch.add(new SimpleStatement("INSERT INTO ecks_itva.ectbl (partk, clustk, value) VALUES (4, '4', 'valid')"));
batch.add(new SimpleStatement(statement));
assertThatExceptionOfType(InvalidQueryException.class).isThrownBy(() -> privateSession.execute(batch));
}
Expand Down
Loading

0 comments on commit df8ecfa

Please sign in to comment.