Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Managed Identity json parse errors as CredentialUnAvailableException #32272

Merged
merged 2 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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