Skip to content

Commit fe1951b

Browse files
committed
Implement Microsoft.PowerShell.SecretManagement extension
1 parent 86c3ef6 commit fe1951b

File tree

5 files changed

+99
-3
lines changed

5 files changed

+99
-3
lines changed

build.ps1

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ $filesForWindowsPackage = @(
5757
'assertion.dsc.resource.json',
5858
'group.dsc.resource.json',
5959
'include.dsc.resource.json',
60+
'microsoft.powershell.dsc.extension.json',
61+
'microsoft.powershell.secret.ps1',
6062
'NOTICE.txt',
6163
'osinfo.exe',
6264
'osinfo.dsc.resource.json',
@@ -95,6 +97,8 @@ $filesForLinuxPackage = @(
9597
'apt.dsc.resource.sh',
9698
'group.dsc.resource.json',
9799
'include.dsc.resource.json',
100+
'microsoft.powershell.dsc.extension.json',
101+
'microsoft.powershell.secret.ps1',
98102
'NOTICE.txt',
99103
'osinfo',
100104
'osinfo.dsc.resource.json',
@@ -120,6 +124,8 @@ $filesForMacPackage = @(
120124
'brew.dsc.resource.sh',
121125
'group.dsc.resource.json',
122126
'include.dsc.resource.json',
127+
'microsoft.powershell.dsc.extension.json',
128+
'microsoft.powershell.secret.ps1',
123129
'NOTICE.txt',
124130
'osinfo',
125131
'osinfo.dsc.resource.json',
@@ -324,9 +330,9 @@ if (!$SkipBuild) {
324330
}
325331

326332
# make sure dependencies are built first so clippy runs correctly
327-
$windows_projects = @("pal", "registry_lib", "registry", "reboot_pending", "wmi-adapter", "configurations/windows", 'extensions/appx')
328-
$macOS_projects = @("resources/brew")
329-
$linux_projects = @("resources/apt")
333+
$windows_projects = @("pal", "registry_lib", "registry", "reboot_pending", "wmi-adapter", "configurations/windows", "extensions/appx", "extensions/powershell/secret")
334+
$macOS_projects = @("resources/brew", "extensions/powershell/secret")
335+
$linux_projects = @("resources/apt", "extensions/powershell/secret")
330336

331337
# projects are in dependency order
332338
$projects = @(
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
microsoft.powershell.secret.ps1
2+
microsoft.powershell.dsc.extension.json
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/extension/manifest.json",
3+
"type": "Microsoft.PowerShell/SecretManagement",
4+
"version": "0.1.0",
5+
"description": "Retrieve secrets using the Microsoft.PowerShell.SecretManagement module",
6+
"secret": {
7+
"executable": "pwsh",
8+
"args": [
9+
"-NoLogo",
10+
"-NonInteractive",
11+
"-NoProfile",
12+
"-Command",
13+
"./microsoft.powershell.secret.ps1",
14+
{
15+
"nameArg": "-Name"
16+
},
17+
{
18+
"vaultArg": "-Vault"
19+
}
20+
]
21+
}
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
[CmdletBinding()]
5+
param(
6+
[Parameter(Mandatory = $true)]
7+
[string]$Name,
8+
[Parameter()]
9+
[string]$Vault
10+
)
11+
12+
if (Get-Command Get-Secret -ErrorAction Ignore) {
13+
$secretParams = @{
14+
Name = $Name
15+
AsPlainText = $true
16+
}
17+
18+
if (-not ([string]::IsNullOrEmpty($Vault))) {
19+
$secretParams['Vault'] = $Vault
20+
}
21+
22+
$secret = Get-Secret @secretParams -ErrorAction Ignore
23+
24+
Write-Output $secret
25+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
BeforeDiscovery {
5+
$runningInCI = $false
6+
}
7+
8+
BeforeAll {
9+
$FullyQualifiedName = @()
10+
$FullyQualifiedName += @{ModuleName="Microsoft.PowerShell.SecretManagement";ModuleVersion="1.1.2"}
11+
$FullyQualifiedName += @{ModuleName="Microsoft.PowerShell.SecretStore";ModuleVersion="1.0.6"}
12+
foreach ($module in $FullyQualifiedName) {
13+
if (-not (Get-Module -ListAvailable -FullyQualifiedName $module)) {
14+
Save-PSResource -Name $module.ModuleName -Version $module.ModuleVersion -Path $TestDrive -Repository PSGallery -TrustRepository
15+
}
16+
}
17+
18+
$env:PSModulePath += [System.IO.Path]::PathSeparator + $TestDrive
19+
}
20+
21+
Describe 'Tests for PowerShell Secret Management' -Skip:($runningInCI) {
22+
It 'Should get secret from default store' {
23+
# Instead of doing it in the BeforeAll block, reset the store here as we know we are running in the CI
24+
Reset-SecretStore -Password (ConvertTo-SecureString -AsPlainText -String 'P@ssw0rd' -Force) -Force
25+
Register-SecretVault -Name 'VaultA' -ModuleName 'Microsoft.PowerShell.SecretStore' -DefaultVault
26+
Set-Secret -Name TestSecret -Secret "Super@SecretPassword"
27+
28+
$configYaml = @'
29+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
30+
resources:
31+
- name: Echo
32+
type: Microsoft.DSC.Debug/Echo
33+
properties:
34+
output: "[secret('TestSecret')]"
35+
'@
36+
$out = dsc -l trace config get -i $configYaml 2> $TestDrive/error.log | ConvertFrom-Json
37+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Raw -Path $TestDrive/error.log)
38+
$out.results.Count | Should -Be 1
39+
$out.results[0].result.actualState.Output | Should -BeExactly 'Super@SecretPassword'
40+
}
41+
}

0 commit comments

Comments
 (0)