use of com.datastax.driver.core.exceptions.UnauthorizedException in project cassandra by apache.
the class AuditLoggerAuthTest method executeWithCredentials.
/**
* Helper methods
*/
private static void executeWithCredentials(List<String> queries, String username, String password, AuditLogEntryType expectedType) {
boolean authFailed = false;
try (Cluster cluster = Cluster.builder().addContactPoints(InetAddress.getLoopbackAddress()).withoutJMXReporting().withCredentials(username, password).withPort(DatabaseDescriptor.getNativeTransportPort()).build()) {
try (Session session = cluster.connect()) {
for (String query : queries) session.execute(query);
} catch (AuthenticationException e) {
authFailed = true;
} catch (UnauthorizedException ue) {
// no-op, taken care by caller
} catch (SyntaxError se) {
// no-op, taken care of by caller
}
}
if (expectedType != null) {
assertTrue(getInMemAuditLogger().size() > 0);
AuditLogEntry logEntry = getInMemAuditLogger().poll();
assertEquals(expectedType, logEntry.getType());
assertTrue(!authFailed || logEntry.getType() == AuditLogEntryType.LOGIN_ERROR);
assertSource(logEntry, username);
// drain all remaining login related events, as there's no specification how connections and login attempts
// should be handled by the driver, so we can't assert a fixed number of login events
getInMemAuditLogger().removeIf(auditLogEntry -> auditLogEntry.getType() == AuditLogEntryType.LOGIN_ERROR || auditLogEntry.getType() == AuditLogEntryType.LOGIN_SUCCESS);
}
}
Aggregations