This commit is contained in:
OCram85 2019-03-28 10:52:22 +01:00
parent 3ab629d17d
commit ab8811b7d5
2 changed files with 25 additions and 12 deletions

View File

@ -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

View File

@ -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) {
$Cert = Get-PfxCertificate -FilePath $CertFile
$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
return [PSCredential]::new($UserName, $Password)
}
New-CredentialStoreItem -RemoteHost 'foobarcerts'
Remove-Item -Path 'Function:\Get-Credential'
$writtenItem = Get-CredentialStoreItem -Path $tmpCS -Shared -RemoteHost 'foobarcerts'
[PSCredential]::new($UserName, $Password) | New-CredentialStoreItem -RemoteHost 'foobarcerts'
$writtenItem = Get-CredentialStoreItem -RemoteHost 'foobarcerts'
$writtenItem.UserName | Should -Be "testuser"
$writtenItem.GetNetworkCredential().Password | Should -Be 'mypasswd'
}