Skip to content

Commit 20eb381

Browse files
authored
Merge branch '4.x' into JAVA-2841
2 parents acc08c0 + 571fcfc commit 20eb381

File tree

9 files changed

+323
-75
lines changed

9 files changed

+323
-75
lines changed

CONTRIBUTING.md

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ mvn xml-format:xml-format
2828
The formatter does not enforce a maximum line length, but please try to keep it below 100 characters
2929
to keep files readable across all mediums (IDE, terminal, Github...).
3030

31+
### Other text files (markdown, etc)
32+
33+
Similarly, enforce a right margin of 100 characters in those files. Editors and IDEs generally have
34+
a way to configure this (for IDEA, install the "Wrap to column" plugin).
35+
3136
## Coding style -- production code
3237

3338
Do not use static imports. They make things harder to understand when you look at the code
@@ -213,6 +218,10 @@ Static imports are permitted in a couple of places:
213218
when(codecRegistry.codecFor(DataTypes.INT)).thenReturn(codec);
214219
verify(codec).decodePrimitive(any(ByteBuffer.class), eq(ProtocolVersion.DEFAULT));
215220
```
221+
* All Awaitility methods, e.g.:
222+
```java
223+
await().until(() -> somethingBecomesTrue());
224+
```
216225
217226
Test methods names use lower snake case, generally start with `should`, and clearly indicate the
218227
purpose of the test, for example: `should_fail_if_key_already_exists`. If you have trouble coming

changelog/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
### 4.8.0 (in progress)
66

77
- [improvement] JAVA-2841: Raise timeouts during connection initialization
8+
- [improvement] JAVA-2850: Ignore credentials in secure connect bundle [DataStax Astra]
9+
- [improvement] JAVA-2813: Don't fail when secure bundle is specified together with other options
810
- [bug] JAVA-2800: Exclude SLF4J from mapper-processor dependencies
911
- [new feature] JAVA-2819: Add DriverConfigLoader.fromString
1012
- [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

+6
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

+36-12
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,24 +678,32 @@ 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());
689-
if (cloudConfig.getAuthProvider().isPresent()) {
690-
withAuthProvider(cloudConfig.getAuthProvider().get());
691-
}
692707
programmaticArguments = programmaticArgumentsBuilder.build();
693708
}
694709

@@ -715,6 +730,15 @@ protected final CompletionStage<CqlSession> buildDefaultSessionAsync() {
715730
}
716731
}
717732

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

core/src/main/java/com/datastax/oss/driver/internal/core/config/cloud/CloudConfig.java

+1-12
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@
1515
*/
1616
package com.datastax.oss.driver.internal.core.config.cloud;
1717

18-
import com.datastax.oss.driver.api.core.auth.AuthProvider;
1918
import com.datastax.oss.driver.api.core.metadata.EndPoint;
2019
import com.datastax.oss.driver.api.core.ssl.SslEngineFactory;
2120
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList;
2221
import edu.umd.cs.findbugs.annotations.NonNull;
23-
import edu.umd.cs.findbugs.annotations.Nullable;
2422
import java.net.InetSocketAddress;
2523
import java.util.List;
26-
import java.util.Optional;
2724
import net.jcip.annotations.ThreadSafe;
2825

2926
@ThreadSafe
@@ -33,19 +30,16 @@ public class CloudConfig {
3330
private final List<EndPoint> endPoints;
3431
private final String localDatacenter;
3532
private final SslEngineFactory sslEngineFactory;
36-
@Nullable private final AuthProvider authProvider;
3733

3834
CloudConfig(
3935
@NonNull InetSocketAddress proxyAddress,
4036
@NonNull List<EndPoint> endPoints,
4137
@NonNull String localDatacenter,
42-
@NonNull SslEngineFactory sslEngineFactory,
43-
@Nullable AuthProvider authProvider) {
38+
@NonNull SslEngineFactory sslEngineFactory) {
4439
this.proxyAddress = proxyAddress;
4540
this.endPoints = ImmutableList.copyOf(endPoints);
4641
this.localDatacenter = localDatacenter;
4742
this.sslEngineFactory = sslEngineFactory;
48-
this.authProvider = authProvider;
4943
}
5044

5145
@NonNull
@@ -67,9 +61,4 @@ public String getLocalDatacenter() {
6761
public SslEngineFactory getSslEngineFactory() {
6862
return sslEngineFactory;
6963
}
70-
71-
@NonNull
72-
public Optional<AuthProvider> getAuthProvider() {
73-
return Optional.ofNullable(authProvider);
74-
}
7564
}

core/src/main/java/com/datastax/oss/driver/internal/core/config/cloud/CloudConfigFactory.java

+9-16
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
*/
1616
package com.datastax.oss.driver.internal.core.config.cloud;
1717

18-
import com.datastax.oss.driver.api.core.auth.AuthProvider;
1918
import com.datastax.oss.driver.api.core.metadata.EndPoint;
20-
import com.datastax.oss.driver.internal.core.auth.ProgrammaticPlainTextAuthProvider;
2119
import com.datastax.oss.driver.internal.core.metadata.SniEndPoint;
2220
import com.datastax.oss.driver.internal.core.ssl.SniSslEngineFactory;
2321
import com.datastax.oss.driver.shaded.guava.common.io.ByteStreams;
@@ -26,7 +24,6 @@
2624
import com.fasterxml.jackson.databind.JsonNode;
2725
import com.fasterxml.jackson.databind.ObjectMapper;
2826
import edu.umd.cs.findbugs.annotations.NonNull;
29-
import edu.umd.cs.findbugs.annotations.Nullable;
3027
import java.io.BufferedReader;
3128
import java.io.ByteArrayInputStream;
3229
import java.io.ByteArrayOutputStream;
@@ -53,10 +50,12 @@
5350
import javax.net.ssl.SSLContext;
5451
import javax.net.ssl.TrustManagerFactory;
5552
import net.jcip.annotations.ThreadSafe;
53+
import org.slf4j.Logger;
54+
import org.slf4j.LoggerFactory;
5655

5756
@ThreadSafe
5857
public class CloudConfigFactory {
59-
58+
private static final Logger LOG = LoggerFactory.getLogger(CloudConfigFactory.class);
6059
/**
6160
* Creates a {@link CloudConfig} with information fetched from the specified Cloud configuration
6261
* URL.
@@ -138,9 +137,8 @@ public CloudConfig createCloudConfig(@NonNull InputStream cloudConfig)
138137
List<EndPoint> endPoints = getEndPoints(proxyMetadataJson, sniProxyAddress);
139138
String localDatacenter = getLocalDatacenter(proxyMetadataJson);
140139
SniSslEngineFactory sslEngineFactory = new SniSslEngineFactory(sslContext);
141-
AuthProvider authProvider = getAuthProvider(configJson);
142-
return new CloudConfig(
143-
sniProxyAddress, endPoints, localDatacenter, sslEngineFactory, authProvider);
140+
validateIfBundleContainsUsernamePassword(configJson);
141+
return new CloudConfig(sniProxyAddress, endPoints, localDatacenter, sslEngineFactory);
144142
}
145143

146144
@NonNull
@@ -176,16 +174,11 @@ protected URL getMetadataServiceUrl(JsonNode configFile) throws MalformedURLExce
176174
}
177175
}
178176

179-
@Nullable
180-
protected AuthProvider getAuthProvider(JsonNode configFile) {
181-
if (configFile.has("username")) {
182-
String username = configFile.get("username").asText();
183-
if (configFile.has("password")) {
184-
String password = configFile.get("password").asText();
185-
return new ProgrammaticPlainTextAuthProvider(username, password);
186-
}
177+
protected void validateIfBundleContainsUsernamePassword(JsonNode configFile) {
178+
if (configFile.has("username") || configFile.has("password")) {
179+
LOG.info(
180+
"The bundle contains config.json with username and/or password. Providing it in the bundle is deprecated and ignored.");
187181
}
188-
return null;
189182
}
190183

191184
@NonNull

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

-2
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

-1
This file was deleted.

0 commit comments

Comments
 (0)