Skip to content

Commit 985b5ca

Browse files
committed
Rewrite to validate property to PSCredential
1 parent eb06406 commit 985b5ca

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
lines changed

powershell-adapter/Tests/win_powershellgroup.tests.ps1

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -211,26 +211,4 @@ resources:
211211
$LASTEXITCODE | Should -Be 0
212212
$out.results[0].result.inDesiredState | Should -Be $inDesiredState
213213
}
214-
215-
It 'Should be able to test against a service with credentials' -Skip:(!$IsWindows) {
216-
$yaml = @"
217-
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
218-
resources:
219-
- name: Test service
220-
type: Microsoft.Windows/WindowsPowerShell
221-
properties:
222-
resources:
223-
- name: Test service
224-
type: PSDesiredStateConfiguration/Service
225-
properties:
226-
Name: 'W32Time'
227-
Credential:
228-
Username: "User"
229-
Password: "Password"
230-
"@
231-
dsc -l trace config test -i $yaml
232-
$out = dsc config test -i $yaml | ConvertFrom-Json
233-
$LASTEXITCODE | Should -Be 0
234-
$out.results[0].result.inDesiredState | Should -Be $false
235-
}
236214
}

powershell-adapter/psDscAdapter/psDscAdapter.psm1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,19 @@ function Invoke-DscOperation {
416416

417417
$ValidProperties = $cachedDscResourceInfo.Properties.Name
418418

419+
$ValidProperties | ConvertTo-Json | Write-DscTrace -Operation Trace
420+
419421
if ($DesiredState.properties) {
420422
# set each property of $dscResourceInstance to the value of the property in the $desiredState INPUT object
421423
$DesiredState.properties.psobject.properties | ForEach-Object -Process {
422424
# handle input objects by converting them to a hash table
423425
if ($_.Value -is [System.Management.Automation.PSCustomObject]) {
424-
if ($_.Name -like '*Credential*' -and $_.Value.Username -and $_.Value.Password) {
426+
$validateProperty = $cachedDscResourceInfo.Properties | Where-Object -Property Name -EQ $_.Value
427+
if ($validateProperty.PropertyType -eq 'PSCredential') {
428+
if (-not $_.Value.Username -and -not $_.Value.Password) {
429+
"Credential property '$($_.Name)' requires both username and password input object" | Write-DscTrace -Operation Error
430+
exit 1
431+
}
425432
$dscResourceInstance.$($_.Name) = [System.Management.Automation.PSCredential]::new($_.Value.Username, (ConvertTo-SecureString -AsPlainText $_.Value.Password -Force))
426433
}
427434
else {

powershell-adapter/psDscAdapter/win_psDscAdapter.psm1

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,16 @@ function Invoke-DscOperation {
364364
# morph the INPUT object into a hashtable named "property" for the cmdlet Invoke-DscResource
365365
$DesiredState.properties.psobject.properties | ForEach-Object -Begin { $property = @{} } -Process {
366366
if ($_.Value -is [System.Management.Automation.PSCustomObject]) {
367-
if ($_.Name -like '*Credential*' -and $_.Value.Username -and $_.Value.Password) {
368-
$property[$_.Name] = [System.Management.Automation.PSCredential]::new($_.Value.Username, (ConvertTo-SecureString -AsPlainText $_.Value.Password -Force))
367+
$validateProperty = $cachedDscResourceInfo.Properties | Where-Object -Property Name -EQ $_.Value
368+
if ($validateProperty.PropertyType -eq 'PSCredential') {
369+
if (-not $_.Value.Username -and -not $_.Value.Password) {
370+
"Credential property '$($_.Name)' requires both username and password input object" | Write-DscTrace -Operation Error
371+
exit 1
372+
}
373+
$dscResourceInstance.$($_.Name) = [System.Management.Automation.PSCredential]::new($_.Value.Username, (ConvertTo-SecureString -AsPlainText $_.Value.Password -Force))
369374
}
370375
else {
371-
$property[$_.Name] = $_.Value.psobject.properties | ForEach-Object -Begin { $propertyHash = @{} } -Process { $propertyHash[$_.Name] = $_.Value } -End { $propertyHash }
376+
$dscResourceInstance.$($_.Name) = $_.Value.psobject.properties | ForEach-Object -Begin { $propertyHash = @{} } -Process { $propertyHash[$_.Name] = $_.Value } -End { $propertyHash }
372377
}
373378
}
374379
else {
@@ -409,7 +414,12 @@ function Invoke-DscOperation {
409414
$DesiredState.properties.psobject.properties | ForEach-Object -Process {
410415
# handle input objects by converting them to a hash table
411416
if ($_.Value -is [System.Management.Automation.PSCustomObject]) {
412-
if ($_.Name -like '*Credential*' -and $_.Value.Username -and $_.Value.Password) {
417+
$validateProperty = $cachedDscResourceInfo.Properties | Where-Object -Property Name -EQ $_.Value
418+
if ($validateProperty.PropertyType -eq 'PSCredential') {
419+
if (-not $_.Value.Username -and -not $_.Value.Password) {
420+
"Credential property '$($_.Name)' requires both username and password input object" | Write-DscTrace -Operation Error
421+
exit 1
422+
}
413423
$dscResourceInstance.$($_.Name) = [System.Management.Automation.PSCredential]::new($_.Value.Username, (ConvertTo-SecureString -AsPlainText $_.Value.Password -Force))
414424
}
415425
else {
@@ -462,11 +472,16 @@ function Invoke-DscOperation {
462472
# morph the INPUT object into a hashtable named "property" for the cmdlet Invoke-DscResource
463473
$DesiredState.properties.psobject.properties | ForEach-Object -Begin { $property = @{} } -Process {
464474
if ($_.Value -is [System.Management.Automation.PSCustomObject]) {
465-
if ($_.Name -Like '*Credential*' -and $_.Value.Username -and $_.Value.Password) {
466-
$property[$_.Name] = [System.Management.Automation.PSCredential]::new($_.Value.Username, (ConvertTo-SecureString -AsPlainText $_.Value.Password -Force))
475+
$validateProperty = $cachedDscResourceInfo.Properties | Where-Object -Property Name -EQ $_.Value
476+
if ($validateProperty.PropertyType -eq 'PSCredential') {
477+
if (-not $_.Value.Username -and -not $_.Value.Password) {
478+
"Credential property '$($_.Name)' requires both username and password input object" | Write-DscTrace -Operation Error
479+
exit 1
480+
}
481+
$dscResourceInstance.$($_.Name) = [System.Management.Automation.PSCredential]::new($_.Value.Username, (ConvertTo-SecureString -AsPlainText $_.Value.Password -Force))
467482
}
468483
else {
469-
$property[$_.Name] = $_.Value.psobject.properties | ForEach-Object -Begin { $propertyHash = @{} } -Process { $propertyHash[$_.Name] = $_.Value } -End { $propertyHash }
484+
$dscResourceInstance.$($_.Name) = $_.Value.psobject.properties | ForEach-Object -Begin { $propertyHash = @{} } -Process { $propertyHash[$_.Name] = $_.Value } -End { $propertyHash }
470485
}
471486
}
472487
else {

s.ps1

Whitespace-only changes.

0 commit comments

Comments
 (0)