|
| 1 | +package io.lettuce.authx; |
| 2 | + |
| 3 | +import io.lettuce.core.ClientOptions; |
| 4 | +import io.lettuce.core.RedisURI; |
| 5 | +import io.lettuce.core.SocketOptions; |
| 6 | +import io.lettuce.core.TimeoutOptions; |
| 7 | +import io.lettuce.core.api.StatefulRedisConnection; |
| 8 | +import io.lettuce.core.cluster.ClusterClientOptions; |
| 9 | +import io.lettuce.core.cluster.ClusterTopologyRefreshOptions; |
| 10 | +import io.lettuce.core.cluster.RedisClusterClient; |
| 11 | +import io.lettuce.core.cluster.api.StatefulRedisClusterConnection; |
| 12 | +import io.lettuce.core.cluster.api.sync.RedisAdvancedClusterCommands; |
| 13 | +import io.lettuce.core.resource.ClientResources; |
| 14 | +import io.lettuce.core.resource.DnsResolver; |
| 15 | +import io.lettuce.test.env.Endpoints; |
| 16 | +import io.lettuce.test.env.Endpoints.Endpoint; |
| 17 | +import org.junit.jupiter.api.AfterAll; |
| 18 | +import org.junit.jupiter.api.Assumptions; |
| 19 | +import org.junit.jupiter.api.BeforeAll; |
| 20 | +import org.junit.jupiter.api.Tag; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import redis.clients.authentication.core.TokenAuthConfig; |
| 23 | +import redis.clients.authentication.entraid.EntraIDTokenAuthConfigBuilder; |
| 24 | + |
| 25 | +import java.time.Duration; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.UUID; |
| 31 | +import java.util.concurrent.ExecutionException; |
| 32 | + |
| 33 | +import static io.lettuce.TestTags.ENTRA_ID; |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | +import static org.assertj.core.api.Assertions.fail; |
| 36 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 37 | + |
| 38 | +@Tag(ENTRA_ID) |
| 39 | +public class EntraIdClusterIntegrationTests { |
| 40 | + |
| 41 | + private static final EntraIdTestContext testCtx = EntraIdTestContext.DEFAULT; |
| 42 | + |
| 43 | + private static TokenBasedRedisCredentialsProvider credentialsProvider; |
| 44 | + |
| 45 | + private static RedisClusterClient clusterClient; |
| 46 | + |
| 47 | + private static ClientResources resources; |
| 48 | + |
| 49 | + private static Endpoint cluster; |
| 50 | + |
| 51 | + @BeforeAll |
| 52 | + public static void setup() { |
| 53 | + cluster = Endpoints.DEFAULT.getEndpoint("cluster-entraid-acl"); |
| 54 | + if (cluster != null) { |
| 55 | + Assumptions.assumeTrue(testCtx.getClientId() != null && testCtx.getClientSecret() != null, |
| 56 | + "Skipping EntraID tests. Azure AD credentials not provided!"); |
| 57 | + |
| 58 | + // Configure timeout options to assure fast test failover |
| 59 | + ClusterClientOptions clientOptions = ClusterClientOptions.builder() |
| 60 | + .socketOptions(SocketOptions.builder().connectTimeout(Duration.ofSeconds(1)).build()) |
| 61 | + .timeoutOptions(TimeoutOptions.enabled(Duration.ofSeconds(1))) |
| 62 | + // enable re-authentication |
| 63 | + .reauthenticateBehavior(ClientOptions.ReauthenticateBehavior.ON_NEW_CREDENTIALS).build(); |
| 64 | + |
| 65 | + TokenAuthConfig tokenAuthConfig = EntraIDTokenAuthConfigBuilder.builder().clientId(testCtx.getClientId()) |
| 66 | + .secret(testCtx.getClientSecret()).authority(testCtx.getAuthority()).scopes(testCtx.getRedisScopes()) |
| 67 | + .expirationRefreshRatio(0.0000001F).build(); |
| 68 | + |
| 69 | + credentialsProvider = TokenBasedRedisCredentialsProvider.create(tokenAuthConfig); |
| 70 | + |
| 71 | + resources = ClientResources.builder() |
| 72 | + // .dnsResolver(DnsResolver.jvmDefault()) |
| 73 | + .build(); |
| 74 | + |
| 75 | + List<RedisURI> seedURI = new ArrayList<>(); |
| 76 | + for (String addr : cluster.getRawEndpoints().get(0).getAddr()) { |
| 77 | + seedURI.add(RedisURI.builder().withAuthentication(credentialsProvider).withHost(addr) |
| 78 | + .withPort(cluster.getRawEndpoints().get(0).getPort()).build()); |
| 79 | + } |
| 80 | + |
| 81 | + clusterClient = RedisClusterClient.create(resources, seedURI); |
| 82 | + clusterClient.setOptions(clientOptions); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @AfterAll |
| 87 | + public static void cleanup() { |
| 88 | + if (credentialsProvider != null) { |
| 89 | + credentialsProvider.close(); |
| 90 | + } |
| 91 | + if (resources != null) { |
| 92 | + resources.shutdown(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // T.1.1 |
| 97 | + // Verify authentication using Azure AD with service principals using Redis Cluster Client |
| 98 | + @Test |
| 99 | + public void clusterWithSecret_azureServicePrincipalIntegrationTest() throws ExecutionException, InterruptedException { |
| 100 | + assumeTrue(cluster != null, "Skipping EntraID tests. Redis host with enabled EntraId not provided!"); |
| 101 | + |
| 102 | + try (StatefulRedisClusterConnection<String, String> defaultConnection = clusterClient.connect()) { |
| 103 | + RedisAdvancedClusterCommands<String, String> sync = defaultConnection.sync(); |
| 104 | + String keyPrefix = UUID.randomUUID().toString(); |
| 105 | + Map<String, String> mset = prepareMset(keyPrefix); |
| 106 | + |
| 107 | + assertThat(sync.mset(mset)).isEqualTo("OK"); |
| 108 | + |
| 109 | + for (String mykey : mset.keySet()) { |
| 110 | + assertThat(defaultConnection.sync().get(mykey)).isEqualTo("value-" + mykey); |
| 111 | + assertThat(defaultConnection.async().get(mykey).get()).isEqualTo("value-" + mykey); |
| 112 | + assertThat(defaultConnection.reactive().get(mykey).block()).isEqualTo("value-" + mykey); |
| 113 | + } |
| 114 | + assertThat(sync.del(mset.keySet().toArray(new String[0]))).isEqualTo(mset.keySet().size()); |
| 115 | + |
| 116 | + // Test connections to each node |
| 117 | + defaultConnection.getPartitions().forEach((partition) -> { |
| 118 | + StatefulRedisConnection<?, ?> nodeConnection = defaultConnection.getConnection(partition.getNodeId()); |
| 119 | + assertThat(nodeConnection.sync().ping()).isEqualTo("PONG"); |
| 120 | + }); |
| 121 | + |
| 122 | + defaultConnection.getPartitions().forEach((partition) -> { |
| 123 | + StatefulRedisConnection<?, ?> nodeConnection = defaultConnection.getConnection(partition.getUri().getHost(), |
| 124 | + partition.getUri().getPort()); |
| 125 | + assertThat(nodeConnection.sync().ping()).isEqualTo("PONG"); |
| 126 | + }); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + Map<String, String> prepareMset(String keyPrefix) { |
| 131 | + Map<String, String> mset = new HashMap<>(); |
| 132 | + for (char c = 'a'; c <= 'z'; c++) { |
| 133 | + String keySuffix = new String(new char[] { c, c, c }); // Generates "aaa", "bbb", etc. |
| 134 | + String key = String.format("%s-{%s}", keyPrefix, keySuffix); |
| 135 | + mset.put(key, "value-" + key); |
| 136 | + } |
| 137 | + return mset; |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments