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);
}
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;
}
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);
}
}
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());
}
Aggregations