Elffuss BraidHub

kikocis/java-driver public

1663c57e5b30 · 6.3 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 if
129available, which provides better performance.
130
131##### Converting your client certificates for OpenSSL
132
133OpenSSL doesn't use keystores, so if you use client authentication and
134generated your certificates with keytool, you need to convert them.
135
136* use this command to extract the public certificate chain:
137
138 ```
139 keytool -export -keystore client.keystore -alias client -rfc -file client.crt
140 ```
141* follow
142 [this tutorial](http://www.herongyang.com/crypto/Migrating_Keys_keytool_to_OpenSSL_3.html)
143 to extract your client's private key from `client.keystore` to a text
144 file `client.key` in PEM format.
145
146##### Updating your dependencies
147
148Netty-tcnative provides the native integration with OpenSSL. Follow
149[these instructions](http://netty.io/wiki/forked-tomcat-native.html) to
150add it to your dependencies.
151
152Note that using netty-tcnative requires JDK 1.7 or above.
153
154##### Configuring the context
155
156Use the following Java code to configure OpenSSL with your certificates:
157
158```java
159KeyStore ks = KeyStore.getInstance("JKS");
160// make sure you close this stream properly (not shown here for brevity)
161InputStream trustStore = new FileInputStream("client.truststore");
162ks.load(trustStore, "password123".toCharArray());
163TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
164tmf.init(ks);
165
166SslContextBuilder builder = SslContextBuilder
167 .forClient()
168 .sslProvider(SslProvider.OPENSSL)
169 .trustManager(tmf);
170 // only if you use client authentication
171 .keyManager(new File("client.crt"), new File("client.key"));
172
173SSLOptions sslOptions = new NettySSLOptions(builder.build());
174
175Cluster cluster = Cluster.builder()
176 .addContactPoint("127.0.0.1")
177 .withSSL(sslOptions)
178 .build();
179```
180
181[SSLOptions]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/SSLOptions.html
182[JdkSSLOptions]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/JdkSSLOptions.html
183[NettySSLOptions]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/NettySSLOptions.html
184[NettyOptions]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/NettyOptions.html