Skip to content

Commit 89866a2

Browse files
Implemented GitHub feedback
1 parent 6e1f6fa commit 89866a2

File tree

6 files changed

+9
-8
lines changed

6 files changed

+9
-8
lines changed

src/client/Microsoft.Identity.Client/ManagedIdentity/ImdsManagedIdentitySource.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ public static async Task<bool> ProbeImdsEndpointAsync(
276276

277277
var queryParams = ImdsQueryParamsHelper(requestContext, apiVersionQueryParam, imdsApiVersion);
278278

279+
// probe omits the "Metadata: true" header and then treats 400 Bad Request as success
279280
var headers = new Dictionary<string, string>
280281
{
281282
{ OAuth2Header.XMsCorrelationId, requestContext.CorrelationId.ToString() }
@@ -310,6 +311,7 @@ public static async Task<bool> ProbeImdsEndpointAsync(
310311
return false;
311312
}
312313

314+
// probe omits the "Metadata: true" header and then treats 400 Bad Request as success
313315
if (response.StatusCode == HttpStatusCode.BadRequest)
314316
{
315317
requestContext.Logger.Info(() => $"[Managed Identity] {imdsStringHelper} managed identity is available.");

src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityClient.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private async Task<AbstractManagedIdentity> GetOrSelectManagedIdentitySourceAsyn
9393
ManagedIdentitySource.AzureArc => AzureArcManagedIdentitySource.Create(requestContext),
9494
ManagedIdentitySource.ImdsV2 => ImdsV2ManagedIdentitySource.Create(requestContext),
9595
ManagedIdentitySource.Imds => ImdsManagedIdentitySource.Create(requestContext),
96-
_ => throw new MsalServiceException(MsalError.ManagedIdentityAllSourcesUnavailable, MsalErrorMessage.ManagedIdentityAllSourcesUnavailable)
96+
_ => throw new MsalClientException(MsalError.ManagedIdentityAllSourcesUnavailable, MsalErrorMessage.ManagedIdentityAllSourcesUnavailable)
9797
};
9898
}
9999
}
@@ -102,8 +102,7 @@ private async Task<AbstractManagedIdentity> GetOrSelectManagedIdentitySourceAsyn
102102
// This method is perf sensitive any changes should be benchmarked.
103103
internal async Task<ManagedIdentitySource> GetManagedIdentitySourceAsync(
104104
RequestContext requestContext,
105-
bool isMtlsPopRequested,
106-
bool noImdsV2 = false)
105+
bool isMtlsPopRequested)
107106
{
108107
// First check env vars to avoid the probe if possible
109108
ManagedIdentitySource source = GetManagedIdentitySourceNoImds(requestContext.Logger);

src/client/Microsoft.Identity.Client/ManagedIdentityApplication.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ public async Task<ManagedIdentitySource> GetManagedIdentitySourceAsync()
6363
return ManagedIdentityClient.s_sourceName;
6464
}
6565

66-
// Create a temporary RequestContext for the CSR metadata probe request.
67-
var csrMetadataProbeRequestContext = new RequestContext(this.ServiceBundle, Guid.NewGuid(), null, CancellationToken.None);
66+
// Create a temporary RequestContext for the logger and the IMDS probe request.
67+
var requestContext = new RequestContext(this.ServiceBundle, Guid.NewGuid(), null, CancellationToken.None);
6868

6969
// GetManagedIdentitySourceAsync might return ImdsV2 = true, but it still requires .WithMtlsProofOfPossesion on the Managed Identity Application object to hit the ImdsV2 flow
70-
return await ManagedIdentityClient.GetManagedIdentitySourceAsync(csrMetadataProbeRequestContext, isMtlsPopRequested: true).ConfigureAwait(false);
70+
return await ManagedIdentityClient.GetManagedIdentitySourceAsync(requestContext, isMtlsPopRequested: true).ConfigureAwait(false);
7171
}
7272

7373
/// <summary>

tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ImdsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ public async Task ProbeImdsEndpointAsync_TimesOutAfterOneSecond()
464464
var miSource = await (managedIdentityApp as ManagedIdentityApplication).GetManagedIdentitySourceAsync().ConfigureAwait(false);
465465
Assert.AreEqual(ManagedIdentitySource.None, miSource); // Probe timed out, no source available
466466

467-
var ex = await Assert.ThrowsExceptionAsync<MsalServiceException>(async () =>
467+
var ex = await Assert.ThrowsExceptionAsync<MsalClientException>(async () =>
468468
await managedIdentityApp.AcquireTokenForManagedIdentity(ManagedIdentityTests.Resource)
469469
.ExecuteAsync().ConfigureAwait(false)
470470
).ConfigureAwait(false);

tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ManagedIdentityTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ public async Task UnavailableManagedIdentitySource_ThrowsExceptionDuringTokenAcq
11621162

11631163
httpManager.AddMockHandler(MockHelpers.MockImdsProbeFailure(ImdsVersion.V1));
11641164

1165-
MsalServiceException ex = await Assert.ThrowsExceptionAsync<MsalServiceException>(async () =>
1165+
var ex = await Assert.ThrowsExceptionAsync<MsalClientException>(async () =>
11661166
await mi.AcquireTokenForManagedIdentity("https://management.azure.com")
11671167
.ExecuteAsync()
11681168
.ConfigureAwait(false)).ConfigureAwait(false);

0 commit comments

Comments
 (0)