kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 2d8132f27b8fcfbfe034f53a981dddeb869ce4b11372d386838c409aa828682c |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-11-05 18:55 +01:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-11-05 18:55 +01:00 |
| Parents | 3168d4757d5a, bf4830f0f229 |
7 files changed
modified changelog/README.md +1 −0
| @@ -11,6 +11,7 @@ | ||
| 11 | 11 | - [bug] JAVA-960: Avoid race in control connection shutdown. |
| 12 | 12 | - [bug] JAVA-656: Fix NPE in ControlConnection.updateLocationInfo. |
| 13 | 13 | - [bug] JAVA-966: Count uninitialized connections in conviction policy. |
| 14 | +- [improvement] JAVA-917: Document SSL configuration. | |
| 14 | 15 | |
| 15 | 16 | |
| 16 | 17 | ### 2.0.11 |
modified driver-core/src/main/java/com/datastax/driver/core/SSLOptions.java +1 −7
| @@ -24,8 +24,6 @@ | ||
| 24 | 24 | */ |
| 25 | 25 | public class SSLOptions { |
| 26 | 26 | |
| 27 | - private static final String SSL_PROTOCOL = "TLS"; | |
| 28 | - | |
| 29 | 27 | /** |
| 30 | 28 | * The default SSL cipher suites. |
| 31 | 29 | */ |
| @@ -63,12 +61,8 @@ | ||
| 63 | 61 | |
| 64 | 62 | private static SSLContext makeDefaultContext() throws IllegalStateException { |
| 65 | 63 | try { |
| 66 | - SSLContext ctx = SSLContext.getInstance(SSL_PROTOCOL); | |
| 67 | - ctx.init(null, null, null); // use defaults | |
| 68 | - return ctx; | |
| 64 | + return SSLContext.getDefault(); | |
| 69 | 65 | } catch (NoSuchAlgorithmException e) { |
| 70 | - throw new RuntimeException("This JVM doesn't support TLS, this shouldn't happen"); | |
| 71 | - } catch (KeyManagementException e) { | |
| 72 | 66 | throw new IllegalStateException("Cannot initialize SSL Context", e); |
| 73 | 67 | } |
| 74 | 68 | } |
modified driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java +4 −0
| @@ -60,9 +60,13 @@ | ||
| 60 | 60 | public static final String DEFAULT_CLIENT_TRUSTSTORE_PASSWORD = "cassandra1sfun"; |
| 61 | 61 | public static final String DEFAULT_CLIENT_TRUSTSTORE_PATH = "/client.truststore"; |
| 62 | 62 | |
| 63 | + public static final File DEFAULT_CLIENT_TRUSTSTORE_FILE = createTempStore(DEFAULT_CLIENT_TRUSTSTORE_PATH); | |
| 64 | + | |
| 63 | 65 | public static final String DEFAULT_CLIENT_KEYSTORE_PASSWORD = "cassandra1sfun"; |
| 64 | 66 | public static final String DEFAULT_CLIENT_KEYSTORE_PATH = "/client.keystore"; |
| 65 | 67 | |
| 68 | + public static final File DEFAULT_CLIENT_KEYSTORE_FILE = createTempStore(DEFAULT_CLIENT_KEYSTORE_PATH); | |
| 69 | + | |
| 66 | 70 | public static final String DEFAULT_SERVER_TRUSTSTORE_PASSWORD = "cassandra1sfun"; |
| 67 | 71 | public static final String DEFAULT_SERVER_TRUSTSTORE_PATH = "/server.truststore"; |
| 68 | 72 |
modified driver-core/src/test/java/com/datastax/driver/core/SSLAuthenticatedEncryptionTest.java +23 −2
| @@ -20,8 +20,7 @@ | ||
| 20 | 20 | |
| 21 | 21 | import com.datastax.driver.core.exceptions.NoHostAvailableException; |
| 22 | 22 | |
| 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.*; | |
| 25 | 24 | |
| 26 | 25 | public class SSLAuthenticatedEncryptionTest extends SSLTestBase { |
| 27 | 26 | |
| @@ -59,4 +58,26 @@ | ||
| 59 | 58 | public void should_not_connect_without_client_auth_but_node_requires_auth() throws Exception { |
| 60 | 59 | connectWithSSLOptions(getSSLOptions(Optional.<String>absent(), Optional.of(DEFAULT_CLIENT_TRUSTSTORE_PATH))); |
| 61 | 60 | } |
| 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 | + } | |
| 62 | 83 | } |
modified driver-core/src/test/java/com/datastax/driver/core/SSLEncryptionTest.java +21 −2
| @@ -23,8 +23,7 @@ | ||
| 23 | 23 | import com.datastax.driver.core.exceptions.NoHostAvailableException; |
| 24 | 24 | |
| 25 | 25 | 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.*; | |
| 28 | 27 | |
| 29 | 28 | public class SSLEncryptionTest extends SSLTestBase { |
| 30 | 29 | |
| @@ -118,4 +117,24 @@ | ||
| 118 | 117 | cluster.close(); |
| 119 | 118 | } |
| 120 | 119 | } |
| 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 | + } | |
| 121 | 140 | } |
modified driver-core/src/test/java/com/datastax/driver/core/SSLTestBase.java +36 −0
| @@ -25,7 +25,9 @@ | ||
| 25 | 25 | import org.testng.annotations.AfterClass; |
| 26 | 26 | import org.testng.annotations.BeforeClass; |
| 27 | 27 | |
| 28 | +import static com.datastax.driver.core.CCMBridge.DEFAULT_CLIENT_KEYSTORE_FILE; | |
| 28 | 29 | import static com.datastax.driver.core.CCMBridge.DEFAULT_CLIENT_KEYSTORE_PASSWORD; |
| 30 | +import static com.datastax.driver.core.CCMBridge.DEFAULT_CLIENT_TRUSTSTORE_FILE; | |
| 29 | 31 | import static com.datastax.driver.core.CCMBridge.DEFAULT_CLIENT_TRUSTSTORE_PASSWORD; |
| 30 | 32 | public abstract class SSLTestBase { |
| 31 | 33 | |
| @@ -74,6 +76,40 @@ | ||
| 74 | 76 | } |
| 75 | 77 | } |
| 76 | 78 | |
| 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 | + | |
| 77 | 113 | /** |
| 78 | 114 | * @param keyStorePath Path to keystore, if absent is not used. |
| 79 | 115 | * @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 |