Search in sources :

Example 11 with TransportLayer

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

the class SaslAuthenticatorTest method createClientConnectionWithoutSaslAuthenticateHeader.

private void createClientConnectionWithoutSaslAuthenticateHeader(final SecurityProtocol securityProtocol, final String saslMechanism, String node) throws Exception {
    final ListenerName listenerName = ListenerName.forSecurityProtocol(securityProtocol);
    final Map<String, ?> configs = Collections.emptyMap();
    final JaasContext jaasContext = JaasContext.loadClientContext(configs);
    final Map<String, JaasContext> jaasContexts = Collections.singletonMap(saslMechanism, jaasContext);
    SaslChannelBuilder clientChannelBuilder = new SaslChannelBuilder(Mode.CLIENT, jaasContexts, securityProtocol, listenerName, false, saslMechanism, true, null, null, null, time, new LogContext(), null) {

        @Override
        protected SaslClientAuthenticator buildClientAuthenticator(Map<String, ?> configs, AuthenticateCallbackHandler callbackHandler, String id, String serverHost, String servicePrincipal, TransportLayer transportLayer, Subject subject) {
            return new SaslClientAuthenticator(configs, callbackHandler, id, subject, servicePrincipal, serverHost, saslMechanism, true, transportLayer, time, new LogContext()) {

                @Override
                protected SaslHandshakeRequest createSaslHandshakeRequest(short version) {
                    return buildSaslHandshakeRequest(saslMechanism, (short) 0);
                }

                @Override
                protected void setSaslAuthenticateAndHandshakeVersions(ApiVersionsResponse apiVersionsResponse) {
                // Don't set version so that headers are disabled
                }
            };
        }
    };
    clientChannelBuilder.configure(saslClientConfigs);
    this.selector = NetworkTestUtils.createSelector(clientChannelBuilder, time);
    InetSocketAddress addr = new InetSocketAddress("localhost", server.port());
    selector.connect(node, addr, BUFFER_SIZE, BUFFER_SIZE);
}
Also used : ApiVersionsResponse(org.apache.kafka.common.requests.ApiVersionsResponse) InetSocketAddress(java.net.InetSocketAddress) LogContext(org.apache.kafka.common.utils.LogContext) ListenerName(org.apache.kafka.common.network.ListenerName) AuthenticateCallbackHandler(org.apache.kafka.common.security.auth.AuthenticateCallbackHandler) Subject(javax.security.auth.Subject) TransportLayer(org.apache.kafka.common.network.TransportLayer) JaasContext(org.apache.kafka.common.security.JaasContext) SaslChannelBuilder(org.apache.kafka.common.network.SaslChannelBuilder) Map(java.util.Map) HashMap(java.util.HashMap)

Example 12 with TransportLayer

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

the class SaslServerAuthenticatorTest method testApiVersionsRequest.

private void testApiVersionsRequest(short version, String expectedSoftwareName, String expectedSoftwareVersion) throws IOException {
    TransportLayer transportLayer = mock(TransportLayer.class, Answers.RETURNS_DEEP_STUBS);
    Map<String, ?> configs = Collections.singletonMap(BrokerSecurityConfigs.SASL_ENABLED_MECHANISMS_CONFIG, Collections.singletonList(SCRAM_SHA_256.mechanismName()));
    ChannelMetadataRegistry metadataRegistry = new DefaultChannelMetadataRegistry();
    SaslServerAuthenticator authenticator = setupAuthenticator(configs, transportLayer, SCRAM_SHA_256.mechanismName(), metadataRegistry);
    RequestHeader header = new RequestHeader(ApiKeys.API_VERSIONS, version, "clientId", 0);
    ByteBuffer headerBuffer = RequestTestUtils.serializeRequestHeader(header);
    ApiVersionsRequest request = new ApiVersionsRequest.Builder().build(version);
    ByteBuffer requestBuffer = request.serialize();
    requestBuffer.rewind();
    when(transportLayer.socketChannel().socket().getInetAddress()).thenReturn(InetAddress.getLoopbackAddress());
    when(transportLayer.read(any(ByteBuffer.class))).then(invocation -> {
        invocation.<ByteBuffer>getArgument(0).putInt(headerBuffer.remaining() + requestBuffer.remaining());
        return 4;
    }).then(invocation -> {
        invocation.<ByteBuffer>getArgument(0).put(headerBuffer.duplicate()).put(requestBuffer.duplicate());
        return headerBuffer.remaining() + requestBuffer.remaining();
    });
    authenticator.authenticate();
    assertEquals(expectedSoftwareName, metadataRegistry.clientInformation().softwareName());
    assertEquals(expectedSoftwareVersion, metadataRegistry.clientInformation().softwareVersion());
    verify(transportLayer, times(2)).read(any(ByteBuffer.class));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ApiVersionsRequest(org.apache.kafka.common.requests.ApiVersionsRequest) AppInfoParser(org.apache.kafka.common.utils.AppInfoParser) RequestTestUtils(org.apache.kafka.common.requests.RequestTestUtils) HashMap(java.util.HashMap) AuthenticateCallbackHandler(org.apache.kafka.common.security.auth.AuthenticateCallbackHandler) ClientInformation(org.apache.kafka.common.network.ClientInformation) SecurityProtocol(org.apache.kafka.common.security.auth.SecurityProtocol) ByteBuffer(java.nio.ByteBuffer) InetAddress(java.net.InetAddress) ListenerName(org.apache.kafka.common.network.ListenerName) RequestHeader(org.apache.kafka.common.requests.RequestHeader) IllegalSaslStateException(org.apache.kafka.common.errors.IllegalSaslStateException) ApiVersionsResponse(org.apache.kafka.common.requests.ApiVersionsResponse) Map(java.util.Map) PlainLoginModule(org.apache.kafka.common.security.plain.PlainLoginModule) SCRAM_SHA_256(org.apache.kafka.common.security.scram.internals.ScramMechanism.SCRAM_SHA_256) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DefaultChannelMetadataRegistry(org.apache.kafka.common.network.DefaultChannelMetadataRegistry) Answers(org.mockito.Answers) Time(org.apache.kafka.common.utils.Time) BrokerSecurityConfigs(org.apache.kafka.common.config.internals.BrokerSecurityConfigs) TransportLayer(org.apache.kafka.common.network.TransportLayer) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) InvalidReceiveException(org.apache.kafka.common.network.InvalidReceiveException) Mockito.when(org.mockito.Mockito.when) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) Subject(javax.security.auth.Subject) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) ApiMessageType(org.apache.kafka.common.message.ApiMessageType) ChannelMetadataRegistry(org.apache.kafka.common.network.ChannelMetadataRegistry) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) TransportLayer(org.apache.kafka.common.network.TransportLayer) DefaultChannelMetadataRegistry(org.apache.kafka.common.network.DefaultChannelMetadataRegistry) ChannelMetadataRegistry(org.apache.kafka.common.network.ChannelMetadataRegistry) RequestHeader(org.apache.kafka.common.requests.RequestHeader) DefaultChannelMetadataRegistry(org.apache.kafka.common.network.DefaultChannelMetadataRegistry) ByteBuffer(java.nio.ByteBuffer) ApiVersionsRequest(org.apache.kafka.common.requests.ApiVersionsRequest)

Example 13 with TransportLayer

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

the class SaslServerAuthenticatorTest method testUnexpectedRequestType.

@Test
public void testUnexpectedRequestType() throws IOException {
    TransportLayer transportLayer = mock(TransportLayer.class);
    Map<String, ?> configs = Collections.singletonMap(BrokerSecurityConfigs.SASL_ENABLED_MECHANISMS_CONFIG, Collections.singletonList(SCRAM_SHA_256.mechanismName()));
    SaslServerAuthenticator authenticator = setupAuthenticator(configs, transportLayer, SCRAM_SHA_256.mechanismName(), new DefaultChannelMetadataRegistry());
    RequestHeader header = new RequestHeader(ApiKeys.METADATA, (short) 0, "clientId", 13243);
    ByteBuffer headerBuffer = RequestTestUtils.serializeRequestHeader(header);
    when(transportLayer.read(any(ByteBuffer.class))).then(invocation -> {
        invocation.<ByteBuffer>getArgument(0).putInt(headerBuffer.remaining());
        return 4;
    }).then(invocation -> {
        // serialize only the request header. the authenticator should not parse beyond this
        invocation.<ByteBuffer>getArgument(0).put(headerBuffer.duplicate());
        return headerBuffer.remaining();
    });
    try {
        authenticator.authenticate();
        fail("Expected authenticate() to raise an exception");
    } catch (IllegalSaslStateException e) {
    // expected exception
    }
    verify(transportLayer, times(2)).read(any(ByteBuffer.class));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ApiVersionsRequest(org.apache.kafka.common.requests.ApiVersionsRequest) AppInfoParser(org.apache.kafka.common.utils.AppInfoParser) RequestTestUtils(org.apache.kafka.common.requests.RequestTestUtils) HashMap(java.util.HashMap) AuthenticateCallbackHandler(org.apache.kafka.common.security.auth.AuthenticateCallbackHandler) ClientInformation(org.apache.kafka.common.network.ClientInformation) SecurityProtocol(org.apache.kafka.common.security.auth.SecurityProtocol) ByteBuffer(java.nio.ByteBuffer) InetAddress(java.net.InetAddress) ListenerName(org.apache.kafka.common.network.ListenerName) RequestHeader(org.apache.kafka.common.requests.RequestHeader) IllegalSaslStateException(org.apache.kafka.common.errors.IllegalSaslStateException) ApiVersionsResponse(org.apache.kafka.common.requests.ApiVersionsResponse) Map(java.util.Map) PlainLoginModule(org.apache.kafka.common.security.plain.PlainLoginModule) SCRAM_SHA_256(org.apache.kafka.common.security.scram.internals.ScramMechanism.SCRAM_SHA_256) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DefaultChannelMetadataRegistry(org.apache.kafka.common.network.DefaultChannelMetadataRegistry) Answers(org.mockito.Answers) Time(org.apache.kafka.common.utils.Time) BrokerSecurityConfigs(org.apache.kafka.common.config.internals.BrokerSecurityConfigs) TransportLayer(org.apache.kafka.common.network.TransportLayer) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) InvalidReceiveException(org.apache.kafka.common.network.InvalidReceiveException) Mockito.when(org.mockito.Mockito.when) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) Subject(javax.security.auth.Subject) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) ApiMessageType(org.apache.kafka.common.message.ApiMessageType) ChannelMetadataRegistry(org.apache.kafka.common.network.ChannelMetadataRegistry) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) TransportLayer(org.apache.kafka.common.network.TransportLayer) RequestHeader(org.apache.kafka.common.requests.RequestHeader) DefaultChannelMetadataRegistry(org.apache.kafka.common.network.DefaultChannelMetadataRegistry) IllegalSaslStateException(org.apache.kafka.common.errors.IllegalSaslStateException) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

TransportLayer (org.apache.kafka.common.network.TransportLayer)13 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ListenerName (org.apache.kafka.common.network.ListenerName)6 ByteBuffer (java.nio.ByteBuffer)5 ApiVersionsResponse (org.apache.kafka.common.requests.ApiVersionsResponse)5 Subject (javax.security.auth.Subject)4 SaslChannelBuilder (org.apache.kafka.common.network.SaslChannelBuilder)4 JaasContext (org.apache.kafka.common.security.JaasContext)4 Test (org.junit.Test)4 IllegalSaslStateException (org.apache.kafka.common.errors.IllegalSaslStateException)3 ChannelMetadataRegistry (org.apache.kafka.common.network.ChannelMetadataRegistry)3 DefaultChannelMetadataRegistry (org.apache.kafka.common.network.DefaultChannelMetadataRegistry)3 RequestHeader (org.apache.kafka.common.requests.RequestHeader)3 AuthenticateCallbackHandler (org.apache.kafka.common.security.auth.AuthenticateCallbackHandler)3 Test (org.junit.jupiter.api.Test)3 IOException (java.io.IOException)2 InetAddress (java.net.InetAddress)2 Collections (java.util.Collections)2 BrokerSecurityConfigs (org.apache.kafka.common.config.internals.BrokerSecurityConfigs)2