Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix config size limit by validating modules in cache #647

Merged
merged 8 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions powershell-adapter/Tests/powershellgroup.config.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 '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 -
Expand Down
15 changes: 11 additions & 4 deletions powershell-adapter/psDscAdapter/powershell.resource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,18 @@ 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 $_) {
('DSC resource {0} module not found.' -f $_) | Write-DscTrace -Operation Error
exit 1
}
}

foreach ($ds in $desiredState) {
Expand Down