diff --git a/zookeeper-docs/src/main/resources/markdown/zookeeperReconfig.md b/zookeeper-docs/src/main/resources/markdown/zookeeperReconfig.md index afb85df0ad9..75961eface6 100644 --- a/zookeeper-docs/src/main/resources/markdown/zookeeperReconfig.md +++ b/zookeeper-docs/src/main/resources/markdown/zookeeperReconfig.md @@ -88,28 +88,55 @@ feature is disabled by default, and has to be explicitly turned on via ### Specifying the client port -A client port of a server is the port on which the server accepts -client connection requests. Starting with 3.5.0 the -_clientPort_ and _clientPortAddress_ configuration parameters should no longer be used. Instead, -this information is now part of the server keyword specification, which +A client port of a server is the port on which the server accepts plaintext (non-TLS) client connection requests +and secure client port is the port on which the server accepts TLS client connection requests. + +Starting with 3.5.0 the +_clientPort_ and _clientPortAddress_ configuration parameters should no longer be used in zoo.cfg. + +This fork also supports specifying the secure client address in the server entry. +The _secureClientPort_ and _secureClientPortAddress_ settings in zoo.cfg remain +supported and must agree with the dynamic secure address when both are present. + +Instead, this information is now part of the server keyword specification, which becomes as follows: - server. = ::[:role];[:]** + server. = ::[:role];[[:]][;[:]] -The client port specification is to the right of the semicolon. The -client port address is optional, and if not specified it defaults to -"0.0.0.0". As usual, role is also optional, it can be -_participant_ or _observer_ -(_participant_ by default). +- The client port specification is optional and is to the right of the + first semicolon. The secure client port specification is also optional and is to the right + of the second semicolon. A server joining through reconfiguration must specify at least + one client address. Configurations that obtain their client addresses from static + settings can omit them from the server entry. If the user intends to omit client + port specification and provide only secure client port specification (TLS-only server), a second + semicolon should still be specified to indicate an empty client port specification (see last + example below). In either spec, the port address is optional, and if not specified it defaults + to "0.0.0.0". +- As usual, role is also optional, it can be _participant_ or _observer_ (_participant_ by default). Examples of legal server statements: - server.5 = 125.23.63.23:1234:1235;1236 - server.5 = 125.23.63.23:1234:1235:participant;1236 - server.5 = 125.23.63.23:1234:1235:observer;1236 - server.5 = 125.23.63.23:1234:1235;125.23.63.24:1236 - server.5 = 125.23.63.23:1234:1235:participant;125.23.63.23:1236 - + server.5 = 125.23.63.23:1234:1235;1236 (non-TLS server) + server.5 = 125.23.63.23:1234:1235;1236;1237 (non-TLS + TLS server) + server.5 = 125.23.63.23:1234:1235;;1237 (TLS-only server) + server.5 = 125.23.63.23:1234:1235:participant;1236 (non-TLS server) + server.5 = 125.23.63.23:1234:1235:observer;1236 (non-TLS server) + server.5 = 125.23.63.23:1234:1235;125.23.63.24:1236 (non-TLS server) + server.5 = 125.23.63.23:1234:1235:participant;125.23.63.23:1236 (non-TLS server) + server.5 = 125.23.63.23:1234:1235:participant;125.23.63.23:1236;125.23.63.23:1237 (non-TLS + TLS server) + server.5 = 125.23.63.23:1234:1235:participant;;125.23.63.23:1237 (TLS-only server) + +**Compatibility:** Servers and tools that parse entries containing the secure +section must support this extension. Stock 3.6.3 and 3.8.7 configuration parsers +reject these forms. Retrieving configuration bytes is distinct from parsing them; +ordinary clients supplied with an explicit connection string do not parse server +entries, but still need the appropriate TLS support and configuration. + +**Reconfiguration limitation:** Creating a previously absent listener through an +in-place reconfiguration can bind the port without attaching the active server. +Clients then receive connection-loss errors until a subsequent role transition +or restart initializes the listener. This inherited limitation does not apply +to rebinding an existing listener. diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/client/FourLetterWordMain.java b/zookeeper-server/src/main/java/org/apache/zookeeper/client/FourLetterWordMain.java index de2f79dcf1d..39988c39bf9 100644 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/client/FourLetterWordMain.java +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/client/FourLetterWordMain.java @@ -91,7 +91,29 @@ public static String send4LetterWord( String cmd, boolean secure, int timeout) throws IOException, SSLContextException { - LOG.info("connecting to {} {}", host, port); + return send4LetterWord(host, port, cmd, secure, timeout, null); + } + + /** + * Send the 4letterword + * @param host the destination host + * @param port the destination port + * @param cmd the 4letterword + * @param secure whether to use SSL + * @param timeout in milliseconds, maximum time to wait while connecting/reading data + * @param sslContext SSL context + * @return server response + * @throws java.io.IOException + * @throws SSLContextException + */ + public static String send4LetterWord( + String host, + int port, + String cmd, + boolean secure, + int timeout, + SSLContext sslContext) throws IOException, SSLContextException { + LOG.info("connecting to {}:{} (secure={})", host, port, secure); Socket sock = null; BufferedReader reader = null; @@ -101,14 +123,16 @@ public static String send4LetterWord( : new InetSocketAddress(InetAddress.getByName(null), port); if (secure) { LOG.info("using secure socket"); - try (X509Util x509Util = new ClientX509Util()) { - SSLContext sslContext = x509Util.getDefaultSSLContext(); - SSLSocketFactory socketFactory = sslContext.getSocketFactory(); - SSLSocket sslSock = (SSLSocket) socketFactory.createSocket(); - sslSock.connect(hostaddress, timeout); - sslSock.startHandshake(); - sock = sslSock; + if (sslContext == null) { + try (X509Util x509Util = new ClientX509Util()) { + sslContext = x509Util.getDefaultSSLContext(); + } } + SSLSocketFactory socketFactory = sslContext.getSocketFactory(); + SSLSocket sslSock = (SSLSocket) socketFactory.createSocket(); + sslSock.connect(hostaddress, timeout); + sslSock.startHandshake(); + sock = sslSock; } else { sock = new Socket(); sock.connect(hostaddress, timeout); diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/PrepRequestProcessor.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/PrepRequestProcessor.java index b6b631ca0d1..4e06b2556e2 100644 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/server/PrepRequestProcessor.java +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/PrepRequestProcessor.java @@ -499,8 +499,8 @@ protected void pRequest2Txn(int type, long zxid, Request request, Record record, // extract server id x from first part of joiner: server.x Long sid = Long.parseLong(parts[0].substring(parts[0].lastIndexOf('.') + 1)); QuorumServer qs = new QuorumServer(sid, parts[1]); - if (qs.clientAddr == null || qs.electionAddr == null || qs.addr == null) { - throw new KeeperException.BadArgumentsException("Wrong format of server string - each server should have 3 ports specified"); + if ((qs.clientAddr == null && qs.secureClientAddr == null) || qs.electionAddr == null || qs.addr == null) { + throw new KeeperException.BadArgumentsException("Wrong format of server string - each server should have at least 3 ports specified"); } // check duplication of addresses and ports diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeer.java index 99ee52597ee..58f9cd99c9f 100644 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeer.java +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeer.java @@ -19,6 +19,7 @@ package org.apache.zookeeper.server.quorum; import static org.apache.zookeeper.common.NetUtils.formatInetAddr; +import static org.apache.zookeeper.server.quorum.QuorumPeerConfig.configureSSLAuth; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; @@ -149,15 +150,20 @@ public static final class AddressTuple { public final MultipleAddresses quorumAddr; public final MultipleAddresses electionAddr; public final InetSocketAddress clientAddr; + public final InetSocketAddress secureClientAddr; - public AddressTuple(MultipleAddresses quorumAddr, MultipleAddresses electionAddr, InetSocketAddress clientAddr) { + public AddressTuple(MultipleAddresses quorumAddr, MultipleAddresses electionAddr, InetSocketAddress clientAddr, InetSocketAddress secureClientAddr) { this.quorumAddr = quorumAddr; this.electionAddr = electionAddr; this.clientAddr = clientAddr; + this.secureClientAddr = secureClientAddr; } } + private Boolean isClientAddrFromStatic = null; + private Boolean isSecureClientAddrFromStatic = null; + private int observerMasterPort; public int getObserverMasterPort() { @@ -213,6 +219,7 @@ public static class QuorumServer { public MultipleAddresses electionAddr = new MultipleAddresses(); public InetSocketAddress clientAddr = null; + public InetSocketAddress secureClientAddr = null; public long id; @@ -221,20 +228,31 @@ public static class QuorumServer { public LearnerType type = LearnerType.PARTICIPANT; public boolean isClientAddrFromStatic = false; + public boolean isSecureClientAddrFromStatic = false; private List myAddrs; public QuorumServer(long id, InetSocketAddress addr, InetSocketAddress electionAddr, InetSocketAddress clientAddr) { - this(id, addr, electionAddr, clientAddr, LearnerType.PARTICIPANT); + this(id, addr, electionAddr, clientAddr, null, LearnerType.PARTICIPANT); + } + + public QuorumServer(long id, InetSocketAddress addr, InetSocketAddress electionAddr, InetSocketAddress clientAddr, + InetSocketAddress secureClientAddr) { + this(id, addr, electionAddr, clientAddr, secureClientAddr, LearnerType.PARTICIPANT); + } + + public QuorumServer(long id, InetSocketAddress addr, InetSocketAddress electionAddr, InetSocketAddress clientAddr, + LearnerType learnerType) { + this(id, addr, electionAddr, clientAddr, null, learnerType); } public QuorumServer(long id, InetSocketAddress addr, InetSocketAddress electionAddr) { - this(id, addr, electionAddr, null, LearnerType.PARTICIPANT); + this(id, addr, electionAddr, null, null, LearnerType.PARTICIPANT); } // VisibleForTesting public QuorumServer(long id, InetSocketAddress addr) { - this(id, addr, null, null, LearnerType.PARTICIPANT); + this(id, addr, null, null, null, LearnerType.PARTICIPANT); } public long getId() { @@ -281,10 +299,10 @@ public QuorumServer(long sid, String addressStr) throws ConfigException { } public QuorumServer(long id, InetSocketAddress addr, InetSocketAddress electionAddr, LearnerType type) { - this(id, addr, electionAddr, null, type); + this(id, addr, electionAddr, null, null, type); } - public QuorumServer(long id, InetSocketAddress addr, InetSocketAddress electionAddr, InetSocketAddress clientAddr, LearnerType type) { + public QuorumServer(long id, InetSocketAddress addr, InetSocketAddress electionAddr, InetSocketAddress clientAddr, InetSocketAddress secureClientAddr, LearnerType type) { this.id = id; if (addr != null) { this.addr.addAddress(addr); @@ -294,6 +312,7 @@ public QuorumServer(long id, InetSocketAddress addr, InetSocketAddress electionA } this.type = type; this.clientAddr = clientAddr; + this.secureClientAddr = secureClientAddr; setMyAddrs(); } @@ -301,14 +320,15 @@ public QuorumServer(long id, InetSocketAddress addr, InetSocketAddress electionA private static final String wrongFormat = " does not have the form server_config or server_config;client_config" + " where server_config is the pipe separated list of host:port:port or host:port:port:type" - + " and client_config is port or host:port"; + + " and client_config is host:clientPort;host:secureClientPort or clientPort or host:clientPort" + + " or ';secureClientPort' or ';host:secureClientPort'"; private void initializeWithAddressString(String addressStr, Function getInetAddress) throws ConfigException { LearnerType newType = null; String[] serverClientParts = addressStr.split(";"); String[] serverAddresses = serverClientParts[0].split("\\|"); - if (serverClientParts.length == 2) { + if (serverClientParts.length >= 2 && !serverClientParts[1].isEmpty()) { String[] clientParts = ConfigUtils.getHostAndPort(serverClientParts[1]); if (clientParts.length > 2) { throw new ConfigException(addressStr + wrongFormat); @@ -319,8 +339,25 @@ private void initializeWithAddressString(String addressStr, Function 2) { + throw new ConfigException(addressStr + wrongFormat); + } + + // is secure client config a host:port or just a port + String secureClientHostName = (secureClientParts.length == 2) ? secureClientParts[0] : "0.0.0.0"; + try { + secureClientAddr = new InetSocketAddress(secureClientHostName, Integer.parseInt(secureClientParts[secureClientParts.length - 1])); + } catch (NumberFormatException e) { + throw new ConfigException("Address unresolved: " + secureClientHostName + ":" + secureClientParts[secureClientParts.length - 1]); } + // set x509 auth provider if not already set + configureSSLAuth(); } boolean multiAddressEnabled = Boolean.parseBoolean( @@ -335,9 +372,8 @@ private void initializeWithAddressString(String addressStr, Function 2) || (serverParts.length < 3) - || (serverParts.length > 4)) { + String[] serverParts = ConfigUtils.getHostAndPort(serverAddress); + if ((serverParts.length < 3) || (serverParts.length > 4)) { throw new ConfigException(addressStr + wrongFormat); } @@ -412,6 +448,7 @@ private void setMyAddrs() { this.myAddrs = new ArrayList<>(); this.myAddrs.addAll(this.addr.getAllAddresses()); this.myAddrs.add(this.clientAddr); + this.myAddrs.add(this.secureClientAddr); this.myAddrs.addAll(this.electionAddr.getAllAddresses()); this.myAddrs = excludedSpecialAddresses(this.myAddrs); } @@ -445,13 +482,24 @@ public String toString() { sw.append(":participant"); } + boolean clientPortSpecAdded = false; if (clientAddr != null && !isClientAddrFromStatic) { + clientPortSpecAdded = true; sw.append(";"); sw.append(delimitedHostString(clientAddr)); sw.append(":"); sw.append(String.valueOf(clientAddr.getPort())); } + if (secureClientAddr != null & !isSecureClientAddrFromStatic) { + if (!clientPortSpecAdded) { + sw.append(";"); + } + sw.append(";"); + sw.append(delimitedHostString(secureClientAddr)); + sw.append(":"); + sw.append(String.valueOf(secureClientAddr.getPort())); + } return sw.toString(); } @@ -460,7 +508,7 @@ public int hashCode() { return 42; // any arbitrary constant will do } - private boolean checkAddressesEqual(InetSocketAddress addr1, InetSocketAddress addr2) { + private static boolean checkAddressesEqual(InetSocketAddress addr1, InetSocketAddress addr2) { return (addr1 != null || addr2 == null) && (addr1 == null || addr2 != null) && (addr1 == null || addr2 == null || addr1.equals(addr2)); @@ -480,12 +528,16 @@ public boolean equals(Object o) { if (!electionAddr.equals(qs.electionAddr)) { return false; } - return checkAddressesEqual(clientAddr, qs.clientAddr); + if (!checkAddressesEqual(clientAddr, qs.clientAddr)) { + return false; + } + return checkAddressesEqual(secureClientAddr, qs.secureClientAddr); } public void checkAddressDuplicate(QuorumServer s) throws BadArgumentsException { List otherAddrs = new ArrayList<>(s.addr.getAllAddresses()); otherAddrs.add(s.clientAddr); + otherAddrs.add(s.secureClientAddr); otherAddrs.addAll(s.electionAddr.getAllAddresses()); otherAddrs = excludedSpecialAddresses(otherAddrs); @@ -706,6 +758,7 @@ public synchronized void setCurrentVote(Vote v) { * value of one indicates the default backlog will be used. */ protected int clientPortListenBacklog = -1; + protected int maxClientCnxns = -1; /** * The number of ticks that the initial synchronization phase can take @@ -991,7 +1044,7 @@ public void recreateSocketAddresses(long id) { if (qs != null) { qs.recreateSocketAddresses(); if (id == getMyId()) { - setAddrs(qs.addr, qs.electionAddr, qs.clientAddr); + setAddrs(qs.addr, qs.electionAddr, qs.clientAddr, qs.secureClientAddr); } } } @@ -1037,9 +1090,15 @@ public InetSocketAddress getClientAddress() { return (addrs == null) ? null : addrs.clientAddr; } - private void setAddrs(MultipleAddresses quorumAddr, MultipleAddresses electionAddr, InetSocketAddress clientAddr) { + public InetSocketAddress getSecureClientAddress() { + final AddressTuple addrs = myAddrs.get(); + return (addrs == null) ? null : addrs.secureClientAddr; + } + + private void setAddrs(MultipleAddresses quorumAddr, MultipleAddresses electionAddr, InetSocketAddress clientAddr, + InetSocketAddress secureClientAddr) { synchronized (QV_LOCK) { - myAddrs.set(new AddressTuple(quorumAddr, electionAddr, clientAddr)); + myAddrs.set(new AddressTuple(quorumAddr, electionAddr, clientAddr, secureClientAddr)); QV_LOCK.notifyAll(); } } @@ -1302,7 +1361,7 @@ public QuorumPeer(Map quorumPeers, File snapDir, File logDir private static InetSocketAddress getClientAddress(Map quorumPeers, long myid, int clientPort) throws IOException { QuorumServer quorumServer = quorumPeers.get(myid); if (null == quorumServer) { - throw new IOException("No QuorumServer correspoding to myid " + myid); + throw new IOException("No QuorumServer corresponding to myid " + myid); } if (null == quorumServer.clientAddr) { return new InetSocketAddress(clientPort); @@ -1814,6 +1873,16 @@ public void setClientPortListenBacklog(int backlog) { this.clientPortListenBacklog = backlog; } + /** The server max client connections */ + public int getMaxClientCnxns() { + return maxClientCnxns; + } + + /** Sets the server's max client connections */ + public void setMaxClientCnxns(int maxClientCnxns) { + this.maxClientCnxns = maxClientCnxns; + } + /** * Get the number of ticks that the initial synchronization phase can take */ @@ -1964,7 +2033,7 @@ public QuorumVerifier setQuorumVerifier(QuorumVerifier qv, boolean writeToDisk) try { String dynamicConfigFilename = makeDynamicConfigFilename(qv.getVersion()); QuorumPeerConfig.writeDynamicConfig(dynamicConfigFilename, qv, false); - QuorumPeerConfig.editStaticConfig(configFilename, dynamicConfigFilename, needEraseClientInfoFromStaticConfig()); + QuorumPeerConfig.editStaticConfig(configFilename, dynamicConfigFilename, needEraseClientInfoFromStaticConfig(), needEraseSecureClientInfoFromStaticConfig()); } catch (IOException e) { LOG.error("Error closing file", e); } @@ -1978,7 +2047,16 @@ public QuorumVerifier setQuorumVerifier(QuorumVerifier qv, boolean writeToDisk) } QuorumServer qs = qv.getAllMembers().get(getMyId()); if (qs != null) { - setAddrs(qs.addr, qs.electionAddr, qs.clientAddr); + setAddrs(qs.addr, qs.electionAddr, qs.clientAddr, qs.secureClientAddr); + + // we only set this once, because quorum verifier can change based on dynamic reconfig + if (isClientAddrFromStatic == null) { + isClientAddrFromStatic = qs.isClientAddrFromStatic; + } + + if (isSecureClientAddrFromStatic == null) { + isSecureClientAddrFromStatic = qs.isSecureClientAddrFromStatic; + } } updateObserverMasterList(); return prevQV; @@ -1994,6 +2072,11 @@ private boolean needEraseClientInfoFromStaticConfig() { return (server != null && server.clientAddr != null && !server.isClientAddrFromStatic); } + private boolean needEraseSecureClientInfoFromStaticConfig() { + QuorumServer server = quorumVerifier.getAllMembers().get(getMyId()); + return (server != null && server.secureClientAddr != null && !server.isSecureClientAddrFromStatic); + } + /** * Get an instance of LeaderElection */ @@ -2145,6 +2228,13 @@ public int getClientPort() { return -1; } + public int getSecureClientPort() { + if (secureCnxnFactory != null) { + return secureCnxnFactory.getLocalPort(); + } + return -1; + } + public void setTxnFactory(FileTxnSnapLog factory) { this.logFactory = factory; } @@ -2252,6 +2342,7 @@ public boolean processReconfig(QuorumVerifier qv, Long suggestedLeaderId, Long z } InetSocketAddress oldClientAddr = getClientAddress(); + InetSocketAddress oldSecureClientAddr = getSecureClientAddress(); // update last committed quorum verifier, write the new config to disk // and restart leader election if config changed. @@ -2275,8 +2366,53 @@ public boolean processReconfig(QuorumVerifier qv, Long suggestedLeaderId, Long z } QuorumServer myNewQS = newMembers.get(getMyId()); - if (myNewQS != null && myNewQS.clientAddr != null && !myNewQS.clientAddr.equals(oldClientAddr)) { - cnxnFactory.reconfigure(myNewQS.clientAddr); + if (myNewQS != null) { + if (myNewQS.clientAddr == null) { + if (!isClientAddrFromStatic && oldClientAddr != null && cnxnFactory != null) { + // clientAddr omitted in new config, shutdown cnxnFactory + cnxnFactory.shutdown(); + cnxnFactory = null; + } + } else if (!myNewQS.clientAddr.equals(oldClientAddr)) { + // clientAddr has changed + if (cnxnFactory == null) { + // start cnxnFactory first + try { + cnxnFactory = ServerCnxnFactory.createFactory(); + cnxnFactory.configure(myNewQS.clientAddr, getMaxClientCnxns(), getClientPortListenBacklog(), false); + cnxnFactory.start(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } else { + cnxnFactory.reconfigure(myNewQS.clientAddr); + } + } + + if (myNewQS.secureClientAddr == null) { + if (!isSecureClientAddrFromStatic && oldSecureClientAddr != null && secureCnxnFactory != null) { + // secureClientAddr omitted in new config, shutdown secureCnxnFactory + secureCnxnFactory.shutdown(); + secureCnxnFactory = null; + } + } else if (!myNewQS.secureClientAddr.equals(oldSecureClientAddr)) { + // secureClientAddr has changed + if (secureCnxnFactory == null) { + // start secureCnxnFactory first + try { + configureSSLAuth(); + secureCnxnFactory = ServerCnxnFactory.createFactory(); + secureCnxnFactory.configure(myNewQS.secureClientAddr, getMaxClientCnxns(), getClientPortListenBacklog(), true); + secureCnxnFactory.start(); + + } catch (IOException | ConfigException e) { + throw new RuntimeException(e); + } + } else { + secureCnxnFactory.reconfigure(myNewQS.secureClientAddr); + } + } + updateThreadName(); } @@ -2655,6 +2791,7 @@ public static QuorumPeer createFromConfig(QuorumPeerConfig config) throws IOExce quorumPeer.setObserverMasterPort(config.getObserverMasterPort()); quorumPeer.setConfigFileName(config.getConfigFilename()); quorumPeer.setClientPortListenBacklog(config.getClientPortListenBacklog()); + quorumPeer.setMaxClientCnxns(config.getMaxClientCnxns()); quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory())); quorumPeer.setQuorumVerifier(config.getQuorumVerifier(), false); if (config.getLastSeenQuorumVerifier() != null) { diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java index b8e84351f43..471dc50ff09 100644 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java @@ -645,9 +645,11 @@ public void write(Writer out) throws IOException { * it will remove them. * If it needs to erase client port information left by the old config, * "eraseClientPortAddress" should be set true. + * If it needs to erase secure client port information left by the old config, + * "eraseSecureClientPortAddress" should be set true. * It should also updates dynamic file pointer on reconfig. */ - public static void editStaticConfig(final String configFileStr, final String dynamicFileStr, final boolean eraseClientPortAddress) throws IOException { + public static void editStaticConfig(final String configFileStr, final String dynamicFileStr, final boolean eraseClientPortAddress, final boolean eraseSecureClientPortAddress) throws IOException { // Some tests may not have a static config file. if (configFileStr == null) { return; @@ -679,7 +681,10 @@ public void write(Writer out) throws IOException { || key.startsWith("peerType") || (eraseClientPortAddress && (key.startsWith("clientPort") - || key.startsWith("clientPortAddress")))) { + || key.startsWith("clientPortAddress"))) + || (eraseSecureClientPortAddress + && (key.startsWith("secureClientPort") + || key.startsWith("secureClientPortAddress")))) { // not writing them back to static file continue; } @@ -734,6 +739,7 @@ void setupQuorumPeerConfig(Properties prop, boolean configBackwardCompatibilityM quorumVerifier = parseDynamicConfig(prop, electionAlg, true, configBackwardCompatibilityMode, oraclePath); setupMyId(); setupClientPort(); + setupSecureClientPort(); setupPeerType(); checkValidity(); } @@ -840,6 +846,29 @@ private void setupClientPort() throws ConfigException { } } + private void setupSecureClientPort() throws ConfigException { + if (serverId == UNSET_SERVERID) { + return; + } + QuorumServer qs = quorumVerifier.getAllMembers().get(serverId); + if (secureClientPortAddress != null && qs != null && qs.secureClientAddr != null) { + if ((!secureClientPortAddress.getAddress().isAnyLocalAddress() && !secureClientPortAddress.equals(qs.secureClientAddr)) || ( + secureClientPortAddress.getAddress().isAnyLocalAddress() + && secureClientPortAddress.getPort() != qs.secureClientAddr.getPort())) { + throw new ConfigException("secure client address for this server (id = " + serverId + + ") in static config file is " + secureClientPortAddress + + " is different from secure client address found in dynamic file: " + qs.secureClientAddr); + } + } + if (qs != null && qs.secureClientAddr != null) { + secureClientPortAddress = qs.secureClientAddr; + } + if (qs != null && qs.secureClientAddr == null) { + qs.secureClientAddr = secureClientPortAddress; + qs.isSecureClientAddrFromStatic = true; + } + } + private void setupPeerType() { // Warn about inconsistent peer type LearnerType roleByServersList = quorumVerifier.getObservingMembers().containsKey(serverId) diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerMain.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerMain.java index 00cacb94121..af37daf7d00 100644 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerMain.java +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerMain.java @@ -198,6 +198,7 @@ public void runFromConfig(QuorumPeerConfig config) throws IOException, AdminServ quorumPeer.setObserverMasterPort(config.getObserverMasterPort()); quorumPeer.setConfigFileName(config.getConfigFilename()); quorumPeer.setClientPortListenBacklog(config.getClientPortListenBacklog()); + quorumPeer.setMaxClientCnxns(config.getMaxClientCnxns()); quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory())); quorumPeer.setQuorumVerifier(config.getQuorumVerifier(), false); if (config.getLastSeenQuorumVerifier() != null) { diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerConfigTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerConfigTest.java index 407a9d1a58b..75ff0644802 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerConfigTest.java +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerConfigTest.java @@ -19,7 +19,9 @@ package org.apache.zookeeper.server.quorum; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -146,6 +148,62 @@ public void testClientAddrFromClientPort() throws IOException, ConfigException { assertEquals(quorumPeerConfig.getClientPortAddress(), qs.clientAddr); } + @Test + public void testSecureClientAddrFromSecureClientPort() throws IOException, ConfigException { + clearSslAuthProviderProperty(); + long serverId = 1; + QuorumPeerConfig quorumPeerConfig = new MockQuorumPeerConfig(serverId); + Properties zkProp = getDefaultZKProperties(); + int secureClientPort = 12346; + zkProp.setProperty("secureClientPort", Integer.toString(secureClientPort)); + zkProp.setProperty("server.1", "127.0.0.1:2889:3889:participant"); + quorumPeerConfig.parseProperties(zkProp); + + QuorumServer qs = quorumPeerConfig.getQuorumVerifier().getAllMembers().get(serverId); + assertEquals(new InetSocketAddress("0.0.0.0", secureClientPort), quorumPeerConfig.getSecureClientPortAddress()); + assertEquals(quorumPeerConfig.getSecureClientPortAddress(), qs.secureClientAddr); + assertTrue(qs.isSecureClientAddrFromStatic); + assertNull(quorumPeerConfig.getClientPortAddress()); + assertNull(qs.clientAddr); + assertEquals("127.0.0.1:2889:3889:participant", qs.toString()); + } + + @Test + public void testSecureClientAddrFromDynamicServerConfig() throws IOException, ConfigException { + clearSslAuthProviderProperty(); + long serverId = 1; + QuorumPeerConfig quorumPeerConfig = new MockQuorumPeerConfig(serverId); + Properties zkProp = getDefaultZKProperties(); + zkProp.setProperty("server.1", "127.0.0.1:2889:3889:participant;;127.0.0.1:12346"); + quorumPeerConfig.parseProperties(zkProp); + + QuorumServer qs = quorumPeerConfig.getQuorumVerifier().getAllMembers().get(serverId); + assertEquals(new InetSocketAddress("127.0.0.1", 12346), quorumPeerConfig.getSecureClientPortAddress()); + assertFalse(qs.isSecureClientAddrFromStatic); + assertNull(quorumPeerConfig.getClientPortAddress()); + assertNull(qs.clientAddr); + assertEquals("127.0.0.1:2889:3889:participant;;127.0.0.1:12346", qs.toString()); + } + + @Test + public void testStaticSecureClientPortMustMatchDynamicServerConfig() throws IOException, ConfigException { + clearSslAuthProviderProperty(); + Properties zkProp = getDefaultZKProperties(); + zkProp.setProperty("server.1", "127.0.0.1:2889:3889:participant;;127.0.0.1:12346"); + zkProp.setProperty("secureClientPort", "12346"); + QuorumPeerConfig matchingPort = new MockQuorumPeerConfig(1); + matchingPort.parseProperties(zkProp); + assertEquals(new InetSocketAddress("127.0.0.1", 12346), matchingPort.getSecureClientPortAddress()); + + zkProp.setProperty("secureClientPort", "12347"); + ConfigException differentPort = assertThrows(ConfigException.class, () -> new MockQuorumPeerConfig(1).parseProperties(zkProp)); + assertTrue(differentPort.getMessage().contains("secure client address for this server (id = 1)")); + + zkProp.setProperty("secureClientPort", "12346"); + zkProp.setProperty("secureClientPortAddress", "127.0.0.2"); + assertThrows(ConfigException.class, () -> new MockQuorumPeerConfig(1).parseProperties(zkProp)); + } + @Test public void testJvmPauseMonitorConfigured() throws IOException, ConfigException { final Long sleepTime = 444L; @@ -200,6 +258,13 @@ public void testParseBoolean() throws IOException, ConfigException { } } + // testCustomSSLAuth leaves an unknown SSL auth provider configured + private static void clearSslAuthProviderProperty() { + try (ClientX509Util x509Util = new ClientX509Util()) { + System.clearProperty(x509Util.getSslAuthProviderProperty()); + } + } + private Properties getDefaultZKProperties() { Properties zkProp = new Properties(); zkProp.setProperty("dataDir", new File("myDataDir").getAbsolutePath()); diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerMainTLSTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerMainTLSTest.java new file mode 100644 index 00000000000..91020c06fa0 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerMainTLSTest.java @@ -0,0 +1,448 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.server.quorum.QuorumPeerTestBase.MainThread.UNSET_STATIC_CLIENTPORT; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.security.Security; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import org.apache.commons.io.FileUtils; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.admin.ZooKeeperAdmin; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.common.KeyStoreFileType; +import org.apache.zookeeper.common.X509KeyType; +import org.apache.zookeeper.common.X509TestContext; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.util.ConfigUtils; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ReconfigTest; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class QuorumPeerMainTLSTest extends QuorumPeerTestBase { + + protected static final Logger LOG = LoggerFactory.getLogger(QuorumPeerMainTLSTest.class); + private static File tempDir; + private static X509TestContext x509TestContext = null; + + @BeforeAll + public static void beforeAll() throws Exception { + Security.addProvider(new BouncyCastleProvider()); + tempDir = ClientBase.createEmptyTestDir(); + x509TestContext = X509TestContext.newBuilder() + .setTempDir(tempDir) + .setKeyStoreKeyType(X509KeyType.EC) + .setTrustStoreKeyType(X509KeyType.EC) + .build(); + } + + @AfterAll + public static void afterAll() { + Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME); + try { + FileUtils.deleteDirectory(tempDir); + } catch (IOException e) { + // ignore + } + } + + // TODO - test reconfig - NIO cnxn factory initially, reconfig to listen on TLS port, should fail coz Netty cnxn factory is needed + // TODO - equivalent of testReconfigRemoveClientFromStatic, but for secureClientPort + interface QuorumConfigBuilder { + String build(int id, String role, int quorumPort, int leaderPort, int clientPort, int secureClientPort); + } + + static class MaybeSecureServers extends Servers { + public int[] quorumPorts; + public int[] leaderPorts; + public boolean[] isSecureClient; + public int[] secureClientPorts; + int numParticipants; + int numObservers; + String quorumCfg; + + String otherCfg; + + public MaybeSecureServers(int numParticipants, int numObservers, String otherCfg, QuorumConfigBuilder quorumConfigBuilder) throws IOException { + this.numParticipants = numParticipants; + this.numObservers = numObservers; + this.otherCfg = otherCfg; + int size = numParticipants + numObservers; + + this.mt = new MainThread[size]; + this.zk = new ZooKeeper[size]; + this.quorumPorts = new int[size]; + this.leaderPorts = new int[size]; + this.clientPorts = new int[size]; + this.adminPorts = new int[size]; + this.secureClientPorts = new int[size]; + this.isSecureClient = new boolean[size]; + + StringBuilder quorumCfg = new StringBuilder(); + + for (int i = 0; i < size; i++) { + this.quorumPorts[i] = PortAssignment.unique(); + this.leaderPorts[i] = PortAssignment.unique(); + this.clientPorts[i] = PortAssignment.unique(); + this.adminPorts[i] = PortAssignment.unique(); + this.secureClientPorts[i] = PortAssignment.unique(); + String role = i < numParticipants ? "participant" : "observer"; + String serverEntry = quorumConfigBuilder.build(i, role, this.quorumPorts[i], this.leaderPorts[i], this.clientPorts[i], this.secureClientPorts[i]); + quorumCfg.append(serverEntry).append("\n"); + if (serverEntry.endsWith("" + this.secureClientPorts[i])) { + this.isSecureClient[i] = true; + } + } + + this.quorumCfg = quorumCfg.toString(); + for (int i = 0; i < size; i++) { + this.mt[i] = new MainThread(i, UNSET_STATIC_CLIENTPORT, this.adminPorts[i], null, this.quorumCfg, this.otherCfg, null, true, null); + } + } + + public void restartSecureClient(int clientIndex, Watcher watcher) throws IOException, InterruptedException { + if (zk[clientIndex] != null) { + zk[clientIndex].close(); + } + + isSecureClient[clientIndex] = true; + zk[clientIndex] = new ZooKeeper( + "127.0.0.1:" + secureClientPorts[clientIndex], + ClientBase.CONNECTION_TIMEOUT, + watcher, getClientTLSConfigs(x509TestContext)); + } + + public void restartAllServersAndClients(Watcher watcher) throws IOException, InterruptedException { + for (MainThread t : mt) { + if (!t.isAlive()) { + t.start(); + } + } + for (int i = 0; i < zk.length; i++) { + if (isSecureClient[i]) { + restartSecureClient(i, watcher); + } else { + restartClient(i, watcher); + } + } + } + } + + static Map getServerTLSConfigs(X509TestContext x509TestContext) throws IOException { + Map sslConfigs = new HashMap<>(); + sslConfigs.put("ssl.keyStore.location", x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM).getAbsolutePath()); + sslConfigs.put("ssl.trustStore.location", x509TestContext.getTrustStoreFile(KeyStoreFileType.PEM).getAbsolutePath()); + sslConfigs.put("ssl.keyStore.type", "PEM"); + sslConfigs.put("ssl.trustStore.type", "PEM"); + // Netty is required for TLS + sslConfigs.put("serverCnxnFactory", org.apache.zookeeper.server.NettyServerCnxnFactory.class.getName()); + return sslConfigs; + } + + static ZKClientConfig getClientTLSConfigs(X509TestContext x509TestContext) throws IOException { + if (x509TestContext == null) { + throw new RuntimeException("x509TestContext cannot be null"); + } + File clientKeyStore = x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM); + File clientTrustStore = x509TestContext.getTrustStoreFile(KeyStoreFileType.PEM); + + ZKClientConfig zKClientConfig = new ZKClientConfig(); + zKClientConfig.setProperty("zookeeper.client.secure", "true"); + zKClientConfig.setProperty("zookeeper.ssl.keyStore.location", clientKeyStore.getAbsolutePath()); + zKClientConfig.setProperty("zookeeper.ssl.trustStore.location", clientTrustStore.getAbsolutePath()); + zKClientConfig.setProperty("zookeeper.ssl.keyStore.type", "PEM"); + zKClientConfig.setProperty("zookeeper.ssl.trustStore.type", "PEM"); + // only netty supports TLS + zKClientConfig.setProperty("zookeeper.clientCnxnSocket", org.apache.zookeeper.ClientCnxnSocketNetty.class.getName()); + return zKClientConfig; + } + + private static String toConfig(Map configMap) { + StringBuilder configBuilder = new StringBuilder(); + for (Map.Entry entry : configMap.entrySet()) { + configBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("\n"); + } + return configBuilder.toString(); + } + + private static Map reconfigTLSConfigs() throws IOException { + Map configMap = new HashMap<>(); + configMap.put("reconfigEnabled", "true"); + configMap.put("authProvider.x509", "org.apache.zookeeper.server.auth.X509AuthenticationProvider"); + configMap.put("DigestAuthenticationProvider.superDigest", "super:D/InIHSb7yEEbrWz8b9l71RjZJU="/* password is 'test'*/); + configMap.putAll(getServerTLSConfigs(x509TestContext)); + return configMap; + } + + private static String serverEntry(MaybeSecureServers servers, int id, String clientSpec) { + return String.format("server.%d=127.0.0.1:%d:%d:participant%s", id, servers.quorumPorts[id], servers.leaderPorts[id], clientSpec); + } + + private static void assertSecureOnlyListener(QuorumPeer qp, int secureClientPort) { + assertNull(qp.cnxnFactory); + assertNull(qp.getClientAddress()); + assertEquals(-1, qp.getClientPort()); + assertNotNull(qp.secureCnxnFactory); + assertNotNull(qp.getSecureClientAddress()); + assertEquals(secureClientPort, qp.getSecureClientPort()); + assertEquals(secureClientPort, qp.getSecureClientAddress().getPort()); + } + + private ZooKeeper createSecureClient(int port) throws IOException, InterruptedException { + ZooKeeper zk = new ZooKeeper("127.0.0.1:" + port, ClientBase.CONNECTION_TIMEOUT, this, getClientTLSConfigs(x509TestContext)); + waitForOne(zk, ZooKeeper.States.CONNECTED); + return zk; + } + + /** + * Starts a single server in replicated mode + */ + @Test + public void testTLSQuorumPeers() throws IOException, InterruptedException { + Map configMap = new HashMap<>(); + configMap.put("standaloneEnabled", "false"); + configMap.put("authProvider.x509", "org.apache.zookeeper.server.auth.X509AuthenticationProvider"); + configMap.putAll(getServerTLSConfigs(x509TestContext)); + + StringBuilder configBuilder = new StringBuilder(); + for (Map.Entry entry : configMap.entrySet()) { + configBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("\n"); + } + + MaybeSecureServers maybeSecureServers = new MaybeSecureServers(3, 2, configBuilder.toString(), + (id, role, quorumPort, leaderPort, clientPort, secureClientPort) -> String.format("server.%d=127.0.0.1:%d:%d:%s;;127.0.0.1:%d", id, quorumPort, leaderPort, role, secureClientPort)); + + // wire to "servers" of QuorumPeerTestBase, so it can be destroyed in QuorumPeerTestBase.tearDown() + servers = maybeSecureServers; + + // start servers and clients + maybeSecureServers.restartAllServersAndClients(this); + + // wait for clients to connect + waitForAll(maybeSecureServers, ZooKeeper.States.CONNECTED); + + // Find and log leader + maybeSecureServers.findLeader(); + + QuorumPeer qp0 = maybeSecureServers.mt[0].getQuorumPeer(); + + assertNotNull(qp0); + + // verify no listener on client port + assertNull(qp0.cnxnFactory); + assertNull(qp0.getClientAddress()); + assertEquals(-1, qp0.getClientPort()); + + // verify valid secure client port listener exists + assertNotNull(qp0.secureCnxnFactory); + assertNotNull(qp0.getSecureClientAddress()); + assertEquals(maybeSecureServers.secureClientPorts[0], qp0.getSecureClientPort()); + assertEquals(maybeSecureServers.secureClientPorts[0], qp0.getSecureClientAddress().getPort()); + } + + @Test + public void reconfigFromClientPortToSecureClientPort() throws IOException, InterruptedException, KeeperException { + Map configMap = new HashMap<>(); + configMap.put("reconfigEnabled", "true"); + configMap.put("authProvider.x509", "org.apache.zookeeper.server.auth.X509AuthenticationProvider"); + configMap.put("DigestAuthenticationProvider.superDigest", "super:D/InIHSb7yEEbrWz8b9l71RjZJU="/* password is 'test'*/); + configMap.putAll(getServerTLSConfigs(x509TestContext)); + + StringBuilder configBuilder = new StringBuilder(); + for (Map.Entry entry : configMap.entrySet()) { + configBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("\n"); + } + + MaybeSecureServers maybeSecureServers = new MaybeSecureServers(3, 0, configBuilder.toString(), + (id, role, quorumPort, leaderPort, clientPort, secureClientPort) -> String.format("server.%d=127.0.0.1:%d:%d:%s;127.0.0.1:%d", id, quorumPort, leaderPort, role, clientPort)); + + servers = maybeSecureServers; + + maybeSecureServers.restartAllServersAndClients(this); + + waitForAll(maybeSecureServers, ZooKeeper.States.CONNECTED); + + ZooKeeperAdmin zkAdmin = new ZooKeeperAdmin("127.0.0.1:" + maybeSecureServers.clientPorts[0], ClientBase.CONNECTION_TIMEOUT, this); + zkAdmin.addAuthInfo("digest", "super:test".getBytes()); + + List joiningServers = new ArrayList<>(); + List leavingServers = new ArrayList<>(); + + int reconfigIndex = 1; + leavingServers.add(Integer.toString(reconfigIndex)); + joiningServers.add(String.format("server.%d=127.0.0.1:%d:%d:%s;;127.0.0.1:%d", reconfigIndex, + maybeSecureServers.quorumPorts[reconfigIndex], maybeSecureServers.leaderPorts[reconfigIndex], + "participant", maybeSecureServers.secureClientPorts[reconfigIndex])); + + ReconfigTest.reconfig(zkAdmin, null, leavingServers, null, -1); + LOG.info("Reconfig REMOVE done with leavingServers={}!", leavingServers); + ReconfigTest.testServerHasConfig(maybeSecureServers.zk[0], null, leavingServers); + + ReconfigTest.reconfig(zkAdmin, joiningServers, null, null, -1); + LOG.info("Reconfig ADD done with joiningServers={}!", joiningServers); + + ReconfigTest.testServerHasConfig(maybeSecureServers.zk[0], joiningServers, null); + + assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + maybeSecureServers.clientPorts[reconfigIndex], 5000, false)); + + maybeSecureServers.restartSecureClient(reconfigIndex, this); + waitForOne(maybeSecureServers.zk[reconfigIndex], ZooKeeper.States.CONNECTED); + ReconfigTest.testNormalOperation(maybeSecureServers.zk[0], maybeSecureServers.zk[reconfigIndex]); + } + + /** + * A TLS-only quorum publishes the secure-only form in its dynamic config, + * persists it, and comes back secure-only after a restart from that file. + */ + @Test + public void testTLSOnlyDynamicConfigIsServedPersistedAndReloaded() throws Exception { + MaybeSecureServers maybeSecureServers = new MaybeSecureServers(3, 0, toConfig(reconfigTLSConfigs()), + (id, role, quorumPort, leaderPort, clientPort, secureClientPort) -> String.format("server.%d=127.0.0.1:%d:%d:%s;;127.0.0.1:%d", id, quorumPort, leaderPort, role, secureClientPort)); + servers = maybeSecureServers; + maybeSecureServers.restartAllServersAndClients(this); + waitForAll(maybeSecureServers, ZooKeeper.States.CONNECTED); + + List expectedEntries = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + assertSecureOnlyListener(maybeSecureServers.mt[i].getQuorumPeer(), maybeSecureServers.secureClientPorts[i]); + assertFalse(ClientBase.waitForServerUp("127.0.0.1:" + maybeSecureServers.secureClientPorts[i], 1000, false)); + expectedEntries.add(serverEntry(maybeSecureServers, i, ";;127.0.0.1:" + maybeSecureServers.secureClientPorts[i])); + } + ReconfigTest.testNormalOperation(maybeSecureServers.zk[0], maybeSecureServers.zk[1]); + + String config = ReconfigTest.testServerHasConfig(maybeSecureServers.zk[2], expectedEntries, null); + // client-side connect-string extraction only reports plaintext client addresses + assertEquals(config.substring(config.indexOf("version=") + "version=".length()).trim() + " ,,", + ConfigUtils.getClientConfigStr(config)); + + Properties staticCfg = ReconfigLegacyTest.readPropertiesFromFile(maybeSecureServers.mt[2].confFile); + assertFalse(staticCfg.containsKey("clientPort")); + assertFalse(staticCfg.containsKey("secureClientPort")); + File dynamicFile = new File(staticCfg.getProperty("dynamicConfigFile")); + String persisted = new String(Files.readAllBytes(dynamicFile.toPath()), StandardCharsets.UTF_8); + for (String entry : expectedEntries) { + assertTrue(persisted.contains(entry), persisted); + } + + maybeSecureServers.mt[2].shutdown(); + maybeSecureServers.zk[2].close(); + maybeSecureServers.mt[2].start(); + maybeSecureServers.restartSecureClient(2, this); + waitForOne(maybeSecureServers.zk[2], ZooKeeper.States.CONNECTED); + assertSecureOnlyListener(maybeSecureServers.mt[2].getQuorumPeer(), maybeSecureServers.secureClientPorts[2]); + ReconfigTest.testNormalOperation(maybeSecureServers.zk[0], maybeSecureServers.zk[2]); + } + + /** + * Servers configured with both a plaintext and a secure client address serve both. + */ + @Test + public void testDualClientListeners() throws Exception { + MaybeSecureServers maybeSecureServers = new MaybeSecureServers(3, 0, toConfig(reconfigTLSConfigs()), + (id, role, quorumPort, leaderPort, clientPort, secureClientPort) -> String.format("server.%d=127.0.0.1:%d:%d:%s;127.0.0.1:%d;127.0.0.1:%d", id, quorumPort, leaderPort, role, clientPort, secureClientPort)); + servers = maybeSecureServers; + maybeSecureServers.restartAllServersAndClients(this); + waitForAll(maybeSecureServers, ZooKeeper.States.CONNECTED); + + QuorumPeer qp0 = maybeSecureServers.mt[0].getQuorumPeer(); + assertEquals(maybeSecureServers.clientPorts[0], qp0.getClientPort()); + assertEquals(maybeSecureServers.secureClientPorts[0], qp0.getSecureClientPort()); + + ZooKeeper plaintext = ClientBase.createZKClient("127.0.0.1:" + maybeSecureServers.clientPorts[0]); + try { + ReconfigTest.testNormalOperation(plaintext, maybeSecureServers.zk[0]); + ReconfigTest.testServerHasConfig(plaintext, Collections.singletonList(serverEntry(maybeSecureServers, 0, + ";127.0.0.1:" + maybeSecureServers.clientPorts[0] + ";127.0.0.1:" + maybeSecureServers.secureClientPorts[0])), null); + } finally { + plaintext.close(); + } + } + + /** + * Reconfigures the client addresses of a running member in place and verifies + * the listeners that remain or are moved keep serving clients. + */ + @Test + public void testInPlaceReconfigOfClientAddresses() throws Exception { + MaybeSecureServers maybeSecureServers = new MaybeSecureServers(3, 0, toConfig(reconfigTLSConfigs()), + (id, role, quorumPort, leaderPort, clientPort, secureClientPort) -> String.format("server.%d=127.0.0.1:%d:%d:%s;127.0.0.1:%d;127.0.0.1:%d", id, quorumPort, leaderPort, role, clientPort, secureClientPort)); + servers = maybeSecureServers; + maybeSecureServers.restartAllServersAndClients(this); + waitForAll(maybeSecureServers, ZooKeeper.States.CONNECTED); + + ZooKeeperAdmin zkAdmin = new ZooKeeperAdmin("127.0.0.1:" + maybeSecureServers.secureClientPorts[0], + ClientBase.CONNECTION_TIMEOUT, this, getClientTLSConfigs(x509TestContext)); + try { + zkAdmin.addAuthInfo("digest", "super:test".getBytes()); + int target = 2; + QuorumPeer qp = maybeSecureServers.mt[target].getQuorumPeer(); + + // A joiner must have at least one client address. + String noClientAddress = String.format("server.%d=127.0.0.1:%d:%d:participant", 5, PortAssignment.unique(), PortAssignment.unique()); + assertThrows(KeeperException.BadArgumentsException.class, + () -> zkAdmin.reconfigure(Collections.singletonList(noClientAddress), null, null, -1, new Stat())); + + // drop the plaintext listener + String tlsOnly = serverEntry(maybeSecureServers, target, ";;127.0.0.1:" + maybeSecureServers.secureClientPorts[target]); + ReconfigTest.reconfig(zkAdmin, Collections.singletonList(tlsOnly), null, null, -1); + ReconfigTest.testServerHasConfig(maybeSecureServers.zk[0], Collections.singletonList(tlsOnly), null); + assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + maybeSecureServers.clientPorts[target], ClientBase.CONNECTION_TIMEOUT, false)); + assertSecureOnlyListener(qp, maybeSecureServers.secureClientPorts[target]); + ReconfigTest.testNormalOperation(maybeSecureServers.zk[0], maybeSecureServers.zk[target]); + + // move the secure listener + int movedSecurePort = PortAssignment.unique(); + String moved = serverEntry(maybeSecureServers, target, ";;127.0.0.1:" + movedSecurePort); + ReconfigTest.reconfig(zkAdmin, Collections.singletonList(moved), null, null, -1); + ReconfigTest.testServerHasConfig(maybeSecureServers.zk[0], Collections.singletonList(moved), null); + assertSecureOnlyListener(qp, movedSecurePort); + ZooKeeper movedClient = createSecureClient(movedSecurePort); + try { + ReconfigTest.testNormalOperation(maybeSecureServers.zk[0], movedClient); + } finally { + movedClient.close(); + } + } finally { + zkAdmin.close(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerTestBase.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerTestBase.java index adf1b1e455e..15acfca762e 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerTestBase.java +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerTestBase.java @@ -60,14 +60,17 @@ public class QuorumPeerTestBase extends ZKTestCase implements Watcher { @AfterEach public void tearDown() throws Exception { - if (servers == null || servers.mt == null) { + if (servers == null) { LOG.info("No servers to shutdown!"); return; } - for (int i = 0; i < numServers; i++) { - if (i < servers.mt.length) { - servers.mt[i].shutdown(); - } + + if (servers.zk != null) { + servers.shutdownAllClients(); + } + + if (servers.mt != null) { + servers.shutDownAllServers(); } } @@ -419,6 +422,7 @@ protected static class Servers { MainThread[] mt; ZooKeeper[] zk; public int[] clientPorts; + public int[] adminPorts; public void shutDownAllServers() throws InterruptedException { for (MainThread t : mt) { @@ -426,10 +430,19 @@ public void shutDownAllServers() throws InterruptedException { } } + public void shutdownAllClients() throws InterruptedException { + for (ZooKeeper zk : zk) { + zk.close(5000); + } + } + public void restartAllServersAndClients(Watcher watcher) throws IOException, InterruptedException { + int index = 0; for (MainThread t : mt) { if (!t.isAlive()) { + System.setProperty("zookeeper.admin.serverPort", String.valueOf(adminPorts[index])); t.start(); + index++; } } for (int i = 0; i < zk.length; i++) { @@ -522,6 +535,12 @@ protected Servers LaunchServers(int numServers, int numObservers, Integer tickTi i, PortAssignment.unique(), PortAssignment.unique(), role, svrs.clientPorts[i])); } + + svrs.adminPorts = new int[SERVER_COUNT]; + for (int i = 0; i < SERVER_COUNT; i++) { + svrs.adminPorts[i] = PortAssignment.unique(); + } + String quorumCfgSection = sb.toString(); svrs.mt = new MainThread[SERVER_COUNT]; @@ -532,6 +551,7 @@ protected Servers LaunchServers(int numServers, int numObservers, Integer tickTi } else { svrs.mt[i] = new MainThread(i, svrs.clientPorts[i], quorumCfgSection, otherConfigs); } + System.setProperty("zookeeper.admin.serverPort", String.valueOf(svrs.adminPorts[i])); svrs.mt[i].start(); svrs.restartClient(i, this); } diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumServerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumServerTest.java index 71630913b7c..7417cdaec07 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumServerTest.java +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumServerTest.java @@ -17,7 +17,11 @@ package org.apache.zookeeper.server.quorum; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import java.net.InetSocketAddress; import org.apache.zookeeper.KeeperException; @@ -69,6 +73,16 @@ public void testToString() throws ConfigException { expected = "example.com:1234:1236:participant;0.0.0.0:1237"; qs = new QuorumServer(0, provided); assertEquals(expected, qs.toString(), "Use hostname"); + + provided = "example.com:1234:1236:participant;1237;1238"; + expected = "example.com:1234:1236:participant;0.0.0.0:1237;0.0.0.0:1238"; + qs = new QuorumServer(0, provided); + assertEquals(expected, qs.toString(), "clientPort and secureClientPort"); + + provided = ipv4config + ":participant;;1.2.3.4:1237"; + expected = ipv4config + ":participant;;1.2.3.4:1237"; + qs = new QuorumServer(0, provided); + assertEquals(expected, qs.toString(), "Only secureClientPort"); } @Test @@ -112,6 +126,14 @@ public void shouldAllowMultipleAddressesWhenMultiAddressFeatureIsEnabled() throw System.setProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_ENABLED, "true"); QuorumServer qs = new QuorumServer(0, "127.0.0.1:1234:1236|127.0.0.1:2234:2236"); assertEquals("127.0.0.1:1234:1236|127.0.0.1:2234:2236:participant", qs.toString(), "MultiAddress parse error"); + + qs = new QuorumServer(0, "127.0.0.1:1234:1236|127.0.0.1:2234:2236;1237;1238"); + assertEquals("127.0.0.1:1234:1236|127.0.0.1:2234:2236:participant;0.0.0.0:1237;0.0.0.0:1238", qs.toString(), + "MultiAddress parse with clientPort and secureClientPort"); + + qs = new QuorumServer(0, "127.0.0.1:1234:1236|127.0.0.1:2234:2236;;127.0.0.1:1238"); + assertEquals("127.0.0.1:1234:1236|127.0.0.1:2234:2236:participant;;127.0.0.1:1238", qs.toString(), + "MultiAddress parse with only secureClientPort"); } @Test @@ -147,4 +169,93 @@ public void testDuplicate() { }); } + @Test + public void testClientAddrAndSecureClientAddr() throws ConfigException { + QuorumPeer.QuorumServer qs = new QuorumPeer.QuorumServer(0, "example.com:1234:1236:participant;1237;1238"); + assertNotNull(qs.clientAddr, "clientPort specified"); + assertNotNull(qs.secureClientAddr, "secureClientPort specified"); + + qs = new QuorumPeer.QuorumServer(0, "example.com:1234:1236:participant;;1238"); + assertNull(qs.clientAddr, "clientPort not specified"); + assertNotNull(qs.secureClientAddr, "secureClientPort specified"); + + qs = new QuorumPeer.QuorumServer(0, "example.com:1234:1236:participant;1237;"); + assertNotNull(qs.clientAddr, "clientPort specified"); + assertNull(qs.secureClientAddr, "secureClientPort not specified"); + + qs = new QuorumPeer.QuorumServer(0, "example.com:1234:1236:participant;1237"); + assertNotNull(qs.clientAddr, "clientPort specified"); + assertNull(qs.secureClientAddr, "secureClientPort not specified"); + + qs = new QuorumPeer.QuorumServer(0, "example.com:1234:1236:participant"); + assertNull(qs.clientAddr, "clientPort not specified"); + assertNull(qs.secureClientAddr, "secureClientPort not specified"); + + qs = new QuorumPeer.QuorumServer(0, "example.com:1234:1236:participant;;"); + assertNull(qs.clientAddr, "clientPort not specified"); + assertNull(qs.secureClientAddr, "secureClientPort not specified"); + } + + @Test + public void constructionUnderstandsIpv6LiteralsInSecureClientConfig() throws ConfigException { + QuorumServer qs = new QuorumServer(0, "[::1]:1234:1236:participant;;[::1]:1237"); + assertNull(qs.clientAddr); + assertEquals("[0:0:0:0:0:0:0:1]:1234:1236:participant;;[0:0:0:0:0:0:0:1]:1237", qs.toString()); + + qs = new QuorumServer(0, ipv4config + ":participant;[::1]:1236;[::2]:1237"); + assertEquals(ipv4config + ":participant;[0:0:0:0:0:0:0:1]:1236;[0:0:0:0:0:0:0:2]:1237", qs.toString()); + } + + @Test + public void invalidSecureClientConfigFailsToBeParsed() { + assertThrows(ConfigException.class, () -> new QuorumServer(0, ipv4config + ":participant;;[::1:1237")); + assertThrows(ConfigException.class, () -> new QuorumServer(0, ipv4config + ":participant;;1.2.3.4:abc")); + assertThrows(ConfigException.class, () -> new QuorumServer(0, ipv4config + ":participant;1236;1.2.3.4:1237:1238")); + } + + @Test + public void secureClientConfigRoundTripsThroughToString() throws ConfigException { + String[] configs = { + ipv4config + ":participant;127.0.0.1:1237", + ipv4config + ":participant;127.0.0.1:1237;127.0.0.1:1238", + ipv4config + ":observer;;127.0.0.1:1238", + ipv4config + ":participant;;[0:0:0:0:0:0:0:1]:1238"}; + for (String config : configs) { + QuorumServer qs = new QuorumServer(0, config); + assertEquals(config, qs.toString()); + assertEquals(qs, new QuorumServer(0, qs.toString())); + } + } + + @Test + public void secureClientAddressParticipatesInEquality() throws ConfigException { + QuorumServer tlsOnly = new QuorumServer(0, ipv4config + ":participant;;127.0.0.1:1238"); + assertNotEquals(new QuorumServer(0, ipv4config + ":participant"), tlsOnly); + assertNotEquals(new QuorumServer(0, ipv4config + ":participant;;127.0.0.1:1239"), tlsOnly); + assertNotEquals(new QuorumServer(0, ipv4config + ":participant;127.0.0.1:1238"), tlsOnly); + assertEquals(new QuorumServer(0, ipv4config + ":participant;;127.0.0.1:1238"), tlsOnly); + } + + @Test + public void toStringOmitsClientAddressesTakenFromStaticConfig() throws ConfigException { + QuorumServer qs = new QuorumServer(0, ipv4config + ":participant;127.0.0.1:1237;127.0.0.1:1238"); + qs.isClientAddrFromStatic = true; + assertEquals(ipv4config + ":participant;;127.0.0.1:1238", qs.toString()); + + qs.isClientAddrFromStatic = false; + qs.isSecureClientAddrFromStatic = true; + assertEquals(ipv4config + ":participant;127.0.0.1:1237", qs.toString()); + } + + @Test + public void testDuplicateSecureClientAddress() throws ConfigException { + QuorumServer server1 = new QuorumServer(1, "[2500::1:0]:1234:1236:participant;;[2500::1:0]:1238"); + QuorumServer sameSecure = new QuorumServer(2, "[2600::1:0]:1234:1236:participant;;[2500::1:0]:1238"); + QuorumServer secureAsClient = new QuorumServer(3, "[2600::1:0]:1234:1236:participant;[2500::1:0]:1238"); + QuorumServer distinct = new QuorumServer(4, "[2600::1:0]:1234:1236:participant;;[2600::1:0]:1238"); + assertThrows(KeeperException.BadArgumentsException.class, () -> server1.checkAddressDuplicate(sameSecure)); + assertThrows(KeeperException.BadArgumentsException.class, () -> server1.checkAddressDuplicate(secureAsClient)); + assertDoesNotThrow(() -> server1.checkAddressDuplicate(distinct)); + } + } diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigLegacyTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigLegacyTest.java index 498b4b6c4ec..fe6552ed3f5 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigLegacyTest.java +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigLegacyTest.java @@ -18,6 +18,8 @@ package org.apache.zookeeper.server.quorum; +import static org.apache.zookeeper.server.quorum.QuorumPeerMainTLSTest.getClientTLSConfigs; +import static org.apache.zookeeper.server.quorum.QuorumPeerMainTLSTest.getServerTLSConfigs; import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -25,22 +27,56 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.security.Security; import java.util.ArrayList; +import java.util.Map; import java.util.Properties; +import org.apache.commons.io.FileUtils; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.PortAssignment; import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.admin.ZooKeeperAdmin; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.common.X509KeyType; +import org.apache.zookeeper.common.X509TestContext; import org.apache.zookeeper.test.ClientBase; import org.apache.zookeeper.test.ReconfigTest; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; public class ReconfigLegacyTest extends QuorumPeerTestBase { private static final int SERVER_COUNT = 3; + private static File tempDir; + private static X509TestContext x509TestContext = null; + + @BeforeAll + public static void beforeAll() throws Exception { + Security.addProvider(new BouncyCastleProvider()); + tempDir = ClientBase.createEmptyTestDir(); + x509TestContext = X509TestContext.newBuilder() + .setTempDir(tempDir) + .setKeyStoreKeyType(X509KeyType.EC) + .setTrustStoreKeyType(X509KeyType.EC) + .build(); + } + + @AfterAll + public static void afterAll() { + Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME); + try { + FileUtils.deleteDirectory(tempDir); + } catch (IOException e) { + // ignore + } + } @BeforeEach public void setup() { @@ -60,12 +96,12 @@ public void testConfigFileBackwardCompatibility() throws Exception { final int[] clientPorts = new int[SERVER_COUNT]; StringBuilder sb = new StringBuilder(); String server; - ArrayList allServers = new ArrayList(); + ArrayList allServers = new ArrayList<>(); for (int i = 0; i < SERVER_COUNT; i++) { clientPorts[i] = PortAssignment.unique(); server = "server." + i + "=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() - + ":participant;localhost:" + clientPorts[i]; + + ":participant;localhost:" + clientPorts[i]; allServers.add(server); sb.append(server + "\n"); } @@ -144,21 +180,26 @@ public void testConfigFileBackwardCompatibility() throws Exception { * and new port added to dynamic file. * @throws Exception */ - @Test - public void testReconfigRemoveClientFromStatic() throws Exception { + @ParameterizedTest + @ValueSource(booleans = {true, false}) + public void testReconfigRemoveClientFromStatic(boolean isSecure) throws Exception { final int[] clientPorts = new int[SERVER_COUNT]; + final int[] secureClientPorts = new int[SERVER_COUNT]; + final int[] adminServerPorts = new int[SERVER_COUNT]; final int[] quorumPorts = new int[SERVER_COUNT]; final int[] electionPorts = new int[SERVER_COUNT]; final int changedServerId = 0; - final int newClientPort = PortAssignment.unique(); + final int newClientPortOrSecureClientPort = PortAssignment.unique(); StringBuilder sb = new StringBuilder(); - ArrayList allServers = new ArrayList(); - ArrayList newServers = new ArrayList(); + ArrayList allServers = new ArrayList<>(); + ArrayList newServers = new ArrayList<>(); for (int i = 0; i < SERVER_COUNT; i++) { clientPorts[i] = PortAssignment.unique(); + secureClientPorts[i] = PortAssignment.unique(); + adminServerPorts[i] = PortAssignment.unique(); quorumPorts[i] = PortAssignment.unique(); electionPorts[i] = PortAssignment.unique(); @@ -167,7 +208,12 @@ public void testReconfigRemoveClientFromStatic() throws Exception { sb.append(server + "\n"); if (i == changedServerId) { - newServers.add(server + ";0.0.0.0:" + newClientPort); + if (isSecure) { + newServers.add(server + ";;0.0.0.0:" + newClientPortOrSecureClientPort); + } else { + newServers.add(server + ";0.0.0.0:" + newClientPortOrSecureClientPort); + } + } else { newServers.add(server); } @@ -178,27 +224,50 @@ public void testReconfigRemoveClientFromStatic() throws Exception { ZooKeeper[] zk = new ZooKeeper[SERVER_COUNT]; ZooKeeperAdmin[] zkAdmin = new ZooKeeperAdmin[SERVER_COUNT]; + Map configMap = getServerTLSConfigs(x509TestContext); + StringBuilder configBuilder = new StringBuilder(); + for (Map.Entry entry : configMap.entrySet()) { + configBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("\n"); + } + // Start the servers with a static config file, without a dynamic config file. for (int i = 0; i < SERVER_COUNT; i++) { - mt[i] = new MainThread(i, clientPorts[i], quorumCfgSection, false); + if (isSecure) { + mt[i] = new MainThread(i, MainThread.UNSET_STATIC_CLIENTPORT, adminServerPorts[i], secureClientPorts[i], quorumCfgSection, configBuilder.toString(), null, false, null); + } else { + mt[i] = new MainThread(i, clientPorts[i], adminServerPorts[i], quorumCfgSection, null, null, false); + } mt[i].start(); } + + ZKClientConfig clientConfig; + if (isSecure) { + clientConfig = getClientTLSConfigs(x509TestContext); + } else { + clientConfig = null; + } + // Check that when a server starts from old style config, it should keep the client // port in static config file. for (int i = 0; i < SERVER_COUNT; i++) { + String cnxnString = "127.0.0.1:" + (isSecure ? secureClientPorts[i] : clientPorts[i]); assertTrue( - ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT), - "waiting for server " + i + " being up"); - zk[i] = ClientBase.createZKClient("127.0.0.1:" + clientPorts[i]); - zkAdmin[i] = new ZooKeeperAdmin("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this); + ClientBase.waitForServerUp(cnxnString, CONNECTION_TIMEOUT, isSecure, clientConfig), "waiting for server " + i + " being up"); + zk[i] = ClientBase.createZKClient(cnxnString, CONNECTION_TIMEOUT, CONNECTION_TIMEOUT, clientConfig); + zkAdmin[i] = new ZooKeeperAdmin(cnxnString, ClientBase.CONNECTION_TIMEOUT, this, clientConfig); zkAdmin[i].addAuthInfo("digest", "super:test".getBytes()); ReconfigTest.testServerHasConfig(zk[i], allServers, null); Properties cfg = readPropertiesFromFile(mt[i].confFile); assertTrue(cfg.containsKey("dynamicConfigFile")); - assertTrue(cfg.containsKey("clientPort")); + if (isSecure) { + assertTrue(cfg.containsKey("secureClientPort")); + } else { + assertTrue(cfg.containsKey("clientPort")); + } + } ReconfigTest.testNormalOperation(zk[0], zk[1]); @@ -215,10 +284,11 @@ public void testReconfigRemoveClientFromStatic() throws Exception { for (int i = 0; i < SERVER_COUNT; i++) { ReconfigTest.testServerHasConfig(zk[i], newServers, null); Properties staticCfg = readPropertiesFromFile(mt[i].confFile); + String configKey = isSecure ? "secureClientPort" : "clientPort"; if (i == changedServerId) { - assertFalse(staticCfg.containsKey("clientPort")); + assertFalse(staticCfg.containsKey(configKey)); } else { - assertTrue(staticCfg.containsKey("clientPort")); + assertTrue(staticCfg.containsKey(configKey)); } } @@ -255,7 +325,7 @@ public void testRestartZooKeeperServer() throws Exception { for (int i = 0; i < SERVER_COUNT; i++) { clientPorts[i] = PortAssignment.unique(); server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() - + ":participant;127.0.0.1:" + clientPorts[i]; + + ":participant;127.0.0.1:" + clientPorts[i]; sb.append(server + "\n"); } String currentQuorumCfgSection = sb.toString(); diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientBase.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientBase.java index 19e465ab3b6..c8cda86cfac 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientBase.java +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientBase.java @@ -40,6 +40,7 @@ import java.util.concurrent.TimeoutException; import javax.management.MBeanServerConnection; import javax.management.ObjectName; +import javax.net.ssl.SSLContext; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.PortAssignment; import org.apache.zookeeper.TestableZooKeeper; @@ -49,8 +50,11 @@ import org.apache.zookeeper.ZKTestCase; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.common.ClientX509Util; import org.apache.zookeeper.common.Time; import org.apache.zookeeper.common.X509Exception.SSLContextException; +import org.apache.zookeeper.common.X509Util; +import org.apache.zookeeper.common.ZKConfig; import org.apache.zookeeper.server.ServerCnxnFactory; import org.apache.zookeeper.server.ZKDatabase; import org.apache.zookeeper.server.ZooKeeperServer; @@ -124,6 +128,12 @@ public synchronized void process(WatchedEvent event) { public synchronized boolean isConnected() { return connected; } + + protected synchronized String connectionDescription() { + return String.format("connected(%s), syncConnected(%s), readOnlyConnected(%s)", + connected, syncConnected, readOnlyConnected); + } + public synchronized void waitForConnected(long timeout) throws InterruptedException, TimeoutException { long expire = Time.currentElapsedTime() + timeout; long left = timeout; @@ -132,8 +142,7 @@ public synchronized void waitForConnected(long timeout) throws InterruptedExcept left = expire - Time.currentElapsedTime(); } if (!connected) { - throw new TimeoutException("Failed to connect to ZooKeeper server."); - + throw new TimeoutException("Failed to connect to ZooKeeper server: " + connectionDescription()); } } public synchronized void waitForSyncConnected(long timeout) throws InterruptedException, TimeoutException { @@ -144,18 +153,22 @@ public synchronized void waitForSyncConnected(long timeout) throws InterruptedEx left = expire - Time.currentElapsedTime(); } if (!syncConnected) { - throw new TimeoutException("Failed to connect to read-write ZooKeeper server."); + throw new TimeoutException( + "Failed to connect to read-write ZooKeeper server: " + + connectionDescription()); } } public synchronized void waitForReadOnlyConnected(long timeout) throws InterruptedException, TimeoutException { - long expire = System.currentTimeMillis() + timeout; + long expire = Time.currentElapsedTime() + timeout; long left = timeout; while (!readOnlyConnected && left > 0) { wait(left); - left = expire - System.currentTimeMillis(); + left = expire - Time.currentElapsedTime(); } if (!readOnlyConnected) { - throw new TimeoutException("Failed to connect in read-only mode to ZooKeeper server."); + throw new TimeoutException( + "Failed to connect in read-only mode to ZooKeeper server: " + + connectionDescription()); } } public synchronized void waitForDisconnected(long timeout) throws InterruptedException, TimeoutException { @@ -166,8 +179,7 @@ public synchronized void waitForDisconnected(long timeout) throws InterruptedExc left = expire - Time.currentElapsedTime(); } if (connected) { - throw new TimeoutException("Did not disconnect"); - + throw new TimeoutException("Did not disconnect: " + connectionDescription()); } } @@ -234,7 +246,7 @@ public HostPort(String host, int port) { } public static List parseHostPortList(String hplist) { - ArrayList alist = new ArrayList(); + ArrayList alist = new ArrayList<>(); for (String hp : hplist.split(",")) { int idx = hp.lastIndexOf(':'); String host = hp.substring(0, idx); @@ -250,16 +262,27 @@ public static List parseHostPortList(String hplist) { } public static boolean waitForServerUp(String hp, long timeout) { - return waitForServerUp(hp, timeout, false); + return waitForServerUp(hp, timeout, false, null); } public static boolean waitForServerUp(String hp, long timeout, boolean secure) { + return waitForServerUp(hp, timeout, secure, null); + } + + public static boolean waitForServerUp(String hp, long timeout, boolean secure, ZKConfig zkConfig) { long start = Time.currentElapsedTime(); while (true) { try { // if there are multiple hostports, just take the first one HostPort hpobj = parseHostPortList(hp).get(0); - String result = send4LetterWord(hpobj.host, hpobj.port, "stat", secure); + SSLContext sslContext = null; + String result; + if (zkConfig != null) { + try (X509Util x509Util = new ClientX509Util()) { + sslContext = x509Util.createSSLContext(zkConfig); + } + } + result = send4LetterWord(hpobj.host, hpobj.port, "stat", secure, 5000, sslContext); if (result.startsWith("Zookeeper version:") && !result.contains("READ-ONLY")) { return true; } @@ -282,6 +305,7 @@ public static boolean waitForServerUp(String hp, long timeout, boolean secure) { // ignore } } + LOG.error("server failed to come up: {}", hp); return false; } @@ -339,7 +363,7 @@ static void verifyThreadTerminated(Thread thread, long millis) throws Interrupte thread.join(millis); if (thread.isAlive()) { LOG.error("Thread {} : {}", thread.getName(), Arrays.toString(thread.getStackTrace())); - assertFalse(true, "thread " + thread.getName() + " still alive after join"); + fail("thread " + thread.getName() + " still alive after join"); } } @@ -387,22 +411,28 @@ private static int getPort(String hostPort) { return Integer.parseInt(portstr); } + public static void startServerInstance(File dataDir, + ServerCnxnFactory factory, + String hostPort, + int serverId) throws IOException, InterruptedException { + startServerInstance(dataDir, factory, hostPort, serverId, null); + } + /** * Starting the given server instance */ public static void startServerInstance( - File dataDir, - ServerCnxnFactory factory, - String hostPort, - int serverId) throws IOException, InterruptedException { + File dataDir, + ServerCnxnFactory factory, + String hostPort, + int serverId, ZKConfig zkConfig) throws IOException, InterruptedException { final int port = getPort(hostPort); LOG.info("STARTING server instance 127.0.0.1:{}", port); ZooKeeperServer zks = new ZooKeeperServer(dataDir, dataDir, 3000); zks.setCreateSessionTrackerServerId(serverId); factory.startup(zks); assertTrue( - ClientBase.waitForServerUp("127.0.0.1:" + port, CONNECTION_TIMEOUT, factory.isSecure()), - "waiting for server up"); + ClientBase.waitForServerUp("127.0.0.1:" + port, CONNECTION_TIMEOUT, factory.isSecure(), zkConfig), "waiting for server up"); } /** @@ -420,9 +450,9 @@ public static void startServerInstance( * for more information. */ public static ServerCnxnFactory createNewServerInstance( - ServerCnxnFactory factory, - String hostPort, - int maxCnxns) throws IOException, InterruptedException { + ServerCnxnFactory factory, + String hostPort, + int maxCnxns) throws IOException, InterruptedException { final int port = getPort(hostPort); LOG.info("CREATING server instance 127.0.0.1:{}", port); if (factory == null) { @@ -451,8 +481,7 @@ static void shutdownServerInstance(ServerCnxnFactory factory, String hostPort) { final int PORT = getPort(hostPort); assertTrue( - ClientBase.waitForServerDown("127.0.0.1:" + PORT, CONNECTION_TIMEOUT, factory.isSecure()), - "waiting for server down"); + ClientBase.waitForServerDown("127.0.0.1:" + PORT, CONNECTION_TIMEOUT, factory.isSecure()), "waiting for server down"); } } @@ -469,7 +498,7 @@ public static void setupTestEnv() { } protected void setUpAll() throws Exception { - allClients = new LinkedList(); + allClients = new LinkedList<>(); allClientsSetup = true; } diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ReconfigTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ReconfigTest.java index 65026a6b9ba..4664dff413f 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ReconfigTest.java +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ReconfigTest.java @@ -1217,6 +1217,7 @@ private void assertRemotePeerMXBeanAttributes(QuorumServer qs, String beanName) private static class ServerConfigLine { private final int serverId; private Integer clientPort; + private Integer secureClientPort; // hostName -> private final Map> quorumPorts = new HashMap<>(); @@ -1229,7 +1230,7 @@ private ServerConfigLine(String configLine) { serverId = parseInt(parts[0].split("\\.")[1]); String[] serverConfig = parts[1].split(";"); String[] serverAddresses = serverConfig[0].split("\\|"); - if (serverConfig.length > 1) { + if (serverConfig.length > 1 && !serverConfig[1].isEmpty()) { String[] clientParts = serverConfig[1].split(":"); if (clientParts.length > 1) { clientPort = parseInt(clientParts[1]); @@ -1238,6 +1239,15 @@ private ServerConfigLine(String configLine) { } } + if (serverConfig.length > 2 && !serverConfig[2].isEmpty()) { + String[] secureClientParts = serverConfig[2].split(":"); + if (secureClientParts.length > 1) { + secureClientPort = parseInt(secureClientParts[1]); + } else { + secureClientPort = parseInt(secureClientParts[0]); + } + } + for (String addr : serverAddresses) { // addr like: 127.0.0.1:11230:11229:participant or [0:0:0:0:0:0:0:1]:11346:11347 String serverHost; @@ -1268,13 +1278,14 @@ public boolean equals(Object o) { ServerConfigLine that = (ServerConfigLine) o; return serverId == that.serverId && Objects.equals(clientPort, that.clientPort) + && Objects.equals(secureClientPort, that.secureClientPort) && quorumPorts.equals(that.quorumPorts) && electionPorts.equals(that.electionPorts); } @Override public int hashCode() { - return Objects.hash(serverId, clientPort, quorumPorts, electionPorts); + return Objects.hash(serverId, clientPort, secureClientPort, quorumPorts, electionPorts); } }