diff --git a/src/Item/New-CredentialStoreItem.ps1 b/src/Item/New-CredentialStoreItem.ps1 index 5cd5c01..37d0f3d 100644 --- a/src/Item/New-CredentialStoreItem.ps1 +++ b/src/Item/New-CredentialStoreItem.ps1 @@ -121,6 +121,15 @@ function New-CredentialStoreItem { $Cert = Get-ChildItem -Recurse -Path 'Cert:' | Where-Object { $_.Thumbprint -eq $CSContent.Thumbprint } | Select-Object -First 1 + if ($null -eq $Cert) { + $ErrorParams = @{ + ErrorAction = 'Stop' + Exception = [System.Exception]::new( + ('Could not find the linked certificate with thumbprint {0}' -f $CSContent.Thumbprint) + ) + } + Write-Error @ErrorParams + } } else { $Cert = Get-PfxCertificate -FilePath $CSContent.PfxCertificate -ErrorAction Stop diff --git a/tests/Item/03_New-CredentialStoreItem.Tests.ps1 b/tests/Item/03_New-CredentialStoreItem.Tests.ps1 index d02883f..56ce83b 100644 --- a/tests/Item/03_New-CredentialStoreItem.Tests.ps1 +++ b/tests/Item/03_New-CredentialStoreItem.Tests.ps1 @@ -4,7 +4,7 @@ Describe "New-CredentialStoreItem" { # Creat a fresh CredentialStore first New-CredentialStore -Force - [String]$tmp = (65..90) + (97..122) | Get-Random -Count 5 | % {[char]$_} + [String]$tmp = (65..90) + (97..122) | Get-Random -Count 5 | ForEach-Object { [char]$_ } $tmp = $tmp.Replace(' ', '') $tmpUser = "MyUser" $tmpPwd = "fooobarysdfsfs" | ConvertTo-SecureString -AsPlainText -Force @@ -65,7 +65,7 @@ Describe "New-CredentialStoreItem" { } Context "General Exception handling" { - Mock Test-CredentialStore {return $false} + Mock Test-CredentialStore { return $false } It "Missing CredentialStore should throw" { { New-CredentialStoreItem -Shared -Path 'C:\missingStore.json' -RemoteHost 'notrelevant' } | Should -Throw "Could not add anything" } @@ -83,20 +83,24 @@ Describe "New-CredentialStoreItem" { } Context "Testing items with certficiate store" { It "Create item in new store with cert store link" { - { New-CredentialStore -UseCertStore -Force } | Should -Not -Throw + New-CredentialStore -UseCertStore -Force + $Path = Get-DefaultCredentialStorePath $StoreHome = Split-Path -Path $Path -Parent $CertFile = Join-Path -Path $StoreHome -ChildPath 'PSCredentialStore.pfx' - certutil.exe -Importpfx -user MY $CertFile "NoProtect, NoRoot" - function global:Get-Credential ([string]$Message) { - $UserName = 'testuser' - $Password = ConvertTo-SecureString -String "mypasswd" -AsPlainText -Force - return [PSCredential]::new($UserName, $Password) - } - New-CredentialStoreItem -RemoteHost 'foobarcerts' - Remove-Item -Path 'Function:\Get-Credential' + $Cert = Get-PfxCertificate -FilePath $CertFile - $writtenItem = Get-CredentialStoreItem -Path $tmpCS -Shared -RemoteHost 'foobarcerts' + $myStore = [System.Security.Cryptography.X509Certificates.X509Store]::new('My') + $myStore.Open("ReadWrite") + $myStore.Add($Cert) + $MyStore.Close() + + $UserName = 'testuser' + $Password = ConvertTo-SecureString -String "mypasswd" -AsPlainText -Force + + [PSCredential]::new($UserName, $Password) | New-CredentialStoreItem -RemoteHost 'foobarcerts' + + $writtenItem = Get-CredentialStoreItem -RemoteHost 'foobarcerts' $writtenItem.UserName | Should -Be "testuser" $writtenItem.GetNetworkCredential().Password | Should -Be 'mypasswd' }