From 264c137ba37d5593dea15edc12a692a0e01d9851 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 24 Nov 2024 19:03:13 +0100 Subject: [PATCH 01/12] Convert all secret names to and from base64 --- .../private/Base64/ConvertFrom-Base64.ps1 | 24 +++++++++++++++++++ .../private/Base64/ConvertTo-Base64.ps1 | 23 ++++++++++++++++++ src/functions/public/Context/Get-Context.ps1 | 17 ++++++------- .../public/Context/Remove-Context.ps1 | 12 ++++++---- .../public/Context/Rename-Context.ps1 | 12 ++++++++++ src/functions/public/Context/Set-Context.ps1 | 9 ++++++- .../ContextSetting/Get-ContextSetting.ps1 | 10 ++++---- .../ContextSetting/Remove-ContextSetting.ps1 | 10 ++++---- .../ContextSetting/Set-ContextSetting.ps1 | 10 ++++---- 9 files changed, 101 insertions(+), 26 deletions(-) create mode 100644 src/functions/private/Base64/ConvertFrom-Base64.ps1 create mode 100644 src/functions/private/Base64/ConvertTo-Base64.ps1 diff --git a/src/functions/private/Base64/ConvertFrom-Base64.ps1 b/src/functions/private/Base64/ConvertFrom-Base64.ps1 new file mode 100644 index 0000000..29434f1 --- /dev/null +++ b/src/functions/private/Base64/ConvertFrom-Base64.ps1 @@ -0,0 +1,24 @@ +function ConvertFrom-Base64 { + <# + .SYNOPSIS + Converts a Base64 encoded string to a string. + + .DESCRIPTION + Converts a Base64 encoded string to a string. + + .EXAMPLE + ConvertFrom-Base64 -Base64String 'VGhpc0lzQU5pY2VTdHJpbmc=' + ThisIsANiceString + + Converts the Base64 encoded string 'VGhpc0lzQU5pY2VTdHJpbmc=' to a string. + #> + [OutputType([string])] + [CmdletBinding()] + param( + # The Base64 encoded string to convert. + [Parameter(Mandatory = $true)] + [string] $Base64String + ) + + [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($Base64String)) +} diff --git a/src/functions/private/Base64/ConvertTo-Base64.ps1 b/src/functions/private/Base64/ConvertTo-Base64.ps1 new file mode 100644 index 0000000..ea646c9 --- /dev/null +++ b/src/functions/private/Base64/ConvertTo-Base64.ps1 @@ -0,0 +1,23 @@ +function ConvertTo-Base64 { + <# + .SYNOPSIS + Converts a string to a Base64 encoded string. + + .DESCRIPTION + Converts a string to a Base64 encoded string. + + .EXAMPLE + ConvertTo-Base64 -String 'ThisIsANiceString' + VGhpc0lzQU5pY2VTdHJpbmc= + + Converts the string 'ThisIsANiceString' to a Base64 encoded string. + #> + [OutputType([string])] + [CmdletBinding()] + param ( + [Parameter(Mandatory)] + [string] $String + ) + + [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($String)) +} diff --git a/src/functions/public/Context/Get-Context.ps1 b/src/functions/public/Context/Get-Context.ps1 index e74690f..228c6e3 100644 --- a/src/functions/public/Context/Get-Context.ps1 +++ b/src/functions/public/Context/Get-Context.ps1 @@ -42,18 +42,18 @@ filter Get-Context { try { if (-not $PSBoundParameters.ContainsKey('ID')) { Write-Verbose "Retrieving all contexts from [$vaultName]" - $contexts = Get-SecretInfo -Vault $vaultName | Where-Object { $_.Name -like "$secretPrefix*" } + $contexts = Get-SecretInfo -Vault $vaultName | Where-Object { (ConvertFrom-Base64 -Base64String $_.Name) -like "$secretPrefix*" } } elseif ([string]::IsNullOrEmpty($ID)) { Write-Verbose "Return 0 contexts from [$vaultName]" return } elseif ($ID.Contains('*')) { # If wildcards are used, we can use the -Name parameter to filter the results. Its using the -like operator internally in the module. Write-Verbose "Retrieving contexts matching [$ID] from [$vaultName]" - $contexts = Get-SecretInfo -Vault $vaultName -Name $fullID + $contexts = Get-SecretInfo -Vault $vaultName -Name (ConvertTo-Base64 -Base64String $fullID) } else { # Needs to use Where-Object in order to support special characters, like `[` and `]`. Write-Verbose "Retrieving context [$ID] from [$vaultName]" - $contexts = Get-SecretInfo -Vault $vaultName | Where-Object { $_.Name -eq $fullID } + $contexts = Get-SecretInfo -Vault $vaultName | Where-Object { (ConvertFrom-Base64 -Base64String $_.Name) -eq $fullID } } Write-Verbose "Found [$($contexts.Count)] contexts in [$vaultName]" @@ -77,9 +77,10 @@ Register-ArgumentCompleter -CommandName Get-Context -ParameterName ID -ScriptBlo param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - Get-SecretInfo -Vault $vaultName | Where-Object { $_.Name -like "$($script:Config.SecretPrefix)$wordToComplete*" } | ForEach-Object { - $Name = $_.Name -replace "^$($script:Config.SecretPrefix)" - [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) - } + Get-SecretInfo -Vault $vaultName | + Where-Object { (ConvertFrom-Base64 -Base64String $_.Name) -like "$($script:Config.SecretPrefix)$wordToComplete*" } | + ForEach-Object { + $Name = (ConvertFrom-Base64 -Base64String $_.Name) -replace "^$($script:Config.SecretPrefix)" + [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) + } } - diff --git a/src/functions/public/Context/Remove-Context.ps1 b/src/functions/public/Context/Remove-Context.ps1 index fffb82c..018ba1a 100644 --- a/src/functions/public/Context/Remove-Context.ps1 +++ b/src/functions/public/Context/Remove-Context.ps1 @@ -43,7 +43,7 @@ filter Remove-Context { try { if ($PSCmdlet.ShouldProcess($fullID, 'Remove secret')) { - Get-SecretInfo -Vault $vaultName | Where-Object { $_.Name -eq $fullID } | Remove-Secret + Get-SecretInfo -Vault $vaultName | Where-Object { (ConvertFrom-Base64 -Base64String $_.Name) -eq $fullID } | Remove-Secret } } catch { Write-Error $_ @@ -60,8 +60,10 @@ Register-ArgumentCompleter -CommandName Remove-Context -ParameterName ID -Script param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - Get-SecretInfo -Vault $vaultName | Where-Object { $_.Name -like "$($script:Config.SecretPrefix)$wordToComplete*" } | ForEach-Object { - $Name = $_.Name -replace "^$($script:Config.SecretPrefix)" - [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) - } + Get-SecretInfo -Vault $vaultName | + Where-Object { (ConvertFrom-Base64 -Base64String $_.Name) -like "$($script:Config.SecretPrefix)$wordToComplete*" } | + ForEach-Object { + $Name = (ConvertFrom-Base64 -Base64String $_.Name) -replace "^$($script:Config.SecretPrefix)" + [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) + } } diff --git a/src/functions/public/Context/Rename-Context.ps1 b/src/functions/public/Context/Rename-Context.ps1 index 4c75517..0dae813 100644 --- a/src/functions/public/Context/Rename-Context.ps1 +++ b/src/functions/public/Context/Rename-Context.ps1 @@ -61,3 +61,15 @@ Write-Debug "[$commandName] - End" } } + +Register-ArgumentCompleter -CommandName Rename-Context -ParameterName ID -ScriptBlock { + param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) + $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter + + Get-SecretInfo -Vault $vaultName | + Where-Object { (ConvertFrom-Base64 -Base64String $_.Name) -like "$($script:Config.SecretPrefix)$wordToComplete*" } | + ForEach-Object { + $Name = (ConvertFrom-Base64 -Base64String $_.Name) -replace "^$($script:Config.SecretPrefix)" + [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) + } +} diff --git a/src/functions/public/Context/Set-Context.ps1 b/src/functions/public/Context/Set-Context.ps1 index 8994424..d867704 100644 --- a/src/functions/public/Context/Set-Context.ps1 +++ b/src/functions/public/Context/Set-Context.ps1 @@ -48,8 +48,15 @@ function Set-Context { throw 'Failed to convert context to JSON' } + try { + $name64 = ConvertTo-Base64 -String $fullID + } catch { + Write-Error $_ + throw 'Failed to convert ID to Base64' + } + $param = @{ - Name = $fullID + Name = $name64 Secret = $secret Vault = $vaultName } diff --git a/src/functions/public/ContextSetting/Get-ContextSetting.ps1 b/src/functions/public/ContextSetting/Get-ContextSetting.ps1 index baf5b3b..93f0fda 100644 --- a/src/functions/public/ContextSetting/Get-ContextSetting.ps1 +++ b/src/functions/public/ContextSetting/Get-ContextSetting.ps1 @@ -57,10 +57,12 @@ Register-ArgumentCompleter -CommandName Get-ContextSetting -ParameterName ID -Sc param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - Get-SecretInfo -Vault $vaultName | Where-Object { $_.Name -like "$($script:Config.SecretPrefix)$wordToComplete*" } | ForEach-Object { - $Name = $_.Name -replace "^$($script:Config.SecretPrefix)" - [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) - } + Get-SecretInfo -Vault $vaultName | + Where-Object { (ConvertFrom-Base64 -Base64String $_.Name) -like "$($script:Config.SecretPrefix)$wordToComplete*" } | + ForEach-Object { + $Name = (ConvertFrom-Base64 -Base64String $_.Name) -replace "^$($script:Config.SecretPrefix)" + [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) + } } Register-ArgumentCompleter -CommandName Get-ContextSetting -ParameterName Name -ScriptBlock { diff --git a/src/functions/public/ContextSetting/Remove-ContextSetting.ps1 b/src/functions/public/ContextSetting/Remove-ContextSetting.ps1 index 578e120..8b13f1b 100644 --- a/src/functions/public/ContextSetting/Remove-ContextSetting.ps1 +++ b/src/functions/public/ContextSetting/Remove-ContextSetting.ps1 @@ -78,10 +78,12 @@ Register-ArgumentCompleter -CommandName Remove-ContextSetting -ParameterName ID param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - Get-SecretInfo -Vault $vaultName | Where-Object { $_.Name -like "$($script:Config.SecretPrefix)$wordToComplete*" } | ForEach-Object { - $Name = $_.Name -replace "^$($script:Config.SecretPrefix)" - [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) - } + Get-SecretInfo -Vault $vaultName | + Where-Object { (ConvertFrom-Base64 -Base64String $_.Name) -like "$($script:Config.SecretPrefix)$wordToComplete*" } | + ForEach-Object { + $Name = (ConvertFrom-Base64 -Base64String $_.Name) -replace "^$($script:Config.SecretPrefix)" + [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) + } } Register-ArgumentCompleter -CommandName Get-ContextSetting -ParameterName Name -ScriptBlock { diff --git a/src/functions/public/ContextSetting/Set-ContextSetting.ps1 b/src/functions/public/ContextSetting/Set-ContextSetting.ps1 index 1163c42..6601b59 100644 --- a/src/functions/public/ContextSetting/Set-ContextSetting.ps1 +++ b/src/functions/public/ContextSetting/Set-ContextSetting.ps1 @@ -76,8 +76,10 @@ Register-ArgumentCompleter -CommandName Get-ContextSetting -ParameterName ID -Sc param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - Get-SecretInfo -Vault $vaultName | Where-Object { $_.Name -like "$($script:Config.SecretPrefix)$wordToComplete*" } | ForEach-Object { - $Name = $_.Name -replace "^$($script:Config.SecretPrefix)" - [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) - } + Get-SecretInfo -Vault $vaultName | + Where-Object { (ConvertFrom-Base64 -Base64String $_.Name) -like "$($script:Config.SecretPrefix)$wordToComplete*" } | + ForEach-Object { + $Name = (ConvertFrom-Base64 -Base64String $_.Name) -replace "^$($script:Config.SecretPrefix)" + [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) + } } From 7c3328f6609750ffad739fb6c3d6f16f328d4f87 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 24 Nov 2024 19:43:40 +0100 Subject: [PATCH 02/12] =?UTF-8?q?=F0=9F=86=95=20[Add]:=20Implement=20Test-?= =?UTF-8?q?Base64=20function=20to=20validate=20Base64=20strings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/functions/private/Base64/Test-Base64.ps1 | 26 ++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/functions/private/Base64/Test-Base64.ps1 diff --git a/src/functions/private/Base64/Test-Base64.ps1 b/src/functions/private/Base64/Test-Base64.ps1 new file mode 100644 index 0000000..e0e5b2b --- /dev/null +++ b/src/functions/private/Base64/Test-Base64.ps1 @@ -0,0 +1,26 @@ +function Test-Base64 { + <# + .SYNOPSIS + Test if a string is a valid Base64 string. + + .DESCRIPTION + Test if a string is a valid Base64 string. + + .EXAMPLE + Test-Base64 -Base64String 'U29tZSBkYXRh' + True + + Returns $true as the string is a valid Base64 string. + #> + [OutputType([bool])] + [CmdletBinding()] + param ( + [string] $Base64String + ) + try { + [Convert]::FromBase64String($Base64String) | Out-Null + return $true + } catch { + return $false + } +} From 82fcb9c9aae94a9442fa9e74f0879ad6dfbf181a Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 24 Nov 2024 19:43:52 +0100 Subject: [PATCH 03/12] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Refactor=20Get-?= =?UTF-8?q?Context=20to=20utilize=20context=20names=20from=20Get-SecretInf?= =?UTF-8?q?o=20and=20improve=20filtering=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/functions/public/Context/Get-Context.ps1 | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/functions/public/Context/Get-Context.ps1 b/src/functions/public/Context/Get-Context.ps1 index 228c6e3..cbe1442 100644 --- a/src/functions/public/Context/Get-Context.ps1 +++ b/src/functions/public/Context/Get-Context.ps1 @@ -36,24 +36,31 @@ filter Get-Context { $vaultName = $script:Config.VaultName $secretPrefix = $script:Config.SecretPrefix $fullID = "$secretPrefix$ID" + + $contextNames = Get-SecretInfo -Vault $vaultName | Select-Object -ExpandProperty Name | ForEach-Object { + if (Test-Base64 -Base64String $_) { + $name = ConvertFrom-Base64 -Base64String $_ + if ($name.StartsWith($secretPrefix)) { + Write-Verbose " + $name" + $name + } + } + } } process { try { if (-not $PSBoundParameters.ContainsKey('ID')) { Write-Verbose "Retrieving all contexts from [$vaultName]" - $contexts = Get-SecretInfo -Vault $vaultName | Where-Object { (ConvertFrom-Base64 -Base64String $_.Name) -like "$secretPrefix*" } } elseif ([string]::IsNullOrEmpty($ID)) { Write-Verbose "Return 0 contexts from [$vaultName]" return } elseif ($ID.Contains('*')) { - # If wildcards are used, we can use the -Name parameter to filter the results. Its using the -like operator internally in the module. Write-Verbose "Retrieving contexts matching [$ID] from [$vaultName]" - $contexts = Get-SecretInfo -Vault $vaultName -Name (ConvertTo-Base64 -Base64String $fullID) + $contexts = $contextNames | Where-Object { $_ -like $fullID } } else { - # Needs to use Where-Object in order to support special characters, like `[` and `]`. Write-Verbose "Retrieving context [$ID] from [$vaultName]" - $contexts = Get-SecretInfo -Vault $vaultName | Where-Object { (ConvertFrom-Base64 -Base64String $_.Name) -eq $fullID } + $contexts = $contextNames | Where-Object { $_ -eq $fullID } } Write-Verbose "Found [$($contexts.Count)] contexts in [$vaultName]" From a975433c382e1426255c71a0957f0168b619061f Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 24 Nov 2024 19:55:43 +0100 Subject: [PATCH 04/12] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Optimize=20Base?= =?UTF-8?q?64=20validation=20in=20Test-Base64=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/functions/private/Base64/Test-Base64.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions/private/Base64/Test-Base64.ps1 b/src/functions/private/Base64/Test-Base64.ps1 index e0e5b2b..dc10f08 100644 --- a/src/functions/private/Base64/Test-Base64.ps1 +++ b/src/functions/private/Base64/Test-Base64.ps1 @@ -18,7 +18,7 @@ [string] $Base64String ) try { - [Convert]::FromBase64String($Base64String) | Out-Null + $null = [Convert]::FromBase64String($Base64String) return $true } catch { return $false From 9c61966c97765679be7251d1697147bedbf328cd Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 24 Nov 2024 20:19:51 +0100 Subject: [PATCH 05/12] Fix conversion to get secrets --- src/functions/private/Get-ContextInfo.ps1 | 36 ++++++++++++++++++++ src/functions/public/Context/Get-Context.ps1 | 28 +++++---------- 2 files changed, 45 insertions(+), 19 deletions(-) create mode 100644 src/functions/private/Get-ContextInfo.ps1 diff --git a/src/functions/private/Get-ContextInfo.ps1 b/src/functions/private/Get-ContextInfo.ps1 new file mode 100644 index 0000000..dd4888a --- /dev/null +++ b/src/functions/private/Get-ContextInfo.ps1 @@ -0,0 +1,36 @@ +function Get-ContextInfo { + <# + .SYNOPSIS + Retrieves all context info from the context vault. + + .DESCRIPTION + Retrieves all context info from the context vault. + + .EXAMPLE + Get-ContextInfo + + Get all context info from the context vault. + #> + param() + + $vaultName = $script:Config.VaultName + $secretPrefix = $script:Config.SecretPrefix + + Write-Verbose "Retrieving all context info from [$vaultName]" + + Get-SecretInfo -Vault $vaultName | ForEach-Object { + $name64 = $_.Name + if (Test-Base64 -Base64String $name64) { + $name = ConvertFrom-Base64 -Base64String $name64 + if ($name.StartsWith($secretPrefix)) { + Write-Verbose " + $name ($name64)" + [pscustomobject]@{ + Name64 = $name64 + Name = $name + Metadata = $_.Metadata + Type = $_.Type + } + } + } + } +} diff --git a/src/functions/public/Context/Get-Context.ps1 b/src/functions/public/Context/Get-Context.ps1 index cbe1442..e8b9c39 100644 --- a/src/functions/public/Context/Get-Context.ps1 +++ b/src/functions/public/Context/Get-Context.ps1 @@ -37,15 +37,7 @@ filter Get-Context { $secretPrefix = $script:Config.SecretPrefix $fullID = "$secretPrefix$ID" - $contextNames = Get-SecretInfo -Vault $vaultName | Select-Object -ExpandProperty Name | ForEach-Object { - if (Test-Base64 -Base64String $_) { - $name = ConvertFrom-Base64 -Base64String $_ - if ($name.StartsWith($secretPrefix)) { - Write-Verbose " + $name" - $name - } - } - } + $contextInfos = Get-ContextInfo } process { @@ -56,17 +48,16 @@ filter Get-Context { Write-Verbose "Return 0 contexts from [$vaultName]" return } elseif ($ID.Contains('*')) { - Write-Verbose "Retrieving contexts matching [$ID] from [$vaultName]" - $contexts = $contextNames | Where-Object { $_ -like $fullID } + Write-Verbose "Retrieving contexts like [$ID] from [$vaultName]" + $contextInfos = $contextInfos | Where-Object { $_.Name -like $fullID } } else { Write-Verbose "Retrieving context [$ID] from [$vaultName]" - $contexts = $contextNames | Where-Object { $_ -eq $fullID } + $contextInfos = $contextInfos | Where-Object { $_.Name -eq $fullID } } - Write-Verbose "Found [$($contexts.Count)] contexts in [$vaultName]" - $contexts | ForEach-Object { - Write-Verbose " - $($_.Name)" - $contextJson = $_ | Get-Secret -AsPlainText + Write-Verbose "Found [$($contextInfos.Count)] contexts in [$vaultName]" + $contextInfos | ForEach-Object { + $contextJson = Get-Secret -Name $_.Name64 -Vault $vaultName -AsPlainText ConvertFrom-ContextJson -JsonString $contextJson } } catch { @@ -84,10 +75,9 @@ Register-ArgumentCompleter -CommandName Get-Context -ParameterName ID -ScriptBlo param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - Get-SecretInfo -Vault $vaultName | - Where-Object { (ConvertFrom-Base64 -Base64String $_.Name) -like "$($script:Config.SecretPrefix)$wordToComplete*" } | + Get-ContextInfo | Where-Object { $_.Name -like "$($script:Config.SecretPrefix)$wordToComplete*" } | ForEach-Object { - $Name = (ConvertFrom-Base64 -Base64String $_.Name) -replace "^$($script:Config.SecretPrefix)" + $Name = $_.Name -replace "^$($script:Config.SecretPrefix)" [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) } } From 579748c3523d582c34ea718e3391f973ef5268be Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 24 Nov 2024 20:25:52 +0100 Subject: [PATCH 06/12] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Update=20test?= =?UTF-8?q?=20to=20use=20Get-Context=20instead=20of=20Get-Secret=20for=20i?= =?UTF-8?q?mproved=20clarity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/Context.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Context.Tests.ps1 b/tests/Context.Tests.ps1 index ed3c1a1..d6b7a59 100644 --- a/tests/Context.Tests.ps1 +++ b/tests/Context.Tests.ps1 @@ -265,7 +265,7 @@ Describe 'Context' { Set-Context -Context $githubLoginContext -ID 'BigComplexObject' Set-Context -Context $githubLoginContext -ID 'BigComplexObject' Set-Context -Context $githubLoginContext -ID 'BigComplexObject' - Write-Verbose (Get-Secret -Name 'Context:BigComplexObject' -AsPlainText) -Verbose + Write-Verbose (Get-Context -Name 'BigComplexObject') -Verbose $object = Get-Context -ID 'BigComplexObject' $object.ApiRateLimits.Remaining | Should -Be 4985 $object.AuthToken | Should -BeOfType [System.Security.SecureString] From f68973b88ce7d7d96128ce67874fd2d51792bf33 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 24 Nov 2024 20:33:57 +0100 Subject: [PATCH 07/12] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Refactor=20cont?= =?UTF-8?q?ext-related=20functions=20to=20use=20Get-ContextInfo=20instead?= =?UTF-8?q?=20of=20Get-SecretInfo=20for=20improved=20consistency=20and=20c?= =?UTF-8?q?larity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/functions/public/Context/Remove-Context.ps1 | 5 ++--- src/functions/public/Context/Rename-Context.ps1 | 5 ++--- src/functions/public/ContextSetting/Get-ContextSetting.ps1 | 5 ++--- .../public/ContextSetting/Remove-ContextSetting.ps1 | 5 ++--- src/functions/public/ContextSetting/Set-ContextSetting.ps1 | 5 ++--- 5 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/functions/public/Context/Remove-Context.ps1 b/src/functions/public/Context/Remove-Context.ps1 index 018ba1a..35d46ac 100644 --- a/src/functions/public/Context/Remove-Context.ps1 +++ b/src/functions/public/Context/Remove-Context.ps1 @@ -60,10 +60,9 @@ Register-ArgumentCompleter -CommandName Remove-Context -ParameterName ID -Script param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - Get-SecretInfo -Vault $vaultName | - Where-Object { (ConvertFrom-Base64 -Base64String $_.Name) -like "$($script:Config.SecretPrefix)$wordToComplete*" } | + Get-ContextInfo | Where-Object { $_.Name -like "$($script:Config.SecretPrefix)$wordToComplete*" } | ForEach-Object { - $Name = (ConvertFrom-Base64 -Base64String $_.Name) -replace "^$($script:Config.SecretPrefix)" + $Name = $_.Name -replace "^$($script:Config.SecretPrefix)" [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) } } diff --git a/src/functions/public/Context/Rename-Context.ps1 b/src/functions/public/Context/Rename-Context.ps1 index 0dae813..ac1f7a4 100644 --- a/src/functions/public/Context/Rename-Context.ps1 +++ b/src/functions/public/Context/Rename-Context.ps1 @@ -66,10 +66,9 @@ Register-ArgumentCompleter -CommandName Rename-Context -ParameterName ID -Script param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - Get-SecretInfo -Vault $vaultName | - Where-Object { (ConvertFrom-Base64 -Base64String $_.Name) -like "$($script:Config.SecretPrefix)$wordToComplete*" } | + Get-ContextInfo | Where-Object { $_.Name -like "$($script:Config.SecretPrefix)$wordToComplete*" } | ForEach-Object { - $Name = (ConvertFrom-Base64 -Base64String $_.Name) -replace "^$($script:Config.SecretPrefix)" + $Name = $_.Name -replace "^$($script:Config.SecretPrefix)" [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) } } diff --git a/src/functions/public/ContextSetting/Get-ContextSetting.ps1 b/src/functions/public/ContextSetting/Get-ContextSetting.ps1 index 93f0fda..357d57e 100644 --- a/src/functions/public/ContextSetting/Get-ContextSetting.ps1 +++ b/src/functions/public/ContextSetting/Get-ContextSetting.ps1 @@ -57,10 +57,9 @@ Register-ArgumentCompleter -CommandName Get-ContextSetting -ParameterName ID -Sc param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - Get-SecretInfo -Vault $vaultName | - Where-Object { (ConvertFrom-Base64 -Base64String $_.Name) -like "$($script:Config.SecretPrefix)$wordToComplete*" } | + Get-ContextInfo | Where-Object { $_.Name -like "$($script:Config.SecretPrefix)$wordToComplete*" } | ForEach-Object { - $Name = (ConvertFrom-Base64 -Base64String $_.Name) -replace "^$($script:Config.SecretPrefix)" + $Name = $_.Name -replace "^$($script:Config.SecretPrefix)" [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) } } diff --git a/src/functions/public/ContextSetting/Remove-ContextSetting.ps1 b/src/functions/public/ContextSetting/Remove-ContextSetting.ps1 index 8b13f1b..5b55a9e 100644 --- a/src/functions/public/ContextSetting/Remove-ContextSetting.ps1 +++ b/src/functions/public/ContextSetting/Remove-ContextSetting.ps1 @@ -78,10 +78,9 @@ Register-ArgumentCompleter -CommandName Remove-ContextSetting -ParameterName ID param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - Get-SecretInfo -Vault $vaultName | - Where-Object { (ConvertFrom-Base64 -Base64String $_.Name) -like "$($script:Config.SecretPrefix)$wordToComplete*" } | + Get-ContextInfo | Where-Object { $_.Name -like "$($script:Config.SecretPrefix)$wordToComplete*" } | ForEach-Object { - $Name = (ConvertFrom-Base64 -Base64String $_.Name) -replace "^$($script:Config.SecretPrefix)" + $Name = $_.Name -replace "^$($script:Config.SecretPrefix)" [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) } } diff --git a/src/functions/public/ContextSetting/Set-ContextSetting.ps1 b/src/functions/public/ContextSetting/Set-ContextSetting.ps1 index 6601b59..d26912d 100644 --- a/src/functions/public/ContextSetting/Set-ContextSetting.ps1 +++ b/src/functions/public/ContextSetting/Set-ContextSetting.ps1 @@ -76,10 +76,9 @@ Register-ArgumentCompleter -CommandName Get-ContextSetting -ParameterName ID -Sc param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - Get-SecretInfo -Vault $vaultName | - Where-Object { (ConvertFrom-Base64 -Base64String $_.Name) -like "$($script:Config.SecretPrefix)$wordToComplete*" } | + Get-ContextInfo | Where-Object { $_.Name -like "$($script:Config.SecretPrefix)$wordToComplete*" } | ForEach-Object { - $Name = (ConvertFrom-Base64 -Base64String $_.Name) -replace "^$($script:Config.SecretPrefix)" + $Name = $_.Name -replace "^$($script:Config.SecretPrefix)" [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) } } From 837cb60c0848935c52921b7895ba0c166b21df70 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 24 Nov 2024 20:35:35 +0100 Subject: [PATCH 08/12] Fix issue in test --- tests/Context.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Context.Tests.ps1 b/tests/Context.Tests.ps1 index d6b7a59..dfd0f6b 100644 --- a/tests/Context.Tests.ps1 +++ b/tests/Context.Tests.ps1 @@ -265,7 +265,7 @@ Describe 'Context' { Set-Context -Context $githubLoginContext -ID 'BigComplexObject' Set-Context -Context $githubLoginContext -ID 'BigComplexObject' Set-Context -Context $githubLoginContext -ID 'BigComplexObject' - Write-Verbose (Get-Context -Name 'BigComplexObject') -Verbose + Write-Verbose (Get-Context -ID 'BigComplexObject') -Verbose $object = Get-Context -ID 'BigComplexObject' $object.ApiRateLimits.Remaining | Should -Be 4985 $object.AuthToken | Should -BeOfType [System.Security.SecureString] From 1b16a0400c86c1d0eaefa9945664fb448f64980c Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 24 Nov 2024 20:53:29 +0100 Subject: [PATCH 09/12] Put Context: infron of the base64, and have a function manage it. --- src/functions/private/Get-ContextInfo.ps1 | 18 ++++++++---------- src/functions/public/Context/Get-Context.ps1 | 12 ++++-------- .../public/Context/Remove-Context.ps1 | 12 ++++-------- .../public/Context/Rename-Context.ps1 | 5 ++--- src/functions/public/Context/Set-Context.ps1 | 9 ++++----- .../ContextSetting/Get-ContextSetting.ps1 | 5 ++--- .../ContextSetting/Remove-ContextSetting.ps1 | 5 ++--- .../ContextSetting/Set-ContextSetting.ps1 | 5 ++--- 8 files changed, 28 insertions(+), 43 deletions(-) diff --git a/src/functions/private/Get-ContextInfo.ps1 b/src/functions/private/Get-ContextInfo.ps1 index dd4888a..dfcbf64 100644 --- a/src/functions/private/Get-ContextInfo.ps1 +++ b/src/functions/private/Get-ContextInfo.ps1 @@ -18,18 +18,16 @@ Write-Verbose "Retrieving all context info from [$vaultName]" - Get-SecretInfo -Vault $vaultName | ForEach-Object { - $name64 = $_.Name + Get-SecretInfo -Vault $vaultName | Where-Object { ($_.Name).StartsWith($secretPrefix) } | ForEach-Object { + $name64 = $_.Name -replace "^$secretPrefix" if (Test-Base64 -Base64String $name64) { $name = ConvertFrom-Base64 -Base64String $name64 - if ($name.StartsWith($secretPrefix)) { - Write-Verbose " + $name ($name64)" - [pscustomobject]@{ - Name64 = $name64 - Name = $name - Metadata = $_.Metadata - Type = $_.Type - } + Write-Verbose " + $name ($name64)" + [pscustomobject]@{ + Name64 = $name64 + Name = $name + Metadata = $_.Metadata + Type = $_.Type } } } diff --git a/src/functions/public/Context/Get-Context.ps1 b/src/functions/public/Context/Get-Context.ps1 index e8b9c39..9b813c2 100644 --- a/src/functions/public/Context/Get-Context.ps1 +++ b/src/functions/public/Context/Get-Context.ps1 @@ -34,9 +34,6 @@ filter Get-Context { Write-Debug "[$commandName] - Start" $null = Get-ContextVault $vaultName = $script:Config.VaultName - $secretPrefix = $script:Config.SecretPrefix - $fullID = "$secretPrefix$ID" - $contextInfos = Get-ContextInfo } @@ -49,10 +46,10 @@ filter Get-Context { return } elseif ($ID.Contains('*')) { Write-Verbose "Retrieving contexts like [$ID] from [$vaultName]" - $contextInfos = $contextInfos | Where-Object { $_.Name -like $fullID } + $contextInfos = $contextInfos | Where-Object { $_.Name -like $ID } } else { Write-Verbose "Retrieving context [$ID] from [$vaultName]" - $contextInfos = $contextInfos | Where-Object { $_.Name -eq $fullID } + $contextInfos = $contextInfos | Where-Object { $_.Name -eq $ID } } Write-Verbose "Found [$($contextInfos.Count)] contexts in [$vaultName]" @@ -75,9 +72,8 @@ Register-ArgumentCompleter -CommandName Get-Context -ParameterName ID -ScriptBlo param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - Get-ContextInfo | Where-Object { $_.Name -like "$($script:Config.SecretPrefix)$wordToComplete*" } | + Get-ContextInfo | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object { - $Name = $_.Name -replace "^$($script:Config.SecretPrefix)" - [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) + [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name) } } diff --git a/src/functions/public/Context/Remove-Context.ps1 b/src/functions/public/Context/Remove-Context.ps1 index 35d46ac..2e31365 100644 --- a/src/functions/public/Context/Remove-Context.ps1 +++ b/src/functions/public/Context/Remove-Context.ps1 @@ -34,16 +34,13 @@ filter Remove-Context { $commandName = $MyInvocation.MyCommand.Name Write-Debug "[$commandName] - Start" $null = Get-ContextVault - $vaultName = $script:Config.VaultName - $secretPrefix = $script:Config.SecretPrefix - $fullID = "$secretPrefix$ID" } process { try { - if ($PSCmdlet.ShouldProcess($fullID, 'Remove secret')) { - Get-SecretInfo -Vault $vaultName | Where-Object { (ConvertFrom-Base64 -Base64String $_.Name) -eq $fullID } | Remove-Secret + if ($PSCmdlet.ShouldProcess($ID, 'Remove secret')) { + Get-ContextInfo | Where-Object { $_.Name -eq $ID } | Remove-Secret } } catch { Write-Error $_ @@ -60,9 +57,8 @@ Register-ArgumentCompleter -CommandName Remove-Context -ParameterName ID -Script param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - Get-ContextInfo | Where-Object { $_.Name -like "$($script:Config.SecretPrefix)$wordToComplete*" } | + Get-ContextInfo | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object { - $Name = $_.Name -replace "^$($script:Config.SecretPrefix)" - [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) + [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name) } } diff --git a/src/functions/public/Context/Rename-Context.ps1 b/src/functions/public/Context/Rename-Context.ps1 index ac1f7a4..56ad51a 100644 --- a/src/functions/public/Context/Rename-Context.ps1 +++ b/src/functions/public/Context/Rename-Context.ps1 @@ -66,9 +66,8 @@ Register-ArgumentCompleter -CommandName Rename-Context -ParameterName ID -Script param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - Get-ContextInfo | Where-Object { $_.Name -like "$($script:Config.SecretPrefix)$wordToComplete*" } | + Get-ContextInfo | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object { - $Name = $_.Name -replace "^$($script:Config.SecretPrefix)" - [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) + [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name) } } diff --git a/src/functions/public/Context/Set-Context.ps1 b/src/functions/public/Context/Set-Context.ps1 index d867704..f30e4f8 100644 --- a/src/functions/public/Context/Set-Context.ps1 +++ b/src/functions/public/Context/Set-Context.ps1 @@ -37,33 +37,32 @@ function Set-Context { $null = Get-ContextVault $vaultName = $script:Config.VaultName $secretPrefix = $script:Config.SecretPrefix - $fullID = "$secretPrefix$ID" } process { try { - $secret = ConvertTo-ContextJson -Context $Context -ID $fullID + $secret = ConvertTo-ContextJson -Context $Context -ID $ID } catch { Write-Error $_ throw 'Failed to convert context to JSON' } try { - $name64 = ConvertTo-Base64 -String $fullID + $name64 = ConvertTo-Base64 -String $ID } catch { Write-Error $_ throw 'Failed to convert ID to Base64' } $param = @{ - Name = $name64 + Name = "$secretPrefix$name64" Secret = $secret Vault = $vaultName } Write-Verbose ($param | ConvertTo-Json -Depth 5) try { - if ($PSCmdlet.ShouldProcess($fullID, 'Set Secret')) { + if ($PSCmdlet.ShouldProcess($ID, 'Set Secret')) { Set-Secret @param } } catch { diff --git a/src/functions/public/ContextSetting/Get-ContextSetting.ps1 b/src/functions/public/ContextSetting/Get-ContextSetting.ps1 index 357d57e..21726b3 100644 --- a/src/functions/public/ContextSetting/Get-ContextSetting.ps1 +++ b/src/functions/public/ContextSetting/Get-ContextSetting.ps1 @@ -57,10 +57,9 @@ Register-ArgumentCompleter -CommandName Get-ContextSetting -ParameterName ID -Sc param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - Get-ContextInfo | Where-Object { $_.Name -like "$($script:Config.SecretPrefix)$wordToComplete*" } | + Get-ContextInfo | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object { - $Name = $_.Name -replace "^$($script:Config.SecretPrefix)" - [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) + [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name) } } diff --git a/src/functions/public/ContextSetting/Remove-ContextSetting.ps1 b/src/functions/public/ContextSetting/Remove-ContextSetting.ps1 index 5b55a9e..a327fd9 100644 --- a/src/functions/public/ContextSetting/Remove-ContextSetting.ps1 +++ b/src/functions/public/ContextSetting/Remove-ContextSetting.ps1 @@ -78,10 +78,9 @@ Register-ArgumentCompleter -CommandName Remove-ContextSetting -ParameterName ID param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - Get-ContextInfo | Where-Object { $_.Name -like "$($script:Config.SecretPrefix)$wordToComplete*" } | + Get-ContextInfo | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object { - $Name = $_.Name -replace "^$($script:Config.SecretPrefix)" - [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) + [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name) } } diff --git a/src/functions/public/ContextSetting/Set-ContextSetting.ps1 b/src/functions/public/ContextSetting/Set-ContextSetting.ps1 index d26912d..822cee7 100644 --- a/src/functions/public/ContextSetting/Set-ContextSetting.ps1 +++ b/src/functions/public/ContextSetting/Set-ContextSetting.ps1 @@ -76,9 +76,8 @@ Register-ArgumentCompleter -CommandName Get-ContextSetting -ParameterName ID -Sc param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - Get-ContextInfo | Where-Object { $_.Name -like "$($script:Config.SecretPrefix)$wordToComplete*" } | + Get-ContextInfo | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object { - $Name = $_.Name -replace "^$($script:Config.SecretPrefix)" - [System.Management.Automation.CompletionResult]::new($Name, $Name, 'ParameterValue', $Name) + [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name) } } From 1533026b8801f23fd4e6cb1202fcf57d31c3d07d Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 24 Nov 2024 21:11:50 +0100 Subject: [PATCH 10/12] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Update=20Get-Co?= =?UTF-8?q?ntextInfo=20to=20include=20SecretName=20and=20modify=20Get-Cont?= =?UTF-8?q?ext=20to=20use=20SecretName=20for=20improved=20clarity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/functions/private/Get-ContextInfo.ps1 | 9 +++++---- src/functions/public/Context/Get-Context.ps1 | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/functions/private/Get-ContextInfo.ps1 b/src/functions/private/Get-ContextInfo.ps1 index dfcbf64..426fa63 100644 --- a/src/functions/private/Get-ContextInfo.ps1 +++ b/src/functions/private/Get-ContextInfo.ps1 @@ -24,10 +24,11 @@ $name = ConvertFrom-Base64 -Base64String $name64 Write-Verbose " + $name ($name64)" [pscustomobject]@{ - Name64 = $name64 - Name = $name - Metadata = $_.Metadata - Type = $_.Type + Name64 = $name64 + SecretName = $_.Name + Name = $name + Metadata = $_.Metadata + Type = $_.Type } } } diff --git a/src/functions/public/Context/Get-Context.ps1 b/src/functions/public/Context/Get-Context.ps1 index 9b813c2..dfc7d67 100644 --- a/src/functions/public/Context/Get-Context.ps1 +++ b/src/functions/public/Context/Get-Context.ps1 @@ -54,7 +54,7 @@ filter Get-Context { Write-Verbose "Found [$($contextInfos.Count)] contexts in [$vaultName]" $contextInfos | ForEach-Object { - $contextJson = Get-Secret -Name $_.Name64 -Vault $vaultName -AsPlainText + $contextJson = Get-Secret -Name $_.SecretName -Vault $vaultName -AsPlainText ConvertFrom-ContextJson -JsonString $contextJson } } catch { From dc7ef4f0f7553c2a27e655de8b588aec011aa9c0 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 24 Nov 2024 21:19:38 +0100 Subject: [PATCH 11/12] Fix tests --- src/functions/public/Context/Remove-Context.ps1 | 4 +++- tests/Context.Tests.ps1 | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/functions/public/Context/Remove-Context.ps1 b/src/functions/public/Context/Remove-Context.ps1 index 2e31365..2fadfa0 100644 --- a/src/functions/public/Context/Remove-Context.ps1 +++ b/src/functions/public/Context/Remove-Context.ps1 @@ -40,7 +40,9 @@ filter Remove-Context { try { if ($PSCmdlet.ShouldProcess($ID, 'Remove secret')) { - Get-ContextInfo | Where-Object { $_.Name -eq $ID } | Remove-Secret + Get-ContextInfo | Where-Object { $_.Name -eq $ID } | ForEach-Object { + Remove-Secret -Name $_.SecretName -Vault $script:Config.VaultName + } } } catch { Write-Error $_ diff --git a/tests/Context.Tests.ps1 b/tests/Context.Tests.ps1 index dfd0f6b..bde0835 100644 --- a/tests/Context.Tests.ps1 +++ b/tests/Context.Tests.ps1 @@ -119,7 +119,7 @@ Describe 'Context' { $result = Get-Context -ID 'TestID' $result | Should -Not -BeNullOrEmpty $result.Name | Should -Be 'TestName' - $result.ID | Should -Be 'Context:TestID' + $result.ID | Should -Be 'TestID' } It 'Set-Context -Context $Context - Context can hold a bigger object' { $Context = @{ @@ -134,7 +134,7 @@ Describe 'Context' { $result.Count | Should -Be 1 $result | Should -Not -BeNullOrEmpty $result.AccessToken | Should -Be 'MySecret' - $result.ID | Should -Be 'Context:TestID2' + $result.ID | Should -Be 'TestID2' } It 'Set-Context -Context $Context - Context can be saved multiple times' { $Context = @{ @@ -150,7 +150,7 @@ Describe 'Context' { $result | Should -Not -BeNullOrEmpty $result.AccessToken | Should -Be 'MySecret' $result.RefreshToken | Should -Be 'MyRefreshedSecret' - $result.ID | Should -Be 'Context:TestID3' + $result.ID | Should -Be 'TestID3' } } From f77a6b091e502093e5d9fa25c43787442e27df9d Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 24 Nov 2024 21:24:44 +0100 Subject: [PATCH 12/12] Adding the special usecase :) --- tests/Context.Tests.ps1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Context.Tests.ps1 b/tests/Context.Tests.ps1 index bde0835..3a64481 100644 --- a/tests/Context.Tests.ps1 +++ b/tests/Context.Tests.ps1 @@ -262,11 +262,11 @@ Describe 'Context' { } # Test to see if it can be run multiple times - Set-Context -Context $githubLoginContext -ID 'BigComplexObject' - Set-Context -Context $githubLoginContext -ID 'BigComplexObject' - Set-Context -Context $githubLoginContext -ID 'BigComplexObject' - Write-Verbose (Get-Context -ID 'BigComplexObject') -Verbose - $object = Get-Context -ID 'BigComplexObject' + Set-Context -Context $githubLoginContext -ID 'BigComplexObjectWith[specialchars]' + Set-Context -Context $githubLoginContext -ID 'BigComplexObjectWith[specialchars]' + Set-Context -Context $githubLoginContext -ID 'BigComplexObjectWith[specialchars]' + Write-Verbose (Get-Context -ID 'BigComplexObjectWith[specialchars]') -Verbose + $object = Get-Context -ID 'BigComplexObjectWith[specialchars]' $object.ApiRateLimits.Remaining | Should -Be 4985 $object.AuthToken | Should -BeOfType [System.Security.SecureString] $object.AuthToken | ConvertFrom-SecureString -AsPlainText | Should -Be 'ghp_12345ABCDE67890FGHIJ'