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;
}
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();
}
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();
}
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();
}
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;
}
Aggregations