use of org.apache.zookeeper.common.X509Util in project hive by apache.
the class MiniZooKeeperCluster method createServerCnxnFactory.
private ServerCnxnFactory createServerCnxnFactory(int currentClientPort) throws IOException {
ServerCnxnFactory serverCnxnFactory = null;
if (sslEnabled) {
System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, "org.apache.zookeeper.server.NettyServerCnxnFactory");
String dataFileDir = !System.getProperty("test.data.files", "").isEmpty() ? System.getProperty("test.data.files") : configuration.get("test.data.files").replace('\\', '/').replace("c:", "");
X509Util x509Util = new ClientX509Util();
System.setProperty(x509Util.getSslKeystoreLocationProperty(), dataFileDir + File.separator + LOCALHOST_KEY_STORE_NAME);
System.setProperty(x509Util.getSslKeystorePasswdProperty(), KEY_STORE_TRUST_STORE_PASSWORD);
System.setProperty(x509Util.getSslTruststoreLocationProperty(), dataFileDir + File.separator + TRUST_STORE_NAME);
System.setProperty(x509Util.getSslTruststorePasswdProperty(), KEY_STORE_TRUST_STORE_PASSWORD);
serverCnxnFactory = ServerCnxnFactory.createFactory();
serverCnxnFactory.configure(new InetSocketAddress(currentClientPort), configuration.getInt(HConstants.ZOOKEEPER_MAX_CLIENT_CNXNS, HConstants.DEFAULT_ZOOKEPER_MAX_CLIENT_CNXNS), true);
} else {
serverCnxnFactory = ServerCnxnFactory.createFactory();
serverCnxnFactory.configure(new InetSocketAddress(currentClientPort), configuration.getInt(HConstants.ZOOKEEPER_MAX_CLIENT_CNXNS, HConstants.DEFAULT_ZOOKEPER_MAX_CLIENT_CNXNS));
}
return serverCnxnFactory;
}
use of org.apache.zookeeper.common.X509Util in project zookeeper by apache.
the class FourLetterWordMain method send4LetterWord.
/**
* 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
* @return server response
* @throws java.io.IOException
* @throws SSLContextException
*/
public static String send4LetterWord(String host, int port, String cmd, boolean secure, int timeout) throws IOException, SSLContextException {
LOG.info("connecting to {} {}", host, port);
Socket sock;
InetSocketAddress hostaddress = host != null ? new InetSocketAddress(host, port) : 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;
}
} else {
sock = new Socket();
sock.connect(hostaddress, timeout);
}
sock.setSoTimeout(timeout);
BufferedReader reader = null;
try {
OutputStream outstream = sock.getOutputStream();
outstream.write(cmd.getBytes(UTF_8));
outstream.flush();
// this replicates NC - close the output stream before reading
if (!secure) {
// SSL prohibits unilateral half-close
sock.shutdownOutput();
}
reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
return sb.toString();
} catch (SocketTimeoutException e) {
throw new IOException("Exception while executing four letter word: " + cmd, e);
} finally {
sock.close();
if (reader != null) {
reader.close();
}
}
}
Aggregations