-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Refactor VSHostObject credential extraction for COM compatibility and out-of-process execution #52856
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
YuliiaKovalova
merged 1 commit into
release/10.0.3xx
from
dev/ykovalova/add_com_support_to_containers_rebased
Feb 6, 2026
Merged
Refactor VSHostObject credential extraction for COM compatibility and out-of-process execution #52856
Changes from all commits
Commits
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
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
111 changes: 94 additions & 17 deletions
111
src/Containers/Microsoft.NET.Build.Containers/VSHostObject.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 |
|---|---|---|
| @@ -1,44 +1,121 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Reflection; | ||
| using System.Text.Json; | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Utilities; | ||
|
|
||
| namespace Microsoft.NET.Build.Containers.Tasks; | ||
|
|
||
| internal sealed class VSHostObject | ||
| internal sealed class VSHostObject(ITaskHost? hostObject, TaskLoggingHelper log) | ||
| { | ||
| private const string CredentialItemSpecName = "MsDeployCredential"; | ||
| private const string UserMetaDataName = "UserName"; | ||
| private const string PasswordMetaDataName = "Password"; | ||
| IEnumerable<ITaskItem>? _hostObject; | ||
|
|
||
| public VSHostObject(IEnumerable<ITaskItem>? hostObject) | ||
| private readonly ITaskHost? _hostObject = hostObject; | ||
| private readonly TaskLoggingHelper _log = log; | ||
|
|
||
| /// <summary> | ||
| /// Tries to extract credentials from the host object. | ||
| /// </summary> | ||
| /// <returns>A tuple of (username, password) if credentials were found with non-empty username, null otherwise.</returns> | ||
| public (string username, string password)? TryGetCredentials() | ||
| { | ||
| _hostObject = hostObject; | ||
| if (_hostObject is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| IEnumerable<ITaskItem>? taskItems = GetTaskItems(); | ||
| if (taskItems is null) | ||
| { | ||
| _log.LogMessage(MessageImportance.Low, "No task items found in host object."); | ||
| return null; | ||
| } | ||
|
|
||
| ITaskItem? credentialItem = taskItems.FirstOrDefault(p => p.ItemSpec == CredentialItemSpecName); | ||
| if (credentialItem is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| string username = credentialItem.GetMetadata(UserMetaDataName); | ||
| if (string.IsNullOrEmpty(username)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| string password = credentialItem.GetMetadata(PasswordMetaDataName); | ||
| return (username, password); | ||
| } | ||
|
|
||
| public bool ExtractCredentials(out string username, out string password, Action<string> logMethod) | ||
| private IEnumerable<ITaskItem>? GetTaskItems() | ||
| { | ||
| bool retVal = false; | ||
| username = password = string.Empty; | ||
| if (_hostObject != null) | ||
| try | ||
| { | ||
| ITaskItem credentialItem = _hostObject.FirstOrDefault<ITaskItem>(p => p.ItemSpec == CredentialItemSpecName); | ||
| if (credentialItem != null) | ||
| // This call mirrors the behavior of Microsoft.WebTools.Publish.MSDeploy.VSMsDeployTaskHostObject.QueryAllTaskItems. | ||
| // Expected contract: | ||
| // - Instance method on the host object named "QueryAllTaskItems". | ||
| // - Signature: string QueryAllTaskItems(). | ||
| // - Returns a JSON array of objects with the shape: | ||
| // [{ "ItemSpec": "<string>", "Metadata": { "<key>": "<value>", ... } }, ...] | ||
| // The JSON is deserialized into TaskItemDto records and converted to ITaskItem instances. | ||
| // Only UserName and Password metadata are extracted to avoid conflicts with reserved MSBuild metadata. | ||
| string? rawTaskItems = (string?)_hostObject!.GetType().InvokeMember( | ||
| "QueryAllTaskItems", | ||
| BindingFlags.InvokeMethod, | ||
YuliiaKovalova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| null, | ||
| _hostObject, | ||
| null); | ||
|
|
||
| if (!string.IsNullOrEmpty(rawTaskItems)) | ||
| { | ||
| retVal = true; | ||
| username = credentialItem.GetMetadata(UserMetaDataName); | ||
| if (!string.IsNullOrEmpty(username)) | ||
| List<TaskItemDto>? dtos = JsonSerializer.Deserialize<List<TaskItemDto>>(rawTaskItems); | ||
| if (dtos is not null && dtos.Count > 0) | ||
| { | ||
| password = credentialItem.GetMetadata(PasswordMetaDataName); | ||
| _log.LogMessage(MessageImportance.Low, "Successfully retrieved task items via QueryAllTaskItems."); | ||
| return dtos.Select(ConvertToTaskItem).ToList(); | ||
| } | ||
| else | ||
| } | ||
|
|
||
| _log.LogMessage(MessageImportance.Low, "QueryAllTaskItems returned null or empty result."); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _log.LogMessage(MessageImportance.Low, "Exception trying to call QueryAllTaskItems: {0}", ex.Message); | ||
| } | ||
YuliiaKovalova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Fallback: try to use the host object directly as IEnumerable<ITaskItem> (legacy behavior). | ||
| if (_hostObject is IEnumerable<ITaskItem> enumerableHost) | ||
| { | ||
| _log.LogMessage(MessageImportance.Low, "Falling back to IEnumerable<ITaskItem> host object."); | ||
| return enumerableHost; | ||
| } | ||
|
|
||
| return null; | ||
|
|
||
| static TaskItem ConvertToTaskItem(TaskItemDto dto) | ||
| { | ||
| TaskItem taskItem = new(dto.ItemSpec ?? string.Empty); | ||
| if (dto.Metadata is not null) | ||
| { | ||
| if (dto.Metadata.TryGetValue(UserMetaDataName, out string? userName)) | ||
| { | ||
| logMethod("HostObject credentials not detected. Falling back to Docker credential retrieval."); | ||
| taskItem.SetMetadata(UserMetaDataName, userName); | ||
| } | ||
|
|
||
| if (dto.Metadata.TryGetValue(PasswordMetaDataName, out string? password)) | ||
| { | ||
| taskItem.SetMetadata(PasswordMetaDataName, password); | ||
| } | ||
| } | ||
|
|
||
| return taskItem; | ||
| } | ||
| return retVal; | ||
| } | ||
YuliiaKovalova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private readonly record struct TaskItemDto(string? ItemSpec, Dictionary<string, string>? Metadata); | ||
| } | ||
|
|
||
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.