Skip to content

Commit

Permalink
- remove doctest
Browse files Browse the repository at this point in the history
- add unit and integration tests
- add executor to shutdown in TokenManager (review from Ivo)
  • Loading branch information
atakavci committed Nov 29, 2024
1 parent cbae935 commit ced3c82
Show file tree
Hide file tree
Showing 7 changed files with 491 additions and 158 deletions.
38 changes: 0 additions & 38 deletions .github/workflows/doctests.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
Expand Down Expand Up @@ -58,6 +57,7 @@ public void stop() {
stopped = true;
scheduledTask.cancel(true);
scheduler.shutdown();
executor.shutdown();
}

public TokenManagerConfig getConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ public void testBlockForInitialToken() {
TokenRequestException e = assertThrows(TokenRequestException.class,
() -> tokenManager.start(mock(TokenListener.class), true));

assertEquals("Test exception from identity provider!",
e.getCause().getCause().getMessage());
assertEquals("Test exception from identity provider!", e.getCause().getCause().getMessage());
}

@Test
Expand Down Expand Up @@ -220,7 +219,7 @@ public void testTokenManagerWithFailingTokenRequest()
@Test
public void testTokenManagerWithHangingTokenRequest()
throws InterruptedException, ExecutionException, TimeoutException {
int sleepDuration = 200;
int delayDuration = 200;
int executionTimeout = 100;
int tokenLifetime = 50 * 1000;
int numberOfRetries = 5;
Expand All @@ -229,11 +228,7 @@ public void testTokenManagerWithHangingTokenRequest()
IdentityProvider identityProvider = () -> {
requesLatch.countDown();
if (requesLatch.getCount() > 0) {
try {
Thread.sleep(sleepDuration);
} catch (InterruptedException e) {
}
return null;
delay(delayDuration);
}
return new SimpleToken("tokenValX", System.currentTimeMillis() + tokenLifetime,
System.currentTimeMillis(), Collections.singletonMap("oid", "user1"));
Expand All @@ -250,4 +245,11 @@ public void testTokenManagerWithHangingTokenRequest()
verify(listener, times(1)).onTokenRenewed(any());
});
}

private void delay(long durationInMs) {
try {
Thread.sleep(durationInMs);
} catch (InterruptedException e) {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package redis.clients.authentication;

import static org.junit.Assert.assertNotNull;

import java.net.MalformedURLException;
import org.junit.Test;
import redis.clients.authentication.core.Token;
import redis.clients.authentication.entraid.EntraIDIdentityProvider;
import redis.clients.authentication.entraid.ServicePrincipalInfo;

public class EntraIDIntegrationTests {


@Test
public void requestTokenWithSecret() throws MalformedURLException {
TestContext testCtx = TestContext.DEFAULT;
ServicePrincipalInfo servicePrincipalInfo = new ServicePrincipalInfo(
testCtx.getClientId(), testCtx.getClientSecret(),
testCtx.getAuthority());
Token token = new EntraIDIdentityProvider(servicePrincipalInfo,
testCtx.getRedisScopes()).requestToken();

assertNotNull(token.getValue());
}

@Test
public void requestTokenWithCert() throws MalformedURLException {
TestContext testCtx = TestContext.DEFAULT;
ServicePrincipalInfo servicePrincipalInfo = new ServicePrincipalInfo(
testCtx.getClientId(), testCtx.getPrivateKey(), testCtx.getCert(),
testCtx.getAuthority());
Token token = new EntraIDIdentityProvider(servicePrincipalInfo,
testCtx.getRedisScopes()).requestToken();
assertNotNull(token.getValue());
}

}
Original file line number Diff line number Diff line change
@@ -1,36 +1,120 @@
package redis.clients.authentication;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import java.util.UUID;

import java.net.MalformedURLException;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.authentication.core.Token;
import redis.clients.authentication.entraid.EntraIDIdentityProvider;
import redis.clients.authentication.entraid.ServicePrincipalInfo;
import redis.clients.authentication.core.TokenAuthConfig;
import redis.clients.authentication.entraid.EntraIDTokenAuthConfigBuilder;
import redis.clients.authentication.entraid.ManagedIdentityInfo.UserManagedIdentityType;
import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisPooled;

public class RedisEntraIDIntegrationTests {
private static final Logger log = LoggerFactory
.getLogger(RedisEntraIDIntegrationTests.class);

private static TestContext testCtx;
private static EndpointConfig endpointConfig;
private static HostAndPort hnp;

@BeforeClass
public static void before() {
try {
testCtx = TestContext.DEFAULT;
endpointConfig = testCtx.getRedisEndpoint("standalone-entraid-acl1");
hnp = endpointConfig.getHostAndPort();
} catch (IllegalArgumentException e) {
log.warn("Skipping test because no Redis endpoint is configured");
org.junit.Assume.assumeTrue(false);
}
}

// T.1.1
// Verify authentication using Azure AD with managed identities
@Test
public void withUserAssignedId_azureManagedIdentityIntegrationTest() {
TokenAuthConfig tokenAuthConfig = EntraIDTokenAuthConfigBuilder.builder()
.clientId(testCtx.getClientId())
.userAssignedManagedIdentity(UserManagedIdentityType.CLIENT_ID,
"userManagedAuthxId")
.authority(testCtx.getAuthority()).scopes(testCtx.getRedisScopes())
.build();

DefaultJedisClientConfig jedisConfig = DefaultJedisClientConfig.builder()
.tokenAuthConfig(tokenAuthConfig).build();

try (JedisPooled jedis = new JedisPooled(hnp, jedisConfig)) {
String key = UUID.randomUUID().toString();
jedis.set(key, "value");
assertEquals("value", jedis.get(key));
jedis.del(key);
}
}

// T.1.1
// Verify authentication using Azure AD with managed identities
@Test
public void requestTokenWithSecret() throws MalformedURLException {
TestContext testCtx = TestContext.DEFAULT;
public void withSystemAssignedId_azureManagedIdentityIntegrationTest() {
TokenAuthConfig tokenAuthConfig = EntraIDTokenAuthConfigBuilder.builder()
.clientId(testCtx.getClientId()).systemAssignedManagedIdentity()
.authority(testCtx.getAuthority()).scopes(testCtx.getRedisScopes())
.build();

Token token = new EntraIDIdentityProvider(
new ServicePrincipalInfo(testCtx.getClientId(),
testCtx.getClientSecret(), testCtx.getAuthority()),
testCtx.getRedisScopes()).requestToken();
DefaultJedisClientConfig jedisConfig = DefaultJedisClientConfig.builder()
.tokenAuthConfig(tokenAuthConfig).build();

assertNotNull(token.getValue());
try (JedisPooled jedis = new JedisPooled(hnp, jedisConfig)) {
String key = UUID.randomUUID().toString();
jedis.set(key, "value");
assertEquals("value", jedis.get(key));
jedis.del(key);
}
}

// T.1.1
// Verify authentication using Azure AD with service principals
@Test
public void requestTokenWithCert() throws MalformedURLException {
TestContext testCtx = TestContext.DEFAULT;
public void withSecret_azureServicePrincipalIntegrationTest() {
TokenAuthConfig tokenAuthConfig = EntraIDTokenAuthConfigBuilder.builder()
.clientId(testCtx.getClientId()).secret(testCtx.getClientSecret())
.authority(testCtx.getAuthority()).scopes(testCtx.getRedisScopes())
.build();

Token token = new EntraIDIdentityProvider(new ServicePrincipalInfo(
testCtx.getClientId(), testCtx.getPrivateKey(), testCtx.getCert(),
testCtx.getAuthority()), testCtx.getRedisScopes()).requestToken();
DefaultJedisClientConfig jedisConfig = DefaultJedisClientConfig.builder()
.tokenAuthConfig(tokenAuthConfig).build();

assertNotNull(token.getValue());
try (JedisPooled jedis = new JedisPooled(hnp, jedisConfig)) {
String key = UUID.randomUUID().toString();
jedis.set(key, "value");
assertEquals("value", jedis.get(key));
jedis.del(key);
}
}

// T.1.1
// Verify authentication using Azure AD with service principals
@Test
public void withCertificate_azureServicePrincipalIntegrationTest() {
TokenAuthConfig tokenAuthConfig = EntraIDTokenAuthConfigBuilder.builder()
.clientId(testCtx.getClientId()).secret(testCtx.getClientSecret())
.authority(testCtx.getAuthority()).scopes(testCtx.getRedisScopes())
.build();

DefaultJedisClientConfig jedisConfig = DefaultJedisClientConfig.builder()
.tokenAuthConfig(tokenAuthConfig).build();

try (JedisPooled jedis = new JedisPooled(hnp, jedisConfig)) {
String key = UUID.randomUUID().toString();
jedis.set(key, "value");
assertEquals("value", jedis.get(key));
jedis.del(key);
}
}

}
Loading

0 comments on commit ced3c82

Please sign in to comment.