String principalUserName = clientConfig.getProperty(ZKClientConfig.ZK_SASL_CLIENT_USERNAME, ZKClientConfig.ZK_SASL_CLIENT_USERNAME_DEFAULT); String hostName = addr.getHostName(); WrapperInetAddress ia = addr.getAddress(); if (ia == null) { throw new IllegalArgumentException("Unable to canonicalize address " + addr + " because it's not resolvable");
@Test public void testClientNoCanonicalization() throws IOException, InterruptedException { SaslServerPrincipal.WrapperInetSocketAddress addr = mock(SaslServerPrincipal.WrapperInetSocketAddress.class); SaslServerPrincipal.WrapperInetAddress ia = mock(SaslServerPrincipal.WrapperInetAddress.class); when(addr.getHostName()).thenReturn("zookeeper.apache.org"); when(addr.getAddress()).thenReturn(ia); when(ia.getCanonicalHostName()).thenReturn("zk1.apache.org"); when(ia.getHostAddress()).thenReturn("127.0.0.1"); ZKClientConfig conf = new ZKClientConfig(); conf.setProperty(ZKClientConfig.ZK_SASL_CLIENT_CANONICALIZE_HOSTNAME, "false"); String principal = SaslServerPrincipal.getServerPrincipal(addr, conf); Assert.assertEquals("The computed principal does appears to have been canonicalized incorrectly", "zookeeper/zookeeper.apache.org", principal); }
@Test public void testClientCanonicalizationToIp() throws IOException, InterruptedException { SaslServerPrincipal.WrapperInetSocketAddress addr = mock(SaslServerPrincipal.WrapperInetSocketAddress.class); SaslServerPrincipal.WrapperInetAddress ia = mock(SaslServerPrincipal.WrapperInetAddress.class); when(addr.getHostName()).thenReturn("zookeeper.apache.org"); when(addr.getAddress()).thenReturn(ia); when(ia.getCanonicalHostName()).thenReturn("127.0.0.1"); when(ia.getHostAddress()).thenReturn("127.0.0.1"); ZKClientConfig conf = new ZKClientConfig(); String principal = SaslServerPrincipal.getServerPrincipal(addr, conf); Assert.assertEquals("The computed principal does appear to have falled back to the original host name", "zookeeper/zookeeper.apache.org", principal); } }
/** * Get the name of the server principal for a SASL client. * @param addr the address of the host. * @param clientConfig the configuration for the client. * @return the name of the principal. */ static String getServerPrincipal(InetSocketAddress addr, ZKClientConfig clientConfig) { return getServerPrincipal(new WrapperInetSocketAddress(addr), clientConfig); }
@Test public void testClientCanonicalization() throws IOException, InterruptedException { SaslServerPrincipal.WrapperInetSocketAddress addr = mock(SaslServerPrincipal.WrapperInetSocketAddress.class); SaslServerPrincipal.WrapperInetAddress ia = mock(SaslServerPrincipal.WrapperInetAddress.class); when(addr.getHostName()).thenReturn("zookeeper.apache.org"); when(addr.getAddress()).thenReturn(ia); when(ia.getCanonicalHostName()).thenReturn("zk1.apache.org"); when(ia.getHostAddress()).thenReturn("127.0.0.1"); ZKClientConfig conf = new ZKClientConfig(); String principal = SaslServerPrincipal.getServerPrincipal(addr, conf); Assert.assertEquals("The computed principal does not appear to have been canonicalized", "zookeeper/zk1.apache.org", principal); }