-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathTestLocalScript.Extension.psm1
150 lines (120 loc) · 4.77 KB
/
TestLocalScript.Extension.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
function Get-Path
{
param (
[string] $VaultName
)
$path = Join-Path $env:TEMP $VaultName
if (! (Test-Path -Path $path))
{
[System.IO.Directory]::CreateDirectory($path)
}
return $path
}
function Get-Secret
{
param (
[string] $Name,
[string] $VaultName,
[hashtable] $AdditionalParameters
)
if ([WildcardPattern]::ContainsWildcardCharacters($Name))
{
throw "The Name parameter cannot contain wild card characters."
}
$filePath = Join-Path -Path (Get-Path $VaultName) -ChildPath "${Name}.xml"
if (! (Test-Path -Path $filePath))
{
return
}
$secret = Import-Clixml -Path $filePath
if ($secret.GetType().IsArray)
{
return @(,[byte[]] $secret)
}
$verboseEnabled = $AdditionalParameters.ContainsKey('Verbose') -and ($AdditionalParameters['Verbose'] -eq $true)
Write-Verbose "[TestLocalScript.Extension]:Get-SecretVault successfully called for vault: $VaultName" -Verbose:$verboseEnabled
return $secret
}
function Set-Secret
{
param (
[string] $Name,
[object] $Secret,
[string] $VaultName,
[hashtable] $AdditionalParameters
)
$filePath = Join-Path -Path (Get-Path $VaultName) -ChildPath "${Name}.xml"
$Secret | Export-Clixml -Path $filePath -Force
$verboseEnabled = $AdditionalParameters.ContainsKey('Verbose') -and ($AdditionalParameters['Verbose'] -eq $true)
Write-Verbose "[TestLocalScript.Extension]:Set-SecretVault successfully called for vault: $VaultName" -Verbose:$verboseEnabled
return $true
}
function Remove-Secret
{
param (
[string] $Name,
[string] $VaultName,
[hashtable] $AdditionalParameters
)
$filePath = Join-Path -Path (Get-Path $VaultName) -ChildPath "${Name}.xml"
if (! (Test-Path -Path $filePath))
{
Write-Error "The secret, $Name, does not exist."
return $false
}
Remove-Item -Path $filePath
$verboseEnabled = $AdditionalParameters.ContainsKey('Verbose') -and ($AdditionalParameters['Verbose'] -eq $true)
Write-Verbose "[TestLocalScript.Extension]:Remove-SecretVault successfully called for vault: $VaultName" -Verbose:$verboseEnabled
return $true
}
function Get-SecretInfo
{
param(
[string] $Filter,
[string] $VaultName,
[hashtable] $AdditionalParameters
)
if ([string]::IsNullOrEmpty($Filter)) { $Filter = "*" }
$files = Get-ChildItem -Path (Join-Path -Path (Get-Path $VaultName) -ChildPath "${Filter}.xml") 2>$null
foreach ($file in $files)
{
$secretName = [System.IO.Path]::GetFileNameWithoutExtension((Split-Path -Path $file -Leaf))
$secret = Import-Clixml -Path $file.FullName
$type = if ($secret.gettype().IsArray) { [Microsoft.PowerShell.SecretManagement.SecretType]::ByteArray }
elseif ($secret -is [string]) { [Microsoft.PowerShell.SecretManagement.SecretType]::String }
elseif ($secret -is [securestring]) { [Microsoft.PowerShell.SecretManagement.SecretType]::SecureString }
elseif ($secret -is [PSCredential]) { [Microsoft.PowerShell.SecretManagement.SecretType]::PSCredential }
elseif ($secret -is [hashtable]) { [Microsoft.PowerShell.SecretManagement.SecretType]::Hashtable }
else { [Microsoft.PowerShell.SecretManagement.SecretType]::Unknown }
Write-Output (
[Microsoft.PowerShell.SecretManagement.SecretInformation]::new(
$secretName,
$type,
$VaultName)
)
}
$verboseEnabled = $AdditionalParameters.ContainsKey('Verbose') -and ($AdditionalParameters['Verbose'] -eq $true)
Write-Verbose "[TestLocalScript.Extension]:Get-SecretInfo successfully called for vault: $VaultName" -Verbose:$verboseEnabled
Write-Warning "[TestLocalScript.Extension]::Get-SecretInfo bogus warning for vault: $VaultName"
}
function Test-SecretVault
{
param (
[string] $VaultName,
[hashtable] $AdditionalParameters
)
$verboseEnabled = $AdditionalParameters.ContainsKey('Verbose') -and ($AdditionalParameters['Verbose'] -eq $true)
Write-Verbose "[TestLocalScript.Extension]:Test-SecretVault successfully called for vault: $VaultName" -Verbose:$verboseEnabled
return $true
}
function Unregister-SecretVault
{
param (
[string] $VaultName,
[hashtable] $AdditionalParameters
)
$verboseEnabled = $AdditionalParameters.ContainsKey('Verbose') -and ($AdditionalParameters['Verbose'] -eq $true)
Write-Verbose "[TestLocalScript.Extension]:Unregister-SecretVault successfully called for vault: $VaultName" -Verbose:$verboseEnabled
}