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

Add pwsh support for trust level and explicit #4750

Merged
merged 5 commits into from
Sep 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -2874,7 +2874,7 @@ Please specify one of them using the --source option to proceed.</value>
<data name="SettingsSetCommandShortDescription" xml:space="preserve">
<value>Sets the value of an admin setting.</value>
</data>
<data name="SourceRequireExplicitArgumentDescription" xml:space="preserve">
<data name="SourceExplicitArgumentDescription" xml:space="preserve">
<value>Excludes a source from discovery unless specified</value>
</data>
<data name="SourceListExplicit" xml:space="preserve">
Expand Down
4 changes: 4 additions & 0 deletions src/Microsoft.Management.Deployment/PackageCatalogInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ namespace winrt::Microsoft::Management::Deployment::implementation
return PackageCatalogTrustLevel::None;
}
}
bool PackageCatalogInfo::Explicit()
{
return m_sourceDetails.Explicit;
}
}
1 change: 1 addition & 0 deletions src/Microsoft.Management.Deployment/PackageCatalogInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace winrt::Microsoft::Management::Deployment::implementation
winrt::Windows::Foundation::DateTime LastUpdateTime();
winrt::Microsoft::Management::Deployment::PackageCatalogOrigin Origin();
winrt::Microsoft::Management::Deployment::PackageCatalogTrustLevel TrustLevel();
bool Explicit();

#if !defined(INCLUDE_ONLY_INTERFACE_METHODS)
private:
Expand Down
8 changes: 7 additions & 1 deletion src/Microsoft.Management.Deployment/PackageManager.idl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.
namespace Microsoft.Management.Deployment
{
[contractversion(10)]
[contractversion(11)] // For version 1.9
apicontract WindowsPackageManagerContract{};

/// State of the install
Expand Down Expand Up @@ -262,6 +262,12 @@ namespace Microsoft.Management.Deployment
PackageCatalogOrigin Origin { get; };
/// The trust level of the package catalog
PackageCatalogTrustLevel TrustLevel { get; };

[contract(Microsoft.Management.Deployment.WindowsPackageManagerContract, 11)]
{
/// Excludes a source from discovery unless specified.
Boolean Explicit{ get; };
}
}

/// A metadata item of a package version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Adds a new source.
## SYNTAX

```
Add-WinGetSource -Name <String> -Argument <String> [-Type <String>] [<CommonParameters>]
Add-WinGetSource -Name <String> -Argument <String> [-Type <String>] [-TrustLevel {Default | None | Trusted}] [-Explicit] [<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -68,6 +68,42 @@ Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False
```

### -Explicit

Excludes a source from discovery unless specified.

```yaml
Type: System.Management.Automation.SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### -TrustLevel

Specify the trust level of the WinGet source. The parameter accepts the following values:

- `None`
- `Trusted`

```yaml
Type: Microsoft.WinGet.Client.PSObjects.PSSourceTrustLevel
Parameter Sets: (All)
Aliases:
Accepted values: None, Trusted

Required: False
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### -Type

The type of the WinGet source. Most sources are `Microsoft.Rest`. The WinGet community repository
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// <copyright file="AddSourceCmdlet.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
Expand All @@ -7,6 +7,7 @@
namespace Microsoft.WinGet.Client.Cmdlets.Cmdlets
{
using System.Management.Automation;
using Microsoft.WinGet.Client.Cmdlets.PSObjects;
using Microsoft.WinGet.Client.Common;
using Microsoft.WinGet.Client.Engine.Commands;

Expand Down Expand Up @@ -42,13 +43,32 @@ public sealed class AddSourceCmdlet : PSCmdlet
ValueFromPipelineByPropertyName = true)]
public string Type { get; set; }

/// <summary>
/// Gets or sets the trust level of the source to add.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true)]
public PSSourceTrustLevel TrustLevel { get; set; } = PSSourceTrustLevel.Default;

/// <summary>
/// Gets or sets a value indicating whether the source to add is explicit.
/// </summary>
///
[Parameter(ValueFromPipelineByPropertyName = true)]
public SwitchParameter Explicit { get; set; }

/// <summary>
/// Adds source.
/// </summary>
protected override void ProcessRecord()
{
var command = new CliCommand(this);
command.AddSource(this.Name, this.Argument, this.Type);
command.AddSource(this.Name, this.Argument, this.Type, this.ConvertPSSourceTrustLevelToString(this.TrustLevel), this.Explicit.ToBool());
}

private string ConvertPSSourceTrustLevelToString(PSSourceTrustLevel trustLevel) => trustLevel switch
{
PSSourceTrustLevel.Default => string.Empty,
_ => trustLevel.ToString(),
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// -----------------------------------------------------------------------------
// <copyright file="PSSourceTrustLevel.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------

namespace Microsoft.WinGet.Client.Cmdlets.PSObjects
{
/// <summary>
/// This is the powershell argument equivalent of AppInstaller::Repository::SourceTrustLevel.
/// </summary>
public enum PSSourceTrustLevel
{
/// <summary>
/// Let winget decide.
/// </summary>
Default,

/// <summary>
/// None.
/// </summary>
None,

/// <summary>
/// Trusted.
/// </summary>
Trusted,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public CliCommand(PSCmdlet psCmdlet)
public void EnableSetting(string name)
{
Utilities.VerifyAdmin();
_ = this.Run("settings", $"--enable {name}");
_ = this.Run("settings", $"--enable \"{name}\"");
}

/// <summary>
Expand All @@ -43,7 +43,7 @@ public void EnableSetting(string name)
public void DisableSetting(string name)
{
Utilities.VerifyAdmin();
_ = this.Run("settings", $"--disable {name}");
_ = this.Run("settings", $"--disable \"{name}\"");
}

/// <summary>
Expand All @@ -70,17 +70,29 @@ public void GetSettings(bool asPlainText)
/// <param name="name">Name of source.</param>
/// <param name="arg">Arg of source.</param>
/// <param name="type">Type of source.</param>
public void AddSource(string name, string arg, string type)
/// <param name="trustLevel">Trust level of source.</param>
/// <param name="isExplicit">Make source explicit.</param>
public void AddSource(string name, string arg, string type, string trustLevel, bool isExplicit)
{
Utilities.VerifyAdmin();
if (string.IsNullOrEmpty(type))
string parameters = $"add --name \"{name}\" --arg \"{arg}\"";

if (!string.IsNullOrEmpty(type))
{
_ = this.Run("source", $"add --name {name} --arg {arg}", 300000);
parameters += $" --type \"{type}\"";
}
else

if (!string.IsNullOrEmpty(trustLevel))
{
_ = this.Run("source", $"add --name {name} --arg {arg} --type {type}", 300000);
parameters += $" --trust-level \"{trustLevel}\"";
}

if (isExplicit)
{
parameters += " --explicit";
}

_ = this.Run("source", parameters, 300000);
}

/// <summary>
Expand All @@ -90,7 +102,7 @@ public void AddSource(string name, string arg, string type)
public void RemoveSource(string name)
{
Utilities.VerifyAdmin();
_ = this.Run("source", $"remove --name {name}");
_ = this.Run("source", $"remove --name \"{name}\"");
}

/// <summary>
Expand All @@ -100,7 +112,7 @@ public void RemoveSource(string name)
public void ResetSourceByName(string name)
{
Utilities.VerifyAdmin();
_ = this.Run("source", $"reset --name {name} --force");
_ = this.Run("source", $"reset --name \"{name}\" --force");
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// <copyright file="PSSourceResult.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------

namespace Microsoft.WinGet.Client.Engine.PSObjects
{
using System.Management.Automation;

/// <summary>
/// SourceResult wrapper object for displaying to PowerShell.
/// </summary>
Expand All @@ -23,6 +21,8 @@ internal PSSourceResult(Management.Deployment.PackageCatalogReference catalogRef
this.Name = info.Name;
this.Argument = info.Argument;
this.Type = info.Type;
this.TrustLevel = info.TrustLevel.ToString();
this.Explicit = info.Explicit;
}

/// <summary>
Expand All @@ -39,5 +39,15 @@ internal PSSourceResult(Management.Deployment.PackageCatalogReference catalogRef
/// Gets the type of the source.
/// </summary>
public string Type { get; private set; }

/// <summary>
/// Gets the trust level of the source.
/// </summary>
public string TrustLevel { get; private set; }

Copy link
Member

Choose a reason for hiding this comment

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

Why is this a string and not a PSSourceTrustLevel?

Maybe that is normal in PowerShell, it just seems odd to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

The main reason is not load any dependency when the CLR loads this binary as the ALC hasn't been being configured yet at this point. For cswinrt enums, there are PSSomething equivalents that get converted to string and then converted to the cswinrt representation in the engine module. Since this doesn't have it he had to go with strings,

Copy link
Member

Choose a reason for hiding this comment

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

Is it that we can't have a dependency inversion with Engine taking a dependency on Cmdlets? And we can't have this kind of immediate dependency on Engine in types exposed on the interface of any cmdlet because we don't have the ALC yet to resolve it (thus preventing us from putting PSSourceTrustLevel in Engine)?

In any case, I don't have a problem with it being a string. Just wasn't clear why.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, we don't want to have the dependency on Engine until the ALC is setup

/// <summary>
/// Gets a value indicating whether the source must be explicitly specified for discovery.
/// </summary>
public bool Explicit { get; private set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@
<TableColumnHeader>
<Label>Type</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>TrustLevel</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Explicit</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
Expand All @@ -296,6 +302,12 @@
<TableColumnItem>
<ScriptBlock>$_.Type</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.TrustLevel</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.Explicit</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
Expand Down
9 changes: 4 additions & 5 deletions src/PowerShell/tests/Microsoft.WinGet.Client.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ BeforeAll {
Get-WinGetSource -Name 'TestSource'
}
catch {
# TODO: Add-WinGetSource does not support setting trust level yet.
# Add-WinGetSource -Name 'TestSource' -Arg 'https://localhost:5001/TestKit/'
$sourceAddCommand = "${wingetExeName} source add TestSource https://localhost:5001/TestKit/ --trust-level trusted"
Invoke-Expression -Command $sourceAddCommand
Add-WinGetSource -Name 'TestSource' -Arg 'https://localhost:5001/TestKit/' -TrustLevel 'Trusted'
}
}

Expand Down Expand Up @@ -154,7 +151,7 @@ Describe 'Reset-WinGetSource' {
Describe 'Get|Add|Reset-WinGetSource' {

BeforeAll {
AddTestSource
Add-WinGetSource -Name 'TestSource' -Arg 'https://localhost:5001/TestKit/' -TrustLevel 'Trusted' -Explicit
}

It 'Get Test source' {
Expand All @@ -164,6 +161,8 @@ Describe 'Get|Add|Reset-WinGetSource' {
$source.Name | Should -Be 'TestSource'
$source.Argument | Should -Be 'https://localhost:5001/TestKit/'
$source.Type | Should -Be 'Microsoft.PreIndexed.Package'
$source.TrustLevel | Should -Be 'Trusted'
$source.Explicit | Should -Be $true
}

It 'Get fake source' {
Expand Down
Loading