From 746dd82e701366be4cfc932d1f960adc8bf9a1d4 Mon Sep 17 00:00:00 2001 From: ggivo Date: Wed, 18 Dec 2024 10:54:42 +0200 Subject: [PATCH] Remove StreamingCredentialsProvider interface. move credentials() method to RedisCredentialsProvider. Resolve issue with unsafe cast after extending RedisCredentialsProvider with supportsStreaming() method --- .../java/io/lettuce/core/ClientOptions.java | 22 +++++++++------- .../core/RedisAuthenticationHandler.java | 6 ++--- .../core/RedisCredentialsProvider.java | 18 +++++++++++++ .../core/StreamingCredentialsProvider.java | 26 ------------------- .../connection/ReauthenticationEvent.java | 2 +- .../ReauthenticationFailedEvent.java | 2 +- .../MyStreamingRedisCredentialsProvider.java | 7 ++++- 7 files changed, 42 insertions(+), 41 deletions(-) delete mode 100644 src/main/java/io/lettuce/core/StreamingCredentialsProvider.java diff --git a/src/main/java/io/lettuce/core/ClientOptions.java b/src/main/java/io/lettuce/core/ClientOptions.java index 1248800a5..3fd635e4e 100644 --- a/src/main/java/io/lettuce/core/ClientOptions.java +++ b/src/main/java/io/lettuce/core/ClientOptions.java @@ -740,8 +740,8 @@ public TimeoutOptions getTimeoutOptions() { /** * Defines the re-authentication behavior of the Redis client. *

- * Certain implementations of the {@link RedisCredentialsProvider} such as the {@link StreamingCredentialsProvider} could - * emit new credentials at runtime. This setting controls how the driver reacts to these newly emitted credentials. + * Certain implementations of the {@link RedisCredentialsProvider} could emit new credentials at runtime. This setting + * controls how the driver reacts to these newly emitted credentials. */ public enum ReauthenticateBehavior { @@ -750,19 +750,23 @@ public enum ReauthenticateBehavior { * {@link RedisCredentialsProvider} only when the driver needs to, e.g. when the connection is first established or when * it is re-established after a disconnect. *

- * No re-authentication is performed when new credentials are emitted by the {@link StreamingCredentialsProvider} . + *

+ * No re-authentication is performed when new credentials are emitted by a {@link RedisCredentialsProvider} that + * supports streaming. The client does not subscribe to or react to any updates in the credential stream provided by + * {@link RedisCredentialsProvider#credentials()}. + *

*/ DEFAULT, /** - * Automatically triggers re-authentication whenever new credentials are emitted by any implementation of the - * {@link StreamingCredentialsProvider} interface. + * Automatically triggers re-authentication whenever new credentials are emitted by a {@link RedisCredentialsProvider} + * that supports streaming, as indicated by {@link RedisCredentialsProvider#supportsStreaming()}. * *

- * When enabled, the client subscribes to the credential stream provided by the {@link StreamingCredentialsProvider} and - * issues an {@code AUTH} command to the Redis server each time new credentials are received. This behavior supports - * dynamic credential scenarios, such as token-based authentication, or credential rotation where credentials are - * refreshed periodically to maintain access. + * When this behavior is enabled, the client subscribes to the credential stream provided by + * {@link RedisCredentialsProvider#credentials()} and issues an {@code AUTH} command to the Redis server each time new + * credentials are received. This behavior supports dynamic credential scenarios, such as token-based authentication, or + * credential rotation where credentials are refreshed periodically to maintain access. *

* *

diff --git a/src/main/java/io/lettuce/core/RedisAuthenticationHandler.java b/src/main/java/io/lettuce/core/RedisAuthenticationHandler.java index b2467002e..7a05a7a60 100644 --- a/src/main/java/io/lettuce/core/RedisAuthenticationHandler.java +++ b/src/main/java/io/lettuce/core/RedisAuthenticationHandler.java @@ -83,7 +83,7 @@ public RedisAuthenticationHandler(StatefulRedisConnectionImpl connection, * @return a new {@link RedisAuthenticationHandler} if the connection supports re-authentication, otherwise an * implementation of the {@link RedisAuthenticationHandler} that does nothing * @since 6.6.0 - * @see StreamingCredentialsProvider + * @see RedisCredentialsProvider */ public static RedisAuthenticationHandler createHandler(StatefulRedisConnectionImpl connection, RedisCredentialsProvider credentialsProvider, Boolean isPubSubConnection, ClientOptions options) { @@ -102,7 +102,7 @@ public static RedisAuthenticationHandler createHandler(StatefulRedi * * @return a new {@link RedisAuthenticationHandler} * @since 6.6.0 - * @see StreamingCredentialsProvider + * @see RedisCredentialsProvider */ public static RedisAuthenticationHandler createDefaultAuthenticationHandler() { return new DisabledAuthenticationHandler<>(); @@ -123,7 +123,7 @@ public void subscribe() { return; } - Flux credentialsFlux = ((StreamingCredentialsProvider) credentialsProvider).credentials(); + Flux credentialsFlux = credentialsProvider.credentials(); Disposable subscription = credentialsFlux.subscribe(this::onNext, this::onError, this::complete); diff --git a/src/main/java/io/lettuce/core/RedisCredentialsProvider.java b/src/main/java/io/lettuce/core/RedisCredentialsProvider.java index 556483edf..9c57a280a 100644 --- a/src/main/java/io/lettuce/core/RedisCredentialsProvider.java +++ b/src/main/java/io/lettuce/core/RedisCredentialsProvider.java @@ -2,6 +2,7 @@ import java.util.function.Supplier; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import io.lettuce.core.internal.LettuceAssert; @@ -52,6 +53,23 @@ default boolean supportsStreaming() { return false; } + /** + * Returns a {@link Flux} emitting {@link RedisCredentials} that can be used to authorize a Redis connection. + * + * For implementations that support streaming credentials (as indicated by {@link #supportsStreaming()} returning + * {@code true}), this method can emit multiple credentials over time, typically based on external events like token renewal + * or rotation. + * + * For implementations that do not support streaming credentials (where {@link #supportsStreaming()} returns {@code false}), + * this method throws an {@link UnsupportedOperationException} by default. + * + * @return a {@link Flux} emitting {@link RedisCredentials}, or throws an exception if streaming is not supported. + * @throws UnsupportedOperationException if the provider does not support streaming credentials. + */ + default Flux credentials() { + throw new UnsupportedOperationException("Streaming credentials are not supported by this provider."); + } + /** * Extension to {@link RedisCredentialsProvider} that resolves credentials immediately without the need to defer the * credential resolution. diff --git a/src/main/java/io/lettuce/core/StreamingCredentialsProvider.java b/src/main/java/io/lettuce/core/StreamingCredentialsProvider.java deleted file mode 100644 index 77a131f37..000000000 --- a/src/main/java/io/lettuce/core/StreamingCredentialsProvider.java +++ /dev/null @@ -1,26 +0,0 @@ -package io.lettuce.core; - -import reactor.core.publisher.Flux; - -/** - * A provider for streaming credentials that can be used to authorize a Redis connection and re-authenticate the connection when - * new credentials are received. - * - * @author Ivo Gaydajiev - * @since 6.6.0 - */ -public interface StreamingCredentialsProvider extends RedisCredentialsProvider { - - /** - * Returns a {@link Flux} emitting {@link RedisCredentials} that can be used to authorize a Redis connection. This - * credential provider supports streaming credentials, meaning that it can emit multiple credentials over time. - * - * @return - */ - Flux credentials(); - - default boolean supportsStreaming() { - return true; - } - -} diff --git a/src/main/java/io/lettuce/core/event/connection/ReauthenticationEvent.java b/src/main/java/io/lettuce/core/event/connection/ReauthenticationEvent.java index 745046dd9..bb9ab0bc0 100644 --- a/src/main/java/io/lettuce/core/event/connection/ReauthenticationEvent.java +++ b/src/main/java/io/lettuce/core/event/connection/ReauthenticationEvent.java @@ -11,7 +11,7 @@ * * @author Ivo Gaydajiev * @since 6.6.0 - * @see io.lettuce.core.StreamingCredentialsProvider + * @see io.lettuce.core.RedisCredentialsProvider */ public class ReauthenticationEvent implements AuthenticationEvent { diff --git a/src/main/java/io/lettuce/core/event/connection/ReauthenticationFailedEvent.java b/src/main/java/io/lettuce/core/event/connection/ReauthenticationFailedEvent.java index 4ee1e56dd..79164aa96 100644 --- a/src/main/java/io/lettuce/core/event/connection/ReauthenticationFailedEvent.java +++ b/src/main/java/io/lettuce/core/event/connection/ReauthenticationFailedEvent.java @@ -11,7 +11,7 @@ * * @author Ivo Gaydajiev * @since 6.6.0 - * @see io.lettuce.core.StreamingCredentialsProvider + * @see io.lettuce.core.RedisCredentialsProvider */ public class ReauthenticationFailedEvent implements AuthenticationEvent { diff --git a/src/test/java/io/lettuce/core/MyStreamingRedisCredentialsProvider.java b/src/test/java/io/lettuce/core/MyStreamingRedisCredentialsProvider.java index e5b0eaa93..12e9e37d1 100644 --- a/src/test/java/io/lettuce/core/MyStreamingRedisCredentialsProvider.java +++ b/src/test/java/io/lettuce/core/MyStreamingRedisCredentialsProvider.java @@ -10,10 +10,15 @@ * @author Ivo Gaydajiev * @since 6.6.0 */ -public class MyStreamingRedisCredentialsProvider implements StreamingCredentialsProvider { +public class MyStreamingRedisCredentialsProvider implements RedisCredentialsProvider { private final Sinks.Many credentialsSink = Sinks.many().replay().latest(); + @Override + public boolean supportsStreaming() { + return true; + } + @Override public Mono resolveCredentials() {