Skip to content
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

[MSI V2 Feature] Adding IMDSV2 probe logic #5135

Open
wants to merge 1 commit into
base: msiv2-feature
Choose a base branch
from

Conversation

gladjohn
Copy link
Contributor

@gladjohn gladjohn commented Feb 11, 2025

Fixes #5115

Please refer to the above task for more details. This is PR is part of a feature branch.

This pull request introduces several changes to the Microsoft.Identity.Client library, focusing on enhancing managed identity support, particularly with the addition of a new ImdsV2 managed identity source. The changes include creating new classes, updating existing methods, and adding new methods to support asynchronous operations.

Key changes include:

Enhancements to Managed Identity Support:

  • Introduced ImdsV2ManagedIdentitySource class to handle the new ImdsV2 managed identity source. (src/client/Microsoft.Identity.Client/ManagedIdentity/ImdsV2ManagedIdentitySource.cs)
  • Added ImdsCredentialProbeManager class to probe the IMDS credential endpoint and determine its support. (src/client/Microsoft.Identity.Client/ManagedIdentity/ImdsCredentialProbeManager.cs)
  • Updated ManagedIdentitySource enum to include the new ImdsV2 value. (src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentitySource.cs)

Asynchronous Method Updates:

  • Modified ManagedIdentityClient to use asynchronous methods for creating instances and selecting managed identity sources. (src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityClient.cs) [1] [2]
  • Updated SendTokenRequestForManagedIdentityAsync method to use the new asynchronous CreateAsync method for ManagedIdentityClient. (src/client/Microsoft.Identity.Client/Internal/Requests/ManagedIdentityAuthRequest.cs)

Public API Updates:

  • Deprecated the synchronous GetManagedIdentitySource method and introduced an asynchronous GetManagedIdentitySourceAsync method in ManagedIdentityApplication. (src/client/Microsoft.Identity.Client/ManagedIdentityApplication.cs)
  • Added new public API entries for the ImdsV2 managed identity source and the asynchronous method in various PublicAPI.Unshipped.txt files. (src/client/Microsoft.Identity.Client/PublicApi/net462/PublicAPI.Unshipped.txt, src/client/Microsoft.Identity.Client/PublicApi/net472/PublicAPI.Unshipped.txt, src/client/Microsoft.Identity.Client/PublicApi/net8.0-android/PublicAPI.Unshipped.txt)

@gladjohn gladjohn marked this pull request as ready for review February 11, 2025 21:17
@gladjohn gladjohn requested a review from a team as a code owner February 11, 2025 21:17
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static async Task<ManagedIdentitySource> GetManagedIdentitySourceAsync(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: be consistent with visibility modifiers. Use internal everywhere

/// <exception cref="ArgumentNullException"></exception>
public static async Task<ManagedIdentitySource> GetManagedIdentitySourceAsync(
IServiceBundle serviceBundle,
CancellationToken cancellationToken = default)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit; don't use default params in internal code. it leads other developers to use the simpler versions without thinking too much.

@@ -45,6 +46,7 @@ public static void ResetInternalStaticCaches()
OidcRetrieverWithCache.ResetCacheForTest();
AuthorityManager.ClearValidationCache();
SingletonThrottlingManager.GetInstance().ResetCache();
ManagedIdentityClient.ResetManagedIdentitySourceCache();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't the method ResetInternalStaticCaches need to be exposed in public API for Azure SDK ? Probably experimental extensiblity PR. Can be as a separate PR, just making sure it's on your radar.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will track it as part of #5136

}
catch (Exception ex)
{
_logger.Info($"[Credential Probe] Exception during probe: {ex.Message}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be _logger.Error.

{
if (response == null)
{
_logger.Info("[Credential Probe] No response received from the server.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also be logger.error like on line 80?

}
}

private void LogResponseDetails(HttpResponse response)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it looks like this method can be merged into the EvaluateProbeResponse method. they are perfroming the same check on the response and printing the same messages

internal class ImdsV2ManagedIdentitySource : AbstractManagedIdentity
{
/// <summary>
/// Factory method to create an instance of `CredentialManagedIdentitySource`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This factory method doesnt seem to have much logic in it besides the log message. Can the constructor be used to instantiate this instead? it returns a AbstractManagedIdentity but it looks like the type will always be ImdsV2ManagedIdentitySource anyway?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants