Skip to content

Commit b5eeaf3

Browse files
Implemented GitHub feedback
1 parent 89866a2 commit b5eeaf3

File tree

20 files changed

+47
-57
lines changed

20 files changed

+47
-57
lines changed

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ public static string ImdsQueryParamsHelper(
243243

244244
public static async Task<bool> ProbeImdsEndpointAsync(
245245
RequestContext requestContext,
246-
ImdsVersion imdsVersion)
246+
ImdsVersion imdsVersion,
247+
CancellationToken cancellationToken)
247248
{
248249
string apiVersionQueryParam;
249250
string imdsApiVersion;
@@ -289,21 +290,18 @@ public static async Task<bool> ProbeImdsEndpointAsync(
289290

290291
try
291292
{
292-
using (var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(1)))
293-
{
294-
response = await requestContext.ServiceBundle.HttpManager.SendRequestAsync(
295-
GetValidatedEndpoint(requestContext.Logger, imdsEndpoint, queryParams),
296-
headers,
297-
body: null,
298-
method: HttpMethod.Get,
299-
logger: requestContext.Logger,
300-
doNotThrow: false,
301-
mtlsCertificate: null,
302-
validateServerCertificate: null,
303-
cancellationToken: timeoutCts.Token,
304-
retryPolicy: retryPolicy)
305-
.ConfigureAwait(false);
306-
}
293+
response = await requestContext.ServiceBundle.HttpManager.SendRequestAsync(
294+
GetValidatedEndpoint(requestContext.Logger, imdsEndpoint, queryParams),
295+
headers,
296+
body: null,
297+
method: HttpMethod.Get,
298+
logger: requestContext.Logger,
299+
doNotThrow: false,
300+
mtlsCertificate: null,
301+
validateServerCertificate: null,
302+
cancellationToken: cancellationToken,
303+
retryPolicy: retryPolicy)
304+
.ConfigureAwait(false);
307305
}
308306
catch (Exception ex)
309307
{

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ internal async Task<ManagedIdentityResponse> SendTokenRequestForManagedIdentityA
4040
AcquireTokenForManagedIdentityParameters parameters,
4141
CancellationToken cancellationToken)
4242
{
43-
AbstractManagedIdentity msi = await GetOrSelectManagedIdentitySourceAsync(requestContext, parameters.IsMtlsPopRequested).ConfigureAwait(false);
43+
AbstractManagedIdentity msi = await GetOrSelectManagedIdentitySourceAsync(requestContext, parameters.IsMtlsPopRequested, cancellationToken).ConfigureAwait(false);
4444
return await msi.AuthenticateAsync(parameters, cancellationToken).ConfigureAwait(false);
4545
}
4646

4747
// This method tries to create managed identity source for different sources, if none is created then defaults to IMDS.
48-
private async Task<AbstractManagedIdentity> GetOrSelectManagedIdentitySourceAsync(RequestContext requestContext, bool isMtlsPopRequested)
48+
private async Task<AbstractManagedIdentity> GetOrSelectManagedIdentitySourceAsync(
49+
RequestContext requestContext,
50+
bool isMtlsPopRequested,
51+
CancellationToken cancellationToken)
4952
{
5053
using (requestContext.Logger.LogMethodDuration())
5154
{
@@ -57,7 +60,7 @@ private async Task<AbstractManagedIdentity> GetOrSelectManagedIdentitySourceAsyn
5760
if (s_sourceName == ManagedIdentitySource.None)
5861
{
5962
// First invocation: detect and cache
60-
source = await GetManagedIdentitySourceAsync(requestContext, isMtlsPopRequested).ConfigureAwait(false);
63+
source = await GetManagedIdentitySourceAsync(requestContext, isMtlsPopRequested, cancellationToken).ConfigureAwait(false);
6164
}
6265
else
6366
{
@@ -102,7 +105,8 @@ private async Task<AbstractManagedIdentity> GetOrSelectManagedIdentitySourceAsyn
102105
// This method is perf sensitive any changes should be benchmarked.
103106
internal async Task<ManagedIdentitySource> GetManagedIdentitySourceAsync(
104107
RequestContext requestContext,
105-
bool isMtlsPopRequested)
108+
bool isMtlsPopRequested,
109+
CancellationToken cancellationToken)
106110
{
107111
// First check env vars to avoid the probe if possible
108112
ManagedIdentitySource source = GetManagedIdentitySourceNoImds(requestContext.Logger);
@@ -115,7 +119,7 @@ internal async Task<ManagedIdentitySource> GetManagedIdentitySourceAsync(
115119
// skip the ImdsV2 probe if MtlsPop was NOT requested
116120
if (isMtlsPopRequested)
117121
{
118-
var imdsV2Response = await ImdsManagedIdentitySource.ProbeImdsEndpointAsync(requestContext, ImdsVersion.V2).ConfigureAwait(false);
122+
var imdsV2Response = await ImdsManagedIdentitySource.ProbeImdsEndpointAsync(requestContext, ImdsVersion.V2, cancellationToken).ConfigureAwait(false);
119123
if (imdsV2Response)
120124
{
121125
requestContext.Logger.Info("[Managed Identity] ImdsV2 detected.");
@@ -128,7 +132,7 @@ internal async Task<ManagedIdentitySource> GetManagedIdentitySourceAsync(
128132
requestContext.Logger.Info("[Managed Identity] Mtls Pop was not requested; skipping ImdsV2 probe.");
129133
}
130134

131-
var imdsV1Response = await ImdsManagedIdentitySource.ProbeImdsEndpointAsync(requestContext, ImdsVersion.V1).ConfigureAwait(false);
135+
var imdsV1Response = await ImdsManagedIdentitySource.ProbeImdsEndpointAsync(requestContext, ImdsVersion.V1, cancellationToken).ConfigureAwait(false);
132136
if (imdsV1Response)
133137
{
134138
requestContext.Logger.Info("[Managed Identity] ImdsV1 detected.");

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,18 @@ public AcquireTokenForManagedIdentityParameterBuilder AcquireTokenForManagedIden
5656
}
5757

5858
/// <inheritdoc/>
59-
public async Task<ManagedIdentitySource> GetManagedIdentitySourceAsync()
59+
public async Task<ManagedIdentitySource> GetManagedIdentitySourceAsync(CancellationToken cancellationToken)
6060
{
6161
if (ManagedIdentityClient.s_sourceName != ManagedIdentitySource.None)
6262
{
6363
return ManagedIdentityClient.s_sourceName;
6464
}
6565

6666
// Create a temporary RequestContext for the logger and the IMDS probe request.
67-
var requestContext = new RequestContext(this.ServiceBundle, Guid.NewGuid(), null, CancellationToken.None);
67+
var requestContext = new RequestContext(this.ServiceBundle, Guid.NewGuid(), null, cancellationToken);
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(requestContext, isMtlsPopRequested: true).ConfigureAwait(false);
70+
return await ManagedIdentityClient.GetManagedIdentitySourceAsync(requestContext, isMtlsPopRequested: true, cancellationToken).ConfigureAwait(false);
7171
}
7272

7373
/// <summary>

src/client/Microsoft.Identity.Client/PublicApi/net462/PublicAPI.Shipped.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,6 @@ const Microsoft.Identity.Client.MsalError.MtlsNotSupportedForManagedIdentity = "
10861086
const Microsoft.Identity.Client.MsalError.MtlsPopTokenNotSupportedinImdsV1 = "mtls_pop_token_not_supported_in_imds_v1" -> string
10871087
Microsoft.Identity.Client.IMsalMtlsHttpClientFactory
10881088
Microsoft.Identity.Client.IMsalMtlsHttpClientFactory.GetHttpClient(System.Security.Cryptography.X509Certificates.X509Certificate2 x509Certificate2) -> System.Net.Http.HttpClient
1089-
Microsoft.Identity.Client.ManagedIdentityApplication.GetManagedIdentitySourceAsync() -> System.Threading.Tasks.Task<Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource>
10901089
Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource.ImdsV2 = 8 -> Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource
10911090
Microsoft.Identity.Client.ManagedIdentityApplicationBuilder.WithExtraQueryParameters(System.Collections.Generic.IDictionary<string, string> extraQueryParameters) -> Microsoft.Identity.Client.ManagedIdentityApplicationBuilder
10921091
static Microsoft.Identity.Client.ApplicationBase.ResetStateForTest() -> void
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
const Microsoft.Identity.Client.MsalError.ManagedIdentityAllSourcesUnavailable = "managed_identity_all_sources_unavailable" -> string
2+
Microsoft.Identity.Client.ManagedIdentityApplication.GetManagedIdentitySourceAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource>

src/client/Microsoft.Identity.Client/PublicApi/net472/PublicAPI.Shipped.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,6 @@ const Microsoft.Identity.Client.MsalError.MtlsNotSupportedForManagedIdentity = "
10861086
const Microsoft.Identity.Client.MsalError.MtlsPopTokenNotSupportedinImdsV1 = "mtls_pop_token_not_supported_in_imds_v1" -> string
10871087
Microsoft.Identity.Client.IMsalMtlsHttpClientFactory
10881088
Microsoft.Identity.Client.IMsalMtlsHttpClientFactory.GetHttpClient(System.Security.Cryptography.X509Certificates.X509Certificate2 x509Certificate2) -> System.Net.Http.HttpClient
1089-
Microsoft.Identity.Client.ManagedIdentityApplication.GetManagedIdentitySourceAsync() -> System.Threading.Tasks.Task<Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource>
10901089
Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource.ImdsV2 = 8 -> Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource
10911090
Microsoft.Identity.Client.ManagedIdentityApplicationBuilder.WithExtraQueryParameters(System.Collections.Generic.IDictionary<string, string> extraQueryParameters) -> Microsoft.Identity.Client.ManagedIdentityApplicationBuilder
10921091
static Microsoft.Identity.Client.ApplicationBase.ResetStateForTest() -> void
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
const Microsoft.Identity.Client.MsalError.ManagedIdentityAllSourcesUnavailable = "managed_identity_all_sources_unavailable" -> string
2+
Microsoft.Identity.Client.ManagedIdentityApplication.GetManagedIdentitySourceAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource>

src/client/Microsoft.Identity.Client/PublicApi/net8.0-android/PublicAPI.Shipped.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,6 @@ const Microsoft.Identity.Client.MsalError.MtlsNotSupportedForManagedIdentity = "
10521052
const Microsoft.Identity.Client.MsalError.MtlsPopTokenNotSupportedinImdsV1 = "mtls_pop_token_not_supported_in_imds_v1" -> string
10531053
Microsoft.Identity.Client.IMsalMtlsHttpClientFactory
10541054
Microsoft.Identity.Client.IMsalMtlsHttpClientFactory.GetHttpClient(System.Security.Cryptography.X509Certificates.X509Certificate2 x509Certificate2) -> System.Net.Http.HttpClient
1055-
Microsoft.Identity.Client.ManagedIdentityApplication.GetManagedIdentitySourceAsync() -> System.Threading.Tasks.Task<Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource>
10561055
Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource.ImdsV2 = 8 -> Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource
10571056
Microsoft.Identity.Client.ManagedIdentityApplicationBuilder.WithExtraQueryParameters(System.Collections.Generic.IDictionary<string, string> extraQueryParameters) -> Microsoft.Identity.Client.ManagedIdentityApplicationBuilder
10581057
static Microsoft.Identity.Client.ApplicationBase.ResetStateForTest() -> void
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
const Microsoft.Identity.Client.MsalError.ManagedIdentityAllSourcesUnavailable = "managed_identity_all_sources_unavailable" -> string
2+
Microsoft.Identity.Client.ManagedIdentityApplication.GetManagedIdentitySourceAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource>

src/client/Microsoft.Identity.Client/PublicApi/net8.0-ios/PublicAPI.Shipped.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,6 @@ const Microsoft.Identity.Client.MsalError.MtlsNotSupportedForManagedIdentity = "
10541054
const Microsoft.Identity.Client.MsalError.MtlsPopTokenNotSupportedinImdsV1 = "mtls_pop_token_not_supported_in_imds_v1" -> string
10551055
Microsoft.Identity.Client.IMsalMtlsHttpClientFactory
10561056
Microsoft.Identity.Client.IMsalMtlsHttpClientFactory.GetHttpClient(System.Security.Cryptography.X509Certificates.X509Certificate2 x509Certificate2) -> System.Net.Http.HttpClient
1057-
Microsoft.Identity.Client.ManagedIdentityApplication.GetManagedIdentitySourceAsync() -> System.Threading.Tasks.Task<Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource>
10581057
Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource.ImdsV2 = 8 -> Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource
10591058
Microsoft.Identity.Client.ManagedIdentityApplicationBuilder.WithExtraQueryParameters(System.Collections.Generic.IDictionary<string, string> extraQueryParameters) -> Microsoft.Identity.Client.ManagedIdentityApplicationBuilder
10601059
static Microsoft.Identity.Client.ApplicationBase.ResetStateForTest() -> void

0 commit comments

Comments
 (0)