Search in sources :

Example 1 with NetworkReceive

use of org.apache.kafka.common.network.NetworkReceive in project kafka by apache.

the class SaslServerAuthenticator method authenticate.

/**
     * Evaluates client responses via `SaslServer.evaluateResponse` and returns the issued challenge to the client until
     * authentication succeeds or fails.
     *
     * The messages are sent and received as size delimited bytes that consists of a 4 byte network-ordered size N
     * followed by N bytes representing the opaque payload.
     */
public void authenticate() throws IOException {
    if (netOutBuffer != null && !flushNetOutBufferAndUpdateInterestOps())
        return;
    if (saslServer != null && saslServer.isComplete()) {
        setSaslState(SaslState.COMPLETE);
        return;
    }
    if (netInBuffer == null)
        netInBuffer = new NetworkReceive(maxReceiveSize, node);
    netInBuffer.readFrom(transportLayer);
    if (netInBuffer.complete()) {
        netInBuffer.payload().rewind();
        byte[] clientToken = new byte[netInBuffer.payload().remaining()];
        netInBuffer.payload().get(clientToken, 0, clientToken.length);
        // reset the networkReceive as we read all the data.
        netInBuffer = null;
        try {
            switch(saslState) {
                case HANDSHAKE_REQUEST:
                    handleKafkaRequest(clientToken);
                    break;
                case GSSAPI_OR_HANDSHAKE_REQUEST:
                    if (handleKafkaRequest(clientToken))
                        break;
                // This is required for interoperability with 0.9.0.x clients which do not send handshake request
                case AUTHENTICATE:
                    byte[] response = saslServer.evaluateResponse(clientToken);
                    if (response != null) {
                        netOutBuffer = new NetworkSend(node, ByteBuffer.wrap(response));
                        flushNetOutBufferAndUpdateInterestOps();
                    }
                    // update SASL state. Current SASL state will be updated when outgoing writes to the client complete.
                    if (saslServer.isComplete())
                        setSaslState(SaslState.COMPLETE);
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            setSaslState(SaslState.FAILED);
            throw new IOException(e);
        }
    }
}
Also used : NetworkReceive(org.apache.kafka.common.network.NetworkReceive) NetworkSend(org.apache.kafka.common.network.NetworkSend) IOException(java.io.IOException) KafkaException(org.apache.kafka.common.KafkaException) SaslException(javax.security.sasl.SaslException) GSSException(org.ietf.jgss.GSSException) AuthenticationException(org.apache.kafka.common.errors.AuthenticationException) SchemaException(org.apache.kafka.common.protocol.types.SchemaException) IllegalSaslStateException(org.apache.kafka.common.errors.IllegalSaslStateException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) UnsupportedSaslMechanismException(org.apache.kafka.common.errors.UnsupportedSaslMechanismException) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException)

Example 2 with NetworkReceive

use of org.apache.kafka.common.network.NetworkReceive in project kafka by apache.

the class NetworkClientTest method checkSimpleRequestResponse.

private void checkSimpleRequestResponse(NetworkClient networkClient) {
    // has to be before creating any request, as it may send ApiVersionsRequest and its response is mocked with correlation id 0
    awaitReady(networkClient, node);
    ProduceRequest.Builder builder = new ProduceRequest.Builder((short) 1, 1000, Collections.<TopicPartition, MemoryRecords>emptyMap());
    TestCallbackHandler handler = new TestCallbackHandler();
    ClientRequest request = networkClient.newClientRequest(node.idString(), builder, time.milliseconds(), true, handler);
    networkClient.send(request, time.milliseconds());
    networkClient.poll(1, time.milliseconds());
    assertEquals(1, networkClient.inFlightRequestCount());
    ResponseHeader respHeader = new ResponseHeader(request.correlationId());
    Struct resp = new Struct(ApiKeys.PRODUCE.responseSchema(ApiKeys.PRODUCE.latestVersion()));
    resp.set("responses", new Object[0]);
    Struct responseHeaderStruct = respHeader.toStruct();
    int size = responseHeaderStruct.sizeOf() + resp.sizeOf();
    ByteBuffer buffer = ByteBuffer.allocate(size);
    responseHeaderStruct.writeTo(buffer);
    resp.writeTo(buffer);
    buffer.flip();
    selector.completeReceive(new NetworkReceive(node.idString(), buffer));
    List<ClientResponse> responses = networkClient.poll(1, time.milliseconds());
    assertEquals(1, responses.size());
    assertTrue("The handler should have executed.", handler.executed);
    assertTrue("Should have a response body.", handler.response.hasResponse());
    assertEquals("Should be correlated to the original request", request.correlationId(), handler.response.requestHeader().correlationId());
}
Also used : ResponseHeader(org.apache.kafka.common.requests.ResponseHeader) ProduceRequest(org.apache.kafka.common.requests.ProduceRequest) NetworkReceive(org.apache.kafka.common.network.NetworkReceive) ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 3 with NetworkReceive

use of org.apache.kafka.common.network.NetworkReceive in project kafka by apache.

the class NetworkClient method handleCompletedReceives.

/**
     * Handle any completed receives and update the response list with the responses received.
     *
     * @param responses The list of responses to update
     * @param now The current time
     */
private void handleCompletedReceives(List<ClientResponse> responses, long now) {
    for (NetworkReceive receive : this.selector.completedReceives()) {
        String source = receive.source();
        InFlightRequest req = inFlightRequests.completeNext(source);
        AbstractResponse body = parseResponse(receive.payload(), req.header);
        log.trace("Completed receive from node {}, for key {}, received {}", req.destination, req.header.apiKey(), body);
        if (req.isInternalRequest && body instanceof MetadataResponse)
            metadataUpdater.handleCompletedMetadataResponse(req.header, now, (MetadataResponse) body);
        else if (req.isInternalRequest && body instanceof ApiVersionsResponse)
            handleApiVersionsResponse(responses, req, now, (ApiVersionsResponse) body);
        else
            responses.add(req.completed(body, now));
    }
}
Also used : ApiVersionsResponse(org.apache.kafka.common.requests.ApiVersionsResponse) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) NetworkReceive(org.apache.kafka.common.network.NetworkReceive) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse)

Example 4 with NetworkReceive

use of org.apache.kafka.common.network.NetworkReceive in project kafka by apache.

the class NetworkClientTest method maybeSetExpectedApiVersionsResponse.

private void maybeSetExpectedApiVersionsResponse() {
    ByteBuffer buffer = ApiVersionsResponse.API_VERSIONS_RESPONSE.serialize((short) 0, new ResponseHeader(0));
    selector.delayedReceive(new DelayedReceive(node.idString(), new NetworkReceive(node.idString(), buffer)));
}
Also used : ResponseHeader(org.apache.kafka.common.requests.ResponseHeader) NetworkReceive(org.apache.kafka.common.network.NetworkReceive) DelayedReceive(org.apache.kafka.test.DelayedReceive) ByteBuffer(java.nio.ByteBuffer)

Aggregations

NetworkReceive (org.apache.kafka.common.network.NetworkReceive)4 ByteBuffer (java.nio.ByteBuffer)2 ResponseHeader (org.apache.kafka.common.requests.ResponseHeader)2 IOException (java.io.IOException)1 PrivilegedActionException (java.security.PrivilegedActionException)1 SaslException (javax.security.sasl.SaslException)1 KafkaException (org.apache.kafka.common.KafkaException)1 AuthenticationException (org.apache.kafka.common.errors.AuthenticationException)1 IllegalSaslStateException (org.apache.kafka.common.errors.IllegalSaslStateException)1 UnsupportedSaslMechanismException (org.apache.kafka.common.errors.UnsupportedSaslMechanismException)1 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)1 NetworkSend (org.apache.kafka.common.network.NetworkSend)1 SchemaException (org.apache.kafka.common.protocol.types.SchemaException)1 Struct (org.apache.kafka.common.protocol.types.Struct)1 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)1 ApiVersionsResponse (org.apache.kafka.common.requests.ApiVersionsResponse)1 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)1 ProduceRequest (org.apache.kafka.common.requests.ProduceRequest)1 DelayedReceive (org.apache.kafka.test.DelayedReceive)1 GSSException (org.ietf.jgss.GSSException)1