| 1 | ## SSL |
| 2 | |
| 3 | You can secure traffic between the driver and Cassandra with SSL. There |
| 4 | are two aspects to that: |
| 5 | |
| 6 | * **client-to-node encryption**, where the traffic is encrypted, and the |
| 7 | client verifies the identity of the Cassandra nodes it connects to; |
| 8 | * optionally, **client certificate authentication**, where Cassandra |
| 9 | nodes also verify the identity of the client. |
| 10 | |
| 11 | This section describes the driver-side configuration; it assumes that |
| 12 | you've already configured SSL in Cassandra: |
| 13 | |
| 14 | * [the Cassandra documentation](http://docs.datastax.com/en/cassandra/2.0/cassandra/security/secureSSLClientToNode_t.html) |
| 15 | covers a basic approach with self-signed certificates, which is fine |
| 16 | for development and tests. |
| 17 | * [this blog post](http://thelastpickle.com/blog/2015/09/30/hardening-cassandra-step-by-step-part-1-server-to-server.html) |
| 18 | details a more advanced solution based on a Certificate Authority |
| 19 | (CA). |
| 20 | |
| 21 | ### Preparing the certificates |
| 22 | |
| 23 | #### Client truststore |
| 24 | |
| 25 | This is required for client-to-node encryption. |
| 26 | |
| 27 | If you're using self-signed certificates, you need to export the public |
| 28 | part of each node's certificate from that node's keystore: |
| 29 | |
| 30 | ``` |
| 31 | keytool -export -alias cassandra -file cassandranode0.cer -keystore .keystore |
| 32 | ``` |
| 33 | |
| 34 | Then add all public certificates to the client truststore: |
| 35 | |
| 36 | ``` |
| 37 | keytool -import -v -trustcacerts -alias <cassandra_node0> -file cassandranode0.cer -keystore client.truststore |
| 38 | keytool -import -v -trustcacerts -alias <cassandra_node1> -file cassandranode1.cer -keystore client.truststore |
| 39 | ... |
| 40 | ``` |
| 41 | |
| 42 | If you're using a Certificate Authority, the client truststore only |
| 43 | needs to contain the CA's certificate: |
| 44 | |
| 45 | ``` |
| 46 | keytool -import -v -trustcacerts -alias CARoot -file ca.cer -keystore client.truststore |
| 47 | ``` |
| 48 | |
| 49 | #### Client keystore |
| 50 | |
| 51 | If you also intend to use client certificate authentication, generate |
| 52 | the public and private key pair for the client: |
| 53 | |
| 54 | ``` |
| 55 | keytool -genkey -keyalg RSA -alias client -keystore client.keystore |
| 56 | ``` |
| 57 | |
| 58 | If you're using self-signed certificates, extract the public part of the |
| 59 | client certificate, and import it in the truststore of each Cassandra |
| 60 | node: |
| 61 | |
| 62 | ``` |
| 63 | keytool -export -alias client -file client.cer -keystore client.keystore |
| 64 | keytool -import -v -trustcacerts -alias client -file client.cer -keystore server.truststore |
| 65 | ``` |
| 66 | |
| 67 | If you're using a CA, sign the client certificate with it (see the blog |
| 68 | post linked at the top of this page). Then the nodes' truststores only |
| 69 | need to contain the CA's certificate (which should already be the case |
| 70 | if you've followed the steps for inter-node encryption). |
| 71 | |
| 72 | ### Driver configuration |
| 73 | |
| 74 | #### Property-based |
| 75 | |
| 76 | `withSSL()` gives you a basic JSSE configuration: |
| 77 | |
| 78 | ```java |
| 79 | Cluster cluster = Cluster.builder() |
| 80 | .addContactPoint("127.0.0.1") |
| 81 | .withSSL() |
| 82 | .build(); |
| 83 | ``` |
| 84 | |
| 85 | You can then use |
| 86 | [JSSE system properties](http://docs.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#Customization) |
| 87 | for specific details, like keystore locations and passwords: |
| 88 | |
| 89 | ``` |
| 90 | -Djavax.net.ssl.trustStore=/path/to/client.truststore |
| 91 | -Djavax.net.ssl.trustStorePassword=password123 |
| 92 | # If you're using client authentication: |
| 93 | -Djavax.net.ssl.keyStore=/path/to/client.keystore |
| 94 | -Djavax.net.ssl.keyStorePassword=password123 |
| 95 | ``` |
| 96 | |
| 97 | #### Programmatic |
| 98 | |
| 99 | If you need more control than what system properties allow, you can |
| 100 | configure SSL programmatically by creating an [SSLOptions] instance: |
| 101 | |
| 102 | ```java |
| 103 | SSLContext sslContext = ... // create and configure SSL context |
| 104 | String[] cipherSuites = ... |
| 105 | |
| 106 | SSLOptions sslOptions = new SSLOptions(sslContext, cipherSuites); |
| 107 | |
| 108 | Cluster cluster = Cluster.builder() |
| 109 | .addContactPoint("127.0.0.1") |
| 110 | .withSSL(sslOptions) |
| 111 | .build(); |
| 112 | ``` |
| 113 | |
| 114 | A known limitation of the current API is that you can't customize the |
| 115 | `SSLEngine`, which prevents advanced features like hostname |
| 116 | verification. This will be fixed in 3.0 (see |
| 117 | [JAVA-841](https://datastax-oss.atlassian.net/browse/JAVA-841)), in the |
| 118 | meantime see below for a workaround. |
| 119 | |
| 120 | |
| 121 | ### Bypassing `SSLOptions` |
| 122 | |
| 123 | For advanced use cases, it's possible to bypass `SSLOptions` entirely: |
| 124 | since the driver provides a hook into the Netty pipeline (by way of |
| 125 | [NettyOptions]), you can add the SSL handler yourself: |
| 126 | |
| 127 | ```java |
| 128 | import io.netty.handler.ssl.SslHandler; |
| 129 | import javax.net.ssl.SSLContext; |
| 130 | import javax.net.ssl.SSLEngine; |
| 131 | |
| 132 | // Base implementation: each time a connection is established, create an SSL handler |
| 133 | // and add it to the pipeline: |
| 134 | public abstract class SslNettyOptions extends NettyOptions { |
| 135 | @Override |
| 136 | public void afterChannelInitialized(SocketChannel channel) throws Exception { |
| 137 | channel.pipeline().addFirst("ssl", createSslHandler(channel)); |
| 138 | } |
| 139 | |
| 140 | protected abstract SslHandler createSslHandler(SocketChannel channel); |
| 141 | } |
| 142 | |
| 143 | // Concrete implementation based on JSSE |
| 144 | public class MyNettyOptions extends SslNettyOptions { |
| 145 | |
| 146 | private final SSLContext sslContext = createContext(); |
| 147 | |
| 148 | protected SslHandler createSslHandler(SocketChannel channel) { |
| 149 | return new SslHandler(createEngine(sslContext, channel)); |
| 150 | } |
| 151 | |
| 152 | private static SSLContext createContext() { |
| 153 | ... // create and configure context |
| 154 | } |
| 155 | |
| 156 | private static SSLEngine createEngine(SSLContext sslContext, SocketChannel channel) { |
| 157 | SSLEngine engine = sslContext.createSSLEngine(); |
| 158 | engine.setUseClientMode(true); |
| 159 | ... // any extra configuration |
| 160 | return engine; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | cluster = Cluster.builder() |
| 165 | .addContactPoint("127.0.0.1") |
| 166 | .withNettyOptions(new MyNettyOptions()) |
| 167 | .build(); |
| 168 | ``` |
| 169 | |
| 170 | This could be used for the following use cases: |
| 171 | |
| 172 | * fine control over JSSE configuration, for example tuning the SSL |
| 173 | engine for hostname verification; |
| 174 | * bypassing JSSE altogether in favor of another SSL implementation: |
| 175 | recent Netty versions support |
| 176 | [OpenSSL](http://netty.io/wiki/forked-tomcat-native.html) |
| 177 | for improved performance. |
| 178 | |
| 179 | [SSLOptions]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/SSLOptions.html |
| 180 | [NettyOptions]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/NettyOptions.html |