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

Possiblity to overwrite default X509KeyStorageFlags in Connect-PnPOnline #4167

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

PeterRevsbech
Copy link

@PeterRevsbech PeterRevsbech commented Aug 17, 2024

Before creating a pull request, make sure that you have read the contribution file located at

https://github.com/pnp/powerShell/blob/dev/CONTRIBUTING.md

Type

  • Bug Fix
  • New Feature
  • Sample

Related Issues?

Fixes #X, partially fixes #Y, mentioned in #Z, etc.

What is in this Pull Request ?

The optional parameter X509KeyStorageFlags is added to the Connect-PnPOnline cmdlet. This is relevant when authenticating with a certficate in order to specify where and how to import the private key of the X.509 passed certificate.
The X509KeyStorageFlags variable is an enum supporting bitwise operations as described here:
https://learn.microsoft.com/dotnet/api/system.security.cryptography.x509certificates.x509keystorageflags

This variable is passed in the X509Certificate2 constructor, which the cmdlet uses for certificate authentication.
https://learn.microsoft.com/dotnet/api/system.security.cryptography.x509certificates.x509certificate2.-ctor

Previously, it was not possible to overwrite the default X509KeyStorageFlags used in the constructor.
This is problematic in cases that the default key storage flags did not support.

Example: In order to use Connect-PnPOnline from a PowerShell Azure Function App with a self-signed PKCS certificate that is fetched from a key vault, you would have to use the cmdlet like so:
Connect-PnPOnline -Url $SiteUrl -ClientId $clientId -CertificateBase64Encoded $certBase64 -Tenant $spTenant
The cmdlet then currently creates the certificate in the file src/Commands/Base/ConnectOnline.cs with this line
var certificate = new X509Certificate2(certificateBytes, CertificatePassword);

Leaving out the optional parameter X509KeyStorageFlags, effectively forcing it to be the default flags.
In an Azure Function app, that has no user store for private keys, the constructor would throw a WindowsCryptographicException error like this:
ERROR: The system cannot find the file specified. ERROR: The system cannot find the file specified.

This is fixed by setting the X509KeyStorageFlags variable to the flag "MachineKeySet", allowing the private key to be stored in the local computer store. I.e. adding this parameter to the cmdlet would fix the issue.
Connect-PnPOnline -Url $SiteUrl -ClientId $clientId -CertificateBase64Encoded $certBase64 -X509KeyStorageFlags [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::MachineKeySet -Tenant $spTenant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants