Skip to content

Commit 5c18d1b

Browse files
committed
JVMCBC-1679 Add cluster.setAuthenticator()
Motivation ---------- Simplify the credential rotation API. Modifications ------------- Add a setAuthenticator() method to Java and Kotlin's `Cluster` classes. Mark `DelegatingAuthenticator` as internal API, and remove references to it from Javadoc. Change-Id: I46d9b2ed45542662acb3fe49e419fa583d0688b2 Reviewed-on: https://review.couchbase.org/c/couchbase-jvm-clients/+/234329 Tested-by: Build Bot <[email protected]> Reviewed-by: Will Broadbelt <[email protected]>
1 parent c3e0ba6 commit 5c18d1b

File tree

7 files changed

+32
-10
lines changed

7 files changed

+32
-10
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
* @see PasswordAuthenticator
3535
* @see JwtAuthenticator
3636
* @see CertificateAuthenticator
37-
* @see DelegatingAuthenticator
3837
*
3938
* @since 2.0.0
4039
*/

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.couchbase.client.core.env;
1818

19+
import com.couchbase.client.core.annotation.Stability;
1920
import org.jspecify.annotations.NullMarked;
2021

2122
import static java.util.Objects.requireNonNull;
@@ -29,6 +30,7 @@
2930
* @see #create(Authenticator)
3031
*/
3132
@NullMarked
33+
@Stability.Internal
3234
public class DelegatingAuthenticator extends AuthenticatorWrapper {
3335
private volatile Authenticator delegate;
3436

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static PasswordAuthenticator.Builder builder(String username, String pass
8989
* in the volatile field.
9090
*
9191
* @deprecated This method is difficult to use safely, because it's easy to accidentally
92-
* do blocking IO inside the supplier. Please use {@link DelegatingAuthenticator} instead.
92+
* do blocking IO inside the supplier. Please use {@code cluster.setAuthenticator(newAuthenticator)} instead.
9393
*/
9494
@Deprecated
9595
public static PasswordAuthenticator.Builder builder(Supplier<UsernameAndPassword> supplier) {
@@ -236,7 +236,7 @@ public Builder username(final String username) {
236236
* @param username A supplier that returns the username to use.
237237
* @return this builder for chaining purposes.
238238
* @deprecated This method does not support returning username and password as an atomic unit.
239-
* Please use {@link DelegatingAuthenticator} instead.
239+
* Please use {@code cluster.setAuthenticator(newAuthenticator)} instead.
240240
*/
241241
@Deprecated
242242
public Builder username(final Supplier<String> username) {
@@ -278,7 +278,7 @@ public Builder password(final String password) {
278278
* @param password the password to alongside for the username provided.
279279
* @return this builder for chaining purposes.
280280
* @deprecated This method does not support returning username and password as an atomic unit.
281-
* Please use {@link DelegatingAuthenticator} instead.
281+
* Please use {@code cluster.setAuthenticator(newAuthenticator)} instead.
282282
*/
283283
@Deprecated
284284
public Builder password(final Supplier<String> password) {

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.couchbase.client.core.env.Authenticator;
3636
import com.couchbase.client.core.env.ConnectionStringPropertyLoader;
3737
import com.couchbase.client.core.env.CoreEnvironment;
38+
import com.couchbase.client.core.env.DelegatingAuthenticator;
3839
import com.couchbase.client.core.env.OwnedOrExternal;
3940
import com.couchbase.client.core.env.PasswordAuthenticator;
4041
import com.couchbase.client.core.env.SeedNode;
@@ -137,7 +138,7 @@ public class AsyncCluster {
137138

138139
private final CoreCouchbaseOps couchbaseOps;
139140

140-
private final Authenticator authenticator;
141+
private final DelegatingAuthenticator authenticator;
141142

142143
/**
143144
* Stores already opened buckets for reuse.
@@ -244,13 +245,13 @@ String clusterToStringHelper(Class clusterClass) {
244245
*/
245246
AsyncCluster(
246247
final OwnedOrExternal<ClusterEnvironment> environment,
247-
final Authenticator authenticator,
248+
final Authenticator initialAuthenticator,
248249
final ConnectionString connectionString
249250
) {
250251
this.environment = environment;
251252
this.connectionString = connectionString;
252-
this.couchbaseOps = CoreCouchbaseOps.create(environment.get(), authenticator, connectionString);
253-
this.authenticator = authenticator;
253+
this.authenticator = DelegatingAuthenticator.create(initialAuthenticator);
254+
this.couchbaseOps = CoreCouchbaseOps.create(environment.get(), this.authenticator, connectionString);
254255
this.queryOps = couchbaseOps.queryOps();
255256
this.searchOps = couchbaseOps.searchOps(null);
256257

@@ -268,6 +269,10 @@ String clusterToStringHelper(Class clusterClass) {
268269
));
269270
}
270271

272+
public void setAuthenticator(Authenticator newAuthenticator) {
273+
authenticator.setDelegate(newAuthenticator);
274+
}
275+
271276
/**
272277
* Provides access to the configured {@link ClusterEnvironment} for this cluster.
273278
*/

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,10 @@ public ReactiveCluster reactive() {
317317
return reactiveCluster;
318318
}
319319

320+
public void setAuthenticator(Authenticator newAuthenticator) {
321+
asyncCluster.setAuthenticator(newAuthenticator);
322+
}
323+
320324
/**
321325
* Provides access to the underlying {@link Core}.
322326
*

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ private ReactiveCluster(
188188
this.reactor = asyncCluster.environment();
189189
}
190190

191+
public void setAuthenticator(Authenticator newAuthenticator) {
192+
asyncCluster.setAuthenticator(newAuthenticator);
193+
}
194+
191195
/**
192196
* Provides access to the underlying {@link Core}.
193197
*

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.couchbase.client.core.diagnostics.HealthPinger
2727
import com.couchbase.client.core.env.Authenticator
2828
import com.couchbase.client.core.env.CertificateAuthenticator
2929
import com.couchbase.client.core.env.ConnectionStringPropertyLoader
30+
import com.couchbase.client.core.env.DelegatingAuthenticator
3031
import com.couchbase.client.core.env.PasswordAuthenticator
3132
import com.couchbase.client.core.error.UnambiguousTimeoutException
3233
import com.couchbase.client.core.service.ServiceType
@@ -117,12 +118,19 @@ import kotlin.time.toKotlinDuration
117118
public class Cluster internal constructor(
118119
internal val env: ClusterEnvironment,
119120
private val ownsEnvironment: Boolean,
120-
private val authenticator: Authenticator,
121+
initialAuthenticator: Authenticator,
121122
connectionString: ConnectionString,
122123
) {
123-
private val couchbaseOps = CoreCouchbaseOps.create(env, authenticator, connectionString)
124+
private val delegatingAuthenticator: DelegatingAuthenticator = DelegatingAuthenticator.create(initialAuthenticator)
125+
126+
private val couchbaseOps = CoreCouchbaseOps.create(env, delegatingAuthenticator, connectionString)
124127
private val searchOps = couchbaseOps.searchOps(null)
125128

129+
public var authenticator: Authenticator
130+
@Deprecated("Property 'authenticator' is write-only.", level = DeprecationLevel.ERROR)
131+
get() = throw UnsupportedOperationException()
132+
set(value) { this.delegatingAuthenticator.setDelegate(value) }
133+
126134
internal val core: Core
127135
get() = couchbaseOps.asCore()
128136

0 commit comments

Comments
 (0)