Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix DAC chain for 400 responses with 'Identity not found'
  • Loading branch information
christothes committed Oct 18, 2024
commit 51384b47db7a0b25f870e4204229cec1beea2f13
3 changes: 2 additions & 1 deletion sdk/identity/Azure.Identity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
### Breaking Changes

### Bugs Fixed
- Fixed an issue that prevented ManagedIdentityCredential from attempting to detect if Workload Identity is enabled in the current environment. [#46653](https://github.com/Azure/azure-sdk-for-net/issues/46653)
- Fixed an issue that prevented `ManagedIdentityCredential` from attempting to detect if Workload Identity is enabled in the current environment. [#46653](https://github.com/Azure/azure-sdk-for-net/issues/46653)
- Fixed an issue that prevented `DefaultAzureCredential` from progressing past `ManagedIdentityCredential` in some scenarios where the identity was not available. [#46709](https://github.com/Azure/azure-sdk-for-net/issues/46709)

### Other Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ protected override async ValueTask<AccessToken> HandleResponseAsync(bool async,
// handle error status codes indicating managed identity is not available
string baseMessage = response.Status switch
{
400 when IsProbeRequest(message) => throw new ProbeRequestResponseException(),
400 when IsRetriableProbeRequest(message) => throw new ProbeRequestResponseException(),
400 => IdentityUnavailableError,
502 => GatewayError,
504 => GatewayError,
Expand All @@ -169,10 +169,11 @@ protected override async ValueTask<AccessToken> HandleResponseAsync(bool async,
return token;
}

public static bool IsProbeRequest(HttpMessage message)
public static bool IsRetriableProbeRequest(HttpMessage message)
=> message.Request.Uri.Host == s_imdsEndpoint.Host &&
message.Request.Uri.Path == s_imdsEndpoint.AbsolutePath &&
!message.Request.Headers.TryGetValue(metadataHeaderName, out _);
!message.Request.Headers.TryGetValue(metadataHeaderName, out _) &&
!(message.Response.Content?.ToString().Contains("Identity not found") ?? false);

private class ImdsRequestFailedDetailsParser : RequestFailedDetailsParser
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public void VerifyImdsRequestFailureForDockerDesktopThrowsCUE(string error)
{
using var environment = new TestEnvVar(new() { { "MSI_ENDPOINT", null }, { "MSI_SECRET", null }, { "IDENTITY_ENDPOINT", null }, { "IDENTITY_HEADER", null }, { "AZURE_POD_IDENTITY_AUTHORITY_HOST", null } });

var expectedMessage = $"connecting to 169.254.169.254:80: connecting to 169.254.169.254:80: dial tcp 169.254.169.254:80: connectex: A socket operation was attempted to an unreachable {error}.";
var expectedMessage = error;
var response = CreateInvalidJsonResponse(403, expectedMessage);
var mockTransport = new MockTransport(response);
var options = new TokenCredentialOptions() { Transport = mockTransport, IsChainedCredential = true };
Expand Down Expand Up @@ -422,6 +422,31 @@ public void VerifyImdsRequestFailureWithInvalidJsonPopulatesExceptionMessage()
Assert.That(ex.Message, Does.Contain(expectedMessage));
}

[NonParallelizable]
[Test]
[TestCase("""{"error":"invalid_request","error_description":"Identity not found"}""")]
[TestCase(null)]
public void VerifyImdsRequestFailureWithValidJsonIdentityNotFoundErrorThrowsCUE(string content)
{
using var environment = new TestEnvVar(new() { { "MSI_ENDPOINT", null }, { "MSI_SECRET", null }, { "IDENTITY_ENDPOINT", null }, { "IDENTITY_HEADER", null }, { "AZURE_POD_IDENTITY_AUTHORITY_HOST", null } });

var response = CreateResponse(400, content);
var mockTransport = new MockTransport(req => response);
var options = new TokenCredentialOptions() { Transport = mockTransport, IsChainedCredential = true };
var pipeline = CredentialPipeline.GetInstance(options);

ManagedIdentityCredential credential = InstrumentClient(new ManagedIdentityCredential("mock-client-id", pipeline, options));
if (content != null)
{
var ex = Assert.ThrowsAsync<CredentialUnavailableException>(async () => await credential.GetTokenAsync(new TokenRequestContext(MockScopes.Default)));
Assert.That(ex.Message, Does.Contain(ImdsManagedIdentityProbeSource.IdentityUnavailableError));
}
else
{
var ex = Assert.ThrowsAsync<AuthenticationFailedException>(async () => await credential.GetTokenAsync(new TokenRequestContext(MockScopes.Default)));
}
}

[NonParallelizable]
[Test]
[TestCase(502, ImdsManagedIdentitySource.GatewayError)]
Expand Down Expand Up @@ -1128,5 +1153,15 @@ private static MockResponse CreateInvalidJsonResponse(int status, string message
response.SetContent(message);
return response;
}

private static MockResponse CreateResponse(int status, string message)
{
var response = new MockResponse(status);
if (message != null)
{
response.SetContent(message);
}
return response;
}
}
}