Skip to content

Commit

Permalink
Don't stop trying dev credentials on failures
Browse files Browse the repository at this point in the history
Fixes Azure#34733

For our dev time credentials we want to always keep going. This change wraps any failure from the credentials in a `CredentialUnavailableException` so `ChainedTokenCredential` will continue them properly.
  • Loading branch information
billwert committed Jul 6, 2023
1 parent 04d79bb commit 87a2af5
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public Mono<AccessToken> getToken(TokenRequestContext request) {
return identityClient.authenticateWithAzureCli(request)
.doOnNext(token -> LoggingUtil.logTokenSuccess(LOGGER, request))
.doOnError(error -> LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(), request,
error));
error))
.onErrorMap(error -> new CredentialUnavailableException("Azure CLI authentication is not available", error));
}

@Override
Expand All @@ -94,7 +95,7 @@ public AccessToken getTokenSync(TokenRequestContext request) {
return accessToken;
} catch (Exception e) {
LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(), request, e);
throw e;
throw new CredentialUnavailableException("Azure CLI authentication is not available", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public Mono<AccessToken> getToken(TokenRequestContext request) {
return identityClient.authenticateWithAzureDeveloperCli(request)
.doOnNext(token -> LoggingUtil.logTokenSuccess(LOGGER, request))
.doOnError(error -> LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(), request,
error));
error))
.onErrorMap(error -> new CredentialUnavailableException("Azure Developer CLI authentication is not available", error));
}

@Override
Expand All @@ -94,7 +95,7 @@ public AccessToken getTokenSync(TokenRequestContext request) {
return accessToken;
} catch (Exception e) {
LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(), request, e);
throw e;
throw new CredentialUnavailableException("Azure Developer CLI authentication is not available", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public Mono<AccessToken> getToken(TokenRequestContext request) {
return identityClient.authenticateWithAzurePowerShell(request)
.doOnNext(token -> LoggingUtil.logTokenSuccess(LOGGER, request))
.doOnError(error -> LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(), request,
error));
error))
.onErrorMap(error -> new CredentialUnavailableException("Azure Powershell authentication is not available", error));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public Mono<AccessToken> getToken(TokenRequestContext request) {
})
.doOnNext(token -> LoggingUtil.logTokenSuccess(LOGGER, request))
.doOnError(error -> LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(),
request, error));
request, error))
.onErrorMap(error -> new CredentialUnavailableException("IntelliJ authentication is not available", error));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public Mono<AccessToken> getToken(TokenRequestContext request) {
.map(this::updateCache)
.doOnNext(token -> LoggingUtil.logTokenSuccess(LOGGER, request))
.doOnError(error -> LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(),
request, error));
request, error))
.onErrorMap(error -> new CredentialUnavailableException("SharedToken authentication is not available", error));
}

private AccessToken updateCache(MsalToken msalToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void azureCliCredentialWinAzureCLINotInstalledException() throws Exceptio
// test
AzureCliCredential credential = new AzureCliCredentialBuilder().build();
StepVerifier.create(credential.getToken(request))
.expectErrorMatches(e -> e instanceof Exception && e.getMessage().contains("Azure CLI not installed"))
.expectErrorMatches(e -> e instanceof CredentialUnavailableException && e.getCause().getMessage().contains("Azure CLI not installed"))
.verify();
Assert.assertNotNull(identityClientMock);
}
Expand All @@ -79,7 +79,7 @@ public void azureCliCredentialAzNotLogInException() throws Exception {
// test
AzureCliCredential credential = new AzureCliCredentialBuilder().build();
StepVerifier.create(credential.getToken(request))
.expectErrorMatches(e -> e instanceof Exception && e.getMessage().contains("Azure not Login"))
.expectErrorMatches(e -> e instanceof CredentialUnavailableException && e.getCause().getMessage().contains("Azure not Login"))
.verify();
Assert.assertNotNull(identityClientMock);
}
Expand All @@ -99,7 +99,7 @@ public void azureCliCredentialAuthenticationFailedException() throws Exception {
// test
AzureCliCredential credential = new AzureCliCredentialBuilder().build();
StepVerifier.create(credential.getToken(request))
.expectErrorMatches(e -> e instanceof Exception && e.getMessage().contains("other error"))
.expectErrorMatches(e -> e instanceof CredentialUnavailableException && e.getCause().getMessage().contains("other error"))
.verify();
Assert.assertNotNull(identityClientMock);
}
Expand Down Expand Up @@ -127,7 +127,7 @@ public void testInvalidMultiTenantAuth() {
AzureCliCredential credential =
new AzureCliCredentialBuilder().tenantId("tenant").build();
StepVerifier.create(credential.getToken(request))
.expectErrorMatches(e -> e instanceof ClientAuthenticationException && (e.getMessage().startsWith("The current credential is not configured to")))
.expectErrorMatches(e -> e instanceof CredentialUnavailableException && (e.getCause().getMessage().startsWith("The current credential is not configured to")))
.verify();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void azureDeveloperCliCredentialWinAzureCLINotInstalledException() throws
// test
AzureDeveloperCliCredential credential = new AzureDeveloperCliCredentialBuilder().build();
StepVerifier.create(credential.getToken(request))
.expectErrorMatches(e -> e instanceof Exception && e.getMessage().contains("Azure CLI not installed"))
.expectErrorMatches(e -> e instanceof CredentialUnavailableException && e.getCause().getMessage().contains("Azure CLI not installed"))
.verify();
Assert.assertNotNull(identityClientMock);
}
Expand All @@ -77,7 +77,7 @@ public void azureDeveloperCliCredentialAzNotLogInException() throws Exception {
// test
AzureDeveloperCliCredential credential = new AzureDeveloperCliCredentialBuilder().build();
StepVerifier.create(credential.getToken(request))
.expectErrorMatches(e -> e instanceof Exception && e.getMessage().contains("Azure not Login"))
.expectErrorMatches(e -> e instanceof CredentialUnavailableException && e.getCause().getMessage().contains("Azure not Login"))
.verify();
Assert.assertNotNull(identityClientMock);
}
Expand All @@ -97,7 +97,7 @@ public void azureDeveloperCliCredentialAuthenticationFailedException() throws Ex
// test
AzureDeveloperCliCredential credential = new AzureDeveloperCliCredentialBuilder().build();
StepVerifier.create(credential.getToken(request))
.expectErrorMatches(e -> e instanceof Exception && e.getMessage().contains("other error"))
.expectErrorMatches(e -> e instanceof CredentialUnavailableException && e.getCause().getMessage().contains("other error"))
.verify();
Assert.assertNotNull(identityClientMock);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void azurePowerShellCredentialNotInstalledException() throws Exception {
// test
AzurePowerShellCredential credential = new AzurePowerShellCredentialBuilder().build();
StepVerifier.create(credential.getToken(request))
.expectErrorMatches(e -> e instanceof Exception && e.getMessage()
.expectErrorMatches(e -> e instanceof CredentialUnavailableException && e.getCause().getMessage()
.contains("Azure PowerShell not installed"))
.verify();
Assert.assertNotNull(identityClientMock);
Expand Down

0 comments on commit 87a2af5

Please sign in to comment.