Skip to content

Commit c17a5f2

Browse files
committed
Fixes scheduled task user to use LocalSystem instead of anything else
1 parent 787571e commit c17a5f2

File tree

7 files changed

+17
-8
lines changed

7 files changed

+17
-8
lines changed

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
2020
* [#707](https://github.com/Icinga/icinga-powershell-framework/pull/707) Fixes size of the `Icinga for Windows` eventlog by setting it to `20MiB`, allowing to store more events before they are overwritten
2121
* [#710](https://github.com/Icinga/icinga-powershell-framework/pull/710) Fixes various console errors while running Icinga for Windows outside of an administrative shell
2222
* [#714](https://github.com/Icinga/icinga-powershell-framework/pull/714) Fixes missing service environment information during initial setup of Icinga for Windows v1.12 on some systems
23+
* [#715](https://github.com/Icinga/icinga-powershell-framework/pull/715) Fixes internal scheduled task handling and certificate renewal task by setting the user to `LocalSystem` instead of any administrative user or group, ensuring compatibility with all Windows versions as well as managing by using WinRM and SSH
2324

2425
## 1.12.0 (2024-03-26)
2526

lib/core/framework/Install-IcingaForWindowsService.psm1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ function Install-IcingaForWindowsService()
6969

7070
if ($IfWService.Present -eq $FALSE) {
7171
$ServiceCreation = Start-IcingaProcess -Executable 'sc.exe' -Arguments ([string]::Format('create icingapowershell binPath= "{0}" DisplayName= "Icinga PowerShell Service" start= auto', $Path));
72-
$Global:Icinga.Protected.Environment.'PowerShell Service'.Present = $TRUE;
73-
$Global:Icinga.Protected.Environment.'PowerShell Service'.User = $User;
72+
$Global:Icinga.Protected.Environment.'PowerShell Service'.Present = $TRUE;
73+
$Global:Icinga.Protected.Environment.'PowerShell Service'.User = $User;
74+
$Global:Icinga.Protected.Environment.'PowerShell Service'.ServicePath = $Path;
7475

7576
if ($ServiceCreation.ExitCode -ne 0) {
7677
throw ([string]::Format('Failed to install Icinga PowerShell Service: {0}{1}', $ServiceCreation.Message, $ServiceCreation.Error));
@@ -81,6 +82,8 @@ function Install-IcingaForWindowsService()
8182
if ($ServiceUpdate.ExitCode -ne 0) {
8283
throw ([string]::Format('Failed to update config for Icinga PowerShell Service: {0}{1}', $ServiceUpdate.Message, $ServiceUpdate.Error));
8384
}
85+
86+
$Global:Icinga.Protected.Environment.'PowerShell Service'.ServicePath = $Path;
8487
}
8588

8689
# This is just a hotfix to ensure we setup the service properly before assigning it to

lib/core/framework/Invoke-IcingaForWindowsMigration.psm1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ function Invoke-IcingaForWindowsMigration()
108108
# before older ones are faded out
109109
Register-IcingaEventLog;
110110

111+
# Fixes user environment which is now set to LocalSystem, allowing configurations over WinRM and SSH
112+
Register-IcingaWindowsScheduledTaskRenewCertificate -Force;
113+
111114
Set-IcingaForWindowsMigration -MigrationVersion (New-IcingaVersionObject -Version '1.12.1');
112115
}
113116
}

lib/core/framework/Test-IcingaForWindowsService.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function Test-IcingaForWindowsService()
44
[switch]$ResolveProblems = $FALSE
55
);
66

7-
Set-IcingaServiceEnvironment;
7+
Set-IcingaServiceEnvironment -Force;
88

99
$ServiceData = Get-IcingaForWindowsServiceData;
1010
$ServiceConfig = $Global:Icinga.Protected.Environment.'PowerShell Service';

lib/core/wintasks/Invoke-IcingaWindowsScheduledTask.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function Invoke-IcingaWindowsScheduledTask()
3737
};
3838
'UninstallAgent' {
3939
$WinAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument ([string]::Format('-WindowStyle Hidden -Command &{{ Use-Icinga -Minimal; Write-IcingaFileSecure -File {0}{1}{0} -Value (Start-IcingaProcess -Executable {0}MsiExec.exe{0} -Arguments {0}"{2}" /q{0} -FlushNewLines | ConvertTo-Json -Depth 100); }}', "'", $TmpFile.FullName, $FilePath, $TargetPath))
40-
Register-ScheduledTask -TaskName $TaskName -Action $WinAction -RunLevel Highest -TaskPath $TaskPath | Out-Null;
40+
Register-ScheduledTask -User 'System' -TaskName $TaskName -Action $WinAction -TaskPath $TaskPath | Out-Null;
4141

4242
Start-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath;
4343

@@ -69,7 +69,7 @@ function Invoke-IcingaWindowsScheduledTask()
6969
};
7070
'InstallJEA' {
7171
$WinAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument ([string]::Format('-Command &{{ Use-Icinga -Minimal; Install-IcingaJEAProfile; Restart-IcingaForWindows; }}', "'", $TmpFile.FullName, $FilePath))
72-
Register-ScheduledTask -TaskName $TaskName -Action $WinAction -RunLevel Highest -TaskPath $TaskPath | Out-Null;
72+
Register-ScheduledTask -User 'System' -TaskName $TaskName -Action $WinAction -TaskPath $TaskPath | Out-Null;
7373
Start-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath;
7474

7575
Wait-IcingaWindowsScheduledTask;

lib/core/wintasks/Invoke-IcingaWindowsServiceHandlerTask.psm1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ function Invoke-IcingaWindowsServiceHandlerTask()
2121

2222
$WinAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument ([string]::Format("-WindowStyle Hidden -Command &{{ & '{0}' -ServiceName '{1}' -TmpFilePath '{2}' }}", $ScriptPath, $ServiceName, $TmpFile));
2323
$TaskSettings = New-ScheduledTaskSettingsSet -DontStopIfGoingOnBatteries -AllowStartIfOnBatteries -StartWhenAvailable;
24-
Register-ScheduledTask -TaskName $TaskName -Action $WinAction -RunLevel Highest -TaskPath $TaskPath -Settings $TaskSettings -Force | Out-Null;
24+
# We need to schedule this task as LocalSystem to ensure we can fetch the information while connected over WinRM/SSH
25+
# We require high admin privilleges anyway, therefor this shouldn't hurt
26+
Register-ScheduledTask -User 'System' -TaskName $TaskName -Action $WinAction -TaskPath $TaskPath -Settings $TaskSettings -Force | Out-Null;
2527

2628
Start-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath;
2729

lib/core/wintasks/daemon/Register-TaskRenewCertificate.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ function Register-IcingaWindowsScheduledTaskRenewCertificate()
1717
$ScriptPath = Join-Path -Path (Get-IcingaFrameworkRootPath) -ChildPath '\jobs\RenewCertificate.ps1';
1818
$TaskTrigger = New-ScheduledTaskTrigger -Daily -DaysInterval 1 -At '1am';
1919
$TaskAction = New-ScheduledTaskAction -Execute 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -Argument ([string]::Format("-WindowStyle Hidden -Command &{{ & '{0}' }}", $ScriptPath));
20-
$TaskPrincipal = New-ScheduledTaskPrincipal -GroupId 'S-1-5-32-544' -RunLevel 'Highest';
2120
$TaskSettings = New-ScheduledTaskSettingsSet -DontStopIfGoingOnBatteries -AllowStartIfOnBatteries -StartWhenAvailable;
2221

23-
Register-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -Force -Principal $TaskPrincipal -Action $TaskAction -Trigger $TaskTrigger -Settings $TaskSettings | Out-Null;
22+
# Set our user to execute the renewal script to LocalSystem, ensuring we have enough privilliges to create the certificate file and be able to use WinRM/SSH for service registering
23+
Register-ScheduledTask -User 'System' -TaskName $TaskName -TaskPath $TaskPath -Force -Action $TaskAction -Trigger $TaskTrigger -Settings $TaskSettings | Out-Null;
2424

2525
Write-IcingaConsoleNotice -Message 'The task "{0}" has been successfully registered at location "{1}".' -Objects $TaskName, $TaskPath;
2626
}

0 commit comments

Comments
 (0)