Search in sources :

Example 6 with ApiVersion

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

the class NodeApiVersionsTest method testUsableVersionLatestVersions.

@Test
public void testUsableVersionLatestVersions() {
    List<ApiVersion> versionList = new LinkedList<>();
    for (ApiVersion apiVersion : ApiVersionsResponse.API_VERSIONS_RESPONSE.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.usableVersion(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 7 with ApiVersion

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

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 8 with ApiVersion

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

the class SaslAuthenticatorTest method startServerWithoutSaslAuthenticateHeader.

private NioEchoServer startServerWithoutSaslAuthenticateHeader(final SecurityProtocol securityProtocol, String saslMechanism) throws Exception {
    final ListenerName listenerName = ListenerName.forSecurityProtocol(securityProtocol);
    final Map<String, ?> configs = Collections.emptyMap();
    final JaasContext jaasContext = JaasContext.loadServerContext(listenerName, saslMechanism, configs);
    final Map<String, JaasContext> jaasContexts = Collections.singletonMap(saslMechanism, jaasContext);
    boolean isScram = ScramMechanism.isScram(saslMechanism);
    if (isScram)
        ScramCredentialUtils.createCache(credentialCache, Arrays.asList(saslMechanism));
    SaslChannelBuilder serverChannelBuilder = new SaslChannelBuilder(Mode.SERVER, jaasContexts, securityProtocol, listenerName, false, saslMechanism, true, credentialCache, null) {

        @Override
        protected SaslServerAuthenticator buildServerAuthenticator(Map<String, ?> configs, String id, TransportLayer transportLayer, Map<String, Subject> subjects) throws IOException {
            return new SaslServerAuthenticator(configs, id, jaasContexts, subjects, null, credentialCache, listenerName, securityProtocol, transportLayer, null) {

                @Override
                protected ApiVersionsResponse apiVersionsResponse() {
                    List<ApiVersion> apiVersions = new ArrayList<>(ApiVersionsResponse.defaultApiVersionsResponse().apiVersions());
                    for (Iterator<ApiVersion> it = apiVersions.iterator(); it.hasNext(); ) {
                        ApiVersion apiVersion = it.next();
                        if (apiVersion.apiKey == ApiKeys.SASL_AUTHENTICATE.id) {
                            it.remove();
                            break;
                        }
                    }
                    return new ApiVersionsResponse(0, Errors.NONE, apiVersions);
                }

                @Override
                protected void enableKafkaSaslAuthenticateHeaders(boolean flag) {
                // Don't enable Kafka SASL_AUTHENTICATE headers
                }
            };
        }
    };
    serverChannelBuilder.configure(saslServerConfigs);
    server = new NioEchoServer(listenerName, securityProtocol, new TestSecurityConfig(saslServerConfigs), "localhost", serverChannelBuilder, credentialCache);
    server.start();
    return server;
}
Also used : ApiVersion(org.apache.kafka.common.requests.ApiVersionsResponse.ApiVersion) ApiVersionsResponse(org.apache.kafka.common.requests.ApiVersionsResponse) ArrayList(java.util.ArrayList) ListenerName(org.apache.kafka.common.network.ListenerName) TransportLayer(org.apache.kafka.common.network.TransportLayer) JaasContext(org.apache.kafka.common.security.JaasContext) NioEchoServer(org.apache.kafka.common.network.NioEchoServer) TestSecurityConfig(org.apache.kafka.common.security.TestSecurityConfig) SaslChannelBuilder(org.apache.kafka.common.network.SaslChannelBuilder) Map(java.util.Map) HashMap(java.util.HashMap)

Example 9 with ApiVersion

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

the class NodeApiVersions method toString.

/**
 * Convert the object to a string.
 *
 * @param lineBreaks True if we should add a linebreak after each api.
 */
public String toString(boolean lineBreaks) {
    // The apiVersion collection may not be in sorted order.  We put it into
    // a TreeMap before printing it out to ensure that we always print in
    // ascending order.
    TreeMap<Short, String> apiKeysText = new TreeMap<>();
    for (ApiVersion supportedVersion : this.supportedVersions.values()) apiKeysText.put(supportedVersion.apiKey, apiVersionToText(supportedVersion));
    for (ApiVersion apiVersion : unknownApis) apiKeysText.put(apiVersion.apiKey, apiVersionToText(apiVersion));
    // which may happen when the remote is too old.
    for (ApiKeys apiKey : ApiKeys.values()) {
        if (!apiKeysText.containsKey(apiKey.id)) {
            StringBuilder bld = new StringBuilder();
            bld.append(apiKey.name).append("(").append(apiKey.id).append("): ").append("UNSUPPORTED");
            apiKeysText.put(apiKey.id, bld.toString());
        }
    }
    String separator = lineBreaks ? ",\n\t" : ", ";
    StringBuilder bld = new StringBuilder();
    bld.append("(");
    if (lineBreaks)
        bld.append("\n\t");
    bld.append(Utils.join(apiKeysText.values(), separator));
    if (lineBreaks)
        bld.append("\n");
    bld.append(")");
    return bld.toString();
}
Also used : ApiVersion(org.apache.kafka.common.requests.ApiVersionsResponse.ApiVersion) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) TreeMap(java.util.TreeMap)

Example 10 with ApiVersion

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

the class NodeApiVersions method apiVersionToText.

private String apiVersionToText(ApiVersion apiVersion) {
    StringBuilder bld = new StringBuilder();
    ApiKeys apiKey = null;
    if (ApiKeys.hasId(apiVersion.apiKey)) {
        apiKey = ApiKeys.forId(apiVersion.apiKey);
        bld.append(apiKey.name).append("(").append(apiKey.id).append("): ");
    } else {
        bld.append("UNKNOWN(").append(apiVersion.apiKey).append("): ");
    }
    if (apiVersion.minVersion == apiVersion.maxVersion) {
        bld.append(apiVersion.minVersion);
    } else {
        bld.append(apiVersion.minVersion).append(" to ").append(apiVersion.maxVersion);
    }
    if (apiKey != null) {
        ApiVersion supportedVersion = supportedVersions.get(apiKey);
        if (apiKey.latestVersion() < supportedVersion.minVersion) {
            bld.append(" [unusable: node too new]");
        } else if (supportedVersion.maxVersion < apiKey.oldestVersion()) {
            bld.append(" [unusable: node too old]");
        } else {
            short latestUsableVersion = Utils.min(apiKey.latestVersion(), supportedVersion.maxVersion);
            bld.append(" [usable: ").append(latestUsableVersion).append("]");
        }
    }
    return bld.toString();
}
Also used : ApiKeys(org.apache.kafka.common.protocol.ApiKeys) ApiVersion(org.apache.kafka.common.requests.ApiVersionsResponse.ApiVersion)

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