Elffuss BraidHub

kikocis/java-driver public

Merge pull request #510 from datastax/java917

JAVA-917: Document SSL configuration.
Changeset2d8132f27b8fcfbfe034f53a981dddeb869ce4b11372d386838c409aa828682c
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2015-11-05 18:55 +01:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2015-11-05 18:55 +01:00
Parents3168d4757d5a, bf4830f0f229

Browse the files at this changeset

7 files changed

modified changelog/README.md +1 −0
@@ -11,6 +11,7 @@
1111 - [bug] JAVA-960: Avoid race in control connection shutdown.
1212 - [bug] JAVA-656: Fix NPE in ControlConnection.updateLocationInfo.
1313 - [bug] JAVA-966: Count uninitialized connections in conviction policy.
14+- [improvement] JAVA-917: Document SSL configuration.
1415
1516
1617 ### 2.0.11
modified driver-core/src/main/java/com/datastax/driver/core/SSLOptions.java +1 −7
@@ -24,8 +24,6 @@
2424 */
2525 public class SSLOptions {
2626
27- private static final String SSL_PROTOCOL = "TLS";
28-
2927 /**
3028 * The default SSL cipher suites.
3129 */
@@ -63,12 +61,8 @@
6361
6462 private static SSLContext makeDefaultContext() throws IllegalStateException {
6563 try {
66- SSLContext ctx = SSLContext.getInstance(SSL_PROTOCOL);
67- ctx.init(null, null, null); // use defaults
68- return ctx;
64+ return SSLContext.getDefault();
6965 } catch (NoSuchAlgorithmException e) {
70- throw new RuntimeException("This JVM doesn't support TLS, this shouldn't happen");
71- } catch (KeyManagementException e) {
7266 throw new IllegalStateException("Cannot initialize SSL Context", e);
7367 }
7468 }
modified driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java +4 −0
@@ -60,9 +60,13 @@
6060 public static final String DEFAULT_CLIENT_TRUSTSTORE_PASSWORD = "cassandra1sfun";
6161 public static final String DEFAULT_CLIENT_TRUSTSTORE_PATH = "/client.truststore";
6262
63+ public static final File DEFAULT_CLIENT_TRUSTSTORE_FILE = createTempStore(DEFAULT_CLIENT_TRUSTSTORE_PATH);
64+
6365 public static final String DEFAULT_CLIENT_KEYSTORE_PASSWORD = "cassandra1sfun";
6466 public static final String DEFAULT_CLIENT_KEYSTORE_PATH = "/client.keystore";
6567
68+ public static final File DEFAULT_CLIENT_KEYSTORE_FILE = createTempStore(DEFAULT_CLIENT_KEYSTORE_PATH);
69+
6670 public static final String DEFAULT_SERVER_TRUSTSTORE_PASSWORD = "cassandra1sfun";
6771 public static final String DEFAULT_SERVER_TRUSTSTORE_PATH = "/server.truststore";
6872
modified driver-core/src/test/java/com/datastax/driver/core/SSLAuthenticatedEncryptionTest.java +23 −2
@@ -20,8 +20,7 @@
2020
2121 import com.datastax.driver.core.exceptions.NoHostAvailableException;
2222
23-import static com.datastax.driver.core.CCMBridge.DEFAULT_CLIENT_KEYSTORE_PATH;
24-import static com.datastax.driver.core.CCMBridge.DEFAULT_CLIENT_TRUSTSTORE_PATH;
23+import static com.datastax.driver.core.CCMBridge.*;
2524
2625 public class SSLAuthenticatedEncryptionTest extends SSLTestBase {
2726
@@ -59,4 +58,26 @@
5958 public void should_not_connect_without_client_auth_but_node_requires_auth() throws Exception {
6059 connectWithSSLOptions(getSSLOptions(Optional.<String>absent(), Optional.of(DEFAULT_CLIENT_TRUSTSTORE_PATH)));
6160 }
61+
62+ /**
63+ * <p>
64+ * Validates that SSL connectivity can be configured via the standard javax.net.ssl System properties.
65+ * </p>
66+ *
67+ * @test_category connection:ssl, authentication
68+ * @expected_result Connection can be established.
69+ */
70+ @Test(groups="short")
71+ public void should_use_system_properties_with_default_ssl_options() throws Exception {
72+ try {
73+ System.setProperty("javax.net.ssl.keyStore", DEFAULT_CLIENT_KEYSTORE_FILE.getAbsolutePath());
74+ System.setProperty("javax.net.ssl.keyStorePassword", DEFAULT_CLIENT_KEYSTORE_PASSWORD);
75+ System.setProperty("javax.net.ssl.trustStore", DEFAULT_CLIENT_TRUSTSTORE_FILE.getAbsolutePath());
76+ System.setProperty("javax.net.ssl.trustStorePassword", DEFAULT_CLIENT_TRUSTSTORE_PASSWORD);
77+
78+ connectWithSSL();
79+ } finally {
80+ clearSystemProperties();
81+ }
82+ }
6283 }
modified driver-core/src/test/java/com/datastax/driver/core/SSLEncryptionTest.java +21 −2
@@ -23,8 +23,7 @@
2323 import com.datastax.driver.core.exceptions.NoHostAvailableException;
2424
2525 import static com.datastax.driver.core.Assertions.assertThat;
26-import static com.datastax.driver.core.CCMBridge.DEFAULT_CLIENT_KEYSTORE_PATH;
27-import static com.datastax.driver.core.CCMBridge.DEFAULT_CLIENT_TRUSTSTORE_PATH;
26+import static com.datastax.driver.core.CCMBridge.*;
2827
2928 public class SSLEncryptionTest extends SSLTestBase {
3029
@@ -118,4 +117,24 @@
118117 cluster.close();
119118 }
120119 }
120+
121+ /**
122+ * <p>
123+ * Validates that SSL connectivity can be configured via the standard javax.net.ssl System properties.
124+ * </p>
125+ *
126+ * @test_category connection:ssl
127+ * @expected_result Connection can be established.
128+ */
129+ @Test(groups="short")
130+ public void should_use_system_properties_with_default_ssl_options() throws Exception {
131+ try {
132+ System.setProperty("javax.net.ssl.trustStore", DEFAULT_CLIENT_TRUSTSTORE_FILE.getAbsolutePath());
133+ System.setProperty("javax.net.ssl.trustStorePassword", DEFAULT_CLIENT_TRUSTSTORE_PASSWORD);
134+
135+ connectWithSSL();
136+ } finally {
137+ clearSystemProperties();
138+ }
139+ }
121140 }
modified driver-core/src/test/java/com/datastax/driver/core/SSLTestBase.java +36 −0
@@ -25,7 +25,9 @@
2525 import org.testng.annotations.AfterClass;
2626 import org.testng.annotations.BeforeClass;
2727
28+import static com.datastax.driver.core.CCMBridge.DEFAULT_CLIENT_KEYSTORE_FILE;
2829 import static com.datastax.driver.core.CCMBridge.DEFAULT_CLIENT_KEYSTORE_PASSWORD;
30+import static com.datastax.driver.core.CCMBridge.DEFAULT_CLIENT_TRUSTSTORE_FILE;
2931 import static com.datastax.driver.core.CCMBridge.DEFAULT_CLIENT_TRUSTSTORE_PASSWORD;
3032 public abstract class SSLTestBase {
3133
@@ -74,6 +76,40 @@
7476 }
7577 }
7678
79+ /**
80+ * <p>
81+ * Attempts to connect to a cassandra cluster with using {@link Cluster.Builder#withSSL} with no
82+ * provided {@link SSLOptions} and then closes the created {@link Cluster} instance.
83+ * </p>
84+ *
85+ * @throws Exception A {@link com.datastax.driver.core.exceptions.NoHostAvailableException} will be
86+ * raised here if connection cannot be established.
87+ */
88+ protected void connectWithSSL() throws Exception {
89+ Cluster cluster = null;
90+ try {
91+ cluster = Cluster.builder()
92+ .addContactPoint(CCMBridge.IP_PREFIX + '1')
93+ .withSSL()
94+ .build();
95+
96+ cluster.connect();
97+ } finally {
98+ if (cluster != null)
99+ cluster.close();
100+ }
101+ }
102+
103+ /**
104+ * Clears all System properties associated with SSL key and trust stores.
105+ */
106+ protected void clearSystemProperties() {
107+ System.clearProperty("javax.net.ssl.keyStore");
108+ System.clearProperty("javax.net.ssl.keyStorePassword");
109+ System.clearProperty("javax.net.ssl.trustStore");
110+ System.clearProperty("javax.net.ssl.trustStorePassword");
111+ }
112+
77113 /**
78114 * @param keyStorePath Path to keystore, if absent is not used.
79115 * @param trustStorePath Path to truststore, if absent is not used.
added features/ssl/README.md +180 −0
@@ -0,0 +1,180 @@
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.0/com/datastax/driver/core/SSLOptions.html
180+[NettyOptions]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/NettyOptions.html