-
Notifications
You must be signed in to change notification settings - Fork 369
CSR Metadata request acts as a probe for ImdsV2 #5359
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
base: rginsburg/msiv2_feature_branch
Are you sure you want to change the base?
CSR Metadata request acts as a probe for ImdsV2 #5359
Conversation
src/client/Microsoft.Identity.Client/ManagedIdentity/CsrMetadata.cs
Outdated
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/ManagedIdentity/CsrMetadata.cs
Outdated
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/ManagedIdentity/ImdsV2ManagedIdentitySource.cs
Outdated
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/ManagedIdentity/ImdsV2ManagedIdentitySource.cs
Outdated
Show resolved
Hide resolved
tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ImdsV2Tests.cs
Outdated
Show resolved
Hide resolved
tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ImdsV2Tests.cs
Outdated
Show resolved
Hide resolved
tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ImdsV2Tests.cs
Outdated
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityClient.cs
Outdated
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityClient.cs
Outdated
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/PublicApi/net472/PublicAPI.Shipped.txt
Outdated
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/ManagedIdentity/CsrMetadata.cs
Outdated
Show resolved
Hide resolved
tests/Microsoft.Identity.Test.Common/Core/Mocks/MockHttpMessageHandler.cs
Outdated
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/ManagedIdentity/ImdsV2ManagedIdentitySource.cs
Outdated
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/ManagedIdentity/ImdsManagedIdentitySource.cs
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/ManagedIdentity/ImdsV2ManagedIdentitySource.cs
Outdated
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/ManagedIdentity/ImdsV2ManagedIdentitySource.cs
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/ManagedIdentity/ImdsV2ManagedIdentitySource.cs
Outdated
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/ManagedIdentityApplication.cs
Outdated
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityClient.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to undo the changes to public API to get the managed identity source
f0f4eb3
to
6aa43d0
Compare
@neha-bhargava: done, after refactoring some code |
src/client/Microsoft.Identity.Client/ManagedIdentity/CsrMetadata.cs
Outdated
Show resolved
Hide resolved
retryPolicy: retryPolicy) | ||
.ConfigureAwait(false); | ||
} | ||
catch (Exception ex) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe @gladjohn said that probe failure occurs exclusively on 404?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(i.e. delete the catch block)
} | ||
} | ||
|
||
if (response.StatusCode != HttpStatusCode.OK) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gladjohn to review this. I believe you said probe failure should only happen on 404
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IF probe returns 404 - then no /metadata endpoint, flows continues with other source detection, if none are found we fallback to IMDS (Kubernetes etc.)
We should pass a "." in the body to the endpoint and if it exists we will get a bad request (400), then we should inspect the header, spec here
We should not throw anywhere if probe fails or if we do not find the endpoint.
something like,
private bool EvaluateProbeResponse(HttpResponse response)
{
if (response == null)
{
_logger.Info("[Probe] No response received from the server.");
return false;
}
_logger.Info($"[Probe] Evaluating response from credential endpoint. Status Code: {response.StatusCode}");
if (response.HeadersAsDictionary.TryGetValue("Server", out string serverHeader) &&
serverHeader.StartsWith(ImdsHeader, StringComparison.OrdinalIgnoreCase))
{
_logger.Info($"[Probe] Credential endpoint supported. Server: {serverHeader}");
return true;
}
_logger.Info($"[Probe] Credential endpoint not supported. Status Code: {response.StatusCode}");
return false;
}
|
||
if (!ValidateCsrMetadataResponse(response, requestContext.Logger, probeMode)) | ||
{ | ||
return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a good way to fail. Just throw exception like InvalidOperationException or smth
src/client/Microsoft.Identity.Client/ManagedIdentity/ImdsV2ManagedIdentitySource.cs
Outdated
Show resolved
Hide resolved
string serverHeader = response.HeadersAsDictionary.TryGetValue("server", out var value) ? value : null; | ||
if (serverHeader == null) | ||
{ | ||
if (probeMode) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment. ProbeMode only look at 404?
); | ||
if (!match.Success || !int.TryParse(match.Groups[1].Value, out int version) || version <= 1324) | ||
{ | ||
if (probeMode) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment
src/client/Microsoft.Identity.Client/ManagedIdentity/ImdsV2ManagedIdentitySource.cs
Outdated
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityClient.cs
Show resolved
Hide resolved
tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/AppServiceTests.cs
Outdated
Show resolved
Hide resolved
tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/AppServiceTests.cs
Show resolved
Hide resolved
@@ -55,13 +55,28 @@ public AcquireTokenForManagedIdentityParameterBuilder AcquireTokenForManagedIden | |||
resource); | |||
} | |||
|
|||
/// <inheritdoc/> | |||
public async Task<ManagedIdentitySource> GetManagedIdentitySourceAsync() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not keep this as static as well. The method to get the source was created static so that Azure Identity can detect if managed identity is available before creating the source. Was this discussed with them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@neha-bhargava, this method passes the Service Bundle through the ManagedIdentityClient
's GetManagedIdentitySourceAsync
method, and into ImdsV2ManagedIdentitySource
, where the csr metadata probe uses things from the Service Bundle: IdType, UserAssignedId, RetryPolicyFactory, HttpManager, Logger, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gladjohn Can you confirm that this would work with Azure Identity? Did you discuss with them?
This PR introduces ImdsV2 as a Managed Identity source, and focuses on executing a network request to get metadata to be used in the rest of the flow.