Elffuss BraidHub

kikocis/java-driver public

3.1.x · 6.7 KiBRenderedRawPermalink
1## SSL
2
3You can secure traffic between the driver and Cassandra with SSL. There
4are 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
11This section describes the driver-side configuration; it assumes that
12you'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
25This is required for client-to-node encryption.
26
27If you're using self-signed certificates, you need to export the public
28part of each node's certificate from that node's keystore:
29
30```
31keytool -export -alias cassandra -file cassandranode0.cer -keystore .keystore
32```
33
34Then add all public certificates to the client truststore:
35
36```
37keytool -import -v -trustcacerts -alias <cassandra_node0> -file cassandranode0.cer -keystore client.truststore
38keytool -import -v -trustcacerts -alias <cassandra_node1> -file cassandranode1.cer -keystore client.truststore
39...
40```
41
42If you're using a Certificate Authority, the client truststore only
43needs to contain the CA's certificate:
44
45```
46keytool -import -v -trustcacerts -alias CARoot -file ca.cer -keystore client.truststore
47```
48
49#### Client keystore
50
51If you also intend to use client certificate authentication, generate
52the public and private key pair for the client:
53
54```
55keytool -genkey -keyalg RSA -alias client -keystore client.keystore
56```
57
58If you're using self-signed certificates, extract the public part of the
59client certificate, and import it in the truststore of each Cassandra
60node:
61
62```
63keytool -export -alias client -file client.cer -keystore client.keystore
64keytool -import -v -trustcacerts -alias client -file client.cer -keystore server.truststore
65```
66
67If you're using a CA, sign the client certificate with it (see the blog
68post linked at the top of this page). Then the nodes' truststores only
69need to contain the CA's certificate (which should already be the case
70if you've followed the steps for inter-node encryption).
71
72### Driver configuration
73
74The base class to configure SSL is [SSLOptions]. It's very generic, but
75you don't necessarily need to deal with it directly: the default
76instance, 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
83Cluster cluster = Cluster.builder()
84 .addContactPoint("127.0.0.1")
85 .withSSL()
86 .build();
87```
88
89You can then use
90[JSSE system properties](http://docs.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#Customization)
91for 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
103If you need more control than what system properties allow, you can
104configure SSL programmatically with [JdkSSLOptions]:
105
106```java
107SSLContext sslContext = ... // create and configure SSL context
108
109JdkSSLOptions sslOptions = JdkSSLOptions.builder()
110 .withSSLContext(context)
111 .build();
112
113Cluster cluster = Cluster.builder()
114 .addContactPoint("127.0.0.1")
115 .withSSL(sslOptions)
116 .build();
117```
118
119Note that you can also extend the class and override
120[newSSLEngine(SocketChannel)][newSSLEngine] if you need specific
121configuration 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
128the JDK directly. The advantage is that Netty can use OpenSSL directly,
129which provides better performance and generates less garbage. A disadvantage of
130using the OpenSSL provider is that it requires platform-specific dependencies,
131unlike the JDK provider.
132
133
134##### Converting your client certificates for OpenSSL
135
136OpenSSL doesn't use keystores, so if you use client authentication and
137generated 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
151Netty-tcnative provides the native integration with OpenSSL. Follow
152[these instructions](http://netty.io/wiki/forked-tomcat-native.html) to
153add it to your dependencies.
154
155There are known runtime incompatibilities between newer versions of
156netty-tcnative and the version of netty that the driver uses. For best
157results, use version 1.1.33.Fork18.
158
159Using netty-tcnative requires JDK 1.7 or above and requires the presence of
160OpenSSL on the system. It will not fall back to the JDK implementation.
161
162##### Configuring the context
163
164Use the following Java code to configure OpenSSL with your certificates:
165
166```java
167KeyStore ks = KeyStore.getInstance("JKS");
168// make sure you close this stream properly (not shown here for brevity)
169InputStream trustStore = new FileInputStream("client.truststore");
170ks.load(trustStore, "password123".toCharArray());
171TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
172tmf.init(ks);
173
174SslContextBuilder 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
181SSLOptions sslOptions = new NettySSLOptions(builder.build());
182
183Cluster 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