Search in sources :

Example 11 with ApiVersionsResponse

use of org.apache.kafka.common.requests.ApiVersionsResponse in project kafka by apache.

the class SaslServerAuthenticatorTest method setupAuthenticator.

private SaslServerAuthenticator setupAuthenticator(Map<String, ?> configs, TransportLayer transportLayer, String mechanism, ChannelMetadataRegistry metadataRegistry) {
    TestJaasConfig jaasConfig = new TestJaasConfig();
    jaasConfig.addEntry("jaasContext", PlainLoginModule.class.getName(), new HashMap<String, Object>());
    Map<String, Subject> subjects = Collections.singletonMap(mechanism, new Subject());
    Map<String, AuthenticateCallbackHandler> callbackHandlers = Collections.singletonMap(mechanism, new SaslServerCallbackHandler());
    ApiVersionsResponse apiVersionsResponse = ApiVersionsResponse.defaultApiVersionsResponse(ApiMessageType.ListenerType.ZK_BROKER);
    return new SaslServerAuthenticator(configs, callbackHandlers, "node", subjects, null, new ListenerName("ssl"), SecurityProtocol.SASL_SSL, transportLayer, Collections.emptyMap(), metadataRegistry, Time.SYSTEM, () -> apiVersionsResponse);
}
Also used : ApiVersionsResponse(org.apache.kafka.common.requests.ApiVersionsResponse) PlainLoginModule(org.apache.kafka.common.security.plain.PlainLoginModule) AuthenticateCallbackHandler(org.apache.kafka.common.security.auth.AuthenticateCallbackHandler) ListenerName(org.apache.kafka.common.network.ListenerName) Subject(javax.security.auth.Subject)

Example 12 with ApiVersionsResponse

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

use of org.apache.kafka.common.requests.ApiVersionsResponse in project kafka by apache.

the class NetworkClientTest method testUnsupportedApiVersionsRequestWithoutVersionProvidedByTheBroker.

@Test
public void testUnsupportedApiVersionsRequestWithoutVersionProvidedByTheBroker() {
    // initiate the connection
    client.ready(node, time.milliseconds());
    // handle the connection, initiate first ApiVersionsRequest
    client.poll(0, time.milliseconds());
    // ApiVersionsRequest is in flight but not sent yet
    assertTrue(client.hasInFlightRequests(node.idString()));
    // completes initiated sends
    client.poll(0, time.milliseconds());
    assertEquals(1, selector.completedSends().size());
    ByteBuffer buffer = selector.completedSendBuffers().get(0).buffer();
    RequestHeader header = parseHeader(buffer);
    assertEquals(ApiKeys.API_VERSIONS, header.apiKey());
    assertEquals(3, header.apiVersion());
    // prepare response
    delayedApiVersionsResponse(0, (short) 0, new ApiVersionsResponse(new ApiVersionsResponseData().setErrorCode(Errors.UNSUPPORTED_VERSION.code())));
    // handle ApiVersionResponse, initiate second ApiVersionRequest
    client.poll(0, time.milliseconds());
    // ApiVersionsRequest is in flight but not sent yet
    assertTrue(client.hasInFlightRequests(node.idString()));
    // ApiVersionsResponse has been received
    assertEquals(1, selector.completedReceives().size());
    // clean up the buffers
    selector.completedSends().clear();
    selector.completedSendBuffers().clear();
    selector.completedReceives().clear();
    // completes initiated sends
    client.poll(0, time.milliseconds());
    // ApiVersionsRequest has been sent
    assertEquals(1, selector.completedSends().size());
    buffer = selector.completedSendBuffers().get(0).buffer();
    header = parseHeader(buffer);
    assertEquals(ApiKeys.API_VERSIONS, header.apiKey());
    assertEquals(0, header.apiVersion());
    // prepare response
    delayedApiVersionsResponse(1, (short) 0, defaultApiVersionsResponse());
    // handle completed receives
    client.poll(0, time.milliseconds());
    // the ApiVersionsRequest is gone
    assertFalse(client.hasInFlightRequests(node.idString()));
    assertEquals(1, selector.completedReceives().size());
    // the client is ready
    assertTrue(client.isReady(node, time.milliseconds()));
}
Also used : ApiVersionsResponse(org.apache.kafka.common.requests.ApiVersionsResponse) RequestHeader(org.apache.kafka.common.requests.RequestHeader) ByteBuffer(java.nio.ByteBuffer) ApiVersionsResponseData(org.apache.kafka.common.message.ApiVersionsResponseData) Test(org.junit.jupiter.api.Test)

Example 14 with ApiVersionsResponse

use of org.apache.kafka.common.requests.ApiVersionsResponse in project kafka by apache.

the class SaslClientAuthenticator method reauthenticate.

@Override
public void reauthenticate(ReauthenticationContext reauthenticationContext) throws IOException {
    SaslClientAuthenticator previousSaslClientAuthenticator = (SaslClientAuthenticator) Objects.requireNonNull(reauthenticationContext).previousAuthenticator();
    ApiVersionsResponse apiVersionsResponseFromOriginalAuthentication = previousSaslClientAuthenticator.reauthInfo.apiVersionsResponse();
    previousSaslClientAuthenticator.close();
    reauthInfo.reauthenticating(apiVersionsResponseFromOriginalAuthentication, reauthenticationContext.reauthenticationBeginNanos());
    NetworkReceive netInBufferFromChannel = reauthenticationContext.networkReceive();
    netInBuffer = netInBufferFromChannel;
    // Will set immediately
    setSaslState(SaslState.REAUTH_PROCESS_ORIG_APIVERSIONS_RESPONSE);
    authenticate();
}
Also used : ApiVersionsResponse(org.apache.kafka.common.requests.ApiVersionsResponse) NetworkReceive(org.apache.kafka.common.network.NetworkReceive)

Example 15 with ApiVersionsResponse

use of org.apache.kafka.common.requests.ApiVersionsResponse in project kafka by apache.

the class FetcherTest method testQuotaMetrics.

/*
     * Send multiple requests. Verify that the client side quota metrics have the right values
     */
@Test
public void testQuotaMetrics() {
    buildFetcher();
    MockSelector selector = new MockSelector(time);
    Sensor throttleTimeSensor = Fetcher.throttleTimeSensor(metrics, metricsRegistry);
    Cluster cluster = TestUtils.singletonCluster("test", 1);
    Node node = cluster.nodes().get(0);
    NetworkClient client = new NetworkClient(selector, metadata, "mock", Integer.MAX_VALUE, 1000, 1000, 64 * 1024, 64 * 1024, 1000, 10 * 1000, 127 * 1000, time, true, new ApiVersions(), throttleTimeSensor, new LogContext());
    ApiVersionsResponse apiVersionsResponse = ApiVersionsResponse.defaultApiVersionsResponse(400, ApiMessageType.ListenerType.ZK_BROKER);
    ByteBuffer buffer = RequestTestUtils.serializeResponseWithHeader(apiVersionsResponse, ApiKeys.API_VERSIONS.latestVersion(), 0);
    selector.delayedReceive(new DelayedReceive(node.idString(), new NetworkReceive(node.idString(), buffer)));
    while (!client.ready(node, time.milliseconds())) {
        client.poll(1, time.milliseconds());
        // If a throttled response is received, advance the time to ensure progress.
        time.sleep(client.throttleDelayMs(node, time.milliseconds()));
    }
    selector.clear();
    for (int i = 1; i <= 3; i++) {
        int throttleTimeMs = 100 * i;
        FetchRequest.Builder builder = FetchRequest.Builder.forConsumer(ApiKeys.FETCH.latestVersion(), 100, 100, new LinkedHashMap<>());
        builder.rackId("");
        ClientRequest request = client.newClientRequest(node.idString(), builder, time.milliseconds(), true);
        client.send(request, time.milliseconds());
        client.poll(1, time.milliseconds());
        FetchResponse response = fullFetchResponse(tidp0, nextRecords, Errors.NONE, i, throttleTimeMs);
        buffer = RequestTestUtils.serializeResponseWithHeader(response, ApiKeys.FETCH.latestVersion(), request.correlationId());
        selector.completeReceive(new NetworkReceive(node.idString(), buffer));
        client.poll(1, time.milliseconds());
        // If a throttled response is received, advance the time to ensure progress.
        time.sleep(client.throttleDelayMs(node, time.milliseconds()));
        selector.clear();
    }
    Map<MetricName, KafkaMetric> allMetrics = metrics.metrics();
    KafkaMetric avgMetric = allMetrics.get(metrics.metricInstance(metricsRegistry.fetchThrottleTimeAvg));
    KafkaMetric maxMetric = allMetrics.get(metrics.metricInstance(metricsRegistry.fetchThrottleTimeMax));
    // Throttle times are ApiVersions=400, Fetch=(100, 200, 300)
    assertEquals(250, (Double) avgMetric.metricValue(), EPSILON);
    assertEquals(400, (Double) maxMetric.metricValue(), EPSILON);
    client.close();
}
Also used : ApiVersionsResponse(org.apache.kafka.common.requests.ApiVersionsResponse) Node(org.apache.kafka.common.Node) NetworkReceive(org.apache.kafka.common.network.NetworkReceive) Cluster(org.apache.kafka.common.Cluster) LogContext(org.apache.kafka.common.utils.LogContext) FetchResponse(org.apache.kafka.common.requests.FetchResponse) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) ByteBuffer(java.nio.ByteBuffer) MockSelector(org.apache.kafka.test.MockSelector) MetricName(org.apache.kafka.common.MetricName) NetworkClient(org.apache.kafka.clients.NetworkClient) NodeApiVersions(org.apache.kafka.clients.NodeApiVersions) ApiVersions(org.apache.kafka.clients.ApiVersions) FetchRequest(org.apache.kafka.common.requests.FetchRequest) DelayedReceive(org.apache.kafka.test.DelayedReceive) ClientRequest(org.apache.kafka.clients.ClientRequest) Sensor(org.apache.kafka.common.metrics.Sensor) Test(org.junit.jupiter.api.Test)

Aggregations

ApiVersionsResponse (org.apache.kafka.common.requests.ApiVersionsResponse)30 ApiVersionsRequest (org.apache.kafka.common.requests.ApiVersionsRequest)11 ByteBuffer (java.nio.ByteBuffer)10 Test (org.junit.jupiter.api.Test)8 RequestHeader (org.apache.kafka.common.requests.RequestHeader)7 ApiVersion (org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion)6 NetworkReceive (org.apache.kafka.common.network.NetworkReceive)6 SecurityProtocol (org.apache.kafka.common.security.auth.SecurityProtocol)6 ApiVersionsResponseData (org.apache.kafka.common.message.ApiVersionsResponseData)5 ListenerName (org.apache.kafka.common.network.ListenerName)5 SaslHandshakeResponse (org.apache.kafka.common.requests.SaslHandshakeResponse)5 HashMap (java.util.HashMap)4 SaslChannelBuilder (org.apache.kafka.common.network.SaslChannelBuilder)4 JaasContext (org.apache.kafka.common.security.JaasContext)4 Map (java.util.Map)3 ClientRequest (org.apache.kafka.clients.ClientRequest)3 ApiVersionCollection (org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersionCollection)3 NetworkSend (org.apache.kafka.common.network.NetworkSend)3 NioEchoServer (org.apache.kafka.common.network.NioEchoServer)3 TransportLayer (org.apache.kafka.common.network.TransportLayer)3