Skip to content

Commit

Permalink
Handle Managed Identity json parse errors as CredentialUnAvailableExc…
Browse files Browse the repository at this point in the history
…eption (Azure#32272)

* handle json parse errors as CredentialNotAvailable
  • Loading branch information
christothes authored and schaabs committed Nov 8, 2022
1 parent 53fdeda commit 3a25965
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions sdk/identity/Azure.Identity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Bugs Fixed
- Fixed error message parsing in `AzureCliCredential` which would misinterpret AAD errors with the need to login with `az login`.
- `ManagedIdentityCredential` will no longer fail when a response received from the endpoint is invalid JSON. It now treats this scenario as if the credential is unavailable.

### Other Changes

Expand Down
4 changes: 4 additions & 0 deletions sdk/identity/Azure.Identity/src/ManagedIdentitySource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ protected virtual async ValueTask<AccessToken> HandleResponseAsync(

message = GetMessageFromResponse(json.RootElement);
}
catch (JsonException jex)
{
throw new CredentialUnavailableException(UnexpectedResponse, jex);
}
catch (Exception e)
{
exception = e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Azure.Identity.Tests.Mock;
using Microsoft.AspNetCore.Http;
using Microsoft.Diagnostics.Runtime.Interop;
using Newtonsoft.Json;
using NUnit.Framework;

namespace Azure.Identity.Tests
Expand Down Expand Up @@ -691,7 +692,7 @@ public async Task VerifyClientAuthenticateThrows()
}

[Test]
public async Task VerifyClientAuthenticateReturnsInvalidJson([Values(200, 404)] int status)
public async Task VerifyClientAuthenticateReturnsInvalidJson([Values(200, 404, 403)] int status)
{
using var environment = new TestEnvVar(
new()
Expand All @@ -709,8 +710,8 @@ public async Task VerifyClientAuthenticateReturnsInvalidJson([Values(200, 404)]

ManagedIdentityCredential credential = InstrumentClient(new ManagedIdentityCredential("mock-client-id", pipeline));

var ex = Assert.ThrowsAsync<AuthenticationFailedException>(async () => await credential.GetTokenAsync(new TokenRequestContext(MockScopes.Default)));
Assert.IsInstanceOf(typeof(RequestFailedException), ex.InnerException);
var ex = Assert.ThrowsAsync<CredentialUnavailableException>(async () => await credential.GetTokenAsync(new TokenRequestContext(MockScopes.Default)));
Assert.IsInstanceOf(typeof(System.Text.Json.JsonException), ex.InnerException);
Assert.That(ex.Message, Does.Contain(ManagedIdentitySource.UnexpectedResponse));
await Task.CompletedTask;
}
Expand Down

0 comments on commit 3a25965

Please sign in to comment.