Skip to content

Commit ced3c82

Browse files
committed
- remove doctest
- add unit and integration tests - add executor to shutdown in TokenManager (review from Ivo)
1 parent cbae935 commit ced3c82

File tree

7 files changed

+491
-158
lines changed

7 files changed

+491
-158
lines changed

.github/workflows/doctests.yml

Lines changed: 0 additions & 38 deletions
This file was deleted.

core/src/main/java/redis/clients/authentication/core/TokenManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.util.concurrent.ScheduledExecutorService;
88
import java.util.concurrent.ScheduledFuture;
99
import java.util.concurrent.TimeUnit;
10-
import java.util.concurrent.TimeoutException;
1110
import java.util.concurrent.atomic.AtomicBoolean;
1211

1312
import org.slf4j.Logger;
@@ -58,6 +57,7 @@ public void stop() {
5857
stopped = true;
5958
scheduledTask.cancel(true);
6059
scheduler.shutdown();
60+
executor.shutdown();
6161
}
6262

6363
public TokenManagerConfig getConfig() {

core/src/test/java/redis/clients/authentication/CoreAuthenticationUnitTests.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ public void testBlockForInitialToken() {
162162
TokenRequestException e = assertThrows(TokenRequestException.class,
163163
() -> tokenManager.start(mock(TokenListener.class), true));
164164

165-
assertEquals("Test exception from identity provider!",
166-
e.getCause().getCause().getMessage());
165+
assertEquals("Test exception from identity provider!", e.getCause().getCause().getMessage());
167166
}
168167

169168
@Test
@@ -220,7 +219,7 @@ public void testTokenManagerWithFailingTokenRequest()
220219
@Test
221220
public void testTokenManagerWithHangingTokenRequest()
222221
throws InterruptedException, ExecutionException, TimeoutException {
223-
int sleepDuration = 200;
222+
int delayDuration = 200;
224223
int executionTimeout = 100;
225224
int tokenLifetime = 50 * 1000;
226225
int numberOfRetries = 5;
@@ -229,11 +228,7 @@ public void testTokenManagerWithHangingTokenRequest()
229228
IdentityProvider identityProvider = () -> {
230229
requesLatch.countDown();
231230
if (requesLatch.getCount() > 0) {
232-
try {
233-
Thread.sleep(sleepDuration);
234-
} catch (InterruptedException e) {
235-
}
236-
return null;
231+
delay(delayDuration);
237232
}
238233
return new SimpleToken("tokenValX", System.currentTimeMillis() + tokenLifetime,
239234
System.currentTimeMillis(), Collections.singletonMap("oid", "user1"));
@@ -250,4 +245,11 @@ public void testTokenManagerWithHangingTokenRequest()
250245
verify(listener, times(1)).onTokenRenewed(any());
251246
});
252247
}
248+
249+
private void delay(long durationInMs) {
250+
try {
251+
Thread.sleep(durationInMs);
252+
} catch (InterruptedException e) {
253+
}
254+
}
253255
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package redis.clients.authentication;
2+
3+
import static org.junit.Assert.assertNotNull;
4+
5+
import java.net.MalformedURLException;
6+
import org.junit.Test;
7+
import redis.clients.authentication.core.Token;
8+
import redis.clients.authentication.entraid.EntraIDIdentityProvider;
9+
import redis.clients.authentication.entraid.ServicePrincipalInfo;
10+
11+
public class EntraIDIntegrationTests {
12+
13+
14+
@Test
15+
public void requestTokenWithSecret() throws MalformedURLException {
16+
TestContext testCtx = TestContext.DEFAULT;
17+
ServicePrincipalInfo servicePrincipalInfo = new ServicePrincipalInfo(
18+
testCtx.getClientId(), testCtx.getClientSecret(),
19+
testCtx.getAuthority());
20+
Token token = new EntraIDIdentityProvider(servicePrincipalInfo,
21+
testCtx.getRedisScopes()).requestToken();
22+
23+
assertNotNull(token.getValue());
24+
}
25+
26+
@Test
27+
public void requestTokenWithCert() throws MalformedURLException {
28+
TestContext testCtx = TestContext.DEFAULT;
29+
ServicePrincipalInfo servicePrincipalInfo = new ServicePrincipalInfo(
30+
testCtx.getClientId(), testCtx.getPrivateKey(), testCtx.getCert(),
31+
testCtx.getAuthority());
32+
Token token = new EntraIDIdentityProvider(servicePrincipalInfo,
33+
testCtx.getRedisScopes()).requestToken();
34+
assertNotNull(token.getValue());
35+
}
36+
37+
}
Lines changed: 102 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,120 @@
11
package redis.clients.authentication;
22

3-
import static org.junit.Assert.assertNotNull;
3+
import static org.junit.Assert.assertEquals;
4+
import java.util.UUID;
45

5-
import java.net.MalformedURLException;
6+
import org.junit.BeforeClass;
67
import org.junit.Test;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
710

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;
1117

1218
public class RedisEntraIDIntegrationTests {
19+
private static final Logger log = LoggerFactory
20+
.getLogger(RedisEntraIDIntegrationTests.class);
1321

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
1462
@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();
1768

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();
2271

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+
}
2478
}
2579

80+
// T.1.1
81+
// Verify authentication using Azure AD with service principals
2682
@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();
2988

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();
3391

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+
}
3598
}
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+
36120
}

0 commit comments

Comments
 (0)