Search in sources :

Example 96 with Channel

use of org.jboss.netty.channel.Channel in project storm by apache.

the class KerberosSaslServerHandler method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    Object msg = e.getMessage();
    if (msg == null) {
        return;
    }
    Channel channel = ctx.getChannel();
    if (msg instanceof SaslMessageToken) {
        // client).
        try {
            LOG.debug("Got SaslMessageToken!");
            KerberosSaslNettyServer saslNettyServer = KerberosSaslNettyServerState.getKerberosSaslNettyServer.get(channel);
            if (saslNettyServer == null) {
                LOG.debug("No saslNettyServer for {}  yet; creating now, with topology token: ", channel);
                try {
                    saslNettyServer = new KerberosSaslNettyServer(storm_conf, jaas_section, authorizedUsers);
                    KerberosSaslNettyServerState.getKerberosSaslNettyServer.set(channel, saslNettyServer);
                } catch (RuntimeException ioe) {
                    LOG.error("Error occurred while creating saslNettyServer on server {} for client {}", channel.getLocalAddress(), channel.getRemoteAddress());
                    throw ioe;
                }
            } else {
                LOG.debug("Found existing saslNettyServer on server: {} for client {}", channel.getLocalAddress(), channel.getRemoteAddress());
            }
            byte[] responseBytes = saslNettyServer.response(((SaslMessageToken) msg).getSaslToken());
            SaslMessageToken saslTokenMessageRequest = new SaslMessageToken(responseBytes);
            if (saslTokenMessageRequest.getSaslToken() == null) {
                channel.write(ControlMessage.SASL_COMPLETE_REQUEST);
            } else {
                // Send response to client.
                channel.write(saslTokenMessageRequest);
            }
            if (saslNettyServer.isComplete()) {
                // If authentication of client is complete, we will also send a
                // SASL-Complete message to the client.
                LOG.info("SASL authentication is complete for client with username: {}", saslNettyServer.getUserName());
                channel.write(ControlMessage.SASL_COMPLETE_REQUEST);
                LOG.debug("Removing SaslServerHandler from pipeline since SASL authentication is complete.");
                ctx.getPipeline().remove(this);
                server.authenticated(channel);
            }
            return;
        } catch (Exception ex) {
            LOG.error("Failed to handle SaslMessageToken: ", ex);
            throw ex;
        }
    } else {
        // Client should not be sending other-than-SASL messages before
        // SaslServerHandler has removed itself from the pipeline. Such
        // non-SASL requests will be denied by the Authorize channel handler
        // (the next handler upstream in the server pipeline) if SASL
        // authentication has not completed.
        LOG.warn("Sending upstream an unexpected non-SASL message : {}", msg);
        Channels.fireMessageReceived(ctx, msg);
    }
}
Also used : Channel(org.jboss.netty.channel.Channel) IOException(java.io.IOException)

Example 97 with Channel

use of org.jboss.netty.channel.Channel in project storm by apache.

the class PacemakerClientHandler method channelConnected.

@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent event) {
    // register the newly established channel
    Channel channel = ctx.getChannel();
    client.channelConnected(channel);
    LOG.info("Connection established from {} to {}", channel.getLocalAddress(), channel.getRemoteAddress());
}
Also used : Channel(org.jboss.netty.channel.Channel)

Example 98 with Channel

use of org.jboss.netty.channel.Channel in project storm by apache.

the class SaslStormClientHandler method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) throws Exception {
    LOG.debug("send/recv time (ms): {}", (System.currentTimeMillis() - start_time));
    Channel channel = ctx.getChannel();
    // Generate SASL response to server using Channel-local SASL client.
    SaslNettyClient saslNettyClient = SaslNettyClientState.getSaslNettyClient.get(channel);
    if (saslNettyClient == null) {
        throw new Exception("saslNettyClient was unexpectedly " + "null for channel: " + channel);
    }
    // examine the response message from server
    if (event.getMessage() instanceof ControlMessage) {
        ControlMessage msg = (ControlMessage) event.getMessage();
        if (msg == ControlMessage.SASL_COMPLETE_REQUEST) {
            LOG.debug("Server has sent us the SaslComplete " + "message. Allowing normal work to proceed.");
            if (!saslNettyClient.isComplete()) {
                LOG.error("Server returned a Sasl-complete message, " + "but as far as we can tell, we are not authenticated yet.");
                throw new Exception("Server returned a " + "Sasl-complete message, but as far as " + "we can tell, we are not authenticated yet.");
            }
            ctx.getPipeline().remove(this);
            this.client.channelReady();
            // We call fireMessageReceived since the client is allowed to
            // perform this request. The client's request will now proceed
            // to the next pipeline component namely StormClientHandler.
            Channels.fireMessageReceived(ctx, msg);
            return;
        }
    }
    SaslMessageToken saslTokenMessage = (SaslMessageToken) event.getMessage();
    LOG.debug("Responding to server's token of length: " + saslTokenMessage.getSaslToken().length);
    // Generate SASL response (but we only actually send the response if
    // it's non-null.
    byte[] responseToServer = saslNettyClient.saslResponse(saslTokenMessage);
    if (responseToServer == null) {
        // If we generate a null response, then authentication has completed
        // (if not, warn), and return without sending a response back to the
        // server.
        LOG.debug("Response to server is null: " + "authentication should now be complete.");
        if (!saslNettyClient.isComplete()) {
            LOG.warn("Generated a null response, " + "but authentication is not complete.");
            throw new Exception("Server response is null, but as far as " + "we can tell, we are not authenticated yet.");
        }
        this.client.channelReady();
        return;
    } else {
        LOG.debug("Response to server token has length:" + responseToServer.length);
    }
    // Construct a message containing the SASL response and send it to the
    // server.
    SaslMessageToken saslResponse = new SaslMessageToken(responseToServer);
    channel.write(saslResponse);
}
Also used : Channel(org.jboss.netty.channel.Channel) IOException(java.io.IOException)

Example 99 with Channel

use of org.jboss.netty.channel.Channel in project storm by apache.

the class SaslStormServerHandler method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    Object msg = e.getMessage();
    if (msg == null)
        return;
    Channel channel = ctx.getChannel();
    if (msg instanceof ControlMessage && e.getMessage() == ControlMessage.SASL_TOKEN_MESSAGE_REQUEST) {
        // initialize server-side SASL functionality, if we haven't yet
        // (in which case we are looking at the first SASL message from the
        // client).
        SaslNettyServer saslNettyServer = SaslNettyServerState.getSaslNettyServer.get(channel);
        if (saslNettyServer == null) {
            LOG.debug("No saslNettyServer for " + channel + " yet; creating now, with topology token: ");
            try {
                saslNettyServer = new SaslNettyServer(topologyName, token);
            } catch (IOException ioe) {
                LOG.error("Error occurred while creating saslNettyServer on server " + channel.getLocalAddress() + " for client " + channel.getRemoteAddress());
                saslNettyServer = null;
            }
            SaslNettyServerState.getSaslNettyServer.set(channel, saslNettyServer);
        } else {
            LOG.debug("Found existing saslNettyServer on server:" + channel.getLocalAddress() + " for client " + channel.getRemoteAddress());
        }
        LOG.debug("processToken:  With nettyServer: " + saslNettyServer + " and token length: " + token.length);
        SaslMessageToken saslTokenMessageRequest;
        saslTokenMessageRequest = new SaslMessageToken(saslNettyServer.response(new byte[0]));
        // Send response to client.
        channel.write(saslTokenMessageRequest);
        // to be done for SASL_TOKEN_MESSAGE_REQUEST requests.
        return;
    }
    if (msg instanceof SaslMessageToken) {
        // initialize server-side SASL functionality, if we haven't yet
        // (in which case we are looking at the first SASL message from the
        // client).
        SaslNettyServer saslNettyServer = SaslNettyServerState.getSaslNettyServer.get(channel);
        if (saslNettyServer == null) {
            throw new Exception("saslNettyServer was unexpectedly " + "null for channel: " + channel);
        }
        SaslMessageToken saslTokenMessageRequest = new SaslMessageToken(saslNettyServer.response(((SaslMessageToken) msg).getSaslToken()));
        // Send response to client.
        channel.write(saslTokenMessageRequest);
        if (saslNettyServer.isComplete()) {
            // If authentication of client is complete, we will also send a
            // SASL-Complete message to the client.
            LOG.debug("SASL authentication is complete for client with " + "username: " + saslNettyServer.getUserName());
            channel.write(ControlMessage.SASL_COMPLETE_REQUEST);
            LOG.debug("Removing SaslServerHandler from pipeline since SASL " + "authentication is complete.");
            ctx.getPipeline().remove(this);
            server.authenticated(channel);
        }
    } else {
        // Client should not be sending other-than-SASL messages before
        // SaslServerHandler has removed itself from the pipeline. Such
        // non-SASL requests will be denied by the Authorize channel handler
        // (the next handler upstream in the server pipeline) if SASL
        // authentication has not completed.
        LOG.warn("Sending upstream an unexpected non-SASL message :  " + msg);
        Channels.fireMessageReceived(ctx, msg);
    }
}
Also used : Channel(org.jboss.netty.channel.Channel) IOException(java.io.IOException) IOException(java.io.IOException)

Example 100 with Channel

use of org.jboss.netty.channel.Channel in project storm by apache.

the class PacemakerClient method channelConnected.

@Override
public synchronized void channelConnected(Channel channel) {
    Channel oldChannel = channelRef.get();
    if (oldChannel != null) {
        LOG.debug("Closing oldChannel is connected: {}", oldChannel.toString());
        close_channel();
    }
    LOG.debug("Channel is connected: {}", channel.toString());
    channelRef.set(channel);
    //If we're not going to authenticate, we can begin sending.
    if (authMethod == ThriftNettyClientCodec.AuthMethod.NONE) {
        ready.set(true);
        this.notifyAll();
    }
    retryTimes = 0;
}
Also used : Channel(org.jboss.netty.channel.Channel)

Aggregations

Channel (org.jboss.netty.channel.Channel)176 InetSocketAddress (java.net.InetSocketAddress)53 Test (org.junit.Test)52 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)38 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)36 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)34 SocketAddress (java.net.SocketAddress)33 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)33 ChannelFuture (org.jboss.netty.channel.ChannelFuture)31 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)30 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)24 Test (org.testng.annotations.Test)23 ConditionCheck (com.linkedin.databus2.test.ConditionCheck)22 SimpleObjectCaptureHandler (com.linkedin.databus2.test.container.SimpleObjectCaptureHandler)19 IOException (java.io.IOException)19 Logger (org.apache.log4j.Logger)19 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)17 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)15 ArrayList (java.util.ArrayList)14 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)14