Implement precise lookup hierarchy (fixes #43)

This commit is contained in:
OCram85 2019-04-05 09:05:50 +02:00
parent d92d963979
commit a71bfd0aaf
4 changed files with 64 additions and 11 deletions

View File

@ -89,8 +89,27 @@ function Get-CredentialStoreItem {
if (($CSMembers.MemberType -eq "NoteProperty") -and ($CSMembers.Name -contains $CredentialName)) { if (($CSMembers.MemberType -eq "NoteProperty") -and ($CSMembers.Name -contains $CredentialName)) {
try { try {
if ($null -eq $CS.PfxCertificate) { if ($null -eq $CS.PfxCertificate) {
if ($CS.Type -eq 'Private') {
$Cert = Get-CSCertificate -Thumbprint $CS.Thumbprint $Cert = Get-CSCertificate -Thumbprint $CS.Thumbprint
} }
elseif ($CS.Type -eq 'Shard') {
if (Test-CSCertificate -Thumbprint $CS.Thumbprint -StoreName My -StoreLocation LocalMachine) {
$Cert = Get-CSCertificate -Thumbprint $CS.Thumbprint -StoreName My -StoreLocation LocalMachine
}
elseif (Test-CSCertificate -Thumbprint $CS.Thumbprint -StoreName Root -StoreLocation LocalMachine) {
$Cert = Get-CSCertificate -Thumbprint $CS.Thumbprint -StoreName Root -StoreLocation LocalMachine
}
else {
$ErrorParams = @{
ErrorAction = 'Stop'
Exception = [System.Exception]::new(
('Could not find any certificate with thumbprint {0}' -f $CS.Thumbprint)
)
}
Write-Error @ErrorParams
}
}
}
else { else {
$Cert = Get-PfxCertificate -FilePath $CS.PfxCertificate -ErrorAction Stop $Cert = Get-PfxCertificate -FilePath $CS.PfxCertificate -ErrorAction Stop
} }

View File

@ -118,17 +118,27 @@ function New-CredentialStoreItem {
if ($Credential.UserName) { if ($Credential.UserName) {
try { try {
if ($null -eq $CSContent.PfxCertificate) { if ($null -eq $CSContent.PfxCertificate) {
if ($CSContent.Type -eq 'Private') {
$Cert = Get-CSCertificate -Thumbprint $CSContent.Thumbprint $Cert = Get-CSCertificate -Thumbprint $CSContent.Thumbprint
if ($null -eq $Cert) { }
elseif ($CSContent.Type -eq 'Shard') {
if (Test-CSCertificate -Thumbprint $CSContent.Thumbprint -StoreName My -StoreLocation LocalMachine) {
$Cert = Get-CSCertificate -Thumbprint $CSContent.Thumbprint -StoreName My -StoreLocation LocalMachine
}
elseif (Test-CSCertificate -Thumbprint $CSContent.Thumbprint -StoreName Root -StoreLocation LocalMachine) {
$Cert = Get-CSCertificate -Thumbprint $CSContent.Thumbprint -StoreName Root -StoreLocation LocalMachine
}
else {
$ErrorParams = @{ $ErrorParams = @{
ErrorAction = 'Stop' ErrorAction = 'Stop'
Exception = [System.Security.Cryptography.X509Certificates.FileNotFoundException]::new( Exception = [System.Exception]::new(
('Could not find the linked certificate with thumbprint {0}' -f $CSContent.Thumbprint) ('Could not find any certificate with thumbprint {0}' -f $CSContent.Thumbprint)
) )
} }
Write-Error @ErrorParams Write-Error @ErrorParams
} }
} }
}
else { else {
$Cert = Get-PfxCertificate -FilePath $CSContent.PfxCertificate -ErrorAction Stop $Cert = Get-PfxCertificate -FilePath $CSContent.PfxCertificate -ErrorAction Stop
} }

View File

@ -104,8 +104,27 @@ function Set-CredentialStoreItem {
if ($Credential.UserName) { if ($Credential.UserName) {
try { try {
if ($null -eq $CSContent.PfxCertificate) { if ($null -eq $CSContent.PfxCertificate) {
if ($CSContent.Type -eq 'Private') {
$Cert = Get-CSCertificate -Thumbprint $CSContent.Thumbprint $Cert = Get-CSCertificate -Thumbprint $CSContent.Thumbprint
} }
elseif ($CSContent.Type -eq 'Shard') {
if (Test-CSCertificate -Thumbprint $CSContent.Thumbprint -StoreName My -StoreLocation LocalMachine) {
$Cert = Get-CSCertificate -Thumbprint $CSContent.Thumbprint -StoreName My -StoreLocation LocalMachine
}
elseif (Test-CSCertificate -Thumbprint $CSContent.Thumbprint -StoreName Root -StoreLocation LocalMachine) {
$Cert = Get-CSCertificate -Thumbprint $CSContent.Thumbprint -StoreName Root -StoreLocation LocalMachine
}
else {
$ErrorParams = @{
ErrorAction = 'Stop'
Exception = [System.Exception]::new(
('Could not find any certificate with thumbprint {0}' -f $CSContent.Thumbprint)
)
}
Write-Error @ErrorParams
}
}
}
else { else {
$Cert = Get-PfxCertificate -FilePath $CSContent.PfxCertificate -ErrorAction Stop $Cert = Get-PfxCertificate -FilePath $CSContent.PfxCertificate -ErrorAction Stop
} }

View File

@ -210,7 +210,12 @@ function New-CredentialStore {
} }
else { else {
Write-Verbose 'Importing new PFX certificate file...' Write-Verbose 'Importing new PFX certificate file...'
Import-CSCertificate -Path $PfxParams.CertName -StoreName My -StoreLocation CurrentUser if ($PSCmdlet.ParameterSetName -eq 'Private') {
Import-CSCertificate -Path $PfxParams.CertName -StoreName My -StoreLocation CurrentUser -ErrorAction Stop
}
elseif ($PSCmdlet.ParameterSetName -eq 'Shared') {
Import-CSCertificate -Path $PfxParams.CertName -StoreName My -StoreLocation LocalMachine -ErrorAction Stop
}
} }
} }