Skip to content
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

🩹 [Patch]: Add ContextInfo class and loosen binding to Context class #65

Merged
merged 3 commits into from
Dec 8, 2024
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
19 changes: 19 additions & 0 deletions src/classes/public/ContextInfo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class ContextInfo {
[string] $ID
[hashtable] $Metadata
[string] $SecretName
[string] $SecretType
[string] $VaultName

ContextInfo([hashtable]$Properties) {
foreach ($Property in $Properties.Keys) {
$this.$Property = $Properties.$Property
}
}

ContextInfo([PSCustomObject]$Object) {
$Object.PSObject.Properties | ForEach-Object {
$this.($_.Name) = $_.Value
}
}
}
4 changes: 2 additions & 2 deletions src/functions/public/Get-Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ filter Get-Context {

Get the context called 'MySecret' from the vault.
#>
[OutputType([Context])]
[OutputType([object])]
[CmdletBinding()]
param(
# The name of the context to retrieve from the vault.
Expand Down Expand Up @@ -55,7 +55,7 @@ filter Get-Context {
Write-Debug "$indent`Found [$($contextInfos.Count)] contexts in [$vaultName]"
$contextInfos | ForEach-Object {
$contextJson = Get-Secret -Name $_.SecretName -Vault $vaultName -AsPlainText -Verbose:$false
[Context](ConvertFrom-ContextJson -JsonString $contextJson)
ConvertFrom-ContextJson -JsonString $contextJson
}
} catch {
Write-Error $_
Expand Down
10 changes: 5 additions & 5 deletions src/functions/public/Get-ContextInfo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

Get all context info from the context vault.
#>
[OutputType([PSCustomObject])]
[OutputType([ContextInfo])]
[CmdletBinding()]
param()

Expand All @@ -27,11 +27,11 @@

Get-SecretInfo -Vault $vaultName -Verbose:$false -Name "$secretPrefix*" | ForEach-Object {
$ID = ($_.Name -replace "^$secretPrefix")
[pscustomobject]@{
SecretName = $_.Name
[ContextInfo]@{
ID = $ID
Metadata = $_.Metadata
Type = $_.Type
Metadata = $_.Metadata + @{}
SecretName = $_.Name
SecretType = $_.Type
VaultName = $_.VaultName
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/functions/public/Set-Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function Set-Context {

Creates a context called 'MySecret' in the vault with the settings.
#>
[OutputType([Context])]
[OutputType([object])]
[CmdletBinding(SupportsShouldProcess)]
param(
# The ID of the context.
Expand Down Expand Up @@ -68,7 +68,7 @@ function Set-Context {
}

if ($PassThru) {
[Context](ConvertFrom-ContextJson -JsonString $secret)
ConvertFrom-ContextJson -JsonString $secret
}
}

Expand Down
15 changes: 11 additions & 4 deletions tests/Context.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ BeforeAll {
Get-SecretInfo | Remove-Secret
}

Describe 'Context' {
Describe 'Functions' {
Context 'Function: Set-Context' {
It "Set-Context -ID 'TestID1'" {
{ Set-Context -ID 'TestID1' } | Should -Not -Throw
Write-Verbose (Get-ContextInfo | Out-String) -Verbose
$contextInfo = Get-ContextInfo
Write-Verbose ($contextInfo | Out-String) -Verbose
$contextInfo.Count | Should -Be 1
$contextInfo[0].ID | Should -Be 'TestID1'
$contextInfo[0].SecretName | Should -Be 'Context:TestID1'


$result = Get-Context -ID 'TestID1'
Write-Verbose ($result | Out-String) -Verbose
Expand All @@ -22,14 +27,16 @@ Describe 'Context' {
}
It "Set-Context -ID 'TestID2' -Context @{}" {
{ Set-Context -ID 'TestID2' -Context @{} } | Should -Not -Throw
Get-ContextInfo
$contextInfo = Get-ContextInfo
Write-Verbose ($contextInfo | Out-String) -Verbose
$contextInfo.Count | Should -Be 2

$result = Get-Context -ID 'TestID2'
$result | Should -Not -BeNullOrEmpty
$result.ID | Should -Be 'TestID2'
}
It "Set-Context -ID 'TestID2' -Context @{} - Again" {
{ Set-Context -ID 'TestID2' -Context @{} } | Should -Not -Throw
Get-ContextInfo
$result = Get-Context -ID 'TestID2'
$result | Should -Not -BeNullOrEmpty
$result.ID | Should -Be 'TestID2'
Expand Down
Loading