Skip to content

Commit 6ca0a7e

Browse files
committed
Add support for [SecureString] in PowerShell adapter
1 parent 3ae8105 commit 6ca0a7e

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

adapters/powershell/Tests/TestClassResource/0.0.1/TestClassResource.psm1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class TestClassResource : BaseTestClass
4040
[DscProperty()]
4141
[Ensure] $Ensure
4242

43+
[DscProperty()]
44+
[SecureString] $SecureStringProp
45+
4346
[string] $NonDscProperty # This property shouldn't be in results data
4447

4548
hidden

adapters/powershell/Tests/powershellgroup.resource.tests.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,11 @@ Describe 'PowerShell adapter resource tests' {
376376
$LASTEXITCODE | Should -Be 7
377377
Get-Content -Path $TestDrive/error.log | Should -Match 'Resource not found: TestClassResource/TestClassResource 0.0.2'
378378
}
379+
380+
It 'Can process SecureString property' {
381+
$r = '{"Name":"TestClassResource1","SecureStringProp":"MySecretValue"}' | dsc resource get -r 'TestClassResource/TestClassResource' -f -
382+
$LASTEXITCODE | Should -Be 0
383+
$res = $r | ConvertFrom-Json
384+
$res.actualState.SecureStringProp | Should -Not -BeNullOrEmpty
385+
}
379386
}

adapters/powershell/psDscAdapter/psDscAdapter.psm1

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,21 +424,30 @@ function Invoke-DscOperation {
424424
# set each property of $dscResourceInstance to the value of the property in the $desiredState INPUT object
425425
$DesiredState.properties.psobject.properties | ForEach-Object -Process {
426426
# handle input objects by converting them to a hash table
427+
$validateProperty = $cachedDscResourceInfo.Properties | Where-Object -Property Name -EQ $_.Name
427428
if ($_.Value -is [System.Management.Automation.PSCustomObject]) {
428429
$validateProperty = $cachedDscResourceInfo.Properties | Where-Object -Property Name -EQ $_.Name
429-
if ($validateProperty -and $validateProperty.PropertyType -eq 'PSCredential') {
430+
if ($validateProperty -and $validateProperty.PropertyType -like "*PSCredential") {
430431
if (-not $_.Value.Username -or -not $_.Value.Password) {
431432
"Credential object '$($_.Name)' requires both 'username' and 'password' properties" | Write-DscTrace -Operation Error
432433
exit 1
433434
}
434435
$dscResourceInstance.$($_.Name) = [System.Management.Automation.PSCredential]::new($_.Value.Username, (ConvertTo-SecureString -AsPlainText $_.Value.Password -Force))
435436
}
437+
elseif ($validateProperty -and $validateProperty.PropertyType -like '*SecureString') {
438+
439+
$dscResourceInstance.$($_.Name) = ConvertTo-SecureString -AsPlainText $_.Value -Force
440+
}
436441
else {
437442
$dscResourceInstance.$($_.Name) = $_.Value.psobject.properties | ForEach-Object -Begin { $propertyHash = @{} } -Process { $propertyHash[$_.Name] = $_.Value } -End { $propertyHash }
438443
}
439444
}
440445
else {
441-
$dscResourceInstance.$($_.Name) = $_.Value
446+
if ($validateProperty -and $validateProperty.PropertyType -like '*SecureString' -and -not [string]::IsNullOrEmpty($_.Value)) {
447+
$dscResourceInstance.$($_.Name) = ConvertTo-SecureString -AsPlainText $_.Value -Force
448+
} else {
449+
$dscResourceInstance.$($_.Name) = $_.Value
450+
}
442451
}
443452
}
444453
}

0 commit comments

Comments
 (0)