Search in sources :

Example 1 with UnsupportedVersionException

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;
}
Also used : SchemaException(org.apache.kafka.common.protocol.types.SchemaException) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) UnsupportedSaslMechanismException(org.apache.kafka.common.errors.UnsupportedSaslMechanismException) IllegalSaslStateException(org.apache.kafka.common.errors.IllegalSaslStateException) ByteBuffer(java.nio.ByteBuffer) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) RequestHeader(org.apache.kafka.common.requests.RequestHeader) SaslHandshakeRequest(org.apache.kafka.common.requests.SaslHandshakeRequest) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException)

Example 2 with UnsupportedVersionException

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));
}
Also used : ApiVersion(org.apache.kafka.common.requests.ApiVersionsResponse.ApiVersion) ArrayList(java.util.ArrayList) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) Test(org.junit.Test)

Aggregations

UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)2 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 IllegalSaslStateException (org.apache.kafka.common.errors.IllegalSaslStateException)1 UnsupportedSaslMechanismException (org.apache.kafka.common.errors.UnsupportedSaslMechanismException)1 ApiKeys (org.apache.kafka.common.protocol.ApiKeys)1 SchemaException (org.apache.kafka.common.protocol.types.SchemaException)1 AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)1 ApiVersion (org.apache.kafka.common.requests.ApiVersionsResponse.ApiVersion)1 RequestHeader (org.apache.kafka.common.requests.RequestHeader)1 SaslHandshakeRequest (org.apache.kafka.common.requests.SaslHandshakeRequest)1 Test (org.junit.Test)1