Search in sources :

Example 1 with SslPrincipalMapper

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();
}
Also used : SslPrincipalMapper(org.apache.kafka.common.security.ssl.SslPrincipalMapper) DefaultKafkaPrincipalBuilder(org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder) SSLSession(javax.net.ssl.SSLSession) X500Principal(javax.security.auth.x500.X500Principal) Test(org.junit.jupiter.api.Test)

Aggregations

SSLSession (javax.net.ssl.SSLSession)1 X500Principal (javax.security.auth.x500.X500Principal)1 DefaultKafkaPrincipalBuilder (org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder)1 SslPrincipalMapper (org.apache.kafka.common.security.ssl.SslPrincipalMapper)1 Test (org.junit.jupiter.api.Test)1