-
Notifications
You must be signed in to change notification settings - Fork 403
Add IsMtlsPopSupportedByHost to ManagedIdentitySourceResult #6026
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
Open
bgavrilMS
wants to merge
2
commits into
bogavril/fix-imds-source-caching-6024
Choose a base branch
from
bogavril/mtls-pop-host-check
base: bogavril/fix-imds-source-caching-6024
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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; } | ||
| } | ||
| } |
99 changes: 99 additions & 0 deletions
99
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,99 @@ | ||
| // 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 (OperationCanceledException) | ||
| { | ||
| throw; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.Info($"[Managed Identity] IMDS compute metadata request failed with exception: {ex.Message}"); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logs it as error |
||
| 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; | ||
| } | ||
|
|
||
| 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
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
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
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
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
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
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
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
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
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
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.
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 catch throw?