Skip to content

Commit 8d4e66f

Browse files
authored
Merge branch 'master' into feature/add_code_cache_for_faster_framework_loading
2 parents 37c0824 + fd4d634 commit 8d4e66f

21 files changed

+733
-22
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Please take a look at the following content to get to know the possibilities of
1616
* [Icinga Integration](doc/05-Icinga-Integration.md)
1717
* [Framework Usage Examples](doc/06-Framework-Usage.md)
1818
* [Icinga PowerShell Framework as Service](doc/service/01-Install-Service.md)
19+
* [Knowledge Base](doc/10-Knowledge-Base.md)
1920
* [Changelog](doc/31-Changelog.md)
2021

2122
Developer Guide

doc/10-Knowledge-Base.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Icinga for Windows Knowledge Base
2+
3+
While using Icinga for Windows you might run into issues, permissions problems or different problems during usage. Our main goal is to catch all of those problems and print proper error messages to give ideas on what went went wrong exactly.
4+
5+
However, some problems might be more complex and require further detailed descriptions as an issue could be caused my multiple different events or possible solutions would be way too long to put into a plugin output for example.
6+
7+
For this reason you will find a list of Icinga knowledge base entries below. Entries are assigned a unique 6 digit number referenced by issue messages, lead by the abbreviation `IWKB`. Example `IWKB000001`.
8+
9+
| Knowledge Base Id | Short Message / Description |
10+
| --- | --- |
11+
| [IWKB000001](knowledgebase/IWKB000001.md) | The user you are running this command as does not have permission to access the requested Cim-Object. To fix this, please add the user the Agent is running with to the "Remote Management Users" groups and grant access to the WMI branch for the Class/Namespace mentioned above and add the permission "Remote enable". |
12+
| [IWKB000002](knowledgebase/IWKB000002.md) | Plugin execution fails because arguments could not be validated and properly set. An example error could be `The "*" was not recognized as the name of a program, cmdlet, function, script file, or executable. Check the spelling of the name and that the path is correct (if included), and repeat the process.` |

doc/31-Changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,20 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1919
* [#141](https://github.com/Icinga/icinga-powershell-framework/pull/141) Adds Cmdlet `Convert-IcingaPluginThresholds` as generic approach to convert Icinga Thresholds with units to the lowest unit of this type.
2020
* [#134](https://github.com/Icinga/icinga-powershell-framework/pull/134) Adds Cmdlet `Test-IcingaWindowsInformation` to check if a WMI class exist and if we can fetch data from it. In addition we add support for binary value comparison with the new Cmdlet `Test-IcingaBinaryOperator`
2121
* [#142](https://github.com/Icinga/icinga-powershell-framework/pull/142) **Experimental:** Adds feature to cache the Framework code into a single file to speed up the entire loading process, mitigating the impact on performance on systems with few CPU cores. You enable disables this feature by using `Enable-IcingaFrameworkCodeCache` and `Disable-IcingaFrameworkCodeCache`. Updating the cache is done with `Write-IcingaFrameworkCodeCache`
22+
* [#149](https://github.com/Icinga/icinga-powershell-framework/pull/149) Adds support to add Wmi permissions for a specific user and namespace with `Add-IcingaWmiPermissions`. In addition you can remove users from Wmi namespaces by using `Remove-IcingaWmiPermissions`
2223

2324
### Bugfixes
2425

2526
* [#059](https://github.com/Icinga/icinga-powershell-framework/issues/059), [#060](https://github.com/Icinga/icinga-powershell-framework/pull/060) Fixes interface handling for multiple interfaces and returns only the main interface by fallback to routing table and adds support for Windows 2008 R2
27+
* [#114](https://github.com/Icinga/icinga-powershell-framework/issues/114)[#146](https://github.com/Icinga/icinga-powershell-framework/pull/146) Fixes Icinga Agent API being wrongly disabled after successful certificate configuration and installation
2628
* [#127](https://github.com/Icinga/icinga-powershell-framework/issues/127) Fixes wrong error message on failed MSSQL connection due to database not reachable by using `-IntegratedSecurity`
2729
* [#128](https://github.com/Icinga/icinga-powershell-framework/issues/128) Fixes unhandled output from loading `System.Reflection.Assembly` which can cause weird side effects for plugin outputs
2830
* [#130](https://github.com/Icinga/icinga-powershell-framework/issues/130) Fix crash while running services as background task to collect metrics over time by missing Performance Counter cache initialisation
31+
* [#133](https://github.com/Icinga/icinga-powershell-framework/issues/133), [#147](https://github.com/Icinga/icinga-powershell-framework/pull/147) Fixes an issue while changing the hostname between upper/lower case which might cause unwanted exceptions on one hand but also required manual signing of requests on the CA master as the signing process was not completed
2932
* [#138](https://github.com/Icinga/icinga-powershell-framework/issues/138) Fixes possible value overflow on `Convert-Bytes` while converting from anything larger than MB to Bytes
3033
* [#140](https://github.com/Icinga/icinga-powershell-framework/issues/140) Fixes version fetching for not loaded modules during upgrades/plugin calls with `Get-IcingaPowerShellModuleVersion`
3134
* [#143](https://github.com/Icinga/icinga-powershell-framework/issues/143) Fixes the annoying hint from the analyzer to check space before open brace
35+
* [#152](https://github.com/Icinga/icinga-powershell-framework/issues/152) Fixes incorrect rendering for empty arrays which used `$null` incorrectly instead of `@()` and fixed ValidateSet which now also supports arrays as data type
3236

3337
## 1.2.0 (2020-08-28)
3438

Loading
Loading
Loading

doc/knowledgebase/IWKB000001.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Icinga Knowledge Base - IWKB000001
2+
3+
## Short Message
4+
5+
The user you are running this command as does not have permission to access the requested Cim-Object. To fix this, please add the user the Agent is running with to the "Remote Management Users" groups and grant access to the WMI branch for the Class/Namespace mentioned above and add the permission "Remote enable".
6+
7+
## Example Exception
8+
9+
[UNKNOWN]: Icinga Permission Error was thrown: CimInstance: Class: "MSFT_PhysicalDisk", Namespace: "root\Microsoft\Windows\Storage"
10+
11+
## Reason
12+
13+
To access WMI Objects by either `Get-WmiObject` or `Get-CimInstance` you will require to grant permissions to the user running the plugins to be allowed to fetch data from them. In general default system users should already inherit these permissions.
14+
15+
However, if you are running custom users like an AD user or local users you might require to grant additional permissions.
16+
17+
## Required Permissions
18+
19+
Each plugin should contain a section for WMI permissions in case the plugin fetches WMI data, to give a better understanding which WMI area is accessed and permissions are required. For most plugins, the default space is `Root\Cimv2`, but additional spaces might be required.
20+
21+
## Solution
22+
23+
In case you are running into this issue, you will have to grant permissions on the corresponding WMI path printed at the `Namespace` within the exception message or include the WMI path from the plugin permissions documentation. An example plugin would be [Invoke-IcingaCheckDiskHealth](https://icinga.com/docs/windows/latest/plugins/doc/plugins/20-Invoke-IcingaCheckDiskHealth/#permissions).
24+
25+
### Adding Permissions
26+
27+
#### Using UI configuration
28+
29+
There is a detailed description on the [Microsoft Docs](https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/cc771551(v=ws.11)) available on how permissions for WMI are set. Please keep in mind that granting permissions to the `primary namespace` might not be enough and you will require to change the user permissions to apply for `This namespace and subnamespaces`.
30+
31+
#### Using PowerShell
32+
33+
To add permissions for WMI namespaces you can use our Icinga for Windows Cmdlets. Simply open a new PowerShell as `Administrator` and create a new Icinga Shell instance by typing `icinga`.
34+
After the Icinga PowerShell Framework is loaded, we can use our Wmi permission Cmdlet:
35+
36+
```powershell
37+
Add-IcingaWmiPermissions -User 'icinga' -Namespace 'root\Microsoft\Windows\Storage' -Enable -RemoteAccess -Recurse;
38+
```
39+
40+
The above command will grant the user `icinga` the read and remote access permission for the `root\Microsoft\Windows\Storage` namespace. In addition by using `-Recurse` we will set the permissions for `subnamespaces` in addition.
41+
42+
### Permission Table
43+
44+
| Flag | Description |
45+
| --- | --- |
46+
| Enable | Enables the account and grants the user read permissions. This is a default access right for all users and corresponds to the Enable Account permission on the Security tab of the WMI Control. For more information, see Setting Namespace Security with the WMI Control. |
47+
| MethodExecute | Allows the execution of methods. Providers can perform additional access checks. This is a default access right for all users and corresponds to the Execute Methods permission on the Security tab of the WMI Control. |
48+
| FullWrite | Allows a user account to write to classes in the WMI repository as well as instances. A user cannot write to system classes. Only members of the Administrators group have this permission. WBEM_FULL_WRITE_REP corresponds to the Full Write permission on the Security tab of the WMI Control. |
49+
| PartialWrite | Allows you to write data to instances only, not classes. A user cannot write classes to the WMI repository. Only members of the Administrators group have this right. WBEM_PARTIAL_WRITE_REP corresponds to the Partial Write permission on the Security tab of the WMI Control. |
50+
| ProviderWrite | Allows writing classes and instances to providers. Note that providers can do additional access checks when impersonating a user. This is a default access right for all users and corresponds to the Provider Write permission on the Security tab of the WMI Control. |
51+
| RemoteAccess | Allows a user account to remotely perform any operations allowed by the permissions described above. Only members of the Administrators group have this right. WBEM_REMOTE_ACCESS corresponds to the Remote Enable permission on the Security tab of the WMI Control. |
52+
| Subscribe | Specifies that a consumer can subscribe to the events delivered to a sink. Used in IWbemEventSink::SetSinkSecurity. |
53+
| Publish | Specifies that the account can publish events to the instance of __EventFilter that defines the event filter for a permanent consumer. Available in wbemcli.h. |
54+
| ReadSecurity | The right to read the information in the objects security descriptor, not including the information in the system access control list (SACL). |
55+
| WriteSecurity | The right to modify the discretionary access control list (DACL) in the objects security descriptor. |
56+
57+
**Note:** By default the Cmdlet `Add-IcingaWmiPermissions` only has direct build-in support for `-Enable` and `-RemoteAccess`. To apply different permissions, you will have to use the `-Flags` argument and add the flags by their name as shown in the table.

doc/knowledgebase/IWKB000002.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Icinga Knowledge Base - IWKB000002
2+
3+
## Short Message
4+
5+
Plugin execution fails because arguments could not be validated and properly set. An example error could be `The "*" was not recognized as the name of a program, cmdlet, function, script file, or executable. Check the spelling of the name and that the path is correct (if included), and repeat the process.`
6+
7+
## Example Exception
8+
9+
![Plugin Output Error](../images/04_knowledgebase/IWKB000002/01_Plugin_Output_Error.png)
10+
11+
## Reason
12+
13+
This error can happen if array arguments either contain special characters, like `*` or spaces. By using the configuration provided by Icinga for Windows, array arguments are taken from your configuration and re-rendered using Icinga DSL as PowerShell arrays.
14+
15+
In addition Icinga is not ensuring string values with included spaces are properly escaped.
16+
17+
## Solution
18+
19+
To fix this you can simply put the input values, regardless if you are within an array element of a string, into single quotes `'`. This will ensure multi-part strings are always rendered as one element during plugin execution.
20+
21+
**Example:**
22+
23+
![Escape Strings](../images/04_knowledgebase/IWKB000002/02_Director_Config.png)
24+
25+
Once the Performance Counter is put between single quotes `'`, we can view the proper check result output:
26+
27+
![Escape Strings](../images/04_knowledgebase/IWKB000002/03_Fixed_Output.png)

lib/core/icingaagent/installer/Install-IcingaAgentCertificates.psm1

+58-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,47 @@
1+
<#
2+
.SYNOPSIS
3+
Installs the required certificates for the Icinga Agent including the entire
4+
signing process either by using the CA-Proxy, the CA-Server directly or
5+
by manually signing the request on the CA master
6+
.DESCRIPTION
7+
Installs the required certificates for the Icinga Agent including the entire
8+
signing process either by using the CA-Proxy, the CA-Server directly or
9+
by manually signing the request on the CA master
10+
.FUNCTIONALITY
11+
Creates, installs and signs required certificates for the Icinga Agent
12+
.EXAMPLE
13+
# Connect to the CA server with a ticket to fully complete the request
14+
PS>Install-IcingaAgentCertificates -Hostname 'windows.example.com' -Endpoint 'icinga2.example.com' -Ticket 'my_secret_ticket';
15+
.EXAMPLE
16+
# Connect to the CA server without a ticket, to create the sign request on the master
17+
PS>Install-IcingaAgentCertificates -Hostname 'windows.example.com' -Endpoint 'icinga2.example.com';
18+
.EXAMPLE
19+
# Uses the Icinga ca.crt from a local filesystem and prepares the Icinga Agent for receiving connections from the Master/Satellite for signing
20+
PS>Install-IcingaAgentCertificates -Hostname 'windows.example.com' -CACert 'C:\users\public\icinga2\ca.crt';
21+
.EXAMPLE
22+
# Uses the Icinga ca.crt from a web resource and prepares the Icinga Agent for receiving connections from the Master/Satellite for signing
23+
PS>Install-IcingaAgentCertificates -Hostname 'windows.example.com' -CACert 'https://example.com/icinga2/ca.crt';
24+
.PARAMETER Hostname
25+
The hostname of the local system. Has to match the object name within the Icinga configuration
26+
.PARAMETER Endpoint
27+
The address of either the Icinga CA master or a parent node of the Agent to transmit the request to the CA master
28+
.PARAMETER Port
29+
The port used for Icinga communication. Uses 5665 as default
30+
.PARAMETER CACert
31+
Allows to specify the path to the ca.crt from the Icinga CA master on a local, network or web share to allow certificate generation
32+
in case the Icinga Agent is not able to connect to it's parent hosts
33+
.PARAMETER Ticket
34+
The ticket number for the signing request which is either generated by Icinga 2 or the Icinga Director
35+
.PARAMETER Force
36+
Ignores existing certificates and will force the creation, overriding existing certificates
37+
.INPUTS
38+
System.String
39+
.OUTPUTS
40+
System.Boolean
41+
.LINK
42+
https://github.com/Icinga/icinga-powershell-framework
43+
#>
44+
145
function Install-IcingaAgentCertificates()
246
{
347
param(
@@ -38,6 +82,9 @@ function Install-IcingaAgentCertificates()
3882
Write-IcingaConsoleError 'Failed to generate host certificate';
3983
return $FALSE;
4084
}
85+
86+
# Once we generated new host certificates, we always require to sign them if possible
87+
$Force = $TRUE;
4188
}
4289

4390
if ([string]::IsNullOrEmpty($Endpoint) -And [string]::IsNullOrEmpty($CACert)) {
@@ -182,8 +229,9 @@ function Test-IcingaAgentCertificates()
182229
return $FALSE;
183230
}
184231

185-
[string]$hostCRT = [string]::Format('{0}.crt', $Hostname);
186-
[string]$hostKEY = [string]::Format('{0}.key', $Hostname);
232+
[string]$hostCRT = [string]::Format('{0}.crt', $Hostname);
233+
[string]$hostKEY = [string]::Format('{0}.key', $Hostname);
234+
[bool]$CertNameInvalid = $FALSE;
187235

188236
$certificates = Get-ChildItem -Path $CertDirectory;
189237
# Now loop each file and match their name with our hostname
@@ -192,11 +240,18 @@ function Test-IcingaAgentCertificates()
192240
$file = $cert.Name.Replace('.key', '').Replace('.crt', '');
193241
if (-Not ($file -clike $Hostname)) {
194242
Write-IcingaConsoleWarning ([string]::Format('Certificate file {0} is not matching the hostname {1}. Certificate generation is required.', $cert.Name, $Hostname));
195-
return $FALSE;
243+
$CertNameInvalid = $TRUE;
244+
break;
196245
}
197246
}
198247
}
199248

249+
if ($CertNameInvalid) {
250+
Remove-Item -Path (Join-Path -Path $CertDirectory -ChildPath $hostCRT) -Force;
251+
Remove-Item -Path (Join-Path -Path $CertDirectory -ChildPath $hostKEY) -Force;
252+
return $FALSE;
253+
}
254+
200255
Write-IcingaConsoleNotice 'Icinga host certificates are present and valid. No generation required';
201256

202257
return $TRUE;

lib/core/icingaagent/misc/Start-IcingaAgentInstallWizard.psm1

+1-1
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ function Start-IcingaAgentInstallWizard()
561561
Install-IcingaAgentBaseFeatures;
562562
$CertsInstalled = Install-IcingaAgentCertificates -Hostname $Hostname -Endpoint $CAEndpoint -Port $CAPort -CACert $CAFile -Ticket $Ticket;
563563
Write-IcingaAgentApiConfig -Port $CAPort;
564-
if ($EmptyCA -eq $TRUE -Or $CertsInstalled -eq $FALSE) {
564+
if ($EmptyCA -eq $TRUE -And $CertsInstalled -eq $FALSE) {
565565
Disable-IcingaAgentFeature 'api';
566566
Write-IcingaConsoleWarning `
567567
-Message '{0}{1}{2}{3}{4}' `

lib/core/tools/Get-IcingaCheckCommandConfig.psm1

+8-2
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function Get-IcingaCheckCommandConfig()
165165
'value' = @{
166166
'type' = 'Function';
167167
'body' = [string]::Format(
168-
'var arr = macro("{0}");{1}if (len(arr) == 0) {2}{1}return "$null";{1}{3}{1}return arr.join(",");',
168+
'var arr = macro("{0}");{1}if (len(arr) == 0) {2}{1}return "@()";{1}{3}{1}return arr.join(",");',
169169
$IcingaCustomVariable,
170170
"`r`n",
171171
'{',
@@ -259,10 +259,16 @@ function Get-IcingaCheckCommandConfig()
259259
);
260260

261261
if ($IsDataList) {
262+
[string]$DataListDataType = 'string';
263+
264+
if ($parameter.type.name -eq 'Array') {
265+
$DataListDataType = 'array';
266+
}
267+
262268
$Basket.Datafield[[string]$FieldID].Add(
263269
'settings', @{
264270
'datalist' = $DataListName;
265-
'data_type' = 'string';
271+
'data_type' = $DataListDataType;
266272
'behavior' = 'strict';
267273
}
268274
);

lib/core/tools/Get-IcingaUserSID.psm1

+2-12
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,10 @@ function Get-IcingaUserSID()
88
$User = 'NT Authority\SYSTEM';
99
}
1010

11-
[string]$Username = '';
12-
[string]$Domain = '';
13-
14-
if ($User.Contains('\')) {
15-
$TmpArray = $User.Split('\');
16-
$Domain = $TmpArray[0];
17-
$Username = $TmpArray[1];
18-
} else {
19-
$Domain = Get-IcingaNetbiosName;
20-
$Username = $User;
21-
}
11+
$UserData = Split-IcingaUserDomain -User $User;
2212

2313
try {
24-
$NTUser = New-Object System.Security.Principal.NTAccount($Domain, $Username);
14+
$NTUser = New-Object System.Security.Principal.NTAccount($UserData.Domain, $UserData.User);
2515
$SecurityData = $NTUser.Translate([System.Security.Principal.SecurityIdentifier]);
2616
} catch {
2717
throw $_.Exception;

0 commit comments

Comments
 (0)