Skip to content

Replace exposed hardcoded test secrets with random one #785

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

Merged
merged 1 commit into from
Sep 1, 2022
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
56 changes: 35 additions & 21 deletions test/PSCredentialInfo.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,39 @@ Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force
Describe "Create PSCredentialInfo with VaultName and SecretName" -tags 'CI' {

It "Verifies VaultName is not empty" {
{ New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("", "testsecret") } | Should -Throw -ErrorId "ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand"
$randomSecret = [System.IO.Path]::GetRandomFileName()
{ New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("", $randomSecret) } | Should -Throw -ErrorId "ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand"
}

It "Verifies SecretName is not empty" {
{ New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", "") } | Should -Throw -ErrorId "ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand"
}

It "Creates PSCredentialInfo successfully if VaultName and SecretName are non-empty" {
$credentialInfo = New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", "testsecret")
$randomSecret = [System.IO.Path]::GetRandomFileName()
$credentialInfo = New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", $randomSecret)
$credentialInfo.VaultName | Should -Be "testvault"
$credentialInfo.SecretName | Should -Be "testsecret"
$credentialInfo.SecretName | Should -Be $randomSecret
}
}

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")
$randomSecret = [System.IO.Path]::GetRandomFileName()
$credentialInfo = New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", $randomSecret)

$credentialInfo.VaultName | Should -Be "testvault"
$credentialInfo.SecretName | Should -Be "testsecret"
$credentialInfo.SecretName | Should -Be $randomSecret
}

It "Creates PSCredentialInfo successfully if Credential is non-null and of type PSCredential" {
$randomSecret = [System.IO.Path]::GetRandomFileName()
$credential = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString "password" -AsPlainText -Force))
$credentialInfo = New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", "testsecret", $credential)
$credentialInfo = New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", $randomSecret, $credential)

$credentialInfo.VaultName | Should -Be "testvault"
$credentialInfo.SecretName | Should -Be "testsecret"
$credentialInfo.SecretName | Should -Be $randomSecret
}
}

Expand All @@ -52,59 +56,69 @@ Describe "Create PSCredentialInfo from a PSObject" -tags 'CI' {
}

It "Creates PSCredentialInfo successfully from PSObject with VaultName and SecretName" {
$randomSecret = [System.IO.Path]::GetRandomFileName()
$properties = [PSCustomObject]@{
VaultName = "testvault"
SecretName = "testsecret"
SecretName = $randomSecret
}

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

$credentialInfo.VaultName | Should -Be "testvault"
$credentialInfo.SecretName | Should -Be "testsecret"
$credentialInfo.SecretName | Should -Be $randomSecret
}

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))
$randomSecret = [System.IO.Path]::GetRandomFileName()
$randomPassword = [System.IO.Path]::GetRandomFileName()

$credential = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString $randomPassword -AsPlainText -Force))
$properties = [PSCustomObject]@{
VaultName = "testvault"
SecretName = "testsecret"
SecretName = $randomSecret
Credential = [PSCredential] $credential
}

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

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

It "Creates PSCredentialInfo successfully from PSObject with VaultName, SecretName and string Credential" {
$randomSecret = [System.IO.Path]::GetRandomFileName()
$randomPassword = [System.IO.Path]::GetRandomFileName()

$properties = [PSCustomObject]@{
VaultName = "testvault"
SecretName = "testsecret"
Credential = "password"
SecretName = $randomSecret
Credential = $randomPassword
}

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

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

It "Creates PSCredentialInfo successfully from PSObject with VaultName, SecretName and SecureString Credential" {
$secureString = ConvertTo-SecureString "password" -AsPlainText -Force
$randomSecret = [System.IO.Path]::GetRandomFileName()
$randomPassword = [System.IO.Path]::GetRandomFileName()

$secureString = ConvertTo-SecureString $randomPassword -AsPlainText -Force
$properties = [PSCustomObject]@{
VaultName = "testvault"
SecretName = "testsecret"
SecretName = $randomSecret
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"
$credentialInfo.SecretName | Should -Be $randomSecret
$credentialInfo.Credential.GetNetworkCredential().Password | Should -Be $randomPassword
}
}
19 changes: 11 additions & 8 deletions test/RegisterPSResourceRepository.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ Describe "Test Register-PSResourceRepository" {

$relativeCurrentPath = Get-Location

$credentialInfo1 = New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", "testsecret")
$secureString = ConvertTo-SecureString "testpassword" -AsPlainText -Force
$randomSecret = [System.IO.Path]::GetRandomFileName()
$randomPassword = [System.IO.Path]::GetRandomFileName()

$credentialInfo1 = New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", $randomSecret)
$secureString = ConvertTo-SecureString $randomPassword -AsPlainText -Force
$credential = New-Object pscredential ("testusername", $secureString)
$credentialInfo2 = New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", "testsecret", $credential)
$credentialInfo2 = New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", $randomSecret, $credential)
}
AfterEach {
Get-RevertPSResourceRepositoryFile
Expand Down Expand Up @@ -68,7 +71,7 @@ Describe "Test Register-PSResourceRepository" {
$res.Trusted | Should -Be True
$res.Priority | Should -Be 20
$res.CredentialInfo.VaultName | Should -Be "testvault"
$res.CredentialInfo.SecretName | Should -Be "testsecret"
$res.CredentialInfo.SecretName | Should -Be $randomSecret
}

It "register repository with PSGallery parameter (PSGalleryParameterSet)" {
Expand Down Expand Up @@ -102,7 +105,7 @@ Describe "Test Register-PSResourceRepository" {
$hashtable1 = @{Name = $TestRepoName1; Uri = $tmpDir1Path}
$hashtable2 = @{Name = $TestRepoName2; Uri = $tmpDir2Path; Trusted = $True}
$hashtable3 = @{Name = $TestRepoName3; Uri = $tmpDir3Path; Trusted = $True; Priority = 20}
$hashtable4 = @{Name = $TestRepoName4; Uri = $tmpDir4Path; Trusted = $True; Priority = 30; CredentialInfo = (New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", "testsecret"))}
$hashtable4 = @{Name = $TestRepoName4; Uri = $tmpDir4Path; Trusted = $True; Priority = 30; CredentialInfo = (New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", $randomSecret))}
$arrayOfHashtables = $hashtable1, $hashtable2, $hashtable3, $hashtable4

Register-PSResourceRepository -Repository $arrayOfHashtables
Expand All @@ -126,7 +129,7 @@ Describe "Test Register-PSResourceRepository" {
$res4.Trusted | Should -Be True
$res4.Priority | Should -Be 30
$res4.CredentialInfo.VaultName | Should -Be "testvault"
$res4.CredentialInfo.SecretName | Should -Be "testsecret"
$res4.CredentialInfo.SecretName | Should -Be $randomSecret
$res4.CredentialInfo.Credential | Should -BeNullOrEmpty
}

Expand All @@ -146,7 +149,7 @@ Describe "Test Register-PSResourceRepository" {
$hashtable2 = @{Name = $TestRepoName1; Uri = $tmpDir1Path}
$hashtable3 = @{Name = $TestRepoName2; Uri = $tmpDir2Path; Trusted = $True}
$hashtable4 = @{Name = $TestRepoName3; Uri = $tmpDir3Path; Trusted = $True; Priority = 20}
$hashtable5 = @{Name = $TestRepoName4; Uri = $tmpDir4Path; Trusted = $True; Priority = 30; CredentialInfo = (New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", "testsecret"))}
$hashtable5 = @{Name = $TestRepoName4; Uri = $tmpDir4Path; Trusted = $True; Priority = 30; CredentialInfo = (New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", $randomSecret))}
$arrayOfHashtables = $hashtable1, $hashtable2, $hashtable3, $hashtable4, $hashtable5

Register-PSResourceRepository -Repository $arrayOfHashtables
Expand Down Expand Up @@ -176,7 +179,7 @@ Describe "Test Register-PSResourceRepository" {
$res5.Trusted | Should -Be True
$res5.Priority | Should -Be 30
$res5.CredentialInfo.VaultName | Should -Be "testvault"
$res5.CredentialInfo.SecretName | Should -Be "testsecret"
$res5.CredentialInfo.SecretName | Should -Be $randomSecret
$res5.CredentialInfo.Credential | Should -BeNullOrEmpty
}

Expand Down
15 changes: 9 additions & 6 deletions test/SetPSResourceRepository.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ Describe "Test Set-PSResourceRepository" {

$relativeCurrentPath = Get-Location

$credentialInfo1 = New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", "testsecret")
$secureString = ConvertTo-SecureString "testpassword" -AsPlainText -Force
$randomSecret = [System.IO.Path]::GetRandomFileName()
$randomPassword = [System.IO.Path]::GetRandomFileName()

$credentialInfo1 = New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", $randomSecret)
$secureString = ConvertTo-SecureString $randomPassword -AsPlainText -Force
$credential = New-Object pscredential ("testusername", $secureString)
$credentialInfo2 = New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", "testsecret", $credential)
$credentialInfo2 = New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", $randomSecret, $credential)
}
AfterEach {
Get-RevertPSResourceRepositoryFile
Expand Down Expand Up @@ -89,7 +92,7 @@ Describe "Test Set-PSResourceRepository" {
$res.Priority | Should -Be 50
$res.Trusted | Should -Be False
$res.CredentialInfo.VaultName | Should -Be "testvault"
$res.CredentialInfo.SecretName | Should -Be "testsecret"
$res.CredentialInfo.SecretName | Should -Be $randomSecret
$res.CredentialInfo.Credential | Should -BeNullOrEmpty
}

Expand Down Expand Up @@ -145,7 +148,7 @@ Describe "Test Set-PSResourceRepository" {

$hashtable1 = @{Name = $TestRepoName1; Uri = $tmpDir2Path};
$hashtable2 = @{Name = $TestRepoName2; Priority = 25};
$hashtable3 = @{Name = $TestRepoName3; CredentialInfo = [PSCustomObject] @{ VaultName = "testvault"; SecretName = "testsecret" }};
$hashtable3 = @{Name = $TestRepoName3; CredentialInfo = [PSCustomObject] @{ VaultName = "testvault"; SecretName = $randomSecret }};
$hashtable4 = @{Name = $PSGalleryName; Trusted = $True};
$arrayOfHashtables = $hashtable1, $hashtable2, $hashtable3, $hashtable4

Expand All @@ -170,7 +173,7 @@ Describe "Test Set-PSResourceRepository" {
$res3.Priority | Should -Be 50
$res3.Trusted | Should -Be False
$res3.CredentialInfo.VaultName | Should -Be "testvault"
$res3.CredentialInfo.SecretName | Should -Be "testsecret"
$res3.CredentialInfo.SecretName | Should -Be $randomSecret
$res3.CredentialInfo.Credential | Should -BeNullOrEmpty

$res4 = Get-PSResourceRepository -Name $PSGalleryName
Expand Down