diff --git a/src/Configs.psm1 b/src/Configs.psm1 index f8a70a8..a851fac 100644 --- a/src/Configs.psm1 +++ b/src/Configs.psm1 @@ -148,11 +148,6 @@ function Export-ConfigCapture { $operations.Add([pscustomobject]@{ kind = 'config'; name = $config.name; status = 'Missing'; message = "Computer source is absent: $($location.target)" }) continue } - if ($config.name -eq 'nextcloud-settings') { - $operations.Add([pscustomobject]@{ kind = 'config'; name = $config.name; status = 'Excluded'; message = 'Complete Nextcloud config may contain credentials and was not captured.' }) - continue - } - if ($config.mode -eq 'copy') { $relative = ([string]$config.source).Replace('/', [IO.Path]::DirectorySeparatorChar) $destination = Join-Path $captureRoot $relative @@ -165,6 +160,29 @@ function Export-ConfigCapture { $operations.Add([pscustomobject]@{ kind = 'config'; name = $config.name; status = 'Review'; message = $destination }) continue } + elseif ($config.name -eq 'nextcloud-settings') { + $desired = Read-IniValues -Path $location.source + $desiredCount = @($desired.Keys | ForEach-Object { $desired[$_].Count } | Measure-Object -Sum).Sum + if (-not $desiredCount) { + $operations.Add([pscustomobject]@{ kind = 'config'; name = $config.name; status = 'Excluded'; message = 'No allowlisted Nextcloud keys are declared; complete account config was not captured.' }) + continue + } + $snapshot = Get-IniSnapshot -Path $location.target -Desired $desired + $relative = ([string]$config.source).Replace('/', [IO.Path]::DirectorySeparatorChar) + $destination = Join-Path $captureRoot $relative + [void](New-Item -ItemType Directory -Path (Split-Path -Parent $destination) -Force) + $lines = [Collections.Generic.List[string]]::new() + $lines.Add('; Allowlisted Nextcloud preferences captured without account data.') + foreach ($section in $desired.Keys) { + $presentKeys = @($desired[$section].Keys | Where-Object { $null -ne $snapshot.values[$section][$_] }) + if ($presentKeys.Count -eq 0) { continue } + if ($section) { $lines.Add("[$section]") } + foreach ($key in $presentKeys) { $lines.Add("$key=$($snapshot.values[$section][$key])") } + } + [IO.File]::WriteAllLines($destination, $lines, [Text.UTF8Encoding]::new($false)) + $operations.Add([pscustomobject]@{ kind = 'config'; name = $config.name; status = 'Captured'; message = "$destination ($($snapshot.present) allowlisted values; account data excluded)" }) + continue + } else { $fileName = if ($config.mode -eq 'hosts-block') { 'hosts' } else { [IO.Path]::GetFileName($location.target) } $destination = Join-Path $captureRoot "review\$($config.name)\$fileName" diff --git a/tests/Configs.Tests.ps1 b/tests/Configs.Tests.ps1 index f9f029d..f76e756 100644 --- a/tests/Configs.Tests.ps1 +++ b/tests/Configs.Tests.ps1 @@ -1,6 +1,7 @@ BeforeAll { Import-Module "$PSScriptRoot/../src/State.psm1" -Force Import-Module "$PSScriptRoot/../src/Repository.psm1" -Force + Import-Module "$PSScriptRoot/../src/Adapters.psm1" -Force Import-Module "$PSScriptRoot/../src/Configs.psm1" -Force } @@ -79,9 +80,9 @@ Describe 'Configuration deployment' { New-Item -ItemType Directory (Split-Path -Parent $xnviewTarget) -Force | Out-Null 'computer value' | Set-Content $destination -NoNewline 'portable=true' | Set-Content $xnviewTarget -NoNewline - 'token=never-export-this' | Set-Content $nextcloudTarget -NoNewline + "[General]`nlaunch=false`n[Accounts]`ntoken=never-export-this" | Set-Content $nextcloudTarget -NoNewline '; overlay' | Set-Content "$repo/configs/xnview.ini" - '; safe keys only' | Set-Content "$repo/configs/nextcloud.ini" + "[General]`nlaunch=true" | Set-Content "$repo/configs/nextcloud.ini" $captureConfigs = @( $configs[0], [pscustomobject]@{ name = 'xnviewmp-settings'; source = 'configs/xnview.ini'; target = $xnviewTarget; mode = 'ini-overlay' }, @@ -91,8 +92,10 @@ Describe 'Configuration deployment' { $capture = Export-ConfigCapture -Configs $captureConfigs -RepositoryRoot $repo -Settings $settings -Machine test -OutputPath $output (Get-Content "$output/configs/config.txt" -Raw) | Should -Be 'computer value' (Get-Content "$output/review/xnviewmp-settings/xnview.ini" -Raw) | Should -Be 'portable=true' - Get-ChildItem $output -Recurse -File | Where-Object Name -eq 'nextcloud.cfg' | Should -BeNullOrEmpty - ($capture.operations | Where-Object name -eq 'nextcloud-settings').status | Should -Be 'Excluded' + $nextcloudCapture = Get-Content "$output/configs/nextcloud.ini" -Raw + $nextcloudCapture | Should -Match 'launch=false' + $nextcloudCapture | Should -Not -Match 'token' + ($capture.operations | Where-Object name -eq 'nextcloud-settings').status | Should -Be 'Captured' (Get-Content "$output/capture.json" -Raw | ConvertFrom-Json).repositoryRevision | Should -Be 'abc123' }