From 1d71d50c5f155b90fbbba232865db1b67a11326d Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Tue, 18 Feb 2025 09:35:00 +0100 Subject: [PATCH 1/6] Fix config size limit by validating modules in cache --- .../Tests/powershellgroup.config.tests.ps1 | 17 +++++++++++++++++ .../psDscAdapter/powershell.resource.ps1 | 16 ++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/powershell-adapter/Tests/powershellgroup.config.tests.ps1 b/powershell-adapter/Tests/powershellgroup.config.tests.ps1 index f51f32c1..c4102ad9 100644 --- a/powershell-adapter/Tests/powershellgroup.config.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.config.tests.ps1 @@ -33,6 +33,23 @@ Describe 'PowerShell adapter resource tests' { $res.results[0].result.actualState.result[0].properties.EnumProp | Should -BeExactly 'Expected' } + It 'Get does not work on config when module does not exist' { + + $yaml = @' + $schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json + resources: + - name: Working with class-based resources + type: Microsoft.DSC/PowerShell + properties: + resources: + - name: Class-resource Info + type: TestClassResourceNotExist/TestClassResourceNotExist +'@ + $yaml | dsc -l trace config get -f - 2> $TestDrive/tracing.txt + $LASTEXITCODE | Should -Be 2 + "$TestDrive/tracing.txt" | Should -FileContentMatch 'ERROR: DSC resource TestClassResourceNotExist/TestClassResourceNotExist module not found.' + } + It 'Test works on config with class-based resources' { $r = Get-Content -Raw $pwshConfigPath | dsc config test -f - diff --git a/powershell-adapter/psDscAdapter/powershell.resource.ps1 b/powershell-adapter/psDscAdapter/powershell.resource.ps1 index acaafad5..b4d82fa5 100644 --- a/powershell-adapter/psDscAdapter/powershell.resource.ps1 +++ b/powershell-adapter/psDscAdapter/powershell.resource.ps1 @@ -148,11 +148,19 @@ switch ($Operation) { exit 1 } + # get unique module names from the desiredState input + $moduleInput = $desiredState | Select-Object -ExpandProperty Type | Sort-Object -Unique + + # refresh the cache with the modules that are available on the system $dscResourceCache = Invoke-DscCacheRefresh -module $dscResourceModules - if ($dscResourceCache.count -lt $dscResourceModules.count) { - $trace = @{'Debug' = 'ERROR: DSC resource module not found.' } | ConvertTo-Json -Compress - $host.ui.WriteErrorLine($trace) - exit 1 + + # check if all the desired modules are in the cache + $moduleInput | ForEach-Object { + if ($dscResourceCache.type -notcontains $_) { + $trace = @{'Debug' = ('ERROR: DSC resource {0} module not found.' -f $_)} | ConvertTo-Json -Compress + $host.ui.WriteErrorLine($trace) + exit 1 + } } foreach ($ds in $desiredState) { From aa8bf9e1ce7b188cf87d8c0a176bbb9ffbde02d9 Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Wed, 19 Feb 2025 01:48:09 +0100 Subject: [PATCH 2/6] Resolve remark --- powershell-adapter/psDscAdapter/powershell.resource.ps1 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/powershell-adapter/psDscAdapter/powershell.resource.ps1 b/powershell-adapter/psDscAdapter/powershell.resource.ps1 index b4d82fa5..6ed6d852 100644 --- a/powershell-adapter/psDscAdapter/powershell.resource.ps1 +++ b/powershell-adapter/psDscAdapter/powershell.resource.ps1 @@ -157,8 +157,7 @@ switch ($Operation) { # check if all the desired modules are in the cache $moduleInput | ForEach-Object { if ($dscResourceCache.type -notcontains $_) { - $trace = @{'Debug' = ('ERROR: DSC resource {0} module not found.' -f $_)} | ConvertTo-Json -Compress - $host.ui.WriteErrorLine($trace) + ('ERROR: DSC resource {0} module not found.' -f $_) | Write-DscTrace exit 1 } } From 735727e55503069807f00d6dc789b9c4d567be12 Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Tue, 18 Feb 2025 09:35:00 +0100 Subject: [PATCH 3/6] Fix config size limit by validating modules in cache --- .../Tests/powershellgroup.config.tests.ps1 | 17 +++++++++++++++++ .../psDscAdapter/powershell.resource.ps1 | 16 ++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/powershell-adapter/Tests/powershellgroup.config.tests.ps1 b/powershell-adapter/Tests/powershellgroup.config.tests.ps1 index f51f32c1..c4102ad9 100644 --- a/powershell-adapter/Tests/powershellgroup.config.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.config.tests.ps1 @@ -33,6 +33,23 @@ Describe 'PowerShell adapter resource tests' { $res.results[0].result.actualState.result[0].properties.EnumProp | Should -BeExactly 'Expected' } + It 'Get does not work on config when module does not exist' { + + $yaml = @' + $schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json + resources: + - name: Working with class-based resources + type: Microsoft.DSC/PowerShell + properties: + resources: + - name: Class-resource Info + type: TestClassResourceNotExist/TestClassResourceNotExist +'@ + $yaml | dsc -l trace config get -f - 2> $TestDrive/tracing.txt + $LASTEXITCODE | Should -Be 2 + "$TestDrive/tracing.txt" | Should -FileContentMatch 'ERROR: DSC resource TestClassResourceNotExist/TestClassResourceNotExist module not found.' + } + It 'Test works on config with class-based resources' { $r = Get-Content -Raw $pwshConfigPath | dsc config test -f - diff --git a/powershell-adapter/psDscAdapter/powershell.resource.ps1 b/powershell-adapter/psDscAdapter/powershell.resource.ps1 index acaafad5..b4d82fa5 100644 --- a/powershell-adapter/psDscAdapter/powershell.resource.ps1 +++ b/powershell-adapter/psDscAdapter/powershell.resource.ps1 @@ -148,11 +148,19 @@ switch ($Operation) { exit 1 } + # get unique module names from the desiredState input + $moduleInput = $desiredState | Select-Object -ExpandProperty Type | Sort-Object -Unique + + # refresh the cache with the modules that are available on the system $dscResourceCache = Invoke-DscCacheRefresh -module $dscResourceModules - if ($dscResourceCache.count -lt $dscResourceModules.count) { - $trace = @{'Debug' = 'ERROR: DSC resource module not found.' } | ConvertTo-Json -Compress - $host.ui.WriteErrorLine($trace) - exit 1 + + # check if all the desired modules are in the cache + $moduleInput | ForEach-Object { + if ($dscResourceCache.type -notcontains $_) { + $trace = @{'Debug' = ('ERROR: DSC resource {0} module not found.' -f $_)} | ConvertTo-Json -Compress + $host.ui.WriteErrorLine($trace) + exit 1 + } } foreach ($ds in $desiredState) { From 1ee353788f480394275dd7a565284d7440df4fbf Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Wed, 19 Feb 2025 01:48:09 +0100 Subject: [PATCH 4/6] Resolve remark --- powershell-adapter/psDscAdapter/powershell.resource.ps1 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/powershell-adapter/psDscAdapter/powershell.resource.ps1 b/powershell-adapter/psDscAdapter/powershell.resource.ps1 index b4d82fa5..6ed6d852 100644 --- a/powershell-adapter/psDscAdapter/powershell.resource.ps1 +++ b/powershell-adapter/psDscAdapter/powershell.resource.ps1 @@ -157,8 +157,7 @@ switch ($Operation) { # check if all the desired modules are in the cache $moduleInput | ForEach-Object { if ($dscResourceCache.type -notcontains $_) { - $trace = @{'Debug' = ('ERROR: DSC resource {0} module not found.' -f $_)} | ConvertTo-Json -Compress - $host.ui.WriteErrorLine($trace) + ('ERROR: DSC resource {0} module not found.' -f $_) | Write-DscTrace exit 1 } } From 54460bf4e2b65ce534941d1421b2be492fab9f0c Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Fri, 21 Feb 2025 05:40:01 +0100 Subject: [PATCH 5/6] Use error when writing trace --- powershell-adapter/psDscAdapter/powershell.resource.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powershell-adapter/psDscAdapter/powershell.resource.ps1 b/powershell-adapter/psDscAdapter/powershell.resource.ps1 index 6ed6d852..d0cd3ce7 100644 --- a/powershell-adapter/psDscAdapter/powershell.resource.ps1 +++ b/powershell-adapter/psDscAdapter/powershell.resource.ps1 @@ -157,7 +157,7 @@ switch ($Operation) { # check if all the desired modules are in the cache $moduleInput | ForEach-Object { if ($dscResourceCache.type -notcontains $_) { - ('ERROR: DSC resource {0} module not found.' -f $_) | Write-DscTrace + ('DSC resource {0} module not found.' -f $_) | Write-DscTrace -Operation Error exit 1 } } From 89139f3be981cfe1229086de4380edeb44cd8f01 Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Fri, 21 Feb 2025 06:47:27 +0100 Subject: [PATCH 6/6] Fix test --- powershell-adapter/Tests/powershellgroup.config.tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/powershell-adapter/Tests/powershellgroup.config.tests.ps1 b/powershell-adapter/Tests/powershellgroup.config.tests.ps1 index c4102ad9..cbeb33c3 100644 --- a/powershell-adapter/Tests/powershellgroup.config.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.config.tests.ps1 @@ -45,9 +45,9 @@ Describe 'PowerShell adapter resource tests' { - name: Class-resource Info type: TestClassResourceNotExist/TestClassResourceNotExist '@ - $yaml | dsc -l trace config get -f - 2> $TestDrive/tracing.txt + $yaml | dsc -l trace config get -f - 2> "$TestDrive/tracing.txt" $LASTEXITCODE | Should -Be 2 - "$TestDrive/tracing.txt" | Should -FileContentMatch 'ERROR: DSC resource TestClassResourceNotExist/TestClassResourceNotExist module not found.' + "$TestDrive/tracing.txt" | Should -FileContentMatch 'DSC resource TestClassResourceNotExist/TestClassResourceNotExist module not found.' } It 'Test works on config with class-based resources' {