use of org.apache.kafka.common.errors.UnsupportedVersionException in project kafka by apache.
the class SaslServerAuthenticator method handleKafkaRequest.
private boolean handleKafkaRequest(byte[] requestBytes) throws IOException, AuthenticationException {
boolean isKafkaRequest = false;
String clientMechanism = null;
try {
ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes);
RequestHeader requestHeader = RequestHeader.parse(requestBuffer);
ApiKeys apiKey = ApiKeys.forId(requestHeader.apiKey());
// A valid Kafka request header was received. SASL authentication tokens are now expected only
// following a SaslHandshakeRequest since this is not a GSSAPI client token from a Kafka 0.9.0.x client.
setSaslState(SaslState.HANDSHAKE_REQUEST);
isKafkaRequest = true;
if (!Protocol.apiVersionSupported(requestHeader.apiKey(), requestHeader.apiVersion())) {
if (apiKey == ApiKeys.API_VERSIONS)
sendKafkaResponse(ApiVersionsResponse.unsupportedVersionSend(node, requestHeader));
else
throw new UnsupportedVersionException("Version " + requestHeader.apiVersion() + " is not supported for apiKey " + apiKey);
} else {
AbstractRequest request = AbstractRequest.getRequest(requestHeader.apiKey(), requestHeader.apiVersion(), requestBuffer).request;
LOG.debug("Handle Kafka request {}", apiKey);
switch(apiKey) {
case API_VERSIONS:
handleApiVersionsRequest(requestHeader);
break;
case SASL_HANDSHAKE:
clientMechanism = handleHandshakeRequest(requestHeader, (SaslHandshakeRequest) request);
break;
default:
throw new IllegalSaslStateException("Unexpected Kafka request of type " + apiKey + " during SASL handshake.");
}
}
} catch (SchemaException | IllegalArgumentException e) {
if (saslState == SaslState.GSSAPI_OR_HANDSHAKE_REQUEST) {
// starting with 0x60, revert to GSSAPI for both these exceptions.
if (LOG.isDebugEnabled()) {
StringBuilder tokenBuilder = new StringBuilder();
for (byte b : requestBytes) {
tokenBuilder.append(String.format("%02x", b));
if (tokenBuilder.length() >= 20)
break;
}
LOG.debug("Received client packet of length {} starting with bytes 0x{}, process as GSSAPI packet", requestBytes.length, tokenBuilder);
}
if (enabledMechanisms.contains(SaslConfigs.GSSAPI_MECHANISM)) {
LOG.debug("First client packet is not a SASL mechanism request, using default mechanism GSSAPI");
clientMechanism = SaslConfigs.GSSAPI_MECHANISM;
} else
throw new UnsupportedSaslMechanismException("Exception handling first SASL packet from client, GSSAPI is not supported by server", e);
} else
throw e;
}
if (clientMechanism != null) {
createSaslServer(clientMechanism);
setSaslState(SaslState.AUTHENTICATE);
}
return isKafkaRequest;
}
use of org.apache.kafka.common.errors.UnsupportedVersionException in project kafka by apache.
the class NodeApiVersionsTest method testUsableVersionCalculation.
@Test
public void testUsableVersionCalculation() {
List<ApiVersion> versionList = new ArrayList<>();
versionList.add(new ApiVersion(ApiKeys.CONTROLLED_SHUTDOWN_KEY.id, (short) 0, (short) 0));
versionList.add(new ApiVersion(ApiKeys.FETCH.id, (short) 1, (short) 2));
NodeApiVersions versions = new NodeApiVersions(versionList);
try {
versions.usableVersion(ApiKeys.CONTROLLED_SHUTDOWN_KEY);
Assert.fail("expected UnsupportedVersionException");
} catch (UnsupportedVersionException e) {
// pass
}
assertEquals(2, versions.usableVersion(ApiKeys.FETCH));
}
Aggregations