| 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 | The base class to configure SSL is [SSLOptions]. It's very generic, but |
| 75 | you don't necessarily need to deal with it directly: the default |
| 76 | instance, or the provided subclasses, might be enough for your needs. |
| 77 | |
| 78 | #### JSSE, Property-based |
| 79 | |
| 80 | `withSSL()` gives you a basic JSSE configuration: |
| 81 | |
| 82 | ```java |
| 83 | Cluster cluster = Cluster.builder() |
| 84 | .addContactPoint("127.0.0.1") |
| 85 | .withSSL() |
| 86 | .build(); |
| 87 | ``` |
| 88 | |
| 89 | You can then use |
| 90 | [JSSE system properties](http://docs.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#Customization) |
| 91 | for specific details, like keystore locations and passwords: |
| 92 | |
| 93 | ``` |
| 94 | -Djavax.net.ssl.trustStore=/path/to/client.truststore |
| 95 | -Djavax.net.ssl.trustStorePassword=password123 |
| 96 | # If you're using client authentication: |
| 97 | -Djavax.net.ssl.keyStore=/path/to/client.keystore |
| 98 | -Djavax.net.ssl.keyStorePassword=password123 |
| 99 | ``` |
| 100 | |
| 101 | #### JSSE, programmatic |
| 102 | |
| 103 | If you need more control than what system properties allow, you can |
| 104 | configure SSL programmatically with [JdkSSLOptions]: |
| 105 | |
| 106 | ```java |
| 107 | SSLContext sslContext = ... // create and configure SSL context |
| 108 | |
| 109 | JdkSSLOptions sslOptions = JdkSSLOptions.builder() |
| 110 | .withSSLContext(context) |
| 111 | .build(); |
| 112 | |
| 113 | Cluster cluster = Cluster.builder() |
| 114 | .addContactPoint("127.0.0.1") |
| 115 | .withSSL(sslOptions) |
| 116 | .build(); |
| 117 | ``` |
| 118 | |
| 119 | Note that you can also extend the class and override |
| 120 | [newSSLEngine(SocketChannel)][newSSLEngine] if you need specific |
| 121 | configuration on the `SSLEngine` (for example hostname verification). |
| 122 | |
| 123 | [newSSLEngine]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/JdkSSLOptions.html#newSSLEngine-io.netty.channel.socket.SocketChannel- |
| 124 | |
| 125 | #### Netty |
| 126 | |
| 127 | [NettySSLOptions] allows you to use Netty's `SslContext` instead of |
| 128 | the JDK directly. The advantage is that Netty can use OpenSSL directly, |
| 129 | which provides better performance and generates less garbage. A disadvantage of |
| 130 | using the OpenSSL provider is that it requires platform-specific dependencies, |
| 131 | unlike the JDK provider. |
| 132 | |
| 133 | |
| 134 | ##### Converting your client certificates for OpenSSL |
| 135 | |
| 136 | OpenSSL doesn't use keystores, so if you use client authentication and |
| 137 | generated your certificates with keytool, you need to convert them. |
| 138 | |
| 139 | * use this command to extract the public certificate chain: |
| 140 | |
| 141 | ``` |
| 142 | keytool -export -keystore client.keystore -alias client -rfc -file client.crt |
| 143 | ``` |
| 144 | * follow |
| 145 | [this tutorial](http://www.herongyang.com/crypto/Migrating_Keys_keytool_to_OpenSSL_3.html) |
| 146 | to extract your client's private key from `client.keystore` to a text |
| 147 | file `client.key` in PEM format. |
| 148 | |
| 149 | ##### Updating your dependencies |
| 150 | |
| 151 | Netty-tcnative provides the native integration with OpenSSL. Follow |
| 152 | [these instructions](http://netty.io/wiki/forked-tomcat-native.html) to |
| 153 | add it to your dependencies. |
| 154 | |
| 155 | There are known runtime incompatibilities between newer versions of |
| 156 | netty-tcnative and the version of netty that the driver uses. For best |
| 157 | results, use version 1.1.33.Fork18. |
| 158 | |
| 159 | Using netty-tcnative requires JDK 1.7 or above and requires the presence of |
| 160 | OpenSSL on the system. It will not fall back to the JDK implementation. |
| 161 | |
| 162 | ##### Configuring the context |
| 163 | |
| 164 | Use the following Java code to configure OpenSSL with your certificates: |
| 165 | |
| 166 | ```java |
| 167 | KeyStore ks = KeyStore.getInstance("JKS"); |
| 168 | // make sure you close this stream properly (not shown here for brevity) |
| 169 | InputStream trustStore = new FileInputStream("client.truststore"); |
| 170 | ks.load(trustStore, "password123".toCharArray()); |
| 171 | TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 172 | tmf.init(ks); |
| 173 | |
| 174 | SslContextBuilder builder = SslContextBuilder |
| 175 | .forClient() |
| 176 | .sslProvider(SslProvider.OPENSSL) |
| 177 | .trustManager(tmf); |
| 178 | // only if you use client authentication |
| 179 | .keyManager(new File("client.crt"), new File("client.key")); |
| 180 | |
| 181 | SSLOptions sslOptions = new NettySSLOptions(builder.build()); |
| 182 | |
| 183 | Cluster cluster = Cluster.builder() |
| 184 | .addContactPoint("127.0.0.1") |
| 185 | .withSSL(sslOptions) |
| 186 | .build(); |
| 187 | ``` |
| 188 | |
| 189 | [SSLOptions]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/SSLOptions.html |
| 190 | [JdkSSLOptions]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/JdkSSLOptions.html |
| 191 | [NettySSLOptions]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/NettySSLOptions.html |
| 192 | [NettyOptions]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/NettyOptions.html |