Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
323 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
core/src/main/java/com/datastax/oss/driver/api/core/ssl/DefaultSslEngineFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (C) 2017-2017 DataStax Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.datastax.oss.driver.api.core.ssl; | ||
|
||
import com.datastax.oss.driver.api.core.config.CoreDriverOption; | ||
import com.datastax.oss.driver.api.core.config.DriverConfigProfile; | ||
import java.net.InetSocketAddress; | ||
import java.net.SocketAddress; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.List; | ||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLEngine; | ||
|
||
/** | ||
* Default SSL implementation. | ||
* | ||
* <p>To activate this class, an {@code ssl} section must be included in the driver configuration, | ||
* for example: | ||
* | ||
* <pre> | ||
* datastax-java-driver { | ||
* ssl { | ||
* factory-class = com.datastax.driver.api.core.ssl.DefaultSslEngineFactory | ||
* config { | ||
* cipher-suites = [ "TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_256_CBC_SHA" ] | ||
* } | ||
* } | ||
* } | ||
* </pre> | ||
* | ||
* See the {@code reference.conf} file included with the driver for more information. | ||
*/ | ||
public class DefaultSslEngineFactory implements SslEngineFactory { | ||
|
||
private final SSLContext context; | ||
private final String[] cipherSuites; | ||
|
||
/** Builds a new instance from the driver configuration. */ | ||
public DefaultSslEngineFactory(DriverConfigProfile config) { | ||
try { | ||
this.context = SSLContext.getDefault(); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new IllegalStateException("Cannot initialize SSL Context", e); | ||
} | ||
if (config.isDefined(CoreDriverOption.SSL_CONFIG_CIPHER_SUITES)) { | ||
List<String> list = config.getStringList(CoreDriverOption.SSL_CONFIG_CIPHER_SUITES); | ||
String tmp[] = new String[list.size()]; | ||
this.cipherSuites = list.toArray(tmp); | ||
} else { | ||
this.cipherSuites = null; | ||
} | ||
} | ||
|
||
@Override | ||
public SSLEngine newSslEngine(SocketAddress remoteEndpoint) { | ||
SSLEngine engine; | ||
if (remoteEndpoint instanceof InetSocketAddress) { | ||
InetSocketAddress address = (InetSocketAddress) remoteEndpoint; | ||
engine = context.createSSLEngine(address.getHostName(), address.getPort()); | ||
} else { | ||
engine = context.createSSLEngine(); | ||
} | ||
engine.setUseClientMode(true); | ||
if (cipherSuites != null) { | ||
engine.setEnabledCipherSuites(cipherSuites); | ||
} | ||
return engine; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
core/src/main/java/com/datastax/oss/driver/api/core/ssl/SslEngineFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (C) 2017-2017 DataStax Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.datastax.oss.driver.api.core.ssl; | ||
|
||
import java.net.SocketAddress; | ||
import javax.net.ssl.SSLEngine; | ||
|
||
/** | ||
* Extension point to configure SSL based on the built-in JDK implementation. | ||
* | ||
* <p>Note that, for advanced use cases (such as bypassing the JDK in favor of another SSL | ||
* implementation), the driver's internal API provides a lower-level interface: {@link | ||
* com.datastax.oss.driver.internal.core.ssl.SslHandlerFactory}. | ||
*/ | ||
public interface SslEngineFactory { | ||
/** | ||
* Creates a new SSL engine each time a connection is established. | ||
* | ||
* @param remoteEndpoint the remote endpoint we are connecting to (the address of the Cassandra | ||
* node). | ||
*/ | ||
SSLEngine newSslEngine(SocketAddress remoteEndpoint); | ||
} |
17 changes: 17 additions & 0 deletions
17
core/src/main/java/com/datastax/oss/driver/api/core/ssl/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright (C) 2017-2017 DataStax Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/** Support for secured communication between the driver and Cassandra nodes. */ | ||
package com.datastax.oss.driver.api.core.ssl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
core/src/main/java/com/datastax/oss/driver/internal/core/ssl/JdkSslHandlerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (C) 2017-2017 DataStax Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.datastax.oss.driver.internal.core.ssl; | ||
|
||
import com.datastax.oss.driver.api.core.ssl.SslEngineFactory; | ||
import io.netty.channel.Channel; | ||
import io.netty.handler.ssl.SslHandler; | ||
import java.net.SocketAddress; | ||
import java.util.Optional; | ||
import javax.net.ssl.SSLEngine; | ||
|
||
/** SSL handler factory used when JDK-based SSL was configured through the driver's public API. */ | ||
public class JdkSslHandlerFactory implements SslHandlerFactory { | ||
private final SslEngineFactory sslEngineFactory; | ||
|
||
public JdkSslHandlerFactory(SslEngineFactory sslEngineFactory) { | ||
this.sslEngineFactory = sslEngineFactory; | ||
} | ||
|
||
@Override | ||
public Optional<SslHandler> newSslHandler(Channel channel, SocketAddress remoteEndpoint) { | ||
SSLEngine engine = sslEngineFactory.newSslEngine(remoteEndpoint); | ||
return Optional.of(new SslHandler(engine)); | ||
} | ||
} |
Oops, something went wrong.