Search in sources :

Example 1 with DefaultKafkaPrincipalBuilder

use of org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder in project apache-kafka-on-k8s by banzaicloud.

the class DefaultKafkaPrincipalBuilderTest method testUseOldPrincipalBuilderForSslIfProvided.

@Test
@SuppressWarnings("deprecation")
public void testUseOldPrincipalBuilderForSslIfProvided() throws Exception {
    TransportLayer transportLayer = mock(TransportLayer.class);
    Authenticator authenticator = mock(Authenticator.class);
    PrincipalBuilder oldPrincipalBuilder = mock(PrincipalBuilder.class);
    SSLSession session = mock(SSLSession.class);
    EasyMock.expect(oldPrincipalBuilder.buildPrincipal(transportLayer, authenticator)).andReturn(new DummyPrincipal("foo"));
    oldPrincipalBuilder.close();
    EasyMock.expectLastCall();
    replayAll();
    DefaultKafkaPrincipalBuilder builder = DefaultKafkaPrincipalBuilder.fromOldPrincipalBuilder(authenticator, transportLayer, oldPrincipalBuilder, null);
    KafkaPrincipal principal = builder.build(new SslAuthenticationContext(session, InetAddress.getLocalHost()));
    assertEquals(KafkaPrincipal.USER_TYPE, principal.getPrincipalType());
    assertEquals("foo", principal.getName());
    builder.close();
    verifyAll();
}
Also used : TransportLayer(org.apache.kafka.common.network.TransportLayer) DefaultKafkaPrincipalBuilder(org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder) DefaultKafkaPrincipalBuilder(org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder) SSLSession(javax.net.ssl.SSLSession) Authenticator(org.apache.kafka.common.network.Authenticator) Test(org.junit.Test)

Example 2 with DefaultKafkaPrincipalBuilder

use of org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder in project apache-kafka-on-k8s by banzaicloud.

the class DefaultKafkaPrincipalBuilderTest method testPrincipalBuilderGssapi.

@Test
public void testPrincipalBuilderGssapi() throws Exception {
    SaslServer server = mock(SaslServer.class);
    KerberosShortNamer kerberosShortNamer = mock(KerberosShortNamer.class);
    EasyMock.expect(server.getMechanismName()).andReturn(SaslConfigs.GSSAPI_MECHANISM);
    EasyMock.expect(server.getAuthorizationID()).andReturn("foo/host@REALM.COM");
    EasyMock.expect(kerberosShortNamer.shortName(EasyMock.anyObject(KerberosName.class))).andReturn("foo");
    replayAll();
    DefaultKafkaPrincipalBuilder builder = new DefaultKafkaPrincipalBuilder(kerberosShortNamer);
    KafkaPrincipal principal = builder.build(new SaslAuthenticationContext(server, SecurityProtocol.SASL_PLAINTEXT, InetAddress.getLocalHost()));
    assertEquals(KafkaPrincipal.USER_TYPE, principal.getPrincipalType());
    assertEquals("foo", principal.getName());
    verifyAll();
}
Also used : KerberosShortNamer(org.apache.kafka.common.security.kerberos.KerberosShortNamer) DefaultKafkaPrincipalBuilder(org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder) SaslServer(javax.security.sasl.SaslServer) KerberosName(org.apache.kafka.common.security.kerberos.KerberosName) Test(org.junit.Test)

Example 3 with DefaultKafkaPrincipalBuilder

use of org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder in project apache-kafka-on-k8s by banzaicloud.

the class DefaultKafkaPrincipalBuilderTest method testReturnAnonymousPrincipalForPlaintext.

@Test
public void testReturnAnonymousPrincipalForPlaintext() throws Exception {
    DefaultKafkaPrincipalBuilder builder = new DefaultKafkaPrincipalBuilder(null);
    assertEquals(KafkaPrincipal.ANONYMOUS, builder.build(new PlaintextAuthenticationContext(InetAddress.getLocalHost())));
}
Also used : DefaultKafkaPrincipalBuilder(org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder) Test(org.junit.Test)

Example 4 with DefaultKafkaPrincipalBuilder

use of org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder in project kafka by apache.

the class EnvelopeRequestTest method testGetPrincipal.

@Test
public void testGetPrincipal() {
    KafkaPrincipal kafkaPrincipal = new KafkaPrincipal(KafkaPrincipal.USER_TYPE, "principal", true);
    DefaultKafkaPrincipalBuilder kafkaPrincipalBuilder = new DefaultKafkaPrincipalBuilder(null, null);
    EnvelopeRequest.Builder requestBuilder = new EnvelopeRequest.Builder(ByteBuffer.allocate(0), kafkaPrincipalBuilder.serialize(kafkaPrincipal), "client-address".getBytes());
    EnvelopeRequest request = requestBuilder.build(EnvelopeRequestData.HIGHEST_SUPPORTED_VERSION);
    assertEquals(kafkaPrincipal, kafkaPrincipalBuilder.deserialize(request.requestPrincipal()));
}
Also used : DefaultKafkaPrincipalBuilder(org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder) DefaultKafkaPrincipalBuilder(org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder) KafkaPrincipal(org.apache.kafka.common.security.auth.KafkaPrincipal) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultKafkaPrincipalBuilder

use of org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder in project kafka by apache.

the class DefaultKafkaPrincipalBuilderTest method testPrincipalIfSSLPeerIsNotAuthenticated.

@Test
public void testPrincipalIfSSLPeerIsNotAuthenticated() throws Exception {
    SSLSession session = mock(SSLSession.class);
    when(session.getPeerPrincipal()).thenReturn(KafkaPrincipal.ANONYMOUS);
    DefaultKafkaPrincipalBuilder builder = new DefaultKafkaPrincipalBuilder(null, null);
    KafkaPrincipal principal = builder.build(new SslAuthenticationContext(session, InetAddress.getLocalHost(), SecurityProtocol.PLAINTEXT.name()));
    assertEquals(KafkaPrincipal.ANONYMOUS, principal);
    verify(session, atLeastOnce()).getPeerPrincipal();
}
Also used : DefaultKafkaPrincipalBuilder(org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder) SSLSession(javax.net.ssl.SSLSession) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultKafkaPrincipalBuilder (org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder)16 Test (org.junit.jupiter.api.Test)8 Test (org.junit.Test)6 SSLSession (javax.net.ssl.SSLSession)5 SaslServer (javax.security.sasl.SaslServer)5 KerberosShortNamer (org.apache.kafka.common.security.kerberos.KerberosShortNamer)3 Configurable (org.apache.kafka.common.Configurable)2 InvalidConfigurationException (org.apache.kafka.common.errors.InvalidConfigurationException)2 Authenticator (org.apache.kafka.common.network.Authenticator)2 TransportLayer (org.apache.kafka.common.network.TransportLayer)2 KafkaPrincipalBuilder (org.apache.kafka.common.security.auth.KafkaPrincipalBuilder)2 X500Principal (javax.security.auth.x500.X500Principal)1 KafkaPrincipal (org.apache.kafka.common.security.auth.KafkaPrincipal)1 KerberosName (org.apache.kafka.common.security.kerberos.KerberosName)1 SslPrincipalMapper (org.apache.kafka.common.security.ssl.SslPrincipalMapper)1