Search in sources :

Example 1 with KafkaPrincipalBuilder

use of org.apache.kafka.common.security.auth.KafkaPrincipalBuilder in project apache-kafka-on-k8s by banzaicloud.

the class ChannelBuildersTest method testCreateOldPrincipalBuilder.

@Test
@SuppressWarnings("deprecation")
public void testCreateOldPrincipalBuilder() throws Exception {
    TransportLayer transportLayer = EasyMock.mock(TransportLayer.class);
    Authenticator authenticator = EasyMock.mock(Authenticator.class);
    Map<String, Object> configs = new HashMap<>();
    configs.put(BrokerSecurityConfigs.PRINCIPAL_BUILDER_CLASS_CONFIG, OldPrincipalBuilder.class);
    KafkaPrincipalBuilder builder = ChannelBuilders.createPrincipalBuilder(configs, transportLayer, authenticator, null);
    // test old principal builder is properly configured and delegated to
    assertTrue(OldPrincipalBuilder.configured);
    // test delegation
    KafkaPrincipal principal = builder.build(new PlaintextAuthenticationContext(InetAddress.getLocalHost()));
    assertEquals(OldPrincipalBuilder.PRINCIPAL_NAME, principal.getName());
    assertEquals(KafkaPrincipal.USER_TYPE, principal.getPrincipalType());
}
Also used : HashMap(java.util.HashMap) KafkaPrincipalBuilder(org.apache.kafka.common.security.auth.KafkaPrincipalBuilder) KafkaPrincipal(org.apache.kafka.common.security.auth.KafkaPrincipal) PlaintextAuthenticationContext(org.apache.kafka.common.security.auth.PlaintextAuthenticationContext) Test(org.junit.Test)

Example 2 with KafkaPrincipalBuilder

use of org.apache.kafka.common.security.auth.KafkaPrincipalBuilder in project apache-kafka-on-k8s by banzaicloud.

the class ChannelBuildersTest method testCreateConfigurableKafkaPrincipalBuilder.

@Test
public void testCreateConfigurableKafkaPrincipalBuilder() {
    Map<String, Object> configs = new HashMap<>();
    configs.put(BrokerSecurityConfigs.PRINCIPAL_BUILDER_CLASS_CONFIG, ConfigurableKafkaPrincipalBuilder.class);
    KafkaPrincipalBuilder builder = ChannelBuilders.createPrincipalBuilder(configs, null, null, null);
    assertTrue(builder instanceof ConfigurableKafkaPrincipalBuilder);
    assertTrue(((ConfigurableKafkaPrincipalBuilder) builder).configured);
}
Also used : HashMap(java.util.HashMap) KafkaPrincipalBuilder(org.apache.kafka.common.security.auth.KafkaPrincipalBuilder) Test(org.junit.Test)

Example 3 with KafkaPrincipalBuilder

use of org.apache.kafka.common.security.auth.KafkaPrincipalBuilder 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 4 with KafkaPrincipalBuilder

use of org.apache.kafka.common.security.auth.KafkaPrincipalBuilder 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)

Example 5 with KafkaPrincipalBuilder

use of org.apache.kafka.common.security.auth.KafkaPrincipalBuilder in project kafka by apache.

the class ChannelBuildersTest method testCreateConfigurableKafkaPrincipalBuilder.

@Test
public void testCreateConfigurableKafkaPrincipalBuilder() {
    Map<String, Object> configs = new HashMap<>();
    configs.put(BrokerSecurityConfigs.PRINCIPAL_BUILDER_CLASS_CONFIG, ConfigurableKafkaPrincipalBuilder.class);
    KafkaPrincipalBuilder builder = ChannelBuilders.createPrincipalBuilder(configs, null, null);
    assertTrue(builder instanceof ConfigurableKafkaPrincipalBuilder);
    assertTrue(((ConfigurableKafkaPrincipalBuilder) builder).configured);
}
Also used : HashMap(java.util.HashMap) KafkaPrincipalBuilder(org.apache.kafka.common.security.auth.KafkaPrincipalBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

KafkaPrincipalBuilder (org.apache.kafka.common.security.auth.KafkaPrincipalBuilder)5 HashMap (java.util.HashMap)3 Configurable (org.apache.kafka.common.Configurable)2 InvalidConfigurationException (org.apache.kafka.common.errors.InvalidConfigurationException)2 DefaultKafkaPrincipalBuilder (org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder)2 Test (org.junit.Test)2 KafkaPrincipal (org.apache.kafka.common.security.auth.KafkaPrincipal)1 PlaintextAuthenticationContext (org.apache.kafka.common.security.auth.PlaintextAuthenticationContext)1 Test (org.junit.jupiter.api.Test)1