001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.zookeeper;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import java.io.IOException;
024import java.util.Properties;
025import java.util.Set;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.HBaseConfiguration;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.testclassification.MiscTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.apache.zookeeper.client.ZKClientConfig;
032import org.junit.jupiter.api.Tag;
033import org.junit.jupiter.api.Test;
034
035import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableSet;
036
037@Tag(MiscTests.TAG)
038@Tag(SmallTests.TAG)
039public class TestZKConfig {
040
041  /** Supported ZooKeeper client TLS properties */
042  private static final Set<String> ZOOKEEPER_CLIENT_TLS_PROPERTIES = ImmutableSet.of(
043    "client.secure", "clientCnxnSocket", "ssl.keyStore.location", "ssl.keyStore.password",
044    "ssl.keyStore.passwordPath", "ssl.keyStore.type", "ssl.trustStore.location",
045    "ssl.trustStore.password", "ssl.trustStore.passwordPath", "ssl.trustStore.type");
046
047  @Test
048  public void testZKConfigLoading() throws Exception {
049    Configuration conf = HBaseConfiguration.create();
050    // Test that we read only from the config instance
051    // (i.e. via hbase-default.xml and hbase-site.xml)
052    conf.setInt(HConstants.ZOOKEEPER_CLIENT_PORT, 2181);
053    Properties props = ZKConfig.makeZKProps(conf);
054    assertEquals("2181", props.getProperty("clientPort"),
055      "Property client port should have been default from the HBase config");
056  }
057
058  @Test
059  public void testGetZooKeeperClusterKey() {
060    Configuration conf = HBaseConfiguration.create();
061    conf.set(HConstants.ZOOKEEPER_QUORUM, "\tlocalhost\n");
062    conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, "3333");
063    conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "hbase");
064    String clusterKey = ZKConfig.getZooKeeperClusterKey(conf, "test");
065    assertTrue(!clusterKey.contains("\t") && !clusterKey.contains("\n"));
066    assertEquals("localhost:3333:hbase,test", clusterKey);
067  }
068
069  @Test
070  public void testClusterKey() throws Exception {
071    testKey("server", 2181, "/hbase");
072    testKey("server1,server2,server3", 2181, "/hbase");
073    try {
074      ZKConfig.validateClusterKey("2181:/hbase");
075    } catch (IOException ex) {
076      // OK
077    }
078  }
079
080  @Test
081  public void testClusterKeyWithMultiplePorts() throws Exception {
082    // server has different port than the default port
083    testKey("server1:2182", 2181, "/hbase", true);
084    // multiple servers have their own port
085    testKey("server1:2182,server2:2183,server3:2184", 2181, "/hbase", true);
086    // one server has no specified port, should use default port
087    testKey("server1:2182,server2,server3:2184", 2181, "/hbase", true);
088    // the last server has no specified port, should use default port
089    testKey("server1:2182,server2:2183,server3", 2181, "/hbase", true);
090    // multiple servers have no specified port, should use default port for those servers
091    testKey("server1:2182,server2,server3:2184,server4", 2181, "/hbase", true);
092    // same server, different ports
093    testKey("server1:2182,server1:2183,server1", 2181, "/hbase", true);
094    // mix of same server/different port and different server
095    testKey("server1:2182,server2:2183,server1", 2181, "/hbase", true);
096  }
097
098  @Test
099  public void testZooKeeperTlsProperties() {
100    // Arrange
101    Configuration conf = HBaseConfiguration.create();
102    for (String p : ZOOKEEPER_CLIENT_TLS_PROPERTIES) {
103      conf.set(HConstants.ZK_CFG_PROPERTY_PREFIX + p, p);
104      String zkprop = "zookeeper." + p;
105      System.clearProperty(zkprop);
106    }
107
108    // Act
109    ZKClientConfig zkClientConfig = ZKConfig.getZKClientConfig(conf);
110
111    // Assert
112    for (String p : ZOOKEEPER_CLIENT_TLS_PROPERTIES) {
113      assertEquals(p, zkClientConfig.getProperty("zookeeper." + p),
114        "Invalid or unset system property: " + p);
115    }
116  }
117
118  @Test
119  public void testZooKeeperTlsPropertiesHQuorumPeer() {
120    // Arrange
121    Configuration conf = HBaseConfiguration.create();
122    for (String p : ZOOKEEPER_CLIENT_TLS_PROPERTIES) {
123      conf.set(HConstants.ZK_CFG_PROPERTY_PREFIX + p, p);
124      String zkprop = "zookeeper." + p;
125      System.clearProperty(zkprop);
126    }
127
128    // Act
129    Properties zkProps = ZKConfig.makeZKProps(conf);
130
131    // Assert
132    for (String p : ZOOKEEPER_CLIENT_TLS_PROPERTIES) {
133      assertEquals(p, zkProps.getProperty(p), "Invalid or unset system property: " + p);
134    }
135  }
136
137  private void testKey(String ensemble, int port, String znode) throws IOException {
138    testKey(ensemble, port, znode, false); // not support multiple client ports
139  }
140
141  private void testKey(String ensemble, int port, String znode, Boolean multiplePortSupport)
142    throws IOException {
143    Configuration conf = new Configuration();
144    String key = ensemble + ":" + port + ":" + znode;
145    String ensemble2 = null;
146    ZKConfig.ZKClusterKey zkClusterKey = ZKConfig.transformClusterKey(key);
147    if (multiplePortSupport) {
148      ensemble2 = ZKConfig.standardizeZKQuorumServerString(ensemble, Integer.toString(port));
149      assertEquals(ensemble2, zkClusterKey.getQuorumString());
150    } else {
151      assertEquals(ensemble, zkClusterKey.getQuorumString());
152    }
153    assertEquals(port, zkClusterKey.getClientPort());
154    assertEquals(znode, zkClusterKey.getZnodeParent());
155
156    conf = HBaseConfiguration.createClusterConf(conf, key);
157    assertEquals(zkClusterKey.getQuorumString(), conf.get(HConstants.ZOOKEEPER_QUORUM));
158    assertEquals(zkClusterKey.getClientPort(), conf.getInt(HConstants.ZOOKEEPER_CLIENT_PORT, -1));
159    assertEquals(zkClusterKey.getZnodeParent(), conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT));
160
161    String reconstructedKey = ZKConfig.getZooKeeperClusterKey(conf);
162    if (multiplePortSupport) {
163      String key2 = ensemble2 + ":" + port + ":" + znode;
164      assertEquals(key2, reconstructedKey);
165    } else {
166      assertEquals(key, reconstructedKey);
167    }
168  }
169}