Skip to content

Commit

Permalink
Remove StreamingCredentialsProvider interface.
Browse files Browse the repository at this point in the history
move credentials() method to RedisCredentialsProvider.

Resolve issue with unsafe cast after extending RedisCredentialsProvider with supportsStreaming() method
  • Loading branch information
ggivo committed Dec 18, 2024
1 parent 8e9ab48 commit 746dd82
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 41 deletions.
22 changes: 13 additions & 9 deletions src/main/java/io/lettuce/core/ClientOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -740,8 +740,8 @@ public TimeoutOptions getTimeoutOptions() {
/**
* Defines the re-authentication behavior of the Redis client.
* <p/>
* 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 {

Expand All @@ -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.
* <p/>
* No re-authentication is performed when new credentials are emitted by the {@link StreamingCredentialsProvider} .
* <p>
* 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()}.
* </p>
*/
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()}.
*
* <p>
* 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.
* </p>
*
* <p>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/lettuce/core/RedisAuthenticationHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public RedisAuthenticationHandler(StatefulRedisConnectionImpl<K, V> 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 <K, V> RedisAuthenticationHandler<K, V> createHandler(StatefulRedisConnectionImpl<K, V> connection,
RedisCredentialsProvider credentialsProvider, Boolean isPubSubConnection, ClientOptions options) {
Expand All @@ -102,7 +102,7 @@ public static <K, V> RedisAuthenticationHandler<K, V> createHandler(StatefulRedi
*
* @return a new {@link RedisAuthenticationHandler}
* @since 6.6.0
* @see StreamingCredentialsProvider
* @see RedisCredentialsProvider
*/
public static <K, V> RedisAuthenticationHandler<K, V> createDefaultAuthenticationHandler() {
return new DisabledAuthenticationHandler<>();
Expand All @@ -123,7 +123,7 @@ public void subscribe() {
return;
}

Flux<RedisCredentials> credentialsFlux = ((StreamingCredentialsProvider) credentialsProvider).credentials();
Flux<RedisCredentials> credentialsFlux = credentialsProvider.credentials();

Disposable subscription = credentialsFlux.subscribe(this::onNext, this::onError, this::complete);

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/io/lettuce/core/RedisCredentialsProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<RedisCredentials> 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.
Expand Down
26 changes: 0 additions & 26 deletions src/main/java/io/lettuce/core/StreamingCredentialsProvider.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RedisCredentials> credentialsSink = Sinks.many().replay().latest();

@Override
public boolean supportsStreaming() {
return true;
}

@Override
public Mono<RedisCredentials> resolveCredentials() {

Expand Down

0 comments on commit 746dd82

Please sign in to comment.