Skip to content

Commit 39cacf3

Browse files
committed
- change exception propogation/handling
- fix units tests - set DEFAULT_EXPIRATION_REFRESH_RATIO in entraid 0.75
1 parent dad80ac commit 39cacf3

File tree

6 files changed

+25
-10
lines changed

6 files changed

+25
-10
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void start(TokenListener listener, boolean blockForInitialToken) {
4949
} catch (RuntimeException e) {
5050
throw e;
5151
} catch (Exception e) {
52-
throw new TokenRequestException(unwrap(e), lastException);
52+
throw prepareToPropogate(e);
5353
}
5454
}
5555
}
@@ -89,9 +89,9 @@ protected Token renewToken() {
8989
.getMaxAttempts()) {
9090
scheduledTask = scheduleNext(tokenManagerConfig.getRetryPolicy().getdelayInMs());
9191
} else {
92-
TokenRequestException tre = new TokenRequestException(unwrap(e), lastException);
93-
listener.onError(tre);
94-
throw tre;
92+
RuntimeException propogateExc = prepareToPropogate(e);
93+
listener.onError(propogateExc);
94+
throw propogateExc;
9595
}
9696
}
9797
return null;
@@ -108,8 +108,15 @@ protected Token requestToken() {
108108
}
109109
}
110110

111-
private Throwable unwrap(Exception e) {
112-
return (e instanceof ExecutionException) ? e.getCause() : e;
111+
private RuntimeException prepareToPropogate(Exception e) {
112+
Throwable unwrapped = e;
113+
if (unwrapped instanceof ExecutionException) {
114+
unwrapped = e.getCause();
115+
}
116+
if (unwrapped instanceof TokenRequestException) {
117+
return (RuntimeException) unwrapped;
118+
}
119+
return new TokenRequestException(unwrapped, lastException);
113120
}
114121

115122
public Token getCurrentToken() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +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!", e.getCause().getCause().getMessage());
165+
assertEquals("Test exception from identity provider!", e.getCause().getMessage());
166166
}
167167

168168
@Test

entraid/src/main/java/redis/clients/authentication/entraid/EntraIDTokenAuthConfigBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
public class EntraIDTokenAuthConfigBuilder
1616
extends TokenAuthConfig.Builder<EntraIDTokenAuthConfigBuilder> implements AutoCloseable {
17-
public static final float DEFAULT_EXPIRATION_REFRESH_RATIO = 0.8F;
17+
public static final float DEFAULT_EXPIRATION_REFRESH_RATIO = 0.75F;
1818
public static final int DEFAULT_LOWER_REFRESH_BOUND_MILLIS = 2 * 60 * 1000;
1919
public static final int DEFAULT_TOKEN_REQUEST_EXECUTION_TIMEOUT_IN_MS = 1000;
2020
public static final int DEFAULT_MAX_ATTEMPTS_TO_RETRY = 5;

entraid/src/test/java/redis/clients/authentication/EntraIDIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void requestTokenWithSecret() throws MalformedURLException {
1717
testCtx.getClientId(), testCtx.getClientSecret(),
1818
testCtx.getAuthority());
1919
Token token = new EntraIDIdentityProvider(servicePrincipalInfo,
20-
testCtx.getRedisScopes(),1000).requestToken();
20+
testCtx.getRedisScopes(), 1000).requestToken();
2121

2222
assertNotNull(token.getValue());
2323
}

entraid/src/test/java/redis/clients/authentication/EntraIDUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void tokenRequestfailsWithException_fakeIdentityProviderTest() {
160160
() -> tokenManager.start(mock(TokenListener.class), true));
161161

162162
assertEquals("Test exception from identity provider!",
163-
e.getCause().getCause().getMessage());
163+
e.getCause().getMessage());
164164
}
165165

166166
// T.2.1

entraid/src/test/java/redis/clients/authentication/TestContext.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ public class TestContext {
1919
private static final String AZURE_PRIVATE_KEY = "AZURE_PRIVATE_KEY";
2020
private static final String AZURE_CERT = "AZURE_CERT";
2121
private static final String AZURE_REDIS_SCOPES = "AZURE_REDIS_SCOPES";
22+
private static final String AZURE_USER_ASSIGNED_MANAGED_IDENTITY_CLIENT_ID = "AZURE_USER_ASSIGNED_MANAGED_IDENTITY_CLIENT_ID";
2223

2324
private String clientId;
2425
private String authority;
2526
private String clientSecret;
2627
private PrivateKey privateKey;
2728
private X509Certificate cert;
2829
private Set<String> redisScopes;
30+
private String userAssignedManagedIdentityClientId;
2931

3032
public static final TestContext DEFAULT = new TestContext();
3133

@@ -34,6 +36,8 @@ private TestContext() {
3436
this.clientId = System.getenv(AZURE_CLIENT_ID);
3537
this.authority = System.getenv(AZURE_AUTHORITY);
3638
this.clientSecret = System.getenv(AZURE_CLIENT_SECRET);
39+
this.userAssignedManagedIdentityClientId = System
40+
.getenv(AZURE_USER_ASSIGNED_MANAGED_IDENTITY_CLIENT_ID);
3741
}
3842

3943
public TestContext(String clientId, String authority, String clientSecret,
@@ -78,6 +82,10 @@ public Set<String> getRedisScopes() {
7882
return redisScopes;
7983
}
8084

85+
public String getUserAssignedManagedIdentityClientId() {
86+
return userAssignedManagedIdentityClientId;
87+
}
88+
8189
private PrivateKey getPrivateKey(String privateKey) {
8290
try {
8391
// Decode the base64 encoded key into a byte array

0 commit comments

Comments
 (0)