Skip to content

Commit 83eec17

Browse files
authored
JAVA-2813: Don't fail when secure bundle is specified together with other options (#1476)
1 parent eb93f72 commit 83eec17

File tree

6 files changed

+219
-36
lines changed

6 files changed

+219
-36
lines changed

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### 4.8.0 (in progress)
66

7+
- [improvement] JAVA-2813: Don't fail when secure bundle is specified together with other options
78
- [bug] JAVA-2800: Exclude SLF4J from mapper-processor dependencies
89
- [new feature] JAVA-2819: Add DriverConfigLoader.fromString
910
- [improvement] JAVA-2431: Set all occurrences when bound variables are used multiple times

core/src/main/java/com/datastax/oss/driver/api/core/session/ProgrammaticArguments.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ public Builder withLocalDatacenter(
212212
return this;
213213
}
214214

215+
@NonNull
216+
public Builder clearDatacenters() {
217+
this.localDatacentersBuilder = ImmutableMap.builder();
218+
return this;
219+
}
220+
215221
@NonNull
216222
public Builder withLocalDatacenters(Map<String, String> localDatacenters) {
217223
for (Map.Entry<String, String> entry : localDatacenters.entrySet()) {

core/src/main/java/com/datastax/oss/driver/api/core/session/SessionBuilder.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.datastax.oss.driver.api.core.auth.AuthProvider;
2121
import com.datastax.oss.driver.api.core.auth.PlainTextAuthProviderBase;
2222
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
23+
import com.datastax.oss.driver.api.core.config.DriverConfig;
2324
import com.datastax.oss.driver.api.core.config.DriverConfigLoader;
2425
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
2526
import com.datastax.oss.driver.api.core.context.DriverContext;
@@ -65,6 +66,8 @@
6566
import java.util.function.Predicate;
6667
import javax.net.ssl.SSLContext;
6768
import net.jcip.annotations.NotThreadSafe;
69+
import org.slf4j.Logger;
70+
import org.slf4j.LoggerFactory;
6871

6972
/**
7073
* Base implementation to build session instances.
@@ -77,6 +80,8 @@
7780
@NotThreadSafe
7881
public abstract class SessionBuilder<SelfT extends SessionBuilder, SessionT> {
7982

83+
private static final Logger LOG = LoggerFactory.getLogger(SessionBuilder.class);
84+
8085
@SuppressWarnings("unchecked")
8186
protected final SelfT self = (SelfT) this;
8287

@@ -87,7 +92,8 @@ public abstract class SessionBuilder<SelfT extends SessionBuilder, SessionT> {
8792

8893
protected ProgrammaticArguments.Builder programmaticArgumentsBuilder =
8994
ProgrammaticArguments.builder();
90-
private boolean sslConfigured = false;
95+
private boolean programmaticSslFactory = false;
96+
private boolean programmaticLocalDatacenter = false;
9197

9298
/**
9399
* Sets the configuration loader to use.
@@ -314,7 +320,7 @@ public SelfT withAuthCredentials(
314320
*/
315321
@NonNull
316322
public SelfT withSslEngineFactory(@Nullable SslEngineFactory sslEngineFactory) {
317-
this.sslConfigured = true;
323+
this.programmaticSslFactory = true;
318324
this.programmaticArgumentsBuilder.withSslEngineFactory(sslEngineFactory);
319325
return self;
320326
}
@@ -352,6 +358,7 @@ public SelfT withSslContext(@Nullable SSLContext sslContext) {
352358
* if you use a third-party implementation, refer to their documentation.
353359
*/
354360
public SelfT withLocalDatacenter(@NonNull String profileName, @NonNull String localDatacenter) {
361+
this.programmaticLocalDatacenter = true;
355362
this.programmaticArgumentsBuilder.withLocalDatacenter(profileName, localDatacenter);
356363
return self;
357364
}
@@ -671,18 +678,29 @@ protected final CompletionStage<CqlSession> buildDefaultSessionAsync() {
671678
defaultConfig.getStringList(DefaultDriverOption.CONTACT_POINTS, Collections.emptyList());
672679
if (cloudConfigInputStream != null) {
673680
if (!programmaticContactPoints.isEmpty() || !configContactPoints.isEmpty()) {
674-
throw new IllegalStateException(
675-
"Can't use withCloudSecureConnectBundle and addContactPoint(s). They are mutually exclusive.");
681+
LOG.info(
682+
"Both a secure connect bundle and contact points were provided. These are mutually exclusive. The contact points from the secure bundle will have priority.");
683+
// clear the contact points provided in the setting file and via addContactPoints
684+
configContactPoints = Collections.emptyList();
685+
programmaticContactPoints = new HashSet<>();
676686
}
677-
String configuredSSLFactory =
678-
defaultConfig.getString(DefaultDriverOption.SSL_ENGINE_FACTORY_CLASS, null);
679-
if (sslConfigured || configuredSSLFactory != null) {
680-
throw new IllegalStateException(
681-
"Can't use withCloudSecureConnectBundle and explicitly specify ssl configuration. They are mutually exclusive.");
687+
688+
if (programmaticSslFactory
689+
|| defaultConfig.isDefined(DefaultDriverOption.SSL_ENGINE_FACTORY_CLASS)) {
690+
LOG.info(
691+
"Both a secure connect bundle and SSL options were provided. They are mutually exclusive. The SSL options from the secure bundle will have priority.");
682692
}
683693
CloudConfig cloudConfig =
684694
new CloudConfigFactory().createCloudConfig(cloudConfigInputStream.call());
685695
addContactEndPoints(cloudConfig.getEndPoints());
696+
697+
boolean localDataCenterDefined =
698+
anyProfileHasDatacenterDefined(configLoader.getInitialConfig());
699+
if (programmaticLocalDatacenter || localDataCenterDefined) {
700+
LOG.info(
701+
"Both a secure connect bundle and a local datacenter were provided. They are mutually exclusive. The local datacenter from the secure bundle will have priority.");
702+
programmaticArgumentsBuilder.clearDatacenters();
703+
}
686704
withLocalDatacenter(cloudConfig.getLocalDatacenter());
687705
withSslEngineFactory(cloudConfig.getSslEngineFactory());
688706
withCloudProxyAddress(cloudConfig.getProxyAddress());
@@ -715,6 +733,15 @@ protected final CompletionStage<CqlSession> buildDefaultSessionAsync() {
715733
}
716734
}
717735

736+
private boolean anyProfileHasDatacenterDefined(DriverConfig driverConfig) {
737+
for (DriverExecutionProfile driverExecutionProfile : driverConfig.getProfiles().values()) {
738+
if (driverExecutionProfile.isDefined(DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER)) {
739+
return true;
740+
}
741+
}
742+
return false;
743+
}
744+
718745
/**
719746
* Returns URL based on the configUrl setting. If the configUrl has no protocol provided, the
720747
* method will fallback to file:// protocol and return URL that has file protocol specified.

examples/src/main/java/com/datastax/oss/driver/examples/astra/AstraReadCassandraVersion.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.datastax.oss.driver.examples.astra;
1717

1818
import com.datastax.oss.driver.api.core.CqlSession;
19-
import com.datastax.oss.driver.api.core.config.DriverConfigLoader;
2019
import com.datastax.oss.driver.api.core.cql.ResultSet;
2120
import com.datastax.oss.driver.api.core.cql.Row;
2221
import java.nio.file.Paths;
@@ -57,7 +56,6 @@ public static void main(String[] args) {
5756
.withCloudSecureConnectBundle(Paths.get("/path/to/secure-connect-database_name.zip"))
5857
// Change the user_name and password here for the Astra instance
5958
.withAuthCredentials("user_name", "fakePasswordForTests")
60-
.withConfigLoader(DriverConfigLoader.fromClasspath("application-astra"))
6159
// Uncomment the next line to use a specific keyspace
6260
// .withKeyspace("keyspace_name")
6361
.build()) {

examples/src/main/resources/application-astra.conf

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)