Search in sources :

Example 1 with ApiVersion

use of org.apache.kafka.common.requests.ApiVersionsResponse.ApiVersion 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)

Example 2 with ApiVersion

use of org.apache.kafka.common.requests.ApiVersionsResponse.ApiVersion in project apache-kafka-on-k8s by banzaicloud.

the class SaslClientAuthenticator method authenticate.

/**
 * Sends an empty message to the server to initiate the authentication process. It then evaluates server challenges
 * via `SaslClient.evaluateChallenge` and returns client responses 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 {
    short saslHandshakeVersion = 0;
    if (netOutBuffer != null && !flushNetOutBufferAndUpdateInterestOps())
        return;
    switch(saslState) {
        case SEND_APIVERSIONS_REQUEST:
            // Always use version 0 request since brokers treat requests with schema exceptions as GSSAPI tokens
            ApiVersionsRequest apiVersionsRequest = new ApiVersionsRequest((short) 0);
            send(apiVersionsRequest.toSend(node, nextRequestHeader(ApiKeys.API_VERSIONS, apiVersionsRequest.version())));
            setSaslState(SaslState.RECEIVE_APIVERSIONS_RESPONSE);
            break;
        case RECEIVE_APIVERSIONS_RESPONSE:
            ApiVersionsResponse apiVersionsResponse = (ApiVersionsResponse) receiveKafkaResponse();
            if (apiVersionsResponse == null)
                break;
            else {
                saslHandshakeVersion = apiVersionsResponse.apiVersion(ApiKeys.SASL_HANDSHAKE.id).maxVersion;
                ApiVersion authenticateVersion = apiVersionsResponse.apiVersion(ApiKeys.SASL_AUTHENTICATE.id);
                if (authenticateVersion != null)
                    saslAuthenticateVersion((short) Math.min(authenticateVersion.maxVersion, ApiKeys.SASL_AUTHENTICATE.latestVersion()));
                setSaslState(SaslState.SEND_HANDSHAKE_REQUEST);
            // Fall through to send handshake request with the latest supported version
            }
        case SEND_HANDSHAKE_REQUEST:
            SaslHandshakeRequest handshakeRequest = createSaslHandshakeRequest(saslHandshakeVersion);
            send(handshakeRequest.toSend(node, nextRequestHeader(ApiKeys.SASL_HANDSHAKE, handshakeRequest.version())));
            setSaslState(SaslState.RECEIVE_HANDSHAKE_RESPONSE);
            break;
        case RECEIVE_HANDSHAKE_RESPONSE:
            SaslHandshakeResponse handshakeResponse = (SaslHandshakeResponse) receiveKafkaResponse();
            if (handshakeResponse == null)
                break;
            else {
                handleSaslHandshakeResponse(handshakeResponse);
                setSaslState(SaslState.INITIAL);
            // Fall through and start SASL authentication using the configured client mechanism
            }
        case INITIAL:
            sendSaslClientToken(new byte[0], true);
            setSaslState(SaslState.INTERMEDIATE);
            break;
        case INTERMEDIATE:
            byte[] serverToken = receiveToken();
            boolean noResponsesPending = serverToken != null && !sendSaslClientToken(serverToken, false);
            // For versions with SASL_AUTHENTICATE header, server always sends a response to each SASL_AUTHENTICATE request.
            if (saslClient.isComplete()) {
                if (saslAuthenticateVersion == DISABLE_KAFKA_SASL_AUTHENTICATE_HEADER || noResponsesPending)
                    setSaslState(SaslState.COMPLETE);
                else
                    setSaslState(SaslState.CLIENT_COMPLETE);
            }
            break;
        case CLIENT_COMPLETE:
            byte[] serverResponse = receiveToken();
            if (serverResponse != null)
                setSaslState(SaslState.COMPLETE);
            break;
        case COMPLETE:
            break;
        case FAILED:
            // Should never get here since exception would have been propagated earlier
            throw new IllegalStateException("SASL handshake has already failed");
    }
}
Also used : ApiVersionsResponse(org.apache.kafka.common.requests.ApiVersionsResponse) ApiVersion(org.apache.kafka.common.requests.ApiVersionsResponse.ApiVersion) SaslHandshakeResponse(org.apache.kafka.common.requests.SaslHandshakeResponse) ApiVersionsRequest(org.apache.kafka.common.requests.ApiVersionsRequest) SaslHandshakeRequest(org.apache.kafka.common.requests.SaslHandshakeRequest)

Example 3 with ApiVersion

use of org.apache.kafka.common.requests.ApiVersionsResponse.ApiVersion in project apache-kafka-on-k8s by banzaicloud.

the class NodeApiVersionsTest method testUsableVersionLatestVersions.

@Test
public void testUsableVersionLatestVersions() {
    List<ApiVersion> versionList = new LinkedList<>();
    for (ApiVersion apiVersion : ApiVersionsResponse.defaultApiVersionsResponse().apiVersions()) {
        versionList.add(apiVersion);
    }
    // Add an API key that we don't know about.
    versionList.add(new ApiVersion((short) 100, (short) 0, (short) 1));
    NodeApiVersions versions = new NodeApiVersions(versionList);
    for (ApiKeys apiKey : ApiKeys.values()) {
        assertEquals(apiKey.latestVersion(), versions.latestUsableVersion(apiKey));
    }
}
Also used : ApiVersion(org.apache.kafka.common.requests.ApiVersionsResponse.ApiVersion) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 4 with ApiVersion

use of org.apache.kafka.common.requests.ApiVersionsResponse.ApiVersion in project apache-kafka-on-k8s by banzaicloud.

the class NodeApiVersionsTest method testUnknownApiVersionsToString.

@Test
public void testUnknownApiVersionsToString() {
    ApiVersion unknownApiVersion = new ApiVersion((short) 337, (short) 0, (short) 1);
    NodeApiVersions versions = new NodeApiVersions(Collections.singleton(unknownApiVersion));
    assertTrue(versions.toString().endsWith("UNKNOWN(337): 0 to 1)"));
}
Also used : ApiVersion(org.apache.kafka.common.requests.ApiVersionsResponse.ApiVersion) Test(org.junit.Test)

Example 5 with ApiVersion

use of org.apache.kafka.common.requests.ApiVersionsResponse.ApiVersion in project apache-kafka-on-k8s by banzaicloud.

the class NodeApiVersionsTest method testVersionsToString.

@Test
public void testVersionsToString() {
    List<ApiVersion> versionList = new ArrayList<>();
    for (ApiKeys apiKey : ApiKeys.values()) {
        if (apiKey == ApiKeys.DELETE_TOPICS) {
            versionList.add(new ApiVersion(apiKey.id, (short) 10000, (short) 10001));
        } else {
            versionList.add(new ApiVersion(apiKey));
        }
    }
    NodeApiVersions versions = new NodeApiVersions(versionList);
    StringBuilder bld = new StringBuilder();
    String prefix = "(";
    for (ApiKeys apiKey : ApiKeys.values()) {
        bld.append(prefix);
        if (apiKey == ApiKeys.DELETE_TOPICS) {
            bld.append("DeleteTopics(20): 10000 to 10001 [unusable: node too new]");
        } else {
            bld.append(apiKey.name).append("(").append(apiKey.id).append("): ");
            if (apiKey.oldestVersion() == apiKey.latestVersion()) {
                bld.append(apiKey.oldestVersion());
            } else {
                bld.append(apiKey.oldestVersion()).append(" to ").append(apiKey.latestVersion());
            }
            bld.append(" [usable: ").append(apiKey.latestVersion()).append("]");
        }
        prefix = ", ";
    }
    bld.append(")");
    assertEquals(bld.toString(), versions.toString());
}
Also used : ApiVersion(org.apache.kafka.common.requests.ApiVersionsResponse.ApiVersion) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

ApiVersion (org.apache.kafka.common.requests.ApiVersionsResponse.ApiVersion)10 Test (org.junit.Test)6 ApiKeys (org.apache.kafka.common.protocol.ApiKeys)5 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)2 ApiVersionsResponse (org.apache.kafka.common.requests.ApiVersionsResponse)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)1 ListenerName (org.apache.kafka.common.network.ListenerName)1 NioEchoServer (org.apache.kafka.common.network.NioEchoServer)1 SaslChannelBuilder (org.apache.kafka.common.network.SaslChannelBuilder)1 TransportLayer (org.apache.kafka.common.network.TransportLayer)1 ApiVersionsRequest (org.apache.kafka.common.requests.ApiVersionsRequest)1 SaslHandshakeRequest (org.apache.kafka.common.requests.SaslHandshakeRequest)1 SaslHandshakeResponse (org.apache.kafka.common.requests.SaslHandshakeResponse)1 JaasContext (org.apache.kafka.common.security.JaasContext)1 TestSecurityConfig (org.apache.kafka.common.security.TestSecurityConfig)1