Search in sources :

Example 11 with DefaultKafkaPrincipalBuilder

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

the class ChannelBuilders method createPrincipalBuilder.

public static KafkaPrincipalBuilder createPrincipalBuilder(Map<String, ?> configs, KerberosShortNamer kerberosShortNamer, SslPrincipalMapper sslPrincipalMapper) {
    Class<?> principalBuilderClass = (Class<?>) configs.get(BrokerSecurityConfigs.PRINCIPAL_BUILDER_CLASS_CONFIG);
    final KafkaPrincipalBuilder builder;
    if (principalBuilderClass == null || principalBuilderClass == DefaultKafkaPrincipalBuilder.class) {
        builder = new DefaultKafkaPrincipalBuilder(kerberosShortNamer, sslPrincipalMapper);
    } else if (KafkaPrincipalBuilder.class.isAssignableFrom(principalBuilderClass)) {
        builder = (KafkaPrincipalBuilder) Utils.newInstance(principalBuilderClass);
    } else {
        throw new InvalidConfigurationException("Type " + principalBuilderClass.getName() + " is not " + "an instance of " + KafkaPrincipalBuilder.class.getName());
    }
    if (builder instanceof Configurable)
        ((Configurable) builder).configure(configs);
    return builder;
}
Also used : DefaultKafkaPrincipalBuilder(org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder) KafkaPrincipalBuilder(org.apache.kafka.common.security.auth.KafkaPrincipalBuilder) DefaultKafkaPrincipalBuilder(org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder) Configurable(org.apache.kafka.common.Configurable) InvalidConfigurationException(org.apache.kafka.common.errors.InvalidConfigurationException)

Example 12 with DefaultKafkaPrincipalBuilder

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

the class DefaultKafkaPrincipalBuilderTest method testUseSessionPeerPrincipalForSsl.

@Test
public void testUseSessionPeerPrincipalForSsl() throws Exception {
    SSLSession session = mock(SSLSession.class);
    EasyMock.expect(session.getPeerPrincipal()).andReturn(new DummyPrincipal("foo"));
    replayAll();
    DefaultKafkaPrincipalBuilder builder = new DefaultKafkaPrincipalBuilder(null);
    KafkaPrincipal principal = builder.build(new SslAuthenticationContext(session, InetAddress.getLocalHost()));
    assertEquals(KafkaPrincipal.USER_TYPE, principal.getPrincipalType());
    assertEquals("foo", principal.getName());
    verifyAll();
}
Also used : DefaultKafkaPrincipalBuilder(org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder) SSLSession(javax.net.ssl.SSLSession) Test(org.junit.Test)

Example 13 with DefaultKafkaPrincipalBuilder

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

the class DefaultKafkaPrincipalBuilderTest method testUseOldPrincipalBuilderForPlaintextIfProvided.

@Test
@SuppressWarnings("deprecation")
public void testUseOldPrincipalBuilderForPlaintextIfProvided() throws Exception {
    TransportLayer transportLayer = mock(TransportLayer.class);
    Authenticator authenticator = mock(Authenticator.class);
    PrincipalBuilder oldPrincipalBuilder = mock(PrincipalBuilder.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 PlaintextAuthenticationContext(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) Authenticator(org.apache.kafka.common.network.Authenticator) Test(org.junit.Test)

Example 14 with DefaultKafkaPrincipalBuilder

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

the class DefaultKafkaPrincipalBuilderTest method testPrincipalBuilderScram.

@Test
public void testPrincipalBuilderScram() throws Exception {
    SaslServer server = mock(SaslServer.class);
    EasyMock.expect(server.getMechanismName()).andReturn(ScramMechanism.SCRAM_SHA_256.mechanismName());
    EasyMock.expect(server.getAuthorizationID()).andReturn("foo");
    replayAll();
    DefaultKafkaPrincipalBuilder builder = new DefaultKafkaPrincipalBuilder(null);
    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 : DefaultKafkaPrincipalBuilder(org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder) SaslServer(javax.security.sasl.SaslServer) Test(org.junit.Test)

Example 15 with DefaultKafkaPrincipalBuilder

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

the class ChannelBuilders method createPrincipalBuilder.

@SuppressWarnings("deprecation")
public static KafkaPrincipalBuilder createPrincipalBuilder(Map<String, ?> configs, TransportLayer transportLayer, Authenticator authenticator, KerberosShortNamer kerberosShortNamer) {
    Class<?> principalBuilderClass = (Class<?>) configs.get(BrokerSecurityConfigs.PRINCIPAL_BUILDER_CLASS_CONFIG);
    final KafkaPrincipalBuilder builder;
    if (principalBuilderClass == null || principalBuilderClass == DefaultKafkaPrincipalBuilder.class) {
        builder = new DefaultKafkaPrincipalBuilder(kerberosShortNamer);
    } else if (KafkaPrincipalBuilder.class.isAssignableFrom(principalBuilderClass)) {
        builder = (KafkaPrincipalBuilder) Utils.newInstance(principalBuilderClass);
    } else if (org.apache.kafka.common.security.auth.PrincipalBuilder.class.isAssignableFrom(principalBuilderClass)) {
        org.apache.kafka.common.security.auth.PrincipalBuilder oldPrincipalBuilder = createPrincipalBuilder(principalBuilderClass, configs);
        builder = DefaultKafkaPrincipalBuilder.fromOldPrincipalBuilder(authenticator, transportLayer, oldPrincipalBuilder, kerberosShortNamer);
    } else {
        throw new InvalidConfigurationException("Type " + principalBuilderClass.getName() + " is not " + "an instance of " + org.apache.kafka.common.security.auth.PrincipalBuilder.class.getName() + " or " + KafkaPrincipalBuilder.class.getName());
    }
    if (builder instanceof Configurable)
        ((Configurable) builder).configure(configs);
    return builder;
}
Also used : DefaultKafkaPrincipalBuilder(org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder) KafkaPrincipalBuilder(org.apache.kafka.common.security.auth.KafkaPrincipalBuilder) DefaultKafkaPrincipalBuilder(org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder) Configurable(org.apache.kafka.common.Configurable) InvalidConfigurationException(org.apache.kafka.common.errors.InvalidConfigurationException)

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