Search in sources :

Example 1 with RequestHeaderData

use of org.apache.kafka.common.message.RequestHeaderData in project kafka by apache.

the class SaslAuthenticatorTest method testApiVersionsRequestWithServerUnsupportedVersion.

/**
 * Tests that unsupported version of ApiVersionsRequest before SASL handshake request
 * returns error response and does not result in authentication failure. This test
 * is similar to {@link #testUnauthenticatedApiVersionsRequest(SecurityProtocol, short)}
 * where a non-SASL client is used to send requests that are processed by
 * {@link SaslServerAuthenticator} of the server prior to client authentication.
 */
@Test
public void testApiVersionsRequestWithServerUnsupportedVersion() throws Exception {
    short handshakeVersion = ApiKeys.SASL_HANDSHAKE.latestVersion();
    SecurityProtocol securityProtocol = SecurityProtocol.SASL_PLAINTEXT;
    configureMechanisms("PLAIN", Arrays.asList("PLAIN"));
    server = createEchoServer(securityProtocol);
    // Send ApiVersionsRequest with unsupported version and validate error response.
    String node = "1";
    createClientConnection(SecurityProtocol.PLAINTEXT, node);
    RequestHeader header = new RequestHeader(new RequestHeaderData().setRequestApiKey(ApiKeys.API_VERSIONS.id).setRequestApiVersion(Short.MAX_VALUE).setClientId("someclient").setCorrelationId(1), (short) 2);
    ApiVersionsRequest request = new ApiVersionsRequest.Builder().build();
    selector.send(new NetworkSend(node, request.toSend(header)));
    ByteBuffer responseBuffer = waitForResponse();
    ResponseHeader.parse(responseBuffer, ApiKeys.API_VERSIONS.responseHeaderVersion((short) 0));
    ApiVersionsResponse response = ApiVersionsResponse.parse(responseBuffer, (short) 0);
    assertEquals(Errors.UNSUPPORTED_VERSION.code(), response.data().errorCode());
    ApiVersion apiVersion = response.data().apiKeys().find(ApiKeys.API_VERSIONS.id);
    assertNotNull(apiVersion);
    assertEquals(ApiKeys.API_VERSIONS.id, apiVersion.apiKey());
    assertEquals(ApiKeys.API_VERSIONS.oldestVersion(), apiVersion.minVersion());
    assertEquals(ApiKeys.API_VERSIONS.latestVersion(), apiVersion.maxVersion());
    // Send ApiVersionsRequest with a supported version. This should succeed.
    sendVersionRequestReceiveResponse(node);
    // Test that client can authenticate successfully
    sendHandshakeRequestReceiveResponse(node, handshakeVersion);
    authenticateUsingSaslPlainAndCheckConnection(node, handshakeVersion > 0);
}
Also used : ApiVersionsResponse(org.apache.kafka.common.requests.ApiVersionsResponse) ApiVersion(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion) RequestHeaderData(org.apache.kafka.common.message.RequestHeaderData) SecurityProtocol(org.apache.kafka.common.security.auth.SecurityProtocol) RequestHeader(org.apache.kafka.common.requests.RequestHeader) NetworkSend(org.apache.kafka.common.network.NetworkSend) ApiVersionsRequest(org.apache.kafka.common.requests.ApiVersionsRequest) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 2 with RequestHeaderData

use of org.apache.kafka.common.message.RequestHeaderData in project kafka by apache.

the class SaslClientAuthenticator method nextRequestHeader.

private RequestHeader nextRequestHeader(ApiKeys apiKey, short version) {
    String clientId = (String) configs.get(CommonClientConfigs.CLIENT_ID_CONFIG);
    short requestApiKey = apiKey.id;
    currentRequestHeader = new RequestHeader(new RequestHeaderData().setRequestApiKey(requestApiKey).setRequestApiVersion(version).setClientId(clientId).setCorrelationId(nextCorrelationId()), apiKey.requestHeaderVersion(version));
    return currentRequestHeader;
}
Also used : RequestHeaderData(org.apache.kafka.common.message.RequestHeaderData) RequestHeader(org.apache.kafka.common.requests.RequestHeader)

Example 3 with RequestHeaderData

use of org.apache.kafka.common.message.RequestHeaderData in project kafka by apache.

the class RequestHeader method parse.

public static RequestHeader parse(ByteBuffer buffer) {
    short apiKey = -1;
    try {
        // We derive the header version from the request api version, so we read that first.
        // The request api version is part of `RequestHeaderData`, so we reset the buffer position after the read.
        int position = buffer.position();
        apiKey = buffer.getShort();
        short apiVersion = buffer.getShort();
        short headerVersion = ApiKeys.forId(apiKey).requestHeaderVersion(apiVersion);
        buffer.position(position);
        RequestHeaderData headerData = new RequestHeaderData(new ByteBufferAccessor(buffer), headerVersion);
        // However, we treat a null client ID as equivalent to an empty client ID.
        if (headerData.clientId() == null) {
            headerData.setClientId("");
        }
        return new RequestHeader(headerData, headerVersion);
    } catch (UnsupportedVersionException e) {
        throw new InvalidRequestException("Unknown API key " + apiKey, e);
    } catch (Throwable ex) {
        throw new InvalidRequestException("Error parsing request header. Our best guess of the apiKey is: " + apiKey, ex);
    }
}
Also used : RequestHeaderData(org.apache.kafka.common.message.RequestHeaderData) InvalidRequestException(org.apache.kafka.common.errors.InvalidRequestException) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException)

Example 4 with RequestHeaderData

use of org.apache.kafka.common.message.RequestHeaderData in project kafka by apache.

the class RequestHeaderTest method parseHeaderWithNullClientId.

@Test
public void parseHeaderWithNullClientId() {
    RequestHeaderData headerData = new RequestHeaderData().setClientId(null).setCorrelationId(123).setRequestApiKey(ApiKeys.FIND_COORDINATOR.id).setRequestApiVersion((short) 10);
    ObjectSerializationCache serializationCache = new ObjectSerializationCache();
    ByteBuffer buffer = ByteBuffer.allocate(headerData.size(serializationCache, (short) 2));
    headerData.write(new ByteBufferAccessor(buffer), serializationCache, (short) 2);
    buffer.flip();
    RequestHeader parsed = RequestHeader.parse(buffer);
    assertEquals("", parsed.clientId());
    assertEquals(123, parsed.correlationId());
    assertEquals(ApiKeys.FIND_COORDINATOR, parsed.apiKey());
    assertEquals((short) 10, parsed.apiVersion());
}
Also used : ObjectSerializationCache(org.apache.kafka.common.protocol.ObjectSerializationCache) RequestHeaderData(org.apache.kafka.common.message.RequestHeaderData) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

RequestHeaderData (org.apache.kafka.common.message.RequestHeaderData)4 ByteBuffer (java.nio.ByteBuffer)2 ByteBufferAccessor (org.apache.kafka.common.protocol.ByteBufferAccessor)2 RequestHeader (org.apache.kafka.common.requests.RequestHeader)2 Test (org.junit.jupiter.api.Test)2 InvalidRequestException (org.apache.kafka.common.errors.InvalidRequestException)1 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)1 ApiVersion (org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion)1 NetworkSend (org.apache.kafka.common.network.NetworkSend)1 ObjectSerializationCache (org.apache.kafka.common.protocol.ObjectSerializationCache)1 ApiVersionsRequest (org.apache.kafka.common.requests.ApiVersionsRequest)1 ApiVersionsResponse (org.apache.kafka.common.requests.ApiVersionsResponse)1 SecurityProtocol (org.apache.kafka.common.security.auth.SecurityProtocol)1