Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/release-notes/1.31/dsc-direct-package-path.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Bug Fixes

### Minor Bug Fixes

* Configuration now launches DSC directly from its installed package location and attempts to re-register unhealthy DSC packages before use.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
namespace Microsoft.Management.Configuration.Processor.DSCv3.Helpers
{
using System;
using static Microsoft.Management.Configuration.Processor.DSCv3.Helpers.PackageInformation;

/// <summary>
/// Provides the state machine that decides which DSC package to use.
/// </summary>
internal class FindDscPackageStateMachine
{
private const string StableDscPackageFamilyName = "Microsoft.DesiredStateConfiguration_8wekyb3d8bbwe";
private const string PreviewDscPackageFamilyName = "Microsoft.DesiredStateConfiguration-Preview_8wekyb3d8bbwe";

private readonly Version minimumStableVersion = new Version(3, 1);
private readonly Version minimumPreviewVersion = new Version(3, 1, 7);

Expand Down Expand Up @@ -74,6 +72,11 @@ public enum Transition
NotFound,
}

/// <summary>
/// Gets or sets the diagnostics sink.
/// </summary>
public IDiagnosticsSink? DiagnosticsSink { get; set; }

/// <summary>
/// Gets the file path of the DSC (Desired State Configuration) executable.
/// </summary>
Expand All @@ -90,14 +93,14 @@ public string? DscExecutablePath
PackageInformation stableInformation = new PackageInformation(StableDscPackageFamilyName);
if (stableInformation.IsInstalled && stableInformation.Version >= this.minimumStableVersion)
{
return stableInformation.AliasPath;
return stableInformation.GetExecutablePath(this.DiagnosticsSink);
}
else
{
PackageInformation previewInformation = new PackageInformation(PreviewDscPackageFamilyName);
if (previewInformation.IsInstalled && previewInformation.Version >= this.minimumPreviewVersion)
{
return previewInformation.AliasPath;
return previewInformation.GetExecutablePath(this.DiagnosticsSink);
}
else
{
Expand Down Expand Up @@ -178,7 +181,7 @@ public Transition DetermineNextTransition()

private Transition Found(PackageInformation packageInformation)
{
this.dscExecutablePath = packageInformation.AliasPath;
this.dscExecutablePath = packageInformation.GetExecutablePath(this.DiagnosticsSink);
this.currentState = State.Terminated;
return Transition.Found;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,26 @@ namespace Microsoft.Management.Configuration.Processor.DSCv3.Helpers
{
using System;
using System.IO;
using Windows.ApplicationModel;
using Windows.Management.Deployment;

/// <summary>
/// Contains information about a package.
/// </summary>
internal class PackageInformation
{
/// <summary>
/// The stable DSC package family name.
/// </summary>
public const string StableDscPackageFamilyName = "Microsoft.DesiredStateConfiguration_8wekyb3d8bbwe";

/// <summary>
/// The preview DSC package family name.
/// </summary>
public const string PreviewDscPackageFamilyName = "Microsoft.DesiredStateConfiguration-Preview_8wekyb3d8bbwe";

private const string DscExecutableFileName = "dsc.exe";
private Package? package;

/// <summary>
/// Initializes a new instance of the <see cref="PackageInformation"/> class.
Expand All @@ -37,37 +49,121 @@ public PackageInformation(string familyName)
if (this.Version == null || version > this.Version)
{
this.Version = version;
this.package = package;
}
}
}
}

string localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string aliasPath = Path.Combine(localAppData, "Microsoft\\WindowsApps", familyName, DscExecutableFileName);
/// <summary>
/// Gets a value indicating whether the package is installed or not.
/// </summary>
public bool IsInstalled => this.package != null;

/// <summary>
/// Gets the version of the package.
/// </summary>
public Version? Version { get; private set; }

if (Path.Exists(aliasPath))
/// <summary>
/// Gets the packaged DSC executable path, repairing an unhealthy registration first.
/// </summary>
/// <param name="diagnosticsSink">The diagnostics sink.</param>
/// <returns>The executable path, or null if the package is not installed.</returns>
public string? GetExecutablePath(IDiagnosticsSink? diagnosticsSink)
{
if (this.package == null)
{
this.AliasPath = aliasPath;
return null;
}

if (this.AliasPath != null && this.Version != null)
Package selectedPackage = this.package;
string packageFullName = selectedPackage.Id.FullName;
EnsureHealthy(
packageFullName,
() => selectedPackage.Status.VerifyIsOK(),
() =>
{
PackageManager packageManager = new PackageManager();
DeploymentResult result = packageManager.RegisterPackageByFullNameAsync(
packageFullName, null, DeploymentOptions.None).AsTask().GetAwaiter().GetResult();

if (result.ExtendedErrorCode != null)
{
throw new InvalidOperationException($"Failed to re-register DSC package '{packageFullName}': {result.ErrorText}", result.ExtendedErrorCode);
}

selectedPackage = packageManager.FindPackageForUser(string.Empty, packageFullName)
?? throw new InvalidOperationException($"DSC package '{packageFullName}' was not found after re-registration.");
},
diagnosticsSink);

this.package = selectedPackage;
string executablePath = Path.Combine(selectedPackage.InstalledLocation.Path, DscExecutableFileName);
if (!File.Exists(executablePath))
{
this.IsInstalled = true;
throw new FileNotFoundException($"The DSC executable was not found in package '{packageFullName}'.", executablePath);
}

return executablePath;
}

/// <summary>
/// Gets a value indicating whether the package is installed or not.
/// Finds the immutable package directory containing a registered Store DSC executable.
/// </summary>
public bool IsInstalled { get; private set; }
/// <param name="executablePath">The normalized executable path.</param>
/// <returns>The package directory, or null if the path is not a trusted packaged executable.</returns>
internal static string? FindTrustedPackageRoot(string executablePath)
{
if (!string.Equals(Path.GetFileName(executablePath), DscExecutableFileName, StringComparison.OrdinalIgnoreCase))
{
return null;
}

/// <summary>
/// Gets the path to the dsc.exe alias.
/// </summary>
public string? AliasPath { get; private set; }
foreach (string familyName in new[] { StableDscPackageFamilyName, PreviewDscPackageFamilyName })
{
var information = new PackageInformation(familyName);
Package? package = information.package;
if (package == null || package.IsDevelopmentMode || package.SignatureKind != PackageSignatureKind.Store || !package.Status.VerifyIsOK())
{
continue;
}

string packageRoot = package.InstalledLocation.Path;
if (!string.Equals(executablePath, Path.Combine(packageRoot, DscExecutableFileName), StringComparison.OrdinalIgnoreCase))
{
continue;
}

return packageRoot;
}

return null;
}

/// <summary>
/// Gets the version of the package.
/// Re-registers an unhealthy package once and verifies that the repair succeeded.
/// </summary>
public Version? Version { get; private set; }
/// <param name="packageFullName">The package full name.</param>
/// <param name="verifyIsOK">Checks the current package status.</param>
/// <param name="register">Re-registers the package and refreshes its information.</param>
/// <param name="diagnosticsSink">The diagnostics sink.</param>
internal static void EnsureHealthy(string packageFullName, Func<bool> verifyIsOK, Action register, IDiagnosticsSink? diagnosticsSink)
{
if (verifyIsOK())
{
return;
}

diagnosticsSink?.OnDiagnostics(DiagnosticLevel.Warning, $"DSC package '{packageFullName}' is unhealthy; attempting re-registration.");
register();

if (!verifyIsOK())
{
throw new InvalidOperationException($"DSC package '{packageFullName}' is still unhealthy after re-registration.");
}

diagnosticsSink?.OnDiagnostics(DiagnosticLevel.Informational, $"DSC package '{packageFullName}' was successfully re-registered.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ namespace Microsoft.Management.Configuration.Processor.DSCv3.Helpers
/// also opened without following reparse points and rejected if it is one, so a component that is
/// swapped for a link while the path is being pinned is detected rather than followed.
/// </para>
/// <para>
/// For registered Store DSC packages, pinning stops at the immutable installation directory
/// reported by the package APIs. Its ancestors need not be accessible to the caller.
/// </para>
/// </summary>
internal sealed class ProcessorPathBinding : IDisposable
{
Expand Down Expand Up @@ -122,6 +126,7 @@ private void Pin(string path, string expectedHash, bool isAlias)
try
{
string finalPath = ProcessorPathIntegrity.GetFinalPath(file.ContentHandle);
string? packageRoot = isAlias ? null : PackageInformation.FindTrustedPackageRoot(finalPath);

// Pinning the ancestors is itself done by path, so a rename that happens while the
// ancestors are being pinned could leave a directory pinned that is no longer part of
Expand All @@ -132,7 +137,7 @@ private void Pin(string path, string expectedHash, bool isAlias)
{
try
{
this.PinAncestors(finalPath);
this.PinAncestors(finalPath, packageRoot);

string currentPath = ProcessorPathIntegrity.GetFinalPath(file.ContentHandle);
if (string.Equals(currentPath, finalPath, StringComparison.OrdinalIgnoreCase))
Expand Down Expand Up @@ -170,7 +175,7 @@ private void Pin(string path, string expectedHash, bool isAlias)
}
}

private void PinAncestors(string finalPath)
private void PinAncestors(string finalPath, string? packageRoot)
{
// Stop below the root; a volume root cannot be renamed or deleted, and holding a handle
// to it would interfere with operations such as dismounting the volume.
Expand All @@ -179,6 +184,11 @@ private void PinAncestors(string finalPath)
directory = Path.GetDirectoryName(directory))
{
this.directoryHandles.Add(ProcessorPathIntegrity.PinDirectory(directory));

if (string.Equals(directory, packageRoot, StringComparison.OrdinalIgnoreCase))
{
break;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ public IDSCv3 DSCv3
/// <summary>
/// Gets or sets the diagnostics sink to use.
/// </summary>
public IDiagnosticsSink? DiagnosticsSink { get; set; } = null;
public IDiagnosticsSink? DiagnosticsSink
{
get => this.dscPackageStateMachine.DiagnosticsSink;
set => this.dscPackageStateMachine.DiagnosticsSink = value;
}

/// <summary>
/// Gets or sets a value indicating whether the processor should produce more verbose output.
Expand Down
Loading
Loading