Yubico.NET.SDK icon indicating copy to clipboard operation
Yubico.NET.SDK copied to clipboard

[BUG] Dependency Exception in PowerShell 7.4.6

Open htcfreek opened this issue 1 year ago • 8 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Current Behavior

PS C:\Users\xxx>  cd C:\Users\xxx\Downloads\test\1-12-1-4.7.2\
PS C:\Users\xxx\Downloads\test\1-12-1-4.7.2> Add-Type -Path .\Yubico.Yubikey.dll
PS C:\Users\xxx\Downloads\test\1-12-1-4.7.2> [Yubico.Yubikey.YubikeyDevice]::FindAll()
MethodInvocationException: Exception calling "FindAll" with "0" argument(s): "Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=8.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60'. Das System kann die angegebene Datei nicht finden."

Expected Behavior

The properties of all connected devices are shown.

Steps To Reproduce

  1. Install Pwsh 7.4.6 and download your sadk.
  2. Export your SDK.
  3. Load your SDK in PowerShell.
  4. Run command [Yubico.Yubikey.YubikeyDevice]::FindAll()

Version

1.12.1

Version

???

Anything else?

PS C:\Users\xxx> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.6
PSEdition                      Core
GitCommitId                    7.4.6
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS C:\Users\xxx> [System.Runtime.InteropServices.RuntimeInformation]::FrameworkDescription
.NET 8.0.10

htcfreek avatar Jan 02 '25 12:01 htcfreek

The YubiKey SDK has a dependency on a native shared library: Yubico.NativeShims.dll

Unfortunately, the way that NuGet packages and PowerShell modules handle these kinds of dependencies are different. The recommended folder structure for PowerShell 7.4 is documented here: https://learn.microsoft.com/en-us/powershell/scripting/dev-cross-plat/writing-portable-modules?view=powershell-7.4&viewFallbackFrom=powershell-7#dependency-on-native-libraries

For background, this is how .NET and NuGet handle it: https://learn.microsoft.com/en-us/nuget/create-packages/native-files-in-net-packages

You will need to translate the folder structure of the Yubico.NativeShims.nupkg into the structure expected by PowerShell as documented above.

GregDomzalski avatar Jan 02 '25 13:01 GregDomzalski

Again: Where to download this file. It is not part of the assets.

htcfreek avatar Jan 02 '25 13:01 htcfreek

Which NuGet packages have you downloaded?

You will need:

  • Yubico.YubiKey https://www.nuget.org/packages/Yubico.YubiKey
  • Yubico.Core https://www.nuget.org/packages/Yubico.Core
  • Yubico.DotnetPolyfills https://www.nuget.org/packages/Yubico.DotNetPolyfills
  • Yubico.NativeShims https://www.nuget.org/packages/Yubico.NativeShims

A nupkg file is simply a zip file with extra metadata. If you rename each file from *.nupkg to *.zip you should be able to extract the shared libraries from there.

Yubico.YubiKey, Yubico.Core, and Yubico.DotnetPolyfills should each be extracted to your module directory as they are all managed / .NET libraries.

Within the extracted folder for Yubico.NativeShims you should see the runtimes folder which will have all of the various native versions of this library. (You can see this here: https://nuget.info/packages/Yubico.NativeShims/1.12.1)

GregDomzalski avatar Jan 02 '25 14:01 GregDomzalski

Wow. I used the assets from github release. And there the NativeShims are missing since a few releases.

I will try later again withe the other downloads.

htcfreek avatar Jan 02 '25 14:01 htcfreek

Got it. Even though NativeShims is part of this repo, since it does not change as often - and quite frankly, is complex to build and sign - it is typically not re-released with each version of the SDK. At least not on GitHub. Since .NET's typical consumption route is NuGet, that is where the focus is spent on releasing assets.

But adding all libraries to each GitHub release page, regardless of update, can be something @DennisDyallo and @dainnilsson evaluate as part of their release process.

GregDomzalski avatar Jan 02 '25 14:01 GregDomzalski

@GregDomzalski I found the problem and it is not the Native.Shim.dll. The problem are missing Microsoft .Net dependencies. If I provide the DLL from the nuget package of 'Microsoft.Extensions.Logging.Abstractions, Version=8.0.0.0 then the error is gone and an other dependency is requested ('Microsoft.Extensions.Configuration, Version=8.0.0.0). SO there seems to be a problem loading the required .Net assemblies.

Did you specified fixed version in the project dependencies? (https://github.com/PowerShell/vscode-powershell/issues/4475)

And by the way, version 1.0.0 of your SDK works fine in PowerShell 7.4.6. But is a bit old for using it.

Error before adding MS assembly:

PS $> [Yubico.Yubikey.YubikeyDevice]::FindAll()
MethodInvocationException: Exception calling "FindAll" with "0" argument(s): "Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system can not find the specified file."

After providing the file:

PS C:\Users\xxx\Downloads\yubiky-sdk_1.12.1_netStd21> dir -name -Recurse
Microsoft.Extensions.Logging.Abstractions.dll
Microsoft.Extensions.Logging.dll
Yubico.Core.dll
Yubico.DotNetPolyfills.dll
Yubico.YubiKey.dll
win-x64\Yubico.NativeShims.dll
win-x86\Yubico.NativeShims.dll
PS $> [Yubico.Yubikey.YubikeyDevice]::FindAll()
MethodInvocationException: Exception calling "FindAll" with "0" argument(s): "Could not load file or assembly 'Microsoft.Extensions.Configuration, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system can not find the specified file."

htcfreek avatar Jan 02 '25 18:01 htcfreek

Sorry to break in, what are you trying to build with Powershell? I tried over a year ago and gave up on the NativeShims, and instead did a powershell module in C# so I could get around that.

https://www.powershellgallery.com/packages/powershellYK https://github.com/virot/powershellYK

virot avatar Jan 26 '25 12:01 virot

@virot I simply tried to get information about the connected Yubikeys.

Thank you for the hin.

The problem is my requirement for Powershell 5.1.

htcfreek avatar Jan 26 '25 12:01 htcfreek