Skip to content

Commit

Permalink
🩹 [Patch]: Ensure that the vault is always present before using it (#69)
Browse files Browse the repository at this point in the history
## Description

- Ensure that the vault is always present before using it.

## Type of change

<!-- Use the check-boxes [x] on the options that are relevant. -->

- [ ] 📖 [Docs]
- [ ] 🪲 [Fix]
- [x] 🩹 [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
  • Loading branch information
MariusStorhaug authored Dec 9, 2024
1 parent 6e74d6f commit df94f46
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 77 deletions.
55 changes: 0 additions & 55 deletions src/functions/private/Get-ContextVault.ps1

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#Requires -Modules @{ ModuleName = 'Microsoft.PowerShell.SecretManagement'; RequiredVersion = '1.1.2' }
#Requires -Modules @{ ModuleName = 'Microsoft.PowerShell.SecretStore'; RequiredVersion = '1.0.6' }

function Initialize-ContextVault {
function Set-ContextVault {
<#
.SYNOPSIS
Initialize a context vault.
Sets the context vault.
.DESCRIPTION
Initialize a context vault. If the vault does not exist, it will be created and registered.
Sets the context vault. If the vault does not exist, it will be created and registered.
The SecretStore is created with the following parameters:
- Authentication: None
Expand All @@ -16,20 +16,34 @@ function Initialize-ContextVault {
- Scope: CurrentUser
.EXAMPLE
Initialize-ContextVault
Set-ContextVault
Initializes a context vault named 'ContextVault' using the 'Microsoft.PowerShell.SecretStore' module.
Sets a context vault named 'ContextVault' using the 'Microsoft.PowerShell.SecretStore' module.
.EXAMPLE
Set-ContextVault -Name 'MyVault' -Type 'MyModule'
Sets a context vault named 'MyVault' using the 'MyModule' module.
.EXAMPLE
Set-ContextVault -PassThru
Sets a context vault using the default values and returns the secret vault object.
#>
[OutputType([Microsoft.PowerShell.SecretManagement.SecretVaultInfo])]
[CmdletBinding()]
[CmdletBinding(SupportsShouldProcess)]
param (
# The name of the secret vault.
# The name of the context vault.
[Parameter()]
[string] $Name = $script:Config.VaultName,

# The type of the secret vault.
# The type of the context vault.
[Parameter()]
[string] $Type = $script:Config.VaultType
[string] $Type = $script:Config.VaultType,

# Pass the vault through the pipeline.
[Parameter()]
[switch] $PassThru
)

begin {
Expand All @@ -53,7 +67,9 @@ function Initialize-ContextVault {
Force = $true
Verbose = $false
}
Reset-SecretStore @vaultParameters
if ($PSCmdlet.ShouldProcess('SecretStore', 'Reset')) {
Reset-SecretStore @vaultParameters
}
Write-Debug "[$Type] - Done"
Write-Debug "[$Name] - Registering vault"
$secretVault = @{
Expand All @@ -63,18 +79,20 @@ function Initialize-ContextVault {
Description = 'SecretStore'
Verbose = $false
}
Register-SecretVault @secretVault
if ($PSCmdlet.ShouldProcess('SecretVault', 'Register')) {
$vault = Register-SecretVault @secretVault -PassThru
}
Write-Debug "[$Name] - Done"
}
$script:Config.VaultName = $vault.Name

Get-SecretVault -Verbose:$false | Where-Object { $_.ModuleName -eq $Type }
Write-Debug "[$Name] - Vault registered"
$script:Config.Initialized = $true
Write-Debug "Connected to context vault [$($script:Config.VaultName)]"
} catch {
Write-Error $_
throw 'Failed to initialize context vault'
}
if ($PassThru) {
$vault
}
}

end {
Expand Down
2 changes: 1 addition & 1 deletion src/functions/public/Get-Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ filter Get-Context {
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Debug "[$commandName] - Start"
$null = Get-ContextVault
Set-ContextVault
$vaultName = $script:Config.VaultName
$contextInfos = Get-ContextInfo
}
Expand Down
1 change: 1 addition & 0 deletions src/functions/public/Get-ContextInfo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Debug "[$commandName] - Start"
Set-ContextVault
$vaultName = $script:Config.VaultName
$secretPrefix = $script:Config.SecretPrefix
}
Expand Down
2 changes: 1 addition & 1 deletion src/functions/public/Remove-Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ filter Remove-Context {
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Debug "[$commandName] - Start"
$null = Get-ContextVault
Set-ContextVault
}

process {
Expand Down
6 changes: 4 additions & 2 deletions src/functions/public/Rename-Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Debug "[$commandName] - Start"
Set-ContextVault
}

process {
$context = Get-Context -ID $ID
if (-not $context) {
throw "Context with ID '$ID' not found."
Expand All @@ -39,9 +43,7 @@
if ($existingContext -and -not $Force) {
throw "Context with ID '$NewID' already exists."
}
}

process {
if ($PSCmdlet.ShouldProcess("Renaming context '$ID' to '$NewID'")) {
try {
Set-Context -ID $NewID -Context $context
Expand Down
2 changes: 1 addition & 1 deletion src/functions/public/Set-Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function Set-Context {
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Debug "[$commandName] - Start"
$null = Get-ContextVault
Set-ContextVault
$vaultName = $script:Config.VaultName
$secretPrefix = $script:Config.SecretPrefix
}
Expand Down
1 change: 0 additions & 1 deletion src/variables/private/Config.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
$script:Config = [pscustomobject]@{
Initialized = $false # $script:Config.Initialized
SecretPrefix = 'Context:' # $script:Config.SecretPrefix
VaultName = 'ContextVault' # $script:Config.VaultName
VaultType = 'Microsoft.PowerShell.SecretStore' # $script:Config.VaultType
Expand Down
9 changes: 8 additions & 1 deletion tests/Context.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
param()

BeforeAll {
Get-SecretInfo | Remove-Secret
$secrets = Get-SecretInfo -Verbose
Write-Verbose "Secrets: $($secrets.Count)" -Verbose
Write-Verbose ($secrets | Format-Table | Out-String) -Verbose
$secrets | Remove-Secret -Verbose
$vault = Get-SecretVault -Verbose
Write-Verbose "Vault: $($vault.Count)" -Verbose
Write-Verbose ($vault | Format-Table | Out-String) -Verbose
$vault | Unregister-SecretVault -Verbose
}

Describe 'Functions' {
Expand Down

0 comments on commit df94f46

Please sign in to comment.