Skip to content

Commit a1163bb

Browse files
🪲 [Fix]: Fix an issue with Get-Context -with blank Name (#38)
## Description - Fix an issue with `Get-Context`, where it would return all context if `-Name` is `$null` or `''` (empty string), when it should be returning no contexts. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [x] 🪲 [Fix] - [ ] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent 4769aba commit a1163bb

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/functions/public/Context/Get-Context.ps1

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Requires -Modules @{ ModuleName = 'Microsoft.PowerShell.SecretManagement'; RequiredVersion = '1.1.2' }
22

3-
function Get-Context {
3+
filter Get-Context {
44
<#
55
.SYNOPSIS
66
Retrieves a context from the context vault.
@@ -40,28 +40,23 @@ function Get-Context {
4040
)]
4141
[SupportsWildcards()]
4242
[Alias('Context', 'ContextName')]
43-
[string] $Name,
43+
[string] $Name = '*',
4444

4545
# Switch to retrieve all the contexts secrets as plain text.
4646
[Parameter()]
4747
[switch] $AsPlainText
4848
)
4949

50-
begin {
51-
$filter = if ([string]::IsNullOrEmpty($PSBoundParameters.Name)) { '*' } else { $PSBoundParameters.Name }
52-
$Name = $($script:Config.Name) + $filter
53-
}
50+
$Name = $($script:Config.Name) + $Name
5451

55-
process {
56-
$contextVault = Get-ContextVault
52+
$contextVault = Get-ContextVault
5753

58-
Write-Verbose "Retrieving contexts from vault [$($contextVault.Name)] using pattern [$Name]"
59-
$contexts = Get-SecretInfo -Vault $contextVault.Name | Where-Object { $_.Name -like "$Name" }
54+
Write-Verbose "Retrieving contexts from vault [$($contextVault.Name)] using pattern [$Name]"
55+
$contexts = Get-SecretInfo -Vault $contextVault.Name | Where-Object { $_.Name -like "$Name" }
6056

61-
Write-Verbose "Found [$($contexts.Count)] contexts in context vault [$($contextVault.Name)]"
62-
foreach ($context in $contexts) {
63-
Get-Secret -Name $context.Name -Vault $contextVault.Name -AsPlainText:$AsPlainText
64-
}
57+
Write-Verbose "Found [$($contexts.Count)] contexts in context vault [$($contextVault.Name)]"
58+
foreach ($context in $contexts) {
59+
Get-Secret -Name $context.Name -Vault $contextVault.Name -AsPlainText:$AsPlainText
6560
}
6661
}
6762

tests/Context.Tests.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ Describe 'Context' {
1616
It "Get-Context -Name '*' - Should return all contexts" {
1717
{ Get-Context -Name '*' } | Should -Not -Throw
1818
}
19+
20+
It "Get-Context -Name '' - Should return no contexts" {
21+
{ Get-Context -Name '' } | Should -Not -Throw
22+
Get-Context -Name '' | Should -BeNullOrEmpty
23+
}
24+
It 'Get-Context -Name $null - Should return no contexts' {
25+
{ Get-Context -Name $null } | Should -Not -Throw
26+
Get-Context -Name $null | Should -BeNullOrEmpty
27+
}
1928
}
2029

2130
Context 'Function: Set-Context' {

0 commit comments

Comments
 (0)