Skip to content

Commit f32f8a4

Browse files
committed
Fixes certificate handling to provide systemwide compatibility
1 parent 1840a4e commit f32f8a4

15 files changed

+178
-59
lines changed

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1818
### Bugfixes
1919

2020
* [#718](https://github.com/Icinga/icinga-powershell-framework/issues/718) Fixes Icinga repository JSON validator to report the correct state of the validation status, in case the JSON is not valid
21-
21+
* [#725](https://github.com/Icinga/icinga-powershell-framework/pull/725) Fixes Icinga for Windows certificate handling by keeping the .pfx file on the system as created, without using the certificate store. Also fixes handling for providing thumbprints for certificates, which are now loaded directly from the certificate store by also providing a new filter mechanic to fetch the proper certificates from the store
2222

2323
## 1.12.2 (2024-04-10)
2424

Loading
Loading
Loading
Loading

doc/knowledgebase/IWKB000018.md

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,76 @@ Internal
2626

2727
## Reason
2828

29-
This message happens in case the user assigned to run the Icinga for Windows has no sufficient permissions to access the `icingaforwindows.pfx` certificate file or has no permissions to read the private key from the certificate file.
29+
This message happens in case the user assigned to run Icinga for Windows has insufficient permissions to access the given certificates private key. Most commonly, this will happen when Icinga for Windows will run as `NT Authority\NetworkService` while a certificate is loaded from the certificate store.
3030

3131
## Solution
3232

33-
To resolve this issue, you will either have to use [JEA-Profiles](../130-JEA/01-JEA-Profiles.md) or use a different user having enough permissions to access private key to the file. In general, only `LocalSystem` or `Administrators` have access to this key, which is why we highly recommend the use of JEA.
33+
### Use JEA
34+
35+
Regardless if you are using certificates from the certificate store or the `icingaforwindows.pfx`, we recommend using [JEA-Profiles](../130-JEA/01-JEA-Profiles.md) with a dedicated user like `icinga`. This resolves most permission issues, increases security and is easier to manage.
36+
37+
### Use icingaforwindows.pfx
38+
39+
The easiest solution besides using [JEA](../130-JEA/01-JEA-Profiles.md) would to configure the Icinga for Windows background daemon to look for the default certificate to load while starting the REST-Api. This certificate is the `icingaforwindows.pfx` which is by default located at
40+
41+
```
42+
C:\Program Files\WindowsPowerShell\Modules\icinga-powershell-framework\certificate\
43+
```
44+
45+
This certificate is generated automatically based on your `Icinga Agent certificate` on the machine during the first installation of Icinga for Windows and updated every day at 1:00 am by a scheduled task in the background. In case the certificate is not present and you want to use it or renew the certificate, you can generate it with the following command
46+
47+
```powershell
48+
Start-IcingaWindowsScheduledTaskRenewCertificate;
49+
```
50+
51+
This will generate the certificate and ensure it is present on the system. If you reconfigured your API to use a specific certificate from the cert store, you can restore the default behavior with
52+
53+
```powershell
54+
Register-IcingaBackgroundDaemon -Command Start-IcingaWindowsRESTApi;
55+
```
56+
57+
Afterwards simply restart Icinga for Windows and the API should be working properly
58+
59+
```powershell
60+
Restart-IcingaForWindows;
61+
```
62+
63+
### Grant permission to user for the private key
64+
65+
If you want to use a certificate stored inside the certificate store and you receive the above error message, you will have to make sure the corresponding user has access to the `private key` of the certificate. By using [JEA](../130-JEA/01-JEA-Profiles.md), you can skip this part as long as it is not the `NT Authority\NetworkService` user you are having problems with.
66+
67+
Please make sure as well, that your certificates are stored somewhere in the `LocalMachine` store and **not** in the `CurrentUser` Space.
68+
69+
To change the permission for the private key of the certificate, proceed as follow
70+
71+
1. Hint `Windows Key + R`
72+
2. Enter `mmc` into the Run dialog and hit enter (confirm a possible UAC prompt)
73+
3. Inside the mmc, on the top left click on `File` -> `Add/Remove Snap-In`
74+
4. Inside the new dialog look for `Certificates` and click the `Add` button in the middle
75+
5. A new window `Certificates snap-in` will pop up
76+
6. Select `Computer account` and click `Next`
77+
7. Use the `Local computer` and click `Finish` and on the remaining Window `Ok`
78+
79+
Now you need to navigate to the location on where your certificate is installed to. In our example, we assume it is installed in the `Personal` space
80+
81+
![Cert Store](../images/04_knowledgebase/IWKB000018/01_cert_store.png)
82+
83+
Look for the certificate you want to modify the `private key` for and right-click the certificate.
84+
85+
Now navigate to `All Tasks` -> `Manage Private Keys...`
86+
87+
![Private Key Managament](../images/04_knowledgebase/IWKB000018/02_manage_private_keys.png)
88+
89+
In the new window, click the `Add...` button and add the `NT Authority\NetworkService` user to the list of users having access to the private key. Most commonly you have to simply write `NETWORK SERVICE` into the prompt. Change this user for the ony applying to your use case.
90+
91+
![Private Key Managament](../images/04_knowledgebase/IWKB000018/03_add_networkservice_user.png)
92+
93+
Make sure to grant the user `Read` and `Full Control` permissions and click `Ok` and `Apply`
94+
95+
![Private Key Managament](../images/04_knowledgebase/IWKB000018/04_set_permission_and_apply.png)
96+
97+
Afterwards simply restart Icinga for Windows and the API should be working properly
98+
99+
```powershell
100+
Restart-IcingaForWindows;
101+
```

jobs/RenewCertificate.ps1

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ Use-Icinga -Minimal;
66

77
# To make the configuration of the task as easy as possible, we should fetch
88
# the current configuration of our REST-Api and check if we provide a custom
9-
# certificate file or thumbprint. In case we do, ensure we use this certificate
9+
# certificate file. In case we do, ensure we use this certificate
1010
# for the icingaforwindows.pfx creation instead of the auto lookup
11+
# We do only require to check for cert files on the disk, as the cert store
12+
# is fetched automatically
1113
[hashtable]$RegisteredBackgroundDaemons = Get-IcingaBackgroundDaemons;
1214
[string]$CertificatePath = '';
13-
[string]$CertificateThumbprint = '';
1415

1516
if ($RegisteredBackgroundDaemons.ContainsKey('Start-IcingaWindowsRESTApi')) {
1617
if ($RegisteredBackgroundDaemons['Start-IcingaWindowsRESTApi'].ContainsKey('CertFile')) {
@@ -19,15 +20,9 @@ if ($RegisteredBackgroundDaemons.ContainsKey('Start-IcingaWindowsRESTApi')) {
1920
if ($RegisteredBackgroundDaemons['Start-IcingaWindowsRESTApi'].ContainsKey('-CertFile')) {
2021
$CertificatePath = $RegisteredBackgroundDaemons['Start-IcingaWindowsRESTApi']['-CertFile'];
2122
}
22-
if ($RegisteredBackgroundDaemons['Start-IcingaWindowsRESTApi'].ContainsKey('CertThumbprint')) {
23-
$CertificateThumbprint = $RegisteredBackgroundDaemons['Start-IcingaWindowsRESTApi']['CertThumbprint'];
24-
}
25-
if ($RegisteredBackgroundDaemons['Start-IcingaWindowsRESTApi'].ContainsKey('-CertThumbprint')) {
26-
$CertificateThumbprint = $RegisteredBackgroundDaemons['Start-IcingaWindowsRESTApi']['-CertThumbprint'];
27-
}
2823
}
2924

30-
Install-IcingaForWindowsCertificate -CertFile $CertificatePath -CertThumbprint $CertificateThumbprint;
25+
Install-IcingaForWindowsCertificate -CertFile $CertificatePath;
3126

3227
# Tell the Task-Scheduler that the script was executed fine
3328
exit 0;

lib/core/framework/Invoke-IcingaForWindowsMigration.psm1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,19 @@ function Invoke-IcingaForWindowsMigration()
128128

129129
Set-IcingaForWindowsMigration -MigrationVersion (New-IcingaVersionObject -Version '1.12.2');
130130
}
131+
132+
if (Test-IcingaForWindowsMigration -MigrationVersion (New-IcingaVersionObject -Version '1.12.3')) {
133+
Write-IcingaConsoleNotice 'Applying pending migrations required for Icinga for Windows v1.12.3';
134+
135+
# Updates certificate renew task to properly handle changes in the certificate renewal process
136+
Register-IcingaWindowsScheduledTaskRenewCertificate -Force;
137+
Start-Sleep -Seconds 1;
138+
# Enforce the certificate creation to update broken certificates
139+
Start-IcingaWindowsScheduledTaskRenewCertificate;
140+
# Restart the Icinga for Windows service
141+
Start-Sleep -Seconds 2;
142+
Restart-IcingaForWindows;
143+
144+
Set-IcingaForWindowsMigration -MigrationVersion (New-IcingaVersionObject -Version '1.12.3');
145+
}
131146
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ 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';
20+
$TaskPrincipal = New-ScheduledTaskPrincipal -UserId 'S-1-5-18' -RunLevel 'Highest' -LogonType ServiceAccount;
2121
$TaskSettings = New-ScheduledTaskSettingsSet -DontStopIfGoingOnBatteries -AllowStartIfOnBatteries -StartWhenAvailable;
2222

2323
Register-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -Force -Principal $TaskPrincipal -Action $TaskAction -Trigger $TaskTrigger -Settings $TaskSettings | Out-Null;

lib/daemon/Start-IcingaPowerShellDaemon.psm1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function Start-IcingaForWindowsDaemon()
4242
'Certificate' = $null;
4343
'CertFile' = $null;
4444
'CertThumbprint' = $null;
45+
'CertFilter' = $null;
4546
}
4647
);
4748

@@ -66,6 +67,7 @@ function Start-IcingaForWindowsDaemon()
6667
'Certificate' = $null;
6768
'CertFile' = $null;
6869
'CertThumbprint' = $null;
70+
'CertFilter' = $null;
6971
}
7072
);
7173

0 commit comments

Comments
 (0)