Skip to content

Commit 74ccdec

Browse files
committed
JVMCBC-1679 (followup) Cluster.setAuthenticator should require a compatible authenticator
Motivation ---------- A user can switch from an authenticator that does not require TLS to one that does. In this case, setAuthenticator() should reject the new authenticator if it has an unmet requirement. Modifications ------------- Move the authenticator compatibility check from Core/ProtostellarContext into DelegatingAuthenticator.setDelegate(). Add a `tls` parameter to DelegatingAuthenticator.create() so it can reject incompatible delegates. Change-Id: I6217c047d23163b0af1876dddbc5494fa72b4112 Reviewed-on: https://review.couchbase.org/c/couchbase-jvm-clients/+/235014 Reviewed-by: Michael Reiche <[email protected]> Tested-by: Build Bot <[email protected]>
1 parent 476e768 commit 74ccdec

File tree

5 files changed

+16
-15
lines changed

5 files changed

+16
-15
lines changed

core-io/src/main/java/com/couchbase/client/core/Core.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,6 @@ protected Core(
282282
final Authenticator authenticator,
283283
final ConnectionString connectionString
284284
) {
285-
if (authenticator.requiresTls() && !environment.securityConfig().tlsEnabled()) {
286-
throw new InvalidArgumentException("TLS not enabled but the Authenticator requires TLS!", null, null);
287-
}
288-
289285
checkConnectionStringScheme(connectionString, ConnectionString.Scheme.COUCHBASE, ConnectionString.Scheme.COUCHBASES);
290286
sanityCheckPorts(connectionString);
291287

core-io/src/main/java/com/couchbase/client/core/env/DelegatingAuthenticator.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,34 @@
2727
* The other authenticator can be swapped at runtime
2828
* to support credential rotation.
2929
*
30-
* @see #create(Authenticator)
30+
* @see #create(boolean, Authenticator)
3131
*/
3232
@NullMarked
3333
@Stability.Internal
3434
public class DelegatingAuthenticator extends AuthenticatorWrapper {
3535
private volatile Authenticator delegate;
36+
private final boolean tls;
3637

3738
/**
3839
* Returns a new authenticator that delegates to the given authenticator.
3940
* <p>
4041
* The delegate may be updated later by calling {@link #setDelegate(Authenticator)}.
42+
*
43+
* @param tls true if the connection is secured by TLS, otherwise false.
4144
*/
42-
public static DelegatingAuthenticator create(Authenticator delegate) {
43-
return new DelegatingAuthenticator(delegate);
45+
public static DelegatingAuthenticator create(boolean tls, Authenticator delegate) {
46+
return new DelegatingAuthenticator(tls, delegate);
4447
}
4548

4649
public void setDelegate(Authenticator delegate) {
50+
if (delegate.requiresTls() && !tls) {
51+
throw new IllegalArgumentException("The specified authenticator requires TLS, but TLS is not enabled.");
52+
}
4753
this.delegate = requireNonNull(delegate);
4854
}
4955

50-
DelegatingAuthenticator(Authenticator delegate) {
56+
DelegatingAuthenticator(boolean tls, Authenticator delegate) {
57+
this.tls = tls;
5158
setDelegate(delegate);
5259
}
5360

core-io/src/main/java/com/couchbase/client/core/protostellar/ProtostellarContext.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.couchbase.client.core.cnc.AbstractContext;
2222
import com.couchbase.client.core.env.Authenticator;
2323
import com.couchbase.client.core.env.CoreEnvironment;
24-
import com.couchbase.client.core.error.InvalidArgumentException;
2524
import com.couchbase.client.core.util.CoreIdGenerator;
2625

2726
import java.util.Map;
@@ -41,10 +40,6 @@ public ProtostellarContext(final CoreEnvironment env, final Authenticator authen
4140
this.env = requireNonNull(env);
4241
this.authenticator = requireNonNull(authenticator);
4342
this.coreResources = requireNonNull(coreResources);
44-
45-
if (authenticator.requiresTls() && !env.securityConfig().tlsEnabled()) {
46-
throw InvalidArgumentException.fromMessage("TLS not enabled but the Authenticator requires TLS!");
47-
}
4843
}
4944

5045
public long id() {

java-client/src/main/java/com/couchbase/client/java/AsyncCluster.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ String clusterToStringHelper(Class clusterClass) {
250250
) {
251251
this.environment = environment;
252252
this.connectionString = connectionString;
253-
this.authenticator = DelegatingAuthenticator.create(initialAuthenticator);
253+
this.authenticator = DelegatingAuthenticator.create(environment.get().securityConfig().tlsEnabled(), initialAuthenticator);
254254
this.couchbaseOps = CoreCouchbaseOps.create(environment.get(), this.authenticator, connectionString);
255255
this.queryOps = couchbaseOps.queryOps();
256256
this.searchOps = couchbaseOps.searchOps(null);

kotlin-client/src/main/kotlin/com/couchbase/client/kotlin/Cluster.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ public class Cluster internal constructor(
121121
initialAuthenticator: Authenticator,
122122
connectionString: ConnectionString,
123123
) {
124-
private val delegatingAuthenticator: DelegatingAuthenticator = DelegatingAuthenticator.create(initialAuthenticator)
124+
private val delegatingAuthenticator: DelegatingAuthenticator = DelegatingAuthenticator.create(
125+
env.securityConfig().tlsEnabled(),
126+
initialAuthenticator,
127+
)
125128

126129
private val couchbaseOps = CoreCouchbaseOps.create(env, delegatingAuthenticator, connectionString)
127130
private val searchOps = couchbaseOps.searchOps(null)

0 commit comments

Comments
 (0)