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