|
1 | 1 | package redis.clients.authentication;
|
2 | 2 |
|
3 |
| -import static org.junit.Assert.assertNotNull; |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import java.util.UUID; |
4 | 5 |
|
5 |
| -import java.net.MalformedURLException; |
| 6 | +import org.junit.BeforeClass; |
6 | 7 | import org.junit.Test;
|
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
7 | 10 |
|
8 |
| -import redis.clients.authentication.core.Token; |
9 |
| -import redis.clients.authentication.entraid.EntraIDIdentityProvider; |
10 |
| -import redis.clients.authentication.entraid.ServicePrincipalInfo; |
| 11 | +import redis.clients.authentication.core.TokenAuthConfig; |
| 12 | +import redis.clients.authentication.entraid.EntraIDTokenAuthConfigBuilder; |
| 13 | +import redis.clients.authentication.entraid.ManagedIdentityInfo.UserManagedIdentityType; |
| 14 | +import redis.clients.jedis.DefaultJedisClientConfig; |
| 15 | +import redis.clients.jedis.HostAndPort; |
| 16 | +import redis.clients.jedis.JedisPooled; |
11 | 17 |
|
12 | 18 | public class RedisEntraIDIntegrationTests {
|
| 19 | + private static final Logger log = LoggerFactory |
| 20 | + .getLogger(RedisEntraIDIntegrationTests.class); |
13 | 21 |
|
| 22 | + private static TestContext testCtx; |
| 23 | + private static EndpointConfig endpointConfig; |
| 24 | + private static HostAndPort hnp; |
| 25 | + |
| 26 | + @BeforeClass |
| 27 | + public static void before() { |
| 28 | + try { |
| 29 | + testCtx = TestContext.DEFAULT; |
| 30 | + endpointConfig = testCtx.getRedisEndpoint("standalone-entraid-acl1"); |
| 31 | + hnp = endpointConfig.getHostAndPort(); |
| 32 | + } catch (IllegalArgumentException e) { |
| 33 | + log.warn("Skipping test because no Redis endpoint is configured"); |
| 34 | + org.junit.Assume.assumeTrue(false); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // T.1.1 |
| 39 | + // Verify authentication using Azure AD with managed identities |
| 40 | + @Test |
| 41 | + public void withUserAssignedId_azureManagedIdentityIntegrationTest() { |
| 42 | + TokenAuthConfig tokenAuthConfig = EntraIDTokenAuthConfigBuilder.builder() |
| 43 | + .clientId(testCtx.getClientId()) |
| 44 | + .userAssignedManagedIdentity(UserManagedIdentityType.CLIENT_ID, |
| 45 | + "userManagedAuthxId") |
| 46 | + .authority(testCtx.getAuthority()).scopes(testCtx.getRedisScopes()) |
| 47 | + .build(); |
| 48 | + |
| 49 | + DefaultJedisClientConfig jedisConfig = DefaultJedisClientConfig.builder() |
| 50 | + .tokenAuthConfig(tokenAuthConfig).build(); |
| 51 | + |
| 52 | + try (JedisPooled jedis = new JedisPooled(hnp, jedisConfig)) { |
| 53 | + String key = UUID.randomUUID().toString(); |
| 54 | + jedis.set(key, "value"); |
| 55 | + assertEquals("value", jedis.get(key)); |
| 56 | + jedis.del(key); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // T.1.1 |
| 61 | + // Verify authentication using Azure AD with managed identities |
14 | 62 | @Test
|
15 |
| - public void requestTokenWithSecret() throws MalformedURLException { |
16 |
| - TestContext testCtx = TestContext.DEFAULT; |
| 63 | + public void withSystemAssignedId_azureManagedIdentityIntegrationTest() { |
| 64 | + TokenAuthConfig tokenAuthConfig = EntraIDTokenAuthConfigBuilder.builder() |
| 65 | + .clientId(testCtx.getClientId()).systemAssignedManagedIdentity() |
| 66 | + .authority(testCtx.getAuthority()).scopes(testCtx.getRedisScopes()) |
| 67 | + .build(); |
17 | 68 |
|
18 |
| - Token token = new EntraIDIdentityProvider( |
19 |
| - new ServicePrincipalInfo(testCtx.getClientId(), |
20 |
| - testCtx.getClientSecret(), testCtx.getAuthority()), |
21 |
| - testCtx.getRedisScopes()).requestToken(); |
| 69 | + DefaultJedisClientConfig jedisConfig = DefaultJedisClientConfig.builder() |
| 70 | + .tokenAuthConfig(tokenAuthConfig).build(); |
22 | 71 |
|
23 |
| - assertNotNull(token.getValue()); |
| 72 | + try (JedisPooled jedis = new JedisPooled(hnp, jedisConfig)) { |
| 73 | + String key = UUID.randomUUID().toString(); |
| 74 | + jedis.set(key, "value"); |
| 75 | + assertEquals("value", jedis.get(key)); |
| 76 | + jedis.del(key); |
| 77 | + } |
24 | 78 | }
|
25 | 79 |
|
| 80 | + // T.1.1 |
| 81 | + // Verify authentication using Azure AD with service principals |
26 | 82 | @Test
|
27 |
| - public void requestTokenWithCert() throws MalformedURLException { |
28 |
| - TestContext testCtx = TestContext.DEFAULT; |
| 83 | + public void withSecret_azureServicePrincipalIntegrationTest() { |
| 84 | + TokenAuthConfig tokenAuthConfig = EntraIDTokenAuthConfigBuilder.builder() |
| 85 | + .clientId(testCtx.getClientId()).secret(testCtx.getClientSecret()) |
| 86 | + .authority(testCtx.getAuthority()).scopes(testCtx.getRedisScopes()) |
| 87 | + .build(); |
29 | 88 |
|
30 |
| - Token token = new EntraIDIdentityProvider(new ServicePrincipalInfo( |
31 |
| - testCtx.getClientId(), testCtx.getPrivateKey(), testCtx.getCert(), |
32 |
| - testCtx.getAuthority()), testCtx.getRedisScopes()).requestToken(); |
| 89 | + DefaultJedisClientConfig jedisConfig = DefaultJedisClientConfig.builder() |
| 90 | + .tokenAuthConfig(tokenAuthConfig).build(); |
33 | 91 |
|
34 |
| - assertNotNull(token.getValue()); |
| 92 | + try (JedisPooled jedis = new JedisPooled(hnp, jedisConfig)) { |
| 93 | + String key = UUID.randomUUID().toString(); |
| 94 | + jedis.set(key, "value"); |
| 95 | + assertEquals("value", jedis.get(key)); |
| 96 | + jedis.del(key); |
| 97 | + } |
35 | 98 | }
|
| 99 | + |
| 100 | + // T.1.1 |
| 101 | + // Verify authentication using Azure AD with service principals |
| 102 | + @Test |
| 103 | + public void withCertificate_azureServicePrincipalIntegrationTest() { |
| 104 | + TokenAuthConfig tokenAuthConfig = EntraIDTokenAuthConfigBuilder.builder() |
| 105 | + .clientId(testCtx.getClientId()).secret(testCtx.getClientSecret()) |
| 106 | + .authority(testCtx.getAuthority()).scopes(testCtx.getRedisScopes()) |
| 107 | + .build(); |
| 108 | + |
| 109 | + DefaultJedisClientConfig jedisConfig = DefaultJedisClientConfig.builder() |
| 110 | + .tokenAuthConfig(tokenAuthConfig).build(); |
| 111 | + |
| 112 | + try (JedisPooled jedis = new JedisPooled(hnp, jedisConfig)) { |
| 113 | + String key = UUID.randomUUID().toString(); |
| 114 | + jedis.set(key, "value"); |
| 115 | + assertEquals("value", jedis.get(key)); |
| 116 | + jedis.del(key); |
| 117 | + } |
| 118 | + } |
| 119 | + |
36 | 120 | }
|
0 commit comments