Skip to content

Commit 4b74642

Browse files
authored
Redact error output (Azure#36232)
* Redact error output A bug was reported to me in which I observed that the exception message for the CLI process timing out can include a warning and then the access token. Azure CLI defaults to printing warnings. The fact that the output is not pure JSON causes us to fail parsing it, and the exception includes the token. * add test
1 parent 3255f7f commit 4b74642

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

sdk/identity/azure-identity/src/main/java/com/azure/identity/implementation/IdentityClientBase.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,9 @@ AccessToken getTokenFromAzureCLIAuthentication(StringBuilder azCommand) {
581581
.toOffsetDateTime().withOffsetSameInstant(ZoneOffset.UTC);
582582
token = new AccessToken(accessToken, expiresOn);
583583
} catch (IOException | InterruptedException e) {
584-
throw LOGGER.logExceptionAsError(new IllegalStateException(e));
584+
IllegalStateException ex = new IllegalStateException(redactInfo(e.getMessage()));
585+
ex.setStackTrace(e.getStackTrace());
586+
throw LOGGER.logExceptionAsError(ex);
585587
}
586588
return token;
587589
}
@@ -681,7 +683,9 @@ AccessToken getTokenFromAzureDeveloperCLIAuthentication(StringBuilder azdCommand
681683
.withOffsetSameInstant(ZoneOffset.UTC);
682684
token = new AccessToken(accessToken, expiresOn);
683685
} catch (IOException | InterruptedException e) {
684-
throw LOGGER.logExceptionAsError(new IllegalStateException(e));
686+
IllegalStateException ex = new IllegalStateException(redactInfo(e.getMessage()));
687+
ex.setStackTrace(e.getStackTrace());
688+
throw LOGGER.logExceptionAsError(ex);
685689
}
686690

687691
return token;

sdk/identity/azure-identity/src/test/java/com/azure/identity/implementation/IdentityClientTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
import java.util.UUID;
5050
import java.util.concurrent.CompletableFuture;
5151

52+
import static org.junit.Assert.assertFalse;
53+
import static org.junit.Assert.assertTrue;
5254
import static org.junit.Assert.fail;
5355
import static org.mockito.ArgumentMatchers.any;
5456
import static org.mockito.ArgumentMatchers.anyString;
@@ -582,6 +584,22 @@ private void mockForClientCertificate(TokenRequestContext request, String access
582584
}
583585
}
584586

587+
@Test
588+
public void validateRedaction() {
589+
String s = " WARNING: Could not retrieve credential from local cache for service principal *** under tenant organizations. Trying credential under tenant 72f988bf-86f1-41af-91ab-2d7cd011db47, assuming that is an app credential.\n"
590+
+ " {\n"
591+
+ " \"accessToken\": \"ANACCESSTOKEN\",\n"
592+
+ " \"expiresOn\": \"2023-08-03 12:29:07.000000\",\n"
593+
+ " \"subscription\": \"subscription\",\n"
594+
+ " \"tenant\": \"tenant\",\n"
595+
+ " \"tokenType\": \"Bearer\"\n"
596+
+ " }";
597+
IdentityClient client = new IdentityClientBuilder().clientId("dummy").build();
598+
String redacted = client.redactInfo(s);
599+
assertTrue(redacted.contains("****"));
600+
assertFalse(redacted.contains("accessToken"));
601+
}
602+
585603
private void mockForDeviceCodeFlow(TokenRequestContext request, String accessToken, OffsetDateTime expiresOn, Runnable test) {
586604
try (MockedConstruction<PublicClientApplication.Builder> publicClientApplicationMock = mockConstruction(PublicClientApplication.Builder.class, (builder, context) -> {
587605
when(builder.authority(any())).thenReturn(builder);

0 commit comments

Comments
 (0)