diff --git a/core/src/main/java/redis/clients/authentication/core/TokenManager.java b/core/src/main/java/redis/clients/authentication/core/TokenManager.java index ddda5d5..46e4b3e 100644 --- a/core/src/main/java/redis/clients/authentication/core/TokenManager.java +++ b/core/src/main/java/redis/clients/authentication/core/TokenManager.java @@ -49,7 +49,7 @@ public void start(TokenListener listener, boolean blockForInitialToken) { } catch (RuntimeException e) { throw e; } catch (Exception e) { - throw new TokenRequestException(unwrap(e), lastException); + throw prepareToPropogate(e); } } } @@ -89,9 +89,9 @@ protected Token renewToken() { .getMaxAttempts()) { scheduledTask = scheduleNext(tokenManagerConfig.getRetryPolicy().getdelayInMs()); } else { - TokenRequestException tre = new TokenRequestException(unwrap(e), lastException); - listener.onError(tre); - throw tre; + RuntimeException propogateExc = prepareToPropogate(e); + listener.onError(propogateExc); + throw propogateExc; } } return null; @@ -108,8 +108,15 @@ protected Token requestToken() { } } - private Throwable unwrap(Exception e) { - return (e instanceof ExecutionException) ? e.getCause() : e; + private RuntimeException prepareToPropogate(Exception e) { + Throwable unwrapped = e; + if (unwrapped instanceof ExecutionException) { + unwrapped = e.getCause(); + } + if (unwrapped instanceof TokenRequestException) { + return (RuntimeException) unwrapped; + } + return new TokenRequestException(unwrapped, lastException); } public Token getCurrentToken() { diff --git a/core/src/test/java/redis/clients/authentication/CoreAuthenticationUnitTests.java b/core/src/test/java/redis/clients/authentication/CoreAuthenticationUnitTests.java index 05b1897..c967343 100644 --- a/core/src/test/java/redis/clients/authentication/CoreAuthenticationUnitTests.java +++ b/core/src/test/java/redis/clients/authentication/CoreAuthenticationUnitTests.java @@ -162,7 +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().getMessage()); } @Test diff --git a/entraid/src/main/java/redis/clients/authentication/entraid/EntraIDTokenAuthConfigBuilder.java b/entraid/src/main/java/redis/clients/authentication/entraid/EntraIDTokenAuthConfigBuilder.java index e77fe40..3143660 100644 --- a/entraid/src/main/java/redis/clients/authentication/entraid/EntraIDTokenAuthConfigBuilder.java +++ b/entraid/src/main/java/redis/clients/authentication/entraid/EntraIDTokenAuthConfigBuilder.java @@ -14,7 +14,7 @@ public class EntraIDTokenAuthConfigBuilder extends TokenAuthConfig.Builder implements AutoCloseable { - public static final float DEFAULT_EXPIRATION_REFRESH_RATIO = 0.8F; + public static final float DEFAULT_EXPIRATION_REFRESH_RATIO = 0.75F; public static final int DEFAULT_LOWER_REFRESH_BOUND_MILLIS = 2 * 60 * 1000; public static final int DEFAULT_TOKEN_REQUEST_EXECUTION_TIMEOUT_IN_MS = 1000; public static final int DEFAULT_MAX_ATTEMPTS_TO_RETRY = 5; diff --git a/entraid/src/test/java/redis/clients/authentication/EntraIDIntegrationTests.java b/entraid/src/test/java/redis/clients/authentication/EntraIDIntegrationTests.java index b383c81..207e691 100644 --- a/entraid/src/test/java/redis/clients/authentication/EntraIDIntegrationTests.java +++ b/entraid/src/test/java/redis/clients/authentication/EntraIDIntegrationTests.java @@ -17,7 +17,7 @@ public void requestTokenWithSecret() throws MalformedURLException { testCtx.getClientId(), testCtx.getClientSecret(), testCtx.getAuthority()); Token token = new EntraIDIdentityProvider(servicePrincipalInfo, - testCtx.getRedisScopes(),1000).requestToken(); + testCtx.getRedisScopes(), 1000).requestToken(); assertNotNull(token.getValue()); } diff --git a/entraid/src/test/java/redis/clients/authentication/EntraIDUnitTests.java b/entraid/src/test/java/redis/clients/authentication/EntraIDUnitTests.java index 5920fc5..5558db7 100644 --- a/entraid/src/test/java/redis/clients/authentication/EntraIDUnitTests.java +++ b/entraid/src/test/java/redis/clients/authentication/EntraIDUnitTests.java @@ -160,7 +160,7 @@ public void tokenRequestfailsWithException_fakeIdentityProviderTest() { () -> tokenManager.start(mock(TokenListener.class), true)); assertEquals("Test exception from identity provider!", - e.getCause().getCause().getMessage()); + e.getCause().getMessage()); } // T.2.1 diff --git a/entraid/src/test/java/redis/clients/authentication/TestContext.java b/entraid/src/test/java/redis/clients/authentication/TestContext.java index 3ab6e2d..be096ec 100644 --- a/entraid/src/test/java/redis/clients/authentication/TestContext.java +++ b/entraid/src/test/java/redis/clients/authentication/TestContext.java @@ -19,6 +19,7 @@ public class TestContext { private static final String AZURE_PRIVATE_KEY = "AZURE_PRIVATE_KEY"; private static final String AZURE_CERT = "AZURE_CERT"; private static final String AZURE_REDIS_SCOPES = "AZURE_REDIS_SCOPES"; + private static final String AZURE_USER_ASSIGNED_MANAGED_IDENTITY_CLIENT_ID = "AZURE_USER_ASSIGNED_MANAGED_IDENTITY_CLIENT_ID"; private String clientId; private String authority; @@ -26,6 +27,7 @@ public class TestContext { private PrivateKey privateKey; private X509Certificate cert; private Set redisScopes; + private String userAssignedManagedIdentityClientId; public static final TestContext DEFAULT = new TestContext(); @@ -34,6 +36,8 @@ private TestContext() { this.clientId = System.getenv(AZURE_CLIENT_ID); this.authority = System.getenv(AZURE_AUTHORITY); this.clientSecret = System.getenv(AZURE_CLIENT_SECRET); + this.userAssignedManagedIdentityClientId = System + .getenv(AZURE_USER_ASSIGNED_MANAGED_IDENTITY_CLIENT_ID); } public TestContext(String clientId, String authority, String clientSecret, @@ -78,6 +82,10 @@ public Set getRedisScopes() { return redisScopes; } + public String getUserAssignedManagedIdentityClientId() { + return userAssignedManagedIdentityClientId; + } + private PrivateKey getPrivateKey(String privateKey) { try { // Decode the base64 encoded key into a byte array