Search in sources :

Example 1 with Authenticate

use of com.datastax.oss.protocol.internal.response.Authenticate in project java-driver by datastax.

the class ProtocolInitHandlerTest method should_initialize_with_authentication.

@Test
public void should_initialize_with_authentication() {
    channel.pipeline().addLast(ChannelFactory.INIT_HANDLER_NAME, new ProtocolInitHandler(internalDriverContext, DefaultProtocolVersion.V4, null, END_POINT, DriverChannelOptions.DEFAULT, heartbeatHandler, false));
    String serverAuthenticator = "mockServerAuthenticator";
    AuthProvider authProvider = mock(AuthProvider.class);
    MockAuthenticator authenticator = new MockAuthenticator();
    when(authProvider.newAuthenticator(END_POINT, serverAuthenticator)).thenReturn(authenticator);
    when(internalDriverContext.getAuthProvider()).thenReturn(Optional.of(authProvider));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("localhost", 9042));
    Frame requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Startup.class);
    assertThat(connectFuture).isNotDone();
    // Simulate a response that says that the server requires authentication
    writeInboundFrame(requestFrame, new Authenticate(serverAuthenticator));
    // The connection should have created an authenticator from the auth provider
    verify(authProvider).newAuthenticator(END_POINT, serverAuthenticator);
    // And sent an auth response
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(AuthResponse.class);
    AuthResponse authResponse = (AuthResponse) requestFrame.message;
    assertThat(Bytes.toHexString(authResponse.token)).isEqualTo(MockAuthenticator.INITIAL_RESPONSE);
    assertThat(connectFuture).isNotDone();
    // As long as the server sends an auth challenge, the client should reply with another
    // auth_response
    String mockToken = "0xabcd";
    for (int i = 0; i < 5; i++) {
        writeInboundFrame(requestFrame, new AuthChallenge(Bytes.fromHexString(mockToken)));
        requestFrame = readOutboundFrame();
        assertThat(requestFrame.message).isInstanceOf(AuthResponse.class);
        authResponse = (AuthResponse) requestFrame.message;
        // Our mock impl happens to send back the same token
        assertThat(Bytes.toHexString(authResponse.token)).isEqualTo(mockToken);
        assertThat(connectFuture).isNotDone();
    }
    // When the server finally sends back a success message, should proceed to the cluster name
    // check and succeed
    writeInboundFrame(requestFrame, new AuthSuccess(Bytes.fromHexString(mockToken)));
    assertThat(authenticator.successToken).isEqualTo(mockToken);
    requestFrame = readOutboundFrame();
    writeInboundFrame(requestFrame, TestResponses.clusterNameResponse("someClusterName"));
    assertThat(connectFuture).isSuccess();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Frame(com.datastax.oss.protocol.internal.Frame) AuthChallenge(com.datastax.oss.protocol.internal.response.AuthChallenge) Authenticate(com.datastax.oss.protocol.internal.response.Authenticate) InetSocketAddress(java.net.InetSocketAddress) AuthSuccess(com.datastax.oss.protocol.internal.response.AuthSuccess) AuthProvider(com.datastax.oss.driver.api.core.auth.AuthProvider) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) AuthResponse(com.datastax.oss.protocol.internal.request.AuthResponse) Test(org.junit.Test)

Example 2 with Authenticate

use of com.datastax.oss.protocol.internal.response.Authenticate in project java-driver by datastax.

the class ProtocolInitHandlerTest method should_fail_to_initialize_if_server_sends_auth_error.

@Test
public void should_fail_to_initialize_if_server_sends_auth_error() throws Throwable {
    channel.pipeline().addLast(ChannelFactory.INIT_HANDLER_NAME, new ProtocolInitHandler(internalDriverContext, DefaultProtocolVersion.V4, null, END_POINT, DriverChannelOptions.DEFAULT, heartbeatHandler, false));
    String serverAuthenticator = "mockServerAuthenticator";
    AuthProvider authProvider = mock(AuthProvider.class);
    MockAuthenticator authenticator = new MockAuthenticator();
    when(authProvider.newAuthenticator(END_POINT, serverAuthenticator)).thenReturn(authenticator);
    when(internalDriverContext.getAuthProvider()).thenReturn(Optional.of(authProvider));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("localhost", 9042));
    Frame requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Startup.class);
    assertThat(connectFuture).isNotDone();
    writeInboundFrame(requestFrame, new Authenticate("mockServerAuthenticator"));
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(AuthResponse.class);
    assertThat(connectFuture).isNotDone();
    writeInboundFrame(requestFrame, new Error(ProtocolConstants.ErrorCode.AUTH_ERROR, "mock error"));
    assertThat(connectFuture).isFailed(e -> assertThat(e).isInstanceOf(AuthenticationException.class).hasMessage(String.format("Authentication error on node %s: server replied with 'mock error' to AuthResponse request", END_POINT)));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Frame(com.datastax.oss.protocol.internal.Frame) Authenticate(com.datastax.oss.protocol.internal.response.Authenticate) InetSocketAddress(java.net.InetSocketAddress) Error(com.datastax.oss.protocol.internal.response.Error) AuthProvider(com.datastax.oss.driver.api.core.auth.AuthProvider) Test(org.junit.Test)

Aggregations

AuthProvider (com.datastax.oss.driver.api.core.auth.AuthProvider)2 Frame (com.datastax.oss.protocol.internal.Frame)2 Authenticate (com.datastax.oss.protocol.internal.response.Authenticate)2 ChannelFuture (io.netty.channel.ChannelFuture)2 InetSocketAddress (java.net.InetSocketAddress)2 Test (org.junit.Test)2 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)1 AuthResponse (com.datastax.oss.protocol.internal.request.AuthResponse)1 AuthChallenge (com.datastax.oss.protocol.internal.response.AuthChallenge)1 AuthSuccess (com.datastax.oss.protocol.internal.response.AuthSuccess)1 Error (com.datastax.oss.protocol.internal.response.Error)1