Skip to content

Commit

Permalink
ZOOKEEPER-4819 Fix can't seek for writable tls server if connected to…
Browse files Browse the repository at this point in the history
… readonly server

ClientCnxn::pingRwServer uses raw socket to issue "isro" 4lw command.
This results in unsuccessful handshake to tls server. Use SSLSocket
when zookeeper.client.secure is set to true.
  • Loading branch information
luoxiner committed Oct 10, 2024
1 parent 1880fc0 commit d60b2f1
Showing 1 changed file with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicReference;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.security.auth.login.LoginException;
import javax.security.sasl.SaslException;
import org.apache.jute.BinaryInputArchive;
Expand Down Expand Up @@ -70,7 +73,10 @@
import org.apache.zookeeper.client.HostProvider;
import org.apache.zookeeper.client.ZKClientConfig;
import org.apache.zookeeper.client.ZooKeeperSaslClient;
import org.apache.zookeeper.common.ClientX509Util;
import org.apache.zookeeper.common.Time;
import org.apache.zookeeper.common.X509Exception;
import org.apache.zookeeper.common.X509Util;
import org.apache.zookeeper.proto.AuthPacket;
import org.apache.zookeeper.proto.ConnectRequest;
import org.apache.zookeeper.proto.Create2Response;
Expand Down Expand Up @@ -1307,18 +1313,15 @@ private void pingRwServer() throws RWServerFoundException {
Socket sock = null;
BufferedReader br = null;
try {
sock = new Socket(addr.getHostString(), addr.getPort());
sock.setSoLinger(false, -1);
sock.setSoTimeout(1000);
sock.setTcpNoDelay(true);
sock = newSocket(addr);
sock.getOutputStream().write("isro".getBytes());
sock.getOutputStream().flush();
sock.shutdownOutput();
br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
result = br.readLine();
} catch (ConnectException e) {
// ignore, this just means server is not up
} catch (IOException e) {
} catch (IOException | X509Exception.SSLContextException e) {
// some unexpected error, warn about it
LOG.warn("Exception while seeking for r/w server.", e);
} finally {
Expand Down Expand Up @@ -1348,6 +1351,28 @@ private void pingRwServer() throws RWServerFoundException {
}
}

private Socket newSocket(InetSocketAddress addr) throws IOException, X509Exception.SSLContextException {
boolean useSecure = clientConfig.getBoolean(ZKClientConfig.SECURE_CLIENT);
Socket sock;
if (useSecure) {
try (X509Util util = new ClientX509Util()) {
SSLContext sslContext = util.createSSLContext(clientConfig);
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
SSLSocket sslSock = (SSLSocket) socketFactory.createSocket();
sslSock.connect(addr);
sslSock.startHandshake();
sock = sslSock;
}
} else {
sock = new Socket(addr.getHostString(), addr.getPort());
}

sock.setSoLinger(false, -1);
sock.setSoTimeout(1000);
sock.setTcpNoDelay(true);
return sock;
}

private void cleanup() {
clientCnxnSocket.cleanup();
synchronized (pendingQueue) {
Expand Down

0 comments on commit d60b2f1

Please sign in to comment.