-
Notifications
You must be signed in to change notification settings - Fork 404
Add GetManagedIdentityCapabilitiesAsync host capability discovery API (Phase 1) #6049
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
Merged
Robbie-Microsoft
merged 7 commits into
main
from
rginsburg/GetManagedIdentityCapabilitiesAsync
Jun 4, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bc68491
Add GetManagedIdentityCapabilitiesAsync managed identity capability API
Robbie-Microsoft 619a20c
Add KeyGuard binding-strength tier to IMDSv2 capability discovery
Robbie-Microsoft 339fee0
Document capability-discovery key-provisioning side effect
Robbie-Microsoft 97b4bc7
Address PR review: single-flight discovery lock, clearer errors, chan…
Robbie-Microsoft d93eb8f
Rename MtlsBindingStrength.Bearer to None; simplify compute-metadata …
Robbie-Microsoft 1f8d97b
Remove stray scratch file and fix CHANGELOG enum value name
Robbie-Microsoft 8af439f
Merge branch 'main' into rginsburg/GetManagedIdentityCapabilitiesAsync
Robbie-Microsoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/client/Microsoft.Identity.Client/AppConfig/MtlsBindingStrength.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.Identity.Client.AppConfig | ||
| { | ||
| /// <summary> | ||
| /// Describes the strength with which a token can be bound to a cryptographic key on the | ||
| /// current host. Higher values indicate stronger binding. The value reflects what the host | ||
| /// is capable of producing, not what a particular request used. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This type is shared by managed identity and confidential client mTLS Proof-of-Possession | ||
| /// scenarios. A value greater than <see cref="None"/> means the host can bind a token to a | ||
| /// key; it does <b>not</b> by itself imply hardware attestation. Attestation corresponds to | ||
| /// the <see cref="KeyGuard"/> tier specifically. | ||
| /// </remarks> | ||
| public enum MtlsBindingStrength | ||
| { | ||
| /// <summary> | ||
| /// No key binding is available, so the host cannot perform mTLS Proof-of-Possession. This | ||
| /// is the floor of the range (for example, on .NET Framework 4.6.2, which does not support | ||
| /// PoP). | ||
| /// </summary> | ||
| None = 0, | ||
|
|
||
| /// <summary> | ||
| /// The token can be bound to a software-backed key (for example, a persisted CNG key on | ||
| /// Windows, or a software RSA key elsewhere). The key is not hardware-isolated. | ||
| /// </summary> | ||
| Software = 1, | ||
|
|
||
| // 2 is reserved for a future tier (for example, TPM-backed keys). | ||
|
gladjohn marked this conversation as resolved.
|
||
|
|
||
| /// <summary> | ||
| /// The token can be bound to a key isolated by Virtualization-based Security (VBS), such | ||
| /// as KeyGuard on a Trusted Launch (TVM) or Confidential (CVM) virtual machine. This is | ||
| /// the only tier that implies hardware-backed attestation. | ||
| /// </summary> | ||
| KeyGuard = 3 | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/client/Microsoft.Identity.Client/ManagedIdentity/ComputeMetadataResponse.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Identity.Client.Platforms.net; | ||
| using JsonProperty = System.Text.Json.Serialization.JsonPropertyNameAttribute; | ||
|
|
||
| namespace Microsoft.Identity.Client.ManagedIdentity | ||
| { | ||
| /// <summary> | ||
| /// Represents compute metadata retrieved from the Azure Instance Metadata Service (IMDS). | ||
| /// </summary> | ||
| [JsonObject] | ||
| [Preserve(AllMembers = true)] | ||
| internal class ComputeMetadataResponse | ||
| { | ||
| /// <summary>Operating system type (e.g., Windows, Linux).</summary> | ||
| [JsonProperty("osType")] | ||
| public string OsType { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Security profile indicating platform security posture. May be null when IMDS | ||
| /// does not return security profile information for the current VM. | ||
| /// </summary> | ||
| [JsonProperty("securityProfile")] | ||
| public ComputeSecurityProfile SecurityProfile { get; set; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Represents the security profile of an Azure VM from IMDS compute metadata. | ||
| /// </summary> | ||
| [JsonObject] | ||
| [Preserve(AllMembers = true)] | ||
| internal class ComputeSecurityProfile | ||
| { | ||
| /// <summary>Security type of the VM (e.g., TrustedLaunch, ConfidentialVM).</summary> | ||
| [JsonProperty("securityType")] | ||
| public string SecurityType { get; set; } | ||
| } | ||
| } |
95 changes: 95 additions & 0 deletions
95
src/client/Microsoft.Identity.Client/ManagedIdentity/ImdsComputeMetadataManager.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Identity.Client.Core; | ||
| using Microsoft.Identity.Client.Http; | ||
| using Microsoft.Identity.Client.Http.Retry; | ||
| using Microsoft.Identity.Client.Utils; | ||
|
|
||
| namespace Microsoft.Identity.Client.ManagedIdentity | ||
| { | ||
| /// <summary> | ||
| /// Fetches compute metadata from the Azure Instance Metadata Service (IMDS) | ||
| /// to determine VM characteristics such as OS type and security profile. | ||
| /// </summary> | ||
| internal static class ImdsComputeMetadataManager | ||
| { | ||
| internal const string ImdsComputePath = "/metadata/instance/compute"; | ||
| internal const string ImdsComputeApiVersion = "2021-02-01"; | ||
|
|
||
| internal static async Task<ComputeMetadataResponse> GetComputeMetadataAsync( | ||
| IHttpManager httpManager, | ||
| ILoggerAdapter logger, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| var headers = new Dictionary<string, string> | ||
| { | ||
| { "Metadata", "true" } | ||
| }; | ||
|
|
||
| try | ||
| { | ||
| string queryParams = | ||
| $"{ImdsManagedIdentitySource.ApiVersionQueryParam}={ImdsComputeApiVersion}"; | ||
|
|
||
| Uri endpoint = ImdsManagedIdentitySource.GetValidatedEndpoint( | ||
| logger, | ||
| ImdsComputePath, | ||
| queryParams); | ||
|
|
||
| HttpResponse response = await httpManager.SendRequestAsync( | ||
| endpoint, | ||
| headers, | ||
| body: null, | ||
| method: HttpMethod.Get, | ||
| logger: logger, | ||
| doNotThrow: true, | ||
| mtlsCertificate: null, | ||
| validateServerCertificate: null, | ||
| cancellationToken: cancellationToken, | ||
| retryPolicy: new ImdsRetryPolicy()) | ||
| .ConfigureAwait(false); | ||
|
|
||
| if (response is null || response.StatusCode != HttpStatusCode.OK) | ||
| { | ||
| logger.Info($"[Managed Identity] IMDS compute metadata request failed. " + | ||
| $"StatusCode: {response?.StatusCode}"); | ||
| return null; | ||
| } | ||
|
|
||
| return JsonHelper.TryToDeserializeFromJson<ComputeMetadataResponse>(response.Body); | ||
| } | ||
| catch (Exception ex) when (ex is not OperationCanceledException) | ||
| { | ||
| logger.Info($"[Managed Identity] IMDS compute metadata request failed with exception: {ex.Message}"); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Determines whether the host VM supports mTLS PoP based on compute metadata. | ||
| /// mTLS PoP is supported when the VM runs Windows and is a TVM (TrustedLaunch) or CVM (ConfidentialVM). | ||
| /// </summary> | ||
| internal static bool IsMtlsPopSupported(ComputeMetadataResponse metadata) | ||
| { | ||
| if (metadata is null) | ||
| { | ||
| return false; | ||
|
Robbie-Microsoft marked this conversation as resolved.
|
||
| } | ||
|
|
||
| bool isWindows = string.Equals(metadata.OsType, "Windows", StringComparison.OrdinalIgnoreCase); | ||
|
|
||
| string securityType = metadata.SecurityProfile?.SecurityType; | ||
| bool isTvmOrCvm = string.Equals(securityType, "TrustedLaunch", StringComparison.OrdinalIgnoreCase) | ||
| || string.Equals(securityType, "ConfidentialVM", StringComparison.OrdinalIgnoreCase); | ||
|
|
||
| return isWindows && isTvmOrCvm; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityCapabilities.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Identity.Client.AppConfig; | ||
|
|
||
| namespace Microsoft.Identity.Client.ManagedIdentity | ||
| { | ||
| /// <summary> | ||
| /// Describes the managed identity capabilities detected for the current host, including the | ||
| /// detected source and the strength with which tokens can be bound to a key. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This type is returned by <see cref="ManagedIdentityApplication.GetManagedIdentityCapabilitiesAsync"/>. | ||
| /// It is useful for credential chains such as <c>DefaultAzureCredential</c> to decide whether | ||
| /// managed identity is available and what binding strength the host supports. | ||
| /// </remarks> | ||
| public class ManagedIdentityCapabilities | ||
| { | ||
| /// <summary> | ||
| /// Gets the detected managed identity source. | ||
| /// </summary> | ||
| /// <value> | ||
| /// The <see cref="ManagedIdentitySource"/> detected on the environment, or | ||
| /// <see cref="ManagedIdentitySource.None"/> if no source was detected. The internal | ||
| /// IMDS v1/v2 distinction is not surfaced here; both report | ||
| /// <see cref="ManagedIdentitySource.Imds"/>. | ||
| /// </value> | ||
| public ManagedIdentitySource Source { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the reason detection failed, if any. | ||
| /// </summary> | ||
| /// <value> | ||
| /// A single string describing why managed identity detection failed, or <c>null</c> when | ||
| /// a source was detected. | ||
| /// </value> | ||
| public string ErrorReason { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the highest binding strength the current host is capable of producing. | ||
| /// </summary> | ||
| /// <value> | ||
| /// The strongest <see cref="MtlsBindingStrength"/> available on this host. This is the | ||
| /// primary capability signal; callers should branch on it rather than on the source label. | ||
| /// </value> | ||
| public MtlsBindingStrength MaxSupportedBindingStrength { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets a value indicating whether the host can bind a token to a key (mTLS | ||
| /// Proof-of-Possession). | ||
| /// </summary> | ||
| /// <value> | ||
| /// <c>true</c> when <see cref="MaxSupportedBindingStrength"/> is greater than | ||
| /// <see cref="MtlsBindingStrength.None"/>. This means the host can bind a token to a key; | ||
| /// it does <b>not</b> imply hardware attestation. Callers that require attestation must | ||
| /// check for the <see cref="MtlsBindingStrength.KeyGuard"/> tier. | ||
| /// </value> | ||
| public bool IsMtlsPopSupportedByHost => MaxSupportedBindingStrength > MtlsBindingStrength.None; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ManagedIdentityCapabilities"/> class. | ||
| /// </summary> | ||
| /// <param name="source">The detected managed identity source.</param> | ||
| /// <param name="maxSupportedBindingStrength">The highest binding strength the host supports.</param> | ||
| /// <param name="errorReason">The reason detection failed, or <c>null</c> on success.</param> | ||
| internal ManagedIdentityCapabilities( | ||
| ManagedIdentitySource source, | ||
| MtlsBindingStrength maxSupportedBindingStrength, | ||
| string errorReason = null) | ||
| { | ||
| Source = source; | ||
| MaxSupportedBindingStrength = maxSupportedBindingStrength; | ||
| ErrorReason = errorReason; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.