Skip to content

Commit b725f2c

Browse files
committed
Implement Microsoft.PowerShell.SecretManagement extension
1 parent 728b529 commit b725f2c

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
@@ -60,6 +60,8 @@ $filesForWindowsPackage = @(
6060
'assertion.dsc.resource.json',
6161
'group.dsc.resource.json',
6262
'include.dsc.resource.json',
63+
'microsoft.powershell.dsc.extension.json',
64+
'microsoft.powershell.secret.ps1',
6365
'NOTICE.txt',
6466
'osinfo.exe',
6567
'osinfo.dsc.resource.json',
@@ -98,6 +100,8 @@ $filesForLinuxPackage = @(
98100
'apt.dsc.resource.sh',
99101
'group.dsc.resource.json',
100102
'include.dsc.resource.json',
103+
'microsoft.powershell.dsc.extension.json',
104+
'microsoft.powershell.secret.ps1',
101105
'NOTICE.txt',
102106
'osinfo',
103107
'osinfo.dsc.resource.json',
@@ -123,6 +127,8 @@ $filesForMacPackage = @(
123127
'brew.dsc.resource.sh',
124128
'group.dsc.resource.json',
125129
'include.dsc.resource.json',
130+
'microsoft.powershell.dsc.extension.json',
131+
'microsoft.powershell.secret.ps1',
126132
'NOTICE.txt',
127133
'osinfo',
128134
'osinfo.dsc.resource.json',
@@ -331,9 +337,9 @@ if (!$SkipBuild) {
331337
New-Item -ItemType Directory $target -ErrorAction Ignore > $null
332338

333339
# make sure dependencies are built first so clippy runs correctly
334-
$windows_projects = @("pal", "registry_lib", "registry", "reboot_pending", "wmi-adapter", "configurations/windows", 'extensions/appx')
335-
$macOS_projects = @("resources/brew")
336-
$linux_projects = @("resources/apt")
340+
$windows_projects = @("pal", "registry_lib", "registry", "reboot_pending", "wmi-adapter", "configurations/windows", "extensions/appx", "extensions/powershell/secret")
341+
$macOS_projects = @("resources/brew", "extensions/powershell/secret")
342+
$linux_projects = @("resources/apt", "extensions/powershell/secret")
337343

338344
# projects are in dependency order
339345
$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)