Skip to content

Commit 06dd672

Browse files
committed
Hotfix for DAC probe request
1 parent ae13ec2 commit 06dd672

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

sdk/identity/Azure.Identity/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Release History
22

3+
## 1.11.3 (2024-05-07)
4+
5+
### Bugs Fixed
6+
- Fixed a regression in `DefaultAzureCredential` probe request behavior for IMDS managed identity environments. [#43796](https://github.com/Azure/azure-sdk-for-net/issues/43796)
7+
38
## 1.11.2 (2024-04-19)
49

510
### Bugs Fixed

sdk/identity/Azure.Identity/src/Azure.Identity.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<PropertyGroup>
33
<Description>This is the implementation of the Azure SDK Client Library for Azure Identity</Description>
44
<AssemblyTitle>Microsoft Azure.Identity Component</AssemblyTitle>
5-
<Version>1.11.2</Version>
5+
<Version>1.11.3</Version>
66
<!--The ApiCompatVersion is managed automatically and should not generally be modified manually.-->
7-
<ApiCompatVersion>1.11.1</ApiCompatVersion>
7+
<ApiCompatVersion>1.11.2</ApiCompatVersion>
88
<PackageTags>Microsoft Azure Identity;$(PackageCommonTags)</PackageTags>
99
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
1010
<NoWarn>$(NoWarn);3021;AZC0011</NoWarn>

sdk/identity/Azure.Identity/src/ImdsManagedIdentitySource.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ protected override HttpMessage CreateHttpMessage(Request request)
9191
if (_isFirstRequest && _isChainedCredential)
9292
{
9393
message.NetworkTimeout = _imdsNetworkTimeout;
94-
_isFirstRequest = false;
9594
}
9695

9796
return message;
@@ -140,6 +139,9 @@ protected override async ValueTask<AccessToken> HandleResponseAsync(bool async,
140139
// if we got a response from IMDS we can stop limiting the network timeout
141140
_imdsNetworkTimeout = null;
142141

142+
// Mark that the first request has been made
143+
_isFirstRequest = false;
144+
143145
// handle error status codes indicating managed identity is not available
144146
string baseMessage = response.Status switch
145147
{

sdk/identity/Azure.Identity/tests/ImdsManagedIdentitySourceTests.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,53 @@ public async Task DefaultAzureCredentialProbeUses1secTimeoutWithNoRetries()
6262
CollectionAssert.AreEqual(expectedTimeouts, networkTimeouts);
6363
}
6464

65+
[Test]
66+
public async Task DefaultAzureCredentialUsesFirstRequestBehaviorUntilFirstResponse()
67+
{
68+
int callCount = 0;
69+
List<TimeSpan?> networkTimeouts = new();
70+
71+
// the mock transport succeeds on the 2nd request to avoid long exponential back-offs,
72+
// but is sufficient to validate the initial timeout and retry behavior
73+
var mockTransport = MockTransport.FromMessageCallback(msg =>
74+
{
75+
callCount++;
76+
networkTimeouts.Add(msg.NetworkTimeout);
77+
return callCount switch
78+
{
79+
1 => throw new TaskCanceledException(),
80+
2 => CreateMockResponse(400, "Error").WithHeader("Content-Type", "application/json"),
81+
_ => CreateMockResponse(200, "token").WithHeader("Content-Type", "application/json"),
82+
};
83+
});
84+
85+
var cred = new DefaultAzureCredential(new DefaultAzureCredentialOptions
86+
{
87+
ExcludeAzureCliCredential = true,
88+
ExcludeAzureDeveloperCliCredential = true,
89+
ExcludeAzurePowerShellCredential = true,
90+
ExcludeEnvironmentCredential = true,
91+
ExcludeSharedTokenCacheCredential = true,
92+
ExcludeVisualStudioCodeCredential = true,
93+
ExcludeVisualStudioCredential = true,
94+
ExcludeWorkloadIdentityCredential = true,
95+
Transport = mockTransport
96+
});
97+
98+
//First request times out (throws TaskCancelledException) uses a 1 second timeout and no retries
99+
Assert.ThrowsAsync<CredentialUnavailableException>(async () => await cred.GetTokenAsync(new(new[] { "test" })));
100+
101+
var expectedTimeouts = new TimeSpan?[] { TimeSpan.FromSeconds(1) };
102+
CollectionAssert.AreEqual(expectedTimeouts, networkTimeouts);
103+
networkTimeouts.Clear();
104+
105+
// Second request gets the expected probe response and should use the probe timeout on first request and default timeout on the retry
106+
await cred.GetTokenAsync(new(new[] { "test" }));
107+
108+
expectedTimeouts = new TimeSpan?[] { TimeSpan.FromSeconds(1), null };
109+
CollectionAssert.AreEqual(expectedTimeouts, networkTimeouts);
110+
}
111+
65112
[Test]
66113
public void DefaultAzureCredentialRetryBehaviorIsOverriddenWithOptions()
67114
{

0 commit comments

Comments
 (0)