@Test public void testSetConfiguration() { ZKClientConfig conf = new ZKClientConfig(); String defaultValue = conf.getProperty(ZKClientConfig.ENABLE_CLIENT_SASL_KEY, ZKClientConfig.ENABLE_CLIENT_SASL_DEFAULT); if (defaultValue.equals("true")) { conf.setProperty(ENABLE_CLIENT_SASL_KEY, "false"); } else { conf.setProperty(ENABLE_CLIENT_SASL_KEY, "true"); } assertTrue(conf.getProperty(ENABLE_CLIENT_SASL_KEY) != defaultValue); }
@Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (clientConfig.getBoolean(ZKClientConfig.SECURE_CLIENT)) { initSSL(pipeline); } pipeline.addLast("handler", new ZKClientHandler()); }
protected void initProperties() throws IOException { try { packetLen = clientConfig.getInt(ZKConfig.JUTE_MAXBUFFER, ZKClientConfig.CLIENT_MAX_PACKET_LENGTH_DEFAULT); LOG.info("{} value is {} Bytes", ZKConfig.JUTE_MAXBUFFER, packetLen); } catch (NumberFormatException e) { String msg = MessageFormat.format( "Configured value {0} for property {1} can not be parsed to int", clientConfig.getProperty(ZKConfig.JUTE_MAXBUFFER), ZKConfig.JUTE_MAXBUFFER); LOG.error(msg); throw new IOException(msg); } }
private void initRequestTimeout() { try { requestTimeout = clientConfig.getLong( ZKClientConfig.ZOOKEEPER_REQUEST_TIMEOUT, ZKClientConfig.ZOOKEEPER_REQUEST_TIMEOUT_DEFAULT); LOG.info("{} value is {}. feature enabled=", ZKClientConfig.ZOOKEEPER_REQUEST_TIMEOUT, requestTimeout, requestTimeout > 0); } catch (NumberFormatException e) { LOG.error( "Configured value {} for property {} can not be parsed to long.", clientConfig.getProperty( ZKClientConfig.ZOOKEEPER_REQUEST_TIMEOUT), ZKClientConfig.ZOOKEEPER_REQUEST_TIMEOUT); throw e; } } }
String clientSection = clientConfig.getProperty( ZKClientConfig.LOGIN_CONTEXT_NAME_KEY, ZKClientConfig.LOGIN_CONTEXT_NAME_KEY_DEFAULT); .getProperty(ZKClientConfig.LOGIN_CONTEXT_NAME_KEY); if (explicitClientSection != null) { + explicitClientSection + " section of the supplied JAAS configuration: '" + clientConfig.getJaasConfKey() + "' because of a " + "RuntimeException: " + runtimeException); } else { if (clientConfig.getJaasConfKey() != null) { throw new LoginException( "Zookeeper client cannot authenticate using the '" + clientConfig.getProperty( ZKClientConfig.LOGIN_CONTEXT_NAME_KEY, ZKClientConfig.LOGIN_CONTEXT_NAME_KEY_DEFAULT) + "' section of the supplied JAAS configuration: '" + clientConfig.getJaasConfKey() + "' because of a " + "RuntimeException: " + runtimeException); } else { throw new LoginException( "No JAAS configuration section named '" + clientConfig.getProperty( ZKClientConfig.LOGIN_CONTEXT_NAME_KEY, ZKClientConfig.LOGIN_CONTEXT_NAME_KEY_DEFAULT)
@Test public void testIntegerRetrievalFromProperty() { ZKClientConfig conf = new ZKClientConfig(); String prop = "UnSetProperty" + System.currentTimeMillis(); int defaultValue = 100; // property is not set we should get the default value int result = conf.getInt(prop, defaultValue); assertEquals(defaultValue, result); // property is set but can not be parsed to int, we should get the // NumberFormatException conf.setProperty(ZKConfig.JUTE_MAXBUFFER, "InvlaidIntValue123"); try { result = conf.getInt(ZKConfig.JUTE_MAXBUFFER, defaultValue); fail("NumberFormatException is expected"); } catch (NumberFormatException exception) { // do nothing } assertEquals(defaultValue, result); // property is set to an valid int, we should get the set value int value = ZKClientConfig.CLIENT_MAX_PACKET_LENGTH_DEFAULT; conf.setProperty(ZKConfig.JUTE_MAXBUFFER, Integer.toString(value)); result = conf.getInt(ZKConfig.JUTE_MAXBUFFER, defaultValue); assertEquals(value, result); // property is set but with white spaces value = 12345; conf.setProperty(ZKConfig.JUTE_MAXBUFFER, " " + Integer.toString(value) + " "); result = conf.getInt(ZKConfig.JUTE_MAXBUFFER, defaultValue); assertEquals(value, result); }
@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 testReadConfigurationFile() throws IOException, ConfigException { File file = File.createTempFile("clientConfig", ".conf", testData); file.deleteOnExit(); Properties clientConfProp = new Properties(); clientConfProp.setProperty(ENABLE_CLIENT_SASL_KEY, "true"); clientConfProp.setProperty(ZK_SASL_CLIENT_USERNAME, "ZK"); clientConfProp.setProperty(LOGIN_CONTEXT_NAME_KEY, "MyClient"); clientConfProp.setProperty(ZOOKEEPER_SERVER_REALM, "HADOOP.COM"); clientConfProp.setProperty("dummyProperty", "dummyValue"); OutputStream io = new FileOutputStream(file); try { clientConfProp.store(io, "Client Configurations"); } finally { io.close(); } ZKClientConfig conf = new ZKClientConfig(); conf.addConfiguration(file.getAbsolutePath()); assertEquals(conf.getProperty(ENABLE_CLIENT_SASL_KEY), "true"); assertEquals(conf.getProperty(ZK_SASL_CLIENT_USERNAME), "ZK"); assertEquals(conf.getProperty(LOGIN_CONTEXT_NAME_KEY), "MyClient"); assertEquals(conf.getProperty(ZOOKEEPER_SERVER_REALM), "HADOOP.COM"); assertEquals(conf.getProperty("dummyProperty"), "dummyValue"); // try to delete it now as we have done with the created file, why to // wait for deleteOnExit() deletion file.delete(); }
MockCnxn() throws IOException { super(new ZKClientConfig()); }
@Test public void testSaslClientDisabled() { System.clearProperty(ZKClientConfig.ENABLE_CLIENT_SASL_KEY); Assert.assertTrue("SASL client disabled", new ZKClientConfig().isSaslClientEnabled()); for (String value : Arrays.asList("true", "TRUE")) { System.setProperty(ZKClientConfig.ENABLE_CLIENT_SASL_KEY, value); Assert.assertTrue("SASL client disabled", new ZKClientConfig().isSaslClientEnabled()); } for (String value : Arrays.asList("false", "FALSE")) { System.setProperty(ZKClientConfig.ENABLE_CLIENT_SASL_KEY, value); Assert.assertFalse("SASL client disabled", new ZKClientConfig().isSaslClientEnabled()); } } }
ZKClientConfig conf = new ZKClientConfig(); for (Map.Entry<String, String> e : properties.entrySet()) { assertEquals(e.getValue(), conf.getProperty(e.getKey())); conf = new ZKClientConfig(); String result = conf.getProperty(e.getKey()); assertNull(result);
/** * Returns true if the SASL client is enabled. By default, the client is * enabled but can be disabled by setting the system property * <code>zookeeper.sasl.client</code> to <code>false</code>. See * ZOOKEEPER-1657 for more information. * * @return true if the SASL client is enabled. */ public boolean isSaslClientEnabled() { return Boolean.valueOf(getProperty(ENABLE_CLIENT_SASL_KEY, ENABLE_CLIENT_SASL_DEFAULT)); }
/** * Initialize all the ZooKeeper client properties which are configurable as * java system property */ private void initFromJavaSystemProperties() { setProperty(ZOOKEEPER_REQUEST_TIMEOUT, System.getProperty(ZOOKEEPER_REQUEST_TIMEOUT)); }
public boolean tunnelAuthInProgress() { // 1. SASL client is disabled. if (!clientConfig.isSaslClientEnabled()) { return false; } // 2. SASL login failed. if (saslLoginFailed == true) { return false; } // 3. SendThread has not created the authenticating object yet, // therefore authentication is (at the earliest stage of being) in progress. if (zooKeeperSaslClient == null) { return true; } // 4. authenticating object exists, so ask it for its progress. return zooKeeperSaslClient.clientTunneledAuthenticationInProgress(); }
public ZKClientConfig() { super(); initFromJavaSystemProperties(); }
@Test public void testWhenInvalidJuteMaxBufferIsConfiguredIOExceptionIsThrown() { ZKClientConfig clientConfig = new ZKClientConfig(); String value = "SomeInvalidInt"; clientConfig.setProperty(ZKConfig.JUTE_MAXBUFFER, value); // verify ClientCnxnSocketNIO creation try { new ClientCnxnSocketNIO(clientConfig); fail("IOException is expected."); } catch (IOException e) { assertTrue(e.getMessage().contains(value)); } // verify ClientCnxnSocketNetty creation try { new ClientCnxnSocketNetty(clientConfig); fail("IOException is expected."); } catch (IOException e) { assertTrue(e.getMessage().contains(value)); } }
@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); }
if ((clientConfig.getJaasConfKey() != null) || ((Configuration.getConfiguration() != null) && (Configuration.getConfiguration() .getAppConfigurationEntry(clientConfig.getProperty(ZKClientConfig.LOGIN_CONTEXT_NAME_KEY, ZKClientConfig.LOGIN_CONTEXT_NAME_KEY_DEFAULT)) != null))) {
/** * Get the value of the <code>key</code> property as an <code>long</code>. * If property is not set, the provided <code>defaultValue</code> is * returned * * @param key * property key. * @param defaultValue * default value. * @throws NumberFormatException * when the value is invalid * @return return property value as an <code>long</code>, or * <code>defaultValue</code> */ public long getLong(String key, long defaultValue) { String value = getProperty(key); if (value != null) { return Long.parseLong(value.trim()); } return defaultValue; } }
@Override protected void handleBackwardCompatibility() { /** * backward compatibility for properties which are common to both client * and server */ super.handleBackwardCompatibility(); /** * backward compatibility for client specific properties */ setProperty(ZK_SASL_CLIENT_USERNAME, System.getProperty(ZK_SASL_CLIENT_USERNAME)); setProperty(LOGIN_CONTEXT_NAME_KEY, System.getProperty(LOGIN_CONTEXT_NAME_KEY)); setProperty(ENABLE_CLIENT_SASL_KEY, System.getProperty(ENABLE_CLIENT_SASL_KEY)); setProperty(ZOOKEEPER_SERVER_REALM, System.getProperty(ZOOKEEPER_SERVER_REALM)); setProperty(DISABLE_AUTO_WATCH_RESET, System.getProperty(DISABLE_AUTO_WATCH_RESET)); setProperty(ZOOKEEPER_CLIENT_CNXN_SOCKET, System.getProperty(ZOOKEEPER_CLIENT_CNXN_SOCKET)); setProperty(SECURE_CLIENT, System.getProperty(SECURE_CLIENT)); }