Search in sources :

Example 1 with X509Util

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;
}
Also used : ClientX509Util(org.apache.zookeeper.common.ClientX509Util) InetSocketAddress(java.net.InetSocketAddress) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) X509Util(org.apache.zookeeper.common.X509Util) ClientX509Util(org.apache.zookeeper.common.ClientX509Util)

Example 2 with X509Util

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();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) X509Util(org.apache.zookeeper.common.X509Util) ClientX509Util(org.apache.zookeeper.common.ClientX509Util) ClientX509Util(org.apache.zookeeper.common.ClientX509Util) SocketTimeoutException(java.net.SocketTimeoutException) BufferedReader(java.io.BufferedReader) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)2 ClientX509Util (org.apache.zookeeper.common.ClientX509Util)2 X509Util (org.apache.zookeeper.common.X509Util)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 Socket (java.net.Socket)1 SocketTimeoutException (java.net.SocketTimeoutException)1 SSLContext (javax.net.ssl.SSLContext)1 SSLSocket (javax.net.ssl.SSLSocket)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)1