Search in sources :

Example 6 with ListenerName

use of org.apache.kafka.common.network.ListenerName in project apache-kafka-on-k8s by banzaicloud.

the class RequestContextTest method testSerdeUnsupportedApiVersionRequest.

@Test
public void testSerdeUnsupportedApiVersionRequest() throws Exception {
    int correlationId = 23423;
    RequestHeader header = new RequestHeader(ApiKeys.API_VERSIONS, Short.MAX_VALUE, "", correlationId);
    RequestContext context = new RequestContext(header, "0", InetAddress.getLocalHost(), KafkaPrincipal.ANONYMOUS, new ListenerName("ssl"), SecurityProtocol.SASL_SSL);
    assertEquals(0, context.apiVersion());
    // Write some garbage to the request buffer. This should be ignored since we will treat
    // the unknown version type as v0 which has an empty request body.
    ByteBuffer requestBuffer = ByteBuffer.allocate(8);
    requestBuffer.putInt(3709234);
    requestBuffer.putInt(29034);
    requestBuffer.flip();
    RequestAndSize requestAndSize = context.parseRequest(requestBuffer);
    assertTrue(requestAndSize.request instanceof ApiVersionsRequest);
    ApiVersionsRequest request = (ApiVersionsRequest) requestAndSize.request;
    assertTrue(request.hasUnsupportedRequestVersion());
    Send send = context.buildResponse(new ApiVersionsResponse(0, Errors.UNSUPPORTED_VERSION, Collections.<ApiVersionsResponse.ApiVersion>emptyList()));
    ByteBufferChannel channel = new ByteBufferChannel(256);
    send.writeTo(channel);
    ByteBuffer responseBuffer = channel.buffer();
    responseBuffer.flip();
    // strip off the size
    responseBuffer.getInt();
    ResponseHeader responseHeader = ResponseHeader.parse(responseBuffer);
    assertEquals(correlationId, responseHeader.correlationId());
    Struct struct = ApiKeys.API_VERSIONS.parseResponse((short) 0, responseBuffer);
    ApiVersionsResponse response = (ApiVersionsResponse) AbstractResponse.parseResponse(ApiKeys.API_VERSIONS, struct);
    assertEquals(Errors.UNSUPPORTED_VERSION, response.error());
    assertTrue(response.apiVersions().isEmpty());
}
Also used : ListenerName(org.apache.kafka.common.network.ListenerName) ByteBuffer(java.nio.ByteBuffer) Send(org.apache.kafka.common.network.Send) Struct(org.apache.kafka.common.protocol.types.Struct) Test(org.junit.Test)

Example 7 with ListenerName

use of org.apache.kafka.common.network.ListenerName in project kafka by apache.

the class RequestContextTest method testEnvelopeResponseSerde.

@Test
public void testEnvelopeResponseSerde() throws Exception {
    CreateTopicsResponseData.CreatableTopicResultCollection collection = new CreateTopicsResponseData.CreatableTopicResultCollection();
    collection.add(new CreateTopicsResponseData.CreatableTopicResult().setTopicConfigErrorCode(Errors.CLUSTER_AUTHORIZATION_FAILED.code()).setNumPartitions(5));
    CreateTopicsResponseData expectedResponse = new CreateTopicsResponseData().setThrottleTimeMs(10).setTopics(collection);
    int correlationId = 15;
    String clientId = "clientId";
    RequestHeader header = new RequestHeader(ApiKeys.CREATE_TOPICS, ApiKeys.CREATE_TOPICS.latestVersion(), clientId, correlationId);
    RequestContext context = new RequestContext(header, "0", InetAddress.getLocalHost(), KafkaPrincipal.ANONYMOUS, new ListenerName("ssl"), SecurityProtocol.SASL_SSL, ClientInformation.EMPTY, true);
    ByteBuffer buffer = context.buildResponseEnvelopePayload(new CreateTopicsResponse(expectedResponse));
    assertEquals(buffer.capacity(), buffer.limit(), "Buffer limit and capacity should be the same");
    CreateTopicsResponse parsedResponse = (CreateTopicsResponse) AbstractResponse.parseResponse(buffer, header);
    assertEquals(expectedResponse, parsedResponse.data());
}
Also used : ListenerName(org.apache.kafka.common.network.ListenerName) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 8 with ListenerName

use of org.apache.kafka.common.network.ListenerName in project kafka by apache.

the class RequestContextTest method testSerdeUnsupportedApiVersionRequest.

@Test
public void testSerdeUnsupportedApiVersionRequest() throws Exception {
    int correlationId = 23423;
    RequestHeader header = new RequestHeader(ApiKeys.API_VERSIONS, Short.MAX_VALUE, "", correlationId);
    RequestContext context = new RequestContext(header, "0", InetAddress.getLocalHost(), KafkaPrincipal.ANONYMOUS, new ListenerName("ssl"), SecurityProtocol.SASL_SSL, ClientInformation.EMPTY, false);
    assertEquals(0, context.apiVersion());
    // Write some garbage to the request buffer. This should be ignored since we will treat
    // the unknown version type as v0 which has an empty request body.
    ByteBuffer requestBuffer = ByteBuffer.allocate(8);
    requestBuffer.putInt(3709234);
    requestBuffer.putInt(29034);
    requestBuffer.flip();
    RequestAndSize requestAndSize = context.parseRequest(requestBuffer);
    assertTrue(requestAndSize.request instanceof ApiVersionsRequest);
    ApiVersionsRequest request = (ApiVersionsRequest) requestAndSize.request;
    assertTrue(request.hasUnsupportedRequestVersion());
    Send send = context.buildResponseSend(new ApiVersionsResponse(new ApiVersionsResponseData().setThrottleTimeMs(0).setErrorCode(Errors.UNSUPPORTED_VERSION.code()).setApiKeys(new ApiVersionCollection())));
    ByteBufferChannel channel = new ByteBufferChannel(256);
    send.writeTo(channel);
    ByteBuffer responseBuffer = channel.buffer();
    responseBuffer.flip();
    // strip off the size
    responseBuffer.getInt();
    ResponseHeader responseHeader = ResponseHeader.parse(responseBuffer, ApiKeys.API_VERSIONS.responseHeaderVersion(header.apiVersion()));
    assertEquals(correlationId, responseHeader.correlationId());
    ApiVersionsResponse response = (ApiVersionsResponse) AbstractResponse.parseResponse(ApiKeys.API_VERSIONS, responseBuffer, (short) 0);
    assertEquals(Errors.UNSUPPORTED_VERSION.code(), response.data().errorCode());
    assertTrue(response.data().apiKeys().isEmpty());
}
Also used : ApiVersionCollection(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersionCollection) ListenerName(org.apache.kafka.common.network.ListenerName) ByteBuffer(java.nio.ByteBuffer) Send(org.apache.kafka.common.network.Send) ApiVersionsResponseData(org.apache.kafka.common.message.ApiVersionsResponseData) Test(org.junit.jupiter.api.Test)

Example 9 with ListenerName

use of org.apache.kafka.common.network.ListenerName in project kafka by apache.

the class JaasContextTest method testLoadForServerWithListenerNameOverride.

@Test
public void testLoadForServerWithListenerNameOverride() throws IOException {
    writeConfiguration(Arrays.asList("KafkaServer { test.LoginModuleDefault required; };", "plaintext.KafkaServer { test.LoginModuleOverride requisite; };"));
    JaasContext context = JaasContext.loadServerContext(new ListenerName("plaintext"), "SOME-MECHANISM", Collections.emptyMap());
    assertEquals("plaintext.KafkaServer", context.name());
    assertEquals(JaasContext.Type.SERVER, context.type());
    assertEquals(1, context.configurationEntries().size());
    checkEntry(context.configurationEntries().get(0), "test.LoginModuleOverride", LoginModuleControlFlag.REQUISITE, Collections.emptyMap());
}
Also used : ListenerName(org.apache.kafka.common.network.ListenerName) Test(org.junit.jupiter.api.Test)

Example 10 with ListenerName

use of org.apache.kafka.common.network.ListenerName in project kafka by apache.

the class LoginManagerTest method testServerLoginManager.

@Test
public void testServerLoginManager() throws Exception {
    Map<String, Object> configs = new HashMap<>();
    configs.put("plain.sasl.jaas.config", dynamicPlainContext);
    configs.put("digest-md5.sasl.jaas.config", dynamicDigestContext);
    ListenerName listenerName = new ListenerName("listener1");
    JaasContext plainJaasContext = JaasContext.loadServerContext(listenerName, "PLAIN", configs);
    JaasContext digestJaasContext = JaasContext.loadServerContext(listenerName, "DIGEST-MD5", configs);
    JaasContext scramJaasContext = JaasContext.loadServerContext(listenerName, "SCRAM-SHA-256", configs);
    LoginManager dynamicPlainLogin = LoginManager.acquireLoginManager(plainJaasContext, "PLAIN", DefaultLogin.class, configs);
    assertEquals(dynamicPlainContext, dynamicPlainLogin.cacheKey());
    LoginManager dynamicDigestLogin = LoginManager.acquireLoginManager(digestJaasContext, "DIGEST-MD5", DefaultLogin.class, configs);
    assertNotSame(dynamicPlainLogin, dynamicDigestLogin);
    assertEquals(dynamicDigestContext, dynamicDigestLogin.cacheKey());
    LoginManager staticScramLogin = LoginManager.acquireLoginManager(scramJaasContext, "SCRAM-SHA-256", DefaultLogin.class, configs);
    assertNotSame(dynamicPlainLogin, staticScramLogin);
    assertEquals("KafkaServer", staticScramLogin.cacheKey());
    assertSame(dynamicPlainLogin, LoginManager.acquireLoginManager(plainJaasContext, "PLAIN", DefaultLogin.class, configs));
    assertSame(dynamicDigestLogin, LoginManager.acquireLoginManager(digestJaasContext, "DIGEST-MD5", DefaultLogin.class, configs));
    assertSame(staticScramLogin, LoginManager.acquireLoginManager(scramJaasContext, "SCRAM-SHA-256", DefaultLogin.class, configs));
    verifyLoginManagerRelease(dynamicPlainLogin, 2, plainJaasContext, configs);
    verifyLoginManagerRelease(dynamicDigestLogin, 2, digestJaasContext, configs);
    verifyLoginManagerRelease(staticScramLogin, 2, scramJaasContext, configs);
}
Also used : JaasContext(org.apache.kafka.common.security.JaasContext) HashMap(java.util.HashMap) ListenerName(org.apache.kafka.common.network.ListenerName) Test(org.junit.jupiter.api.Test)

Aggregations

ListenerName (org.apache.kafka.common.network.ListenerName)27 HashMap (java.util.HashMap)11 JaasContext (org.apache.kafka.common.security.JaasContext)9 Test (org.junit.jupiter.api.Test)8 Test (org.junit.Test)7 SaslChannelBuilder (org.apache.kafka.common.network.SaslChannelBuilder)5 ApiVersionsResponse (org.apache.kafka.common.requests.ApiVersionsResponse)5 SecurityProtocol (org.apache.kafka.common.security.auth.SecurityProtocol)5 Map (java.util.Map)4 Subject (javax.security.auth.Subject)4 TransportLayer (org.apache.kafka.common.network.TransportLayer)4 PlainLoginModule (org.apache.kafka.common.security.plain.PlainLoginModule)4 ByteBuffer (java.nio.ByteBuffer)3 ArrayList (java.util.ArrayList)3 ApiVersionsResponseData (org.apache.kafka.common.message.ApiVersionsResponseData)3 ApiVersionCollection (org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersionCollection)3 NioEchoServer (org.apache.kafka.common.network.NioEchoServer)3 TestSecurityConfig (org.apache.kafka.common.security.TestSecurityConfig)3 LogContext (org.apache.kafka.common.utils.LogContext)3 InetSocketAddress (java.net.InetSocketAddress)2