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

Update Azure.ps1 | Support for Linux CertThumbprints #596

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
47 changes: 43 additions & 4 deletions Posh-ACME/Plugins/Azure.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -547,13 +547,52 @@ function Connect-AZTenant {
if ('CertThumbprint' -eq $PSCmdlet.ParameterSetName) {
Write-Debug "Looking for cert thumbprint $AZCertThumbprint"
# Look up the cert based on the thumbprint
$cert = $null

# check CurrentUser first
if (-not ($cert = Get-Item "Cert:\CurrentUser\My\$AZCertThumbprint" -EA Ignore)) {
$cert = Get-Item "Cert:\CurrentUser\My\$AZCertThumbprint" -EA Ignore

if (-not $cert) {
# check LocalMachine
if (-not ($cert = Get-Item "Cert:\LocalMachine\My\$AZCertThumbprint" -EA Ignore)) {
throw "Certificate with thumbprint $AZCertThumbprint not found in CurrentUser or LocalMachine stores."
}
$cert = Get-Item "Cert:\LocalMachine\My\$AZCertThumbprint" -EA Ignore
}

###
# This is the new method I added in to support linux systems
# its entirely possible this method would work for Windows and if so may be better as its not dependant on the cert: PS-Drive provider
# I dont know enough to say for sure though so I just add this below, only triggers if $cert wasnt found already by previous methods

$CurrentErrorAction = $ErrorActionPreference

if (-not $cert) {
# Setting Error Action to Ignore to prevent the error message from being displayed will set back after block
$ErrorActionPreference = "Ignore"
}

if (-not $cert) {
# check CurrentUser .NET Style
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store("My", "CurrentUser")
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadOnly)
$cert = $store.Certificates.Find([System.Security.Cryptography.X509Certificates.X509FindType]::FindByThumbprint, $AZCertThumbprint, $false)
$store.Close()
}

if (-not $cert) {
# check LocalMachine .NET Style
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store("My", "LocalMachine")
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadOnly)
$cert = $store.Certificates.Find([System.Security.Cryptography.X509Certificates.X509FindType]::FindByThumbprint, $AZCertThumbprint, $false)
$store.Close()
}

if (-not $cert) {
throw "Certificate with thumbprint $AZCertThumbprint not found in CurrentUser or LocalMachine stores."
}

if ($ErrorActionPreference -ne $CurrentErrorAction) {
$ErrorActionPreference = $CurrentErrorAction
}

} else {
Write-Debug "Looking for cert pfx $AZCertPfx"
$AZCertPfx = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($AZCertPfx)
Expand Down