use of org.apache.kafka.common.security.ssl.SslPrincipalMapper in project kafka by apache.
the class DefaultKafkaPrincipalBuilderTest method testPrincipalWithSslPrincipalMapper.
@Test
public void testPrincipalWithSslPrincipalMapper() throws Exception {
SSLSession session = mock(SSLSession.class);
when(session.getPeerPrincipal()).thenReturn(new X500Principal("CN=Duke, OU=ServiceUsers, O=Org, C=US")).thenReturn(new X500Principal("CN=Duke, OU=SME, O=mycp, L=Fulton, ST=MD, C=US")).thenReturn(new X500Principal("CN=duke, OU=JavaSoft, O=Sun Microsystems")).thenReturn(new X500Principal("OU=JavaSoft, O=Sun Microsystems, C=US"));
String rules = String.join(", ", "RULE:^CN=(.*),OU=ServiceUsers.*$/$1/L", "RULE:^CN=(.*),OU=(.*),O=(.*),L=(.*),ST=(.*),C=(.*)$/$1@$2/L", "RULE:^.*[Cc][Nn]=([a-zA-Z0-9.]*).*$/$1/U", "DEFAULT");
SslPrincipalMapper mapper = SslPrincipalMapper.fromRules(rules);
DefaultKafkaPrincipalBuilder builder = new DefaultKafkaPrincipalBuilder(null, mapper);
SslAuthenticationContext sslContext = new SslAuthenticationContext(session, InetAddress.getLocalHost(), SecurityProtocol.PLAINTEXT.name());
KafkaPrincipal principal = builder.build(sslContext);
assertEquals("duke", principal.getName());
principal = builder.build(sslContext);
assertEquals("duke@sme", principal.getName());
principal = builder.build(sslContext);
assertEquals("DUKE", principal.getName());
principal = builder.build(sslContext);
assertEquals("OU=JavaSoft,O=Sun Microsystems,C=US", principal.getName());
verify(session, times(4)).getPeerPrincipal();
}
Aggregations