com.datastax.driver.core.JdkSSLOptions Java Examples

The following examples show how to use com.datastax.driver.core.JdkSSLOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CqlCount.java    From cassandra-count with Apache License 2.0 6 votes vote down vote up
private SSLOptions createSSLOptions()
     throws KeyStoreException, FileNotFoundException, IOException, NoSuchAlgorithmException,
            KeyManagementException, CertificateException, UnrecoverableKeyException {
     TrustManagerFactory tmf = null;
     KeyStore tks = KeyStore.getInstance("JKS");
     tks.load((InputStream) new FileInputStream(new File(truststorePath)),
truststorePwd.toCharArray());
     tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
     tmf.init(tks);

     KeyManagerFactory kmf = null;
     if (null != keystorePath) {
         KeyStore kks = KeyStore.getInstance("JKS");
         kks.load((InputStream) new FileInputStream(new File(keystorePath)),
    keystorePwd.toCharArray());
         kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
         kmf.init(kks, keystorePwd.toCharArray());
     }

     SSLContext sslContext = SSLContext.getInstance("TLS");
     sslContext.init(kmf != null? kmf.getKeyManagers() : null,
                     tmf != null ? tmf.getTrustManagers() : null,
                     new SecureRandom());

     return JdkSSLOptions.builder().withSSLContext(sslContext).build(); //SSLOptions.DEFAULT_SSL_CIPHER_SUITES);
 }
 
Example #2
Source File: NamespaceOverrideMapper.java    From hawkular-metrics with Apache License 2.0 6 votes vote down vote up
private Session createSession() {
    Cluster.Builder clusterBuilder = new Cluster.Builder();

    String nodes = System.getProperty("hawkular.metrics.cassandra.nodes", "hawkular-cassandra");
    Arrays.stream(nodes.split(",")).forEach(clusterBuilder::addContactPoint);

    if (System.getProperty("hawkular.metrics.cassandra.use-ssl") != null && !System.getProperty("hawkular.metrics.cassandra.use-ssl").equals("false")) {
        SSLOptions sslOptions = null;
        try {
            String[] defaultCipherSuites = {"TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_256_CBC_SHA"};
            sslOptions = JdkSSLOptions.builder().withSSLContext(SSLContext.getDefault())
                    .withCipherSuites(defaultCipherSuites).build();
            clusterBuilder.withSSL(sslOptions);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("SSL support is required but is not available in the JVM.", e);
        }
    }

    Cluster cluster = clusterBuilder.build();
    cluster.init();

    Session session = cluster.connect();

    return session;
}
 
Example #3
Source File: AbstractCassandraProcessor.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Uses a Cluster.Builder to create a Cassandra cluster reference using the given parameters
 *
 * @param contactPoints The contact points (hostname:port list of Cassandra nodes)
 * @param sslContext    The SSL context (used for secure connections)
 * @param username      The username for connection authentication
 * @param password      The password for connection authentication
 * @param compressionType Enable compression at transport-level requests and responses.
 * @return A reference to the Cluster object associated with the given Cassandra configuration
 */
protected Cluster createCluster(List<InetSocketAddress> contactPoints, SSLContext sslContext,
                                String username, String password, String compressionType) {
    Cluster.Builder builder = Cluster.builder().addContactPointsWithPorts(contactPoints);
    if (sslContext != null) {
        JdkSSLOptions sslOptions = JdkSSLOptions.builder()
                .withSSLContext(sslContext)
                .build();
        builder = builder.withSSL(sslOptions);
        if(ProtocolOptions.Compression.SNAPPY.equals(compressionType)) {
            builder = builder.withCompression(ProtocolOptions.Compression.SNAPPY);
        } else if(ProtocolOptions.Compression.LZ4.equals(compressionType)) {
            builder = builder.withCompression(ProtocolOptions.Compression.LZ4);
        }
    }
    if (username != null && password != null) {
        builder = builder.withCredentials(username, password);
    }
    return builder.build();
}
 
Example #4
Source File: AbstractCassandraProcessor.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Uses a Cluster.Builder to create a Cassandra cluster reference using the given parameters
 *
 * @param contactPoints The contact points (hostname:port list of Cassandra nodes)
 * @param sslContext    The SSL context (used for secure connections)
 * @param username      The username for connection authentication
 * @param password      The password for connection authentication
 * @return A reference to the Cluster object associated with the given Cassandra configuration
 */
protected Cluster createCluster(List<InetSocketAddress> contactPoints, SSLContext sslContext,
                                String username, String password) {
    Cluster.Builder builder = Cluster.builder().addContactPointsWithPorts(contactPoints);
    if (sslContext != null) {
        JdkSSLOptions sslOptions = JdkSSLOptions.builder()
                .withSSLContext(sslContext)
                .build();
        builder = builder.withSSL(sslOptions);
    }
    if (username != null && password != null) {
        builder = builder.withCredentials(username, password);
    }
    return builder.build();
}
 
Example #5
Source File: JDKSSLOptionsFactoryTest.java    From dropwizard-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void returnsInstanceOfJdkSSLOptions() throws Exception {
    final JDKSSLOptionsFactory factory = new JDKSSLOptionsFactory();

    final SSLOptions options = factory.build();

    assertThat(options).isInstanceOf(JdkSSLOptions.class);
}
 
Example #6
Source File: Installer.java    From hawkular-metrics with Apache License 2.0 5 votes vote down vote up
private Session createSession() {
    Cluster.Builder clusterBuilder = new Cluster.Builder();
    clusterBuilder.addContactPoints(cassandraNodes.toArray(new String[] {}));
    if (useSSL) {
        SSLOptions sslOptions = null;
        try {
            String[] defaultCipherSuites = {"TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_256_CBC_SHA"};
            sslOptions = JdkSSLOptions.builder().withSSLContext(SSLContext.getDefault())
                    .withCipherSuites(defaultCipherSuites).build();
            clusterBuilder.withSSL(sslOptions);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("SSL support is required but is not available in the JVM.", e);
        }
    }

    clusterBuilder.withoutJMXReporting();

    Cluster cluster = clusterBuilder.build();
    cluster.init();
    Session createdSession = null;
    try {
        createdSession = cluster.connect("system");
        return createdSession;
    } finally {
        if (createdSession == null) {
            cluster.close();
        }
    }
}
 
Example #7
Source File: CassandraSessionProvider.java    From nifi with Apache License 2.0 5 votes vote down vote up
private Cluster createCluster(List<InetSocketAddress> contactPoints, SSLContext sslContext,
                              String username, String password, String compressionType,
                              Optional<Integer> readTimeoutMillisOptional, Optional<Integer> connectTimeoutMillisOptional) {
    Cluster.Builder builder = Cluster.builder().addContactPointsWithPorts(contactPoints);

    if (sslContext != null) {
        JdkSSLOptions sslOptions = JdkSSLOptions.builder()
                .withSSLContext(sslContext)
                .build();
        builder = builder.withSSL(sslOptions);
    }

    if (username != null && password != null) {
        builder = builder.withCredentials(username, password);
    }

    if(ProtocolOptions.Compression.SNAPPY.equals(compressionType)) {
        builder = builder.withCompression(ProtocolOptions.Compression.SNAPPY);
    } else if(ProtocolOptions.Compression.LZ4.equals(compressionType)) {
        builder = builder.withCompression(ProtocolOptions.Compression.LZ4);
    }

    SocketOptions socketOptions = new SocketOptions();
    readTimeoutMillisOptional.ifPresent(socketOptions::setReadTimeoutMillis);
    connectTimeoutMillisOptional.ifPresent(socketOptions::setConnectTimeoutMillis);

    builder.withSocketOptions(socketOptions);

    return builder.build();
}
 
Example #8
Source File: JDKSSLOptionsFactory.java    From dropwizard-cassandra with Apache License 2.0 4 votes vote down vote up
@Override
public SSLOptions build() {
    return JdkSSLOptions.builder().build();
}