Search in sources :

Example 1 with UnauthorizedException

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);
    }
}
Also used : AuthenticationException(com.datastax.driver.core.exceptions.AuthenticationException) SyntaxError(com.datastax.driver.core.exceptions.SyntaxError) UnauthorizedException(com.datastax.driver.core.exceptions.UnauthorizedException) Cluster(com.datastax.driver.core.Cluster) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Session(com.datastax.driver.core.Session)

Aggregations

Cluster (com.datastax.driver.core.Cluster)1 Session (com.datastax.driver.core.Session)1 AuthenticationException (com.datastax.driver.core.exceptions.AuthenticationException)1 SyntaxError (com.datastax.driver.core.exceptions.SyntaxError)1 UnauthorizedException (com.datastax.driver.core.exceptions.UnauthorizedException)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1