Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 16 additions & 1 deletion src/code/PSCredentialInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Management.Automation;
using System.Net;
using System.Security;

namespace Microsoft.PowerShell.PowerShellGet.UtilClasses
{
Expand Down Expand Up @@ -43,7 +45,20 @@ public PSCredentialInfo(PSObject psObject)

VaultName = (string) psObject.Properties[PSCredentialInfo.VaultNameAttribute]?.Value;
SecretName = (string) psObject.Properties[PSCredentialInfo.SecretNameAttribute]?.Value;
Credential = (PSCredential) psObject.Properties[PSCredentialInfo.CredentialAttribute]?.Value;

var credentialAttr = psObject.Properties[PSCredentialInfo.CredentialAttribute]?.Value;
if (credentialAttr is string credStr)
{
Credential = new PSCredential("PSGetUser", new NetworkCredential("", credStr).SecurePassword);
}
else if ((credentialAttr as PSObject)?.BaseObject is SecureString credSS)
{
Credential = new PSCredential("PSGetUser", credSS);
}
else if (credentialAttr is PSCredential psCred)
{
Credential = psCred;
}
}

#endregion
Expand Down
34 changes: 31 additions & 3 deletions test/PSCredentialInfo.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Describe "Create PSCredentialInfo with VaultName and SecretName" -tags 'CI' {
Describe "Create PSCredentialInfo with VaultName, SecretName, and Credential" -tags 'CI' {

It "Creates PSCredentialInfo successfully if Credential is null" {
$credentialInfo = New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", "testsecret", $null)
$credentialInfo = New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", "testsecret")

$credentialInfo.VaultName | Should -Be "testvault"
$credentialInfo.SecretName | Should -Be "testsecret"
Expand Down Expand Up @@ -63,7 +63,7 @@ Describe "Create PSCredentialInfo from a PSObject" -tags 'CI' {
$credentialInfo.SecretName | Should -Be "testsecret"
}

It "Creates PSCredentialInfo successfully from PSObject with VaultName, SecretName and Credential" {
It "Creates PSCredentialInfo successfully from PSObject with VaultName, SecretName and PSCredential Credential" {
$credential = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString "password" -AsPlainText -Force))
$properties = [PSCustomObject]@{
VaultName = "testvault"
Expand All @@ -77,6 +77,34 @@ Describe "Create PSCredentialInfo from a PSObject" -tags 'CI' {
$credentialInfo.SecretName | Should -Be "testsecret"
$credentialInfo.Credential.UserName | Should -Be "username"
$credentialInfo.Credential.GetNetworkCredential().Password | Should -Be "password"

}

It "Creates PSCredentialInfo successfully from PSObject with VaultName, SecretName and string Credential" {
$properties = [PSCustomObject]@{
VaultName = "testvault"
SecretName = "testsecret"
Credential = "password"
}

$credentialInfo = [Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo] $properties

$credentialInfo.VaultName | Should -Be "testvault"
$credentialInfo.SecretName | Should -Be "testsecret"
$credentialInfo.Credential.GetNetworkCredential().Password | Should -Be "password"
}

It "Creates PSCredentialInfo successfully from PSObject with VaultName, SecretName and SecureString Credential" {
$secureString = ConvertTo-SecureString "password" -AsPlainText -Force
$properties = [PSCustomObject]@{
VaultName = "testvault"
SecretName = "testsecret"
Credential = $secureString
}

$credentialInfo = [Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo] $properties

$credentialInfo.VaultName | Should -Be "testvault"
$credentialInfo.SecretName | Should -Be "testsecret"
$credentialInfo.Credential.GetNetworkCredential().Password | Should -Be "password"
}
}