Skip to content

Commit

Permalink
- change exception propogation/handling
Browse files Browse the repository at this point in the history
- fix units tests
- set DEFAULT_EXPIRATION_REFRESH_RATIO in entraid 0.75
  • Loading branch information
atakavci committed Dec 8, 2024
1 parent dad80ac commit 39cacf3
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

public class EntraIDTokenAuthConfigBuilder
extends TokenAuthConfig.Builder<EntraIDTokenAuthConfigBuilder> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ 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;
private String clientSecret;
private PrivateKey privateKey;
private X509Certificate cert;
private Set<String> redisScopes;
private String userAssignedManagedIdentityClientId;

public static final TestContext DEFAULT = new TestContext();

Expand All @@ -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,
Expand Down Expand Up @@ -78,6 +82,10 @@ public Set<String> getRedisScopes() {
return redisScopes;
}

public String getUserAssignedManagedIdentityClientId() {
return userAssignedManagedIdentityClientId;
}

private PrivateKey getPrivateKey(String privateKey) {
try {
// Decode the base64 encoded key into a byte array
Expand Down

0 comments on commit 39cacf3

Please sign in to comment.