Search in sources :

Example 1 with TransportFactoryInitException

use of com.nextdoor.bender.ipc.TransportFactoryInitException in project bender by Nextdoor.

the class AbstractHttpTransportFactory method getSSLContext.

/**
 * There isn't an easy way in java to trust non-self signed certs. Just allow all until java
 * KeyStore functionality is added to Bender.
 *
 * @return a context that trusts all SSL certs
 */
private SSLContext getSSLContext() {
    /*
     * Create SSLContext and TrustManager that will trust all SSL certs.
     *
     * Copy pasta from http://stackoverflow.com/a/4837230
     */
    TrustManager tm = new X509TrustManager() {

        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };
    SSLContext ctx;
    try {
        ctx = SSLContext.getInstance("TLS");
    } catch (NoSuchAlgorithmException e) {
        throw new TransportFactoryInitException("JVM does not have proper libraries for TSL");
    }
    try {
        ctx.init(null, new TrustManager[] { tm }, new java.security.SecureRandom());
    } catch (KeyManagementException e) {
        throw new TransportFactoryInitException("Unable to init SSLContext with TrustManager", e);
    }
    return ctx;
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) TransportFactoryInitException(com.nextdoor.bender.ipc.TransportFactoryInitException) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) X509Certificate(java.security.cert.X509Certificate) KeyManagementException(java.security.KeyManagementException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 2 with TransportFactoryInitException

use of com.nextdoor.bender.ipc.TransportFactoryInitException in project bender by Nextdoor.

the class TcpTransportFactory method newInstance.

@Override
public Transport newInstance() throws TransportFactoryInitException {
    try {
        Socket socket;
        if (config.getUseSSL()) {
            socket = SSLSocketFactory.getDefault().createSocket(config.getHostname(), config.getPort());
        } else {
            socket = new Socket(config.getHostname(), config.getPort());
        }
        socket.setReuseAddress(true);
        Sink sink = Okio.sink(socket);
        sink.timeout().timeout(config.getTimeout(), TimeUnit.MILLISECONDS);
        return new TcpTransport(sink, config.getRetryCount(), config.getRetryDelay());
    } catch (IOException ex) {
        throw new TransportFactoryInitException("Error while creating tcp transport", ex);
    }
}
Also used : Sink(okio.Sink) TransportFactoryInitException(com.nextdoor.bender.ipc.TransportFactoryInitException) IOException(java.io.IOException) Socket(java.net.Socket)

Aggregations

TransportFactoryInitException (com.nextdoor.bender.ipc.TransportFactoryInitException)2 IOException (java.io.IOException)1 Socket (java.net.Socket)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 X509Certificate (java.security.cert.X509Certificate)1 SSLContext (javax.net.ssl.SSLContext)1 TrustManager (javax.net.ssl.TrustManager)1 X509TrustManager (javax.net.ssl.X509TrustManager)1 Sink (okio.Sink)1