|
| 1 | +package io.lettuce.authx; |
| 2 | + |
| 3 | +import com.azure.identity.DefaultAzureCredential; |
| 4 | +import com.azure.identity.DefaultAzureCredentialBuilder; |
| 5 | +import io.lettuce.core.ClientOptions; |
| 6 | +import io.lettuce.core.RedisClient; |
| 7 | +import io.lettuce.core.RedisCredentials; |
| 8 | +import io.lettuce.core.RedisURI; |
| 9 | +import io.lettuce.core.SocketOptions; |
| 10 | +import io.lettuce.core.TimeoutOptions; |
| 11 | +import io.lettuce.core.api.StatefulRedisConnection; |
| 12 | +import io.lettuce.core.api.sync.RedisCommands; |
| 13 | +import io.lettuce.test.env.Endpoints; |
| 14 | +import io.lettuce.test.env.Endpoints.Endpoint; |
| 15 | +import org.junit.jupiter.api.AfterEach; |
| 16 | +import org.junit.jupiter.api.Assumptions; |
| 17 | +import org.junit.jupiter.api.BeforeEach; |
| 18 | +import org.junit.jupiter.api.Tag; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import redis.clients.authentication.core.TokenAuthConfig; |
| 21 | +import redis.clients.authentication.entraid.AzureTokenAuthConfigBuilder; |
| 22 | + |
| 23 | +import java.time.Duration; |
| 24 | +import java.util.UUID; |
| 25 | +import java.util.concurrent.ExecutionException; |
| 26 | + |
| 27 | +import static io.lettuce.TestTags.ENTRA_ID; |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 30 | + |
| 31 | +@Tag(ENTRA_ID) |
| 32 | +public class DefaultAzureCredentialsIntegrationTests { |
| 33 | + |
| 34 | + private static final EntraIdTestContext testCtx = EntraIdTestContext.DEFAULT; |
| 35 | + |
| 36 | + private RedisClient client; |
| 37 | + |
| 38 | + private Endpoint standalone; |
| 39 | + |
| 40 | + private ClientOptions clientOptions; |
| 41 | + |
| 42 | + private TokenBasedRedisCredentialsProvider credentialsProvider; |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + public void setup() { |
| 46 | + standalone = Endpoints.DEFAULT.getEndpoint("standalone-entraid-acl"); |
| 47 | + assumeTrue(standalone != null, "Skipping EntraID tests. Redis host with enabled EntraId not provided!"); |
| 48 | + Assumptions.assumeTrue(testCtx.getClientId() != null && testCtx.getClientSecret() != null, |
| 49 | + "Skipping EntraID tests. Azure AD credentials not provided!"); |
| 50 | + |
| 51 | + clientOptions = ClientOptions.builder() |
| 52 | + .socketOptions(SocketOptions.builder().connectTimeout(Duration.ofSeconds(1)).build()) |
| 53 | + .timeoutOptions(TimeoutOptions.enabled(Duration.ofSeconds(1))) |
| 54 | + .reauthenticateBehavior(ClientOptions.ReauthenticateBehavior.ON_NEW_CREDENTIALS).build(); |
| 55 | + |
| 56 | + DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build(); |
| 57 | + |
| 58 | + TokenAuthConfig tokenAuthConfig = AzureTokenAuthConfigBuilder.builder().defaultAzureCredential(credential) |
| 59 | + .tokenRequestExecTimeoutInMs(2000).build(); |
| 60 | + |
| 61 | + credentialsProvider = TokenBasedRedisCredentialsProvider.create(tokenAuthConfig); |
| 62 | + |
| 63 | + client = createClient(credentialsProvider); |
| 64 | + } |
| 65 | + |
| 66 | + @AfterEach |
| 67 | + public void cleanUp() { |
| 68 | + if (credentialsProvider != null) { |
| 69 | + credentialsProvider.close(); |
| 70 | + } |
| 71 | + if (client != null) { |
| 72 | + client.shutdown(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void azureTokenAuthWithDefaultAzureCredentials() throws ExecutionException, InterruptedException { |
| 78 | + |
| 79 | + RedisCredentials credentials = credentialsProvider.resolveCredentials().block(Duration.ofSeconds(5)); |
| 80 | + assertThat(credentials).isNotNull(); |
| 81 | + |
| 82 | + String key = UUID.randomUUID().toString(); |
| 83 | + try (StatefulRedisConnection<String, String> connection = client.connect()) { |
| 84 | + RedisCommands<String, String> sync = connection.sync(); |
| 85 | + assertThat(sync.aclWhoami()).isEqualTo(credentials.getUsername()); |
| 86 | + sync.set(key, "value"); |
| 87 | + assertThat(sync.get(key)).isEqualTo("value"); |
| 88 | + assertThat(connection.async().get(key).get()).isEqualTo("value"); |
| 89 | + assertThat(connection.reactive().get(key).block()).isEqualTo("value"); |
| 90 | + sync.del(key); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private RedisClient createClient(TokenBasedRedisCredentialsProvider credentialsProvider) { |
| 95 | + RedisURI uri = RedisURI.create((standalone.getEndpoints().get(0))); |
| 96 | + uri.setCredentialsProvider(credentialsProvider); |
| 97 | + RedisClient redis = RedisClient.create(uri); |
| 98 | + redis.setOptions(clientOptions); |
| 99 | + return redis; |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments