92 lines
6.0 KiB
PowerShell
92 lines
6.0 KiB
PowerShell
# QA-only Task Scheduler service-account test. Never modifies destination services or ACLs.
|
|
$ErrorActionPreference = 'Stop'
|
|
$taskRoot = 'C:\laragon\www\portal-investidor'
|
|
$taskPrivate = Join-Path $taskRoot 'storage\app\private\investmoz-v2\portal-audit'
|
|
$taskPlan = Get-Content -LiteralPath (Join-Path $taskPrivate 'consolidated-localservice-plan.json') -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
$taskQa = [IO.Path]::GetFullPath($taskPlan.source)
|
|
$taskExpectedParent = [IO.Path]::GetFullPath($taskPrivate) + '\'
|
|
if (-not $taskQa.StartsWith($taskExpectedParent, [StringComparison]::OrdinalIgnoreCase) -or
|
|
[IO.Path]::GetFileName($taskQa) -notmatch '^localservice-qa-\d{8}_\d{6}$' -or $taskPlan.target.port -ne 55441 -or
|
|
$taskPlan.target.database -notmatch '^workflow_upgrade_\d{8}_\d{6}_test$' -or $taskPlan.queue -ne 'localservice-qa') { throw 'Exact QA-only scope required' }
|
|
$taskName = 'InvestMoz LocalService QA ' + [IO.Path]::GetFileName($taskQa)
|
|
$taskService = New-Object -ComObject 'Schedule.Service'
|
|
$taskService.Connect()
|
|
$taskFolder = $taskService.GetFolder('\')
|
|
$taskExisting = $null
|
|
try { $taskExisting = $taskFolder.GetTask($taskName) } catch {}
|
|
if ($taskExisting) { throw 'Own QA task exists; inspect instead of replacing' }
|
|
$taskDefinition = $taskService.NewTask(0)
|
|
$taskDefinition.RegistrationInfo.Description = 'QA ONLY: PostgreSQL 55441 / localservice-qa / scanner 13310. No destination services.'
|
|
$taskDefinition.Principal.Id = 'LocalServiceQA'
|
|
$taskDefinition.Principal.UserId = 'S-1-5-19'
|
|
$taskDefinition.Principal.LogonType = 5
|
|
$taskDefinition.Principal.RunLevel = 0
|
|
$taskDefinition.Actions.Context = 'LocalServiceQA'
|
|
$taskDefinition.Settings.Enabled = $false
|
|
$taskDefinition.Settings.MultipleInstances = 2
|
|
$taskDefinition.Settings.ExecutionTimeLimit = 'PT3M'
|
|
$taskDefinition.Settings.DisallowStartIfOnBatteries = $false
|
|
$taskDefinition.Settings.StopIfGoingOnBatteries = $false
|
|
$taskAction = $taskDefinition.Actions.Create(0)
|
|
$taskAction.Path = "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe"
|
|
$taskAction.Arguments = '-NoProfile -ExecutionPolicy Bypass -File "' + (Join-Path $taskQa 'tools\localservice-worker.ps1') + '"'
|
|
$taskAction.WorkingDirectory = $taskQa
|
|
$taskProof = @{at=[DateTime]::UtcNow.ToString('o'); name=$taskName; source=$taskQa; target=$taskPlan.target; principal='S-1-5-19'; active_writes=0; active_tasks_changed=0}
|
|
$taskUtf8 = New-Object System.Text.UTF8Encoding($false)
|
|
try {
|
|
$taskRegistered = $taskFolder.RegisterTaskDefinition($taskName, $taskDefinition, 2, 'S-1-5-19', $null, 5, $null)
|
|
$taskProof.registration = 'registered_disabled'
|
|
} catch {
|
|
$taskProof.registration = 'blocked_by_windows_permissions'
|
|
$taskProof.hresult = $_.Exception.HResult
|
|
[IO.File]::WriteAllText((Join-Path $taskPrivate 'consolidated-localservice-registration.json'), ($taskProof | ConvertTo-Json -Depth 5), $taskUtf8)
|
|
$taskProof | ConvertTo-Json -Compress -Depth 4
|
|
exit 5
|
|
}
|
|
try {
|
|
$taskOwner = [Security.Principal.WindowsIdentity]::GetCurrent().User
|
|
$taskAcl = New-Object Security.AccessControl.DirectorySecurity
|
|
$taskAcl.SetAccessRuleProtection($true, $false)
|
|
foreach ($taskSid in @($taskOwner.Value,'S-1-5-18','S-1-5-32-544')) {
|
|
$taskRule = New-Object Security.AccessControl.FileSystemAccessRule([Security.Principal.SecurityIdentifier]::new($taskSid), 'FullControl', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
|
|
$taskAcl.AddAccessRule($taskRule)
|
|
}
|
|
$taskAcl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule([Security.Principal.SecurityIdentifier]::new('S-1-5-19'), 'ReadAndExecute', 'ContainerInherit,ObjectInherit', 'None', 'Allow')))
|
|
$taskBefore = Get-Acl -LiteralPath $taskQa
|
|
[IO.File]::WriteAllText((Join-Path $taskPrivate 'consolidated-localservice-acl-before.txt'), $taskBefore.Sddl, $taskUtf8)
|
|
Set-Acl -LiteralPath $taskQa -AclObject $taskAcl
|
|
foreach ($taskRelative in @('storage','bootstrap\cache','scanner-writable')) {
|
|
$taskPath = Join-Path $taskQa $taskRelative
|
|
$taskWritable = Get-Acl -LiteralPath $taskPath
|
|
$taskWritable.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule([Security.Principal.SecurityIdentifier]::new('S-1-5-19'), 'Modify', 'ContainerInherit,ObjectInherit', 'None', 'Allow')))
|
|
Set-Acl -LiteralPath $taskPath -AclObject $taskWritable
|
|
}
|
|
$taskSentinel = Join-Path $taskQa 'forbidden-qa-sentinel.txt'
|
|
$taskSentinelAcl = Get-Acl -LiteralPath $taskSentinel
|
|
$taskSentinelAcl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule([Security.Principal.SecurityIdentifier]::new('S-1-5-19'), 'Read', 'Deny')))
|
|
Set-Acl -LiteralPath $taskSentinel -AclObject $taskSentinelAcl
|
|
$taskProof.acl_scope = 'Own new QA directory only; RX code/config/vendor, Modify storage/cache/scanner-writable, forbidden synthetic read denied'
|
|
$taskRegistered.Enabled = $true
|
|
foreach ($taskIteration in 1..5) {
|
|
$taskRun = $taskRegistered.Run($null)
|
|
Start-Sleep -Milliseconds 500
|
|
$taskDeadline = [DateTime]::UtcNow.AddSeconds(190)
|
|
do { Start-Sleep -Milliseconds 500 } while ($taskRegistered.State -eq 4 -and [DateTime]::UtcNow -lt $taskDeadline)
|
|
if ($taskRegistered.State -eq 4 -or $taskRegistered.LastTaskResult -ne 0) { throw 'QA LocalService invocation failed or exceeded deadline' }
|
|
}
|
|
$taskProof.result = 'task_executed_five_times'
|
|
$taskProof.last_task_result = $taskRegistered.LastTaskResult
|
|
$taskProof.task_xml = $taskRegistered.Xml
|
|
} catch {
|
|
$taskProof.result = 'test_failed'
|
|
$taskProof.error_type = $_.Exception.GetType().FullName
|
|
$taskProof.hresult = $_.Exception.HResult
|
|
} finally {
|
|
$taskRegistered.Enabled = $false
|
|
if ($taskRegistered.State -eq 4) { $taskRegistered.Stop(0) }
|
|
$taskFolder.DeleteTask($taskName, 0)
|
|
$taskProof.own_task_removed = $true
|
|
[IO.File]::WriteAllText((Join-Path $taskPrivate 'consolidated-localservice-registration.json'), ($taskProof | ConvertTo-Json -Depth 6), $taskUtf8)
|
|
$taskProof | Select-Object at,name,result,last_task_result,own_task_removed,active_writes | ConvertTo-Json -Compress
|
|
}
|