PowerShell / PSResourceGet

PSResourceGet is the package manager for PowerShell
https://www.powershellgallery.com/packages/Microsoft.PowerShell.PSResourceGet
MIT License
486 stars 92 forks source link

Fail to get packages from azure devops artifacts repo #674

Closed strat-alex closed 2 years ago

strat-alex commented 2 years ago

Prerequisites

Steps to reproduce

I fail to successfully search for packages from my azure devops artifacts repo. Somehow it worked for a minute, but upon trying how the Install-Module commands would interact/mix with the Install-PSResource commands, things started to go nuts and I cannot recreate a working environment. I have also set up a new clean win11 vm on Azure, just to make sure there is no residual config on my machine. But alas, same result. I must be missing something.

The steps I took were:

Install-Module PowershellGet -AllowPrerelease -Repository psgallery -force
#### <RESTART TERMINAL>
Install-PSResource Microsoft.PowerShell.SecretManagement -Repository psgallery
Install-PSResource Microsoft.PowerShell.SecretStore -Repository psgallery
####  <RESTART TERMINAL>

Register-SecretVault -Name SecretStore -ModuleName Microsoft.PowerShell.SecretStore -DefaultVault

$patToken = "MY PAT TOKEN" | ConvertTo-SecureString -AsPlainText -Force
$devopsCreds = New-Object System.Management.Automation.PSCredential("something", $patToken)
Set-Secret -Name devopsCreds -Secret $devopsCreds
#### If it's first run, the password for the store will be requested here

$credsAzureDevopsServices = New-Object Microsoft.Powershell.PowershellGet.UtilClasses.PSCredentialInfo("SecretStore", "devopsCreds")

On the clean VM I hit a little snag here, easily fixed.

### <On the azure VM, I had to Import-Module PowershellGet first>
PS C:\Users\myuserrocks> $credsAzureDevopsServices = New-Object Microsoft.Powershell.PowershellGet.UtilClasses.PSCredentialInfo("SecretStore", "devopsCreds")
New-Object: Cannot find type [Microsoft.Powershell.PowershellGet.UtilClasses.PSCredentialInfo]: verify that the assembly containing this type is loaded.
PS C:\Users\myuserrocks> Import-Module PowershellGet

Then I could continue:

PS C:\Users\myuserrocks> $credsAzureDevopsServices = New-Object Microsoft.Powershell.PowershellGet.UtilClasses.PSCredentialInfo("SecretStore", "devopsCreds")

Register-PSResourceRepository -name MYORG -Uri "https://pkgs.dev.azure.com/MYORG/_packaging/MYREPO/nuget/v3/index.json"  -Trusted -CredentialInfo $credsAzureDevopsServices
Find-PSResource 'dv.ifx.' -Repository MYORG

### FAILS HERE

I also tried with the V2 repo:

Set-PSResourceRepository -name MYORG -Uri "https://pkgs.dev.azure.com/MYORG/_packaging/MYREPO/nuget/v2"  -Trusted -CredentialInfo $credsAzureDevopsServices

For completeness sake, I also tried with the old style credentials, that was necessary in the V2:

$patRaw = "MYPAT"
$oldSecret = '{"endpointCredentials": [{"endpoint":"https://pkgs.dev.azure.com/MYORG/_packaging/MYREPO/nuget/v2", "username":"OPTIONAL", "password":"' + $patRaw + '" }]}'  | ConvertTo-SecureString -AsPlainText -Force

$devopsCreds = New-Object System.Management.Automation.PSCredential("something", $oldSecret)
Set-Secret -Name devopsCreds -Secret $devopsCreds

The PSResourceRepository.Xml file looks fine to me:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <Repository Name="PSGallery" Url="https://www.powershellgallery.com/api/v2" Priority="50" Trusted="false" />
  <Repository Name="MYORG" Url="https://pkgs.dev.azure.com/MYORG/_packaging/MYREPO/nuget/v3/index.json" Priority="50" Trusted="true" VaultName="SecretStore" SecretName="devopsCreds" />
</configuration> 

Expected behavior

Find-PSResource to show me the packages on my Azure Artifacts repo.
And consequently, Install-PSResource to install the package of my choosing.

Actual behavior

Error I'm getting for v3: 
 VERBOSE: Error retrieving resource from repository: Unable to load the service index for source https://pkgs.dev.azure.com/MYORG/_packaging/MYREPO/nuget/v3/index.json. ```

Error I'm getting for v2: 
``` VERBOSE: FindHelper MetadataAsync: error receiving package: Failed to fetch results from V2 feed at 'https://pkgs.dev.azure.com/MYORG/_packaging/MYREPO/nuget/v2/FindPackagesById()?id='dv.ifx.'&semVerLevel=2.0.0' with following message : Response status code does not indicate success: 401 (Unauthorized). ```

Error details

No response

Environment data

Versions:

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Binary     3.0.14     beta14     PowerShellGet                       {Find-PSResource, Get-PSResource, Get-PSResourceRepository, Install-PSResource…}
Binary     1.1.2                 Microsoft.PowerShell.SecretManagem… {Get-Secret, Get-SecretInfo, Get-SecretVault, Register-SecretVault…}
Binary     1.0.6                 Microsoft.PowerShell.SecretStore    {Get-SecretStoreConfiguration, Reset-SecretStore, Set-SecretStoreConfiguration, Set-SecretStorePassword…}


### Visuals

_No response_
SydneyhSmith commented 2 years ago

Thanks @alexv-be are you running Find-PSResource on a particular module? Could you also provide the verbose output please? Could you please also try Invoke-WebRequest (uri) -Credential (Get-Secret your cred) and let us know the status code? Thanks!

strat-alex commented 2 years ago

As meticulous as I did my testing, I must've made a copy paste error somehow. I refreshed my PAT, just to be sure. Re-registered it. And all is working fine now.

The old commands are gone now as well, so no messing up with install-module or something else anymore. I'll be rolling this out in my team this week. Thank you for following up!

alerickson commented 2 years ago

@alexv-be we'd love to hear how this goes for you and your team. If there's any feedback or suggestions, feel free to open a new issue!

strat-alex commented 2 years ago

No real issues found. Everything went smooth. People were a bit concerned about documentation still using the old version commands. And the lack of wildcard searching.

But that was about it. good job!

alerickson commented 2 years ago

We still need to work on creating new documentation tagging @SydneyhSmith here so we can discuss when we want to prioritize that. Unfortunately the lack of wildcard searching has to do with the apis that are implemented for the Azure artifacts repo. We're trying to think of ways we can get around those limitations. Tagging @anamnavi here since she's done a lot of investigating here.