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());
}
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());
}
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());
}
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());
}
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);
}
Aggregations