Skip to content

Commit d02a12e

Browse files
authored
Merge pull request #366 from Icinga:fix/icinga_director_error_handling_imc
Fix: Improves error handling on IMC for Director Fixes error message handling for Icinga Director while using IMC.
2 parents 5ae390f + 9de78c6 commit d02a12e

16 files changed

+168
-31
lines changed

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1717
* [#362](https://github.com/Icinga/icinga-powershell-framework/issues/362) Fixes repository component installation from file share locations
1818
* [#363](https://github.com/Icinga/icinga-powershell-framework/issues/363) Fixes unneeded continue for JEA process lookup, in case no JEA pid is present
1919
* [#365](https://github.com/Icinga/icinga-powershell-framework/issues/365) Fixes Icinga environment corruption on Icinga Agent installation failure
20+
* [#366](https://github.com/Icinga/icinga-powershell-framework/issues/366) Fixes error handling with Icinga Director over IMC, by printing more detailed and user-friendly error messages
2021

2122
### Enhancements
2223

lib/apis/Get-IcingaDirectorSelfServiceConfig.psm1

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,31 @@ function Get-IcingaDirectorSelfServiceConfig()
4242

4343
$EndpointUrl = Join-WebPath -Path $DirectorUrl -ChildPath ([string]::Format('/self-service/powershell-parameters?key={0}', $ApiKey));
4444

45-
$response = Invoke-IcingaWebRequest -Uri $EndpointUrl -UseBasicParsing -Headers @{ 'accept' = 'application/json'; 'X-Director-Accept' = 'application/json' } -Method 'POST';
45+
$response = Invoke-IcingaWebRequest -Uri $EndpointUrl -UseBasicParsing -Headers @{ 'accept' = 'application/json'; 'X-Director-Accept' = 'application/json' } -Method 'POST' -NoErrorMessage;
4646

4747
if ($response.StatusCode -ne 200) {
48-
throw $response.Content;
48+
$ErrorMessage = '';
49+
switch ($response.StatusCode) {
50+
403 {
51+
$ErrorMessage = 'Failed to fetch configuration for host over Self-Service API with key "{0}". This error mostly occurs in case the host object itself is not defined as "Icinga2 Agent" object inside the Icinga Director.';
52+
break;
53+
};
54+
404 {
55+
$ErrorMessage = 'Failed to fetch configuration for host over Self-Service API with key "{0}". Probably the assigned host/template key is not valid or your Icinga Director Url is invalid "{1}".';
56+
break;
57+
};
58+
901 {
59+
$ErrorMessage = 'Failed to fetch host/template configuration from Icinga Director Self-Service API because of SSL/TLS error. Please ensure the certificate is valid and use "Enable-IcingaUntrustedCertificateValidation" for self-signed certificates or install the certificate on this machine.';
60+
break;
61+
}
62+
Default {
63+
$ErrorMessage = ([string]::Format('Failed to fetch host/template configuration from Icinga Director Self-Service API because of unhandled exception: {0}', $response.StatusCode));
64+
break;
65+
};
66+
}
67+
68+
Write-IcingaConsoleError $ErrorMessage -Objects $ApiKey, $DirectorUrl;
69+
throw $ErrorMessage;
4970
}
5071

5172
$JsonContent = ConvertFrom-Json -InputObject $response.Content;

lib/apis/Get-IcingaDirectorSelfServiceTicket.psm1

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,31 @@ function Get-IcingaDirectorSelfServiceTicket()
4242

4343
[string]$url = Join-WebPath -Path $DirectorUrl -ChildPath ([string]::Format('/self-service/ticket?key={0}', $ApiKey));
4444

45-
$response = Invoke-IcingaWebRequest -Uri $url -UseBasicParsing -Headers @{ 'accept' = 'application/json'; 'X-Director-Accept' = 'application/json' } -Method 'POST';
45+
$response = Invoke-IcingaWebRequest -Uri $url -UseBasicParsing -Headers @{ 'accept' = 'application/json'; 'X-Director-Accept' = 'application/json' } -Method 'POST' -NoErrorMessage;
4646

4747
if ($response.StatusCode -ne 200) {
48-
throw $response.Content;
48+
$ErrorMessage = '';
49+
switch ($response.StatusCode) {
50+
404 {
51+
$ErrorMessage = 'Failed to fetch certificate ticket for this host over Self-Service API. Please check that your Icinga Director Url "{1}" is valid and the provided API key "{0}" belongs to a Icinga host object.';
52+
break;
53+
};
54+
500 {
55+
$ErrorMessage = 'Failed to fetch certificate ticket for this host over Self-Service API. Please check that your Icinga CA is running, you have configured a Ticketsalt and that your Icinga Director has enough permissions to communicate with the Icinga 2 API for generating tickets.';
56+
break;
57+
};
58+
901 {
59+
$ErrorMessage = 'Failed to fetch certificate ticket for this host over Self-Service API because of SSL/TLS error. Please ensure the certificate is valid and use "Enable-IcingaUntrustedCertificateValidation" for self-signed certificates or install the certificate on this machine.';
60+
break;
61+
}
62+
Default {
63+
$ErrorMessage = ([string]::Format('Failed to fetch certificate ticket from Icinga Director because of unhandled exception: {0}', $response.StatusCode));
64+
break;
65+
};
66+
}
67+
68+
Write-IcingaConsoleError $ErrorMessage -Objects $ApiKey, $DirectorUrl;
69+
throw $ErrorMessage;
4970
}
5071

5172
$JsonContent = ConvertFrom-Json -InputObject $response.Content;

lib/apis/Register-IcingaDirectorSelfServiceHost.psm1

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,31 @@ function Register-IcingaDirectorSelfServiceHost()
6666

6767
$EndpointUrl = Join-WebPath -Path $DirectorUrl -ChildPath ([string]::Format('/self-service/register-host?name={0}&key={1}', $Hostname, $ApiKey));
6868

69-
$response = Invoke-IcingaWebRequest -Uri $EndpointUrl -UseBasicParsing -Headers @{ 'accept' = 'application/json'; 'X-Director-Accept' = 'application/json' } -Method 'POST' -Body $DirectorConfigJson;
69+
$response = Invoke-IcingaWebRequest -Uri $EndpointUrl -UseBasicParsing -Headers @{ 'accept' = 'application/json'; 'X-Director-Accept' = 'application/json' } -Method 'POST' -Body $DirectorConfigJson -NoErrorMessage;
7070

7171
if ($response.StatusCode -ne 200) {
72-
throw $response.Content;
72+
$ErrorMessage = '';
73+
switch ($response.StatusCode) {
74+
400 {
75+
Write-IcingaConsoleWarning 'Failed to register host inside Icinga Director. The host is probably already registered.'
76+
return $null;
77+
};
78+
404 {
79+
$ErrorMessage = 'Failed to register host with the given API key "{0}" inside Icinga Director. Please ensure the template key you are using is correct and the template is set as "Icinga2 Agent" object. Non-Agent templates will not work over the Self-Service API.';
80+
break;
81+
};
82+
901 {
83+
$ErrorMessage = 'Failed to register host over Self-Service API inside Icinga Director because of SSL/TLS error. Please ensure the certificate is valid and use "Enable-IcingaUntrustedCertificateValidation" for self-signed certificates or install the certificate on this machine.';
84+
break;
85+
}
86+
Default {
87+
$ErrorMessage = ([string]::Format('Failed to register host inside Icinga Director because of unhandled exception: {0}', $response.StatusCode));
88+
break;
89+
};
90+
}
91+
92+
Write-IcingaConsoleError $ErrorMessage -Objects $ApiKey;
93+
throw $ErrorMessage;
7394
}
7495

7596
$JsonContent = ConvertFrom-Json -InputObject $response.Content;

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,20 @@ function Start-IcingaAgentDirectorWizard()
9191

9292
if ($HostKnown -eq $FALSE) {
9393
while ($TRUE) {
94+
[bool]$RegisterFailed = $FALSE;
9495
try {
9596
$SelfServiceAPIKey = Register-IcingaDirectorSelfServiceHost -DirectorUrl $DirectorUrl -ApiKey $SelfServiceAPIKey -Hostname (Get-IcingaHostname @Arguments) -Endpoint $Arguments.IcingaMaster;
96-
break;
97+
98+
if ([string]::IsNullOrEmpty($SelfServiceAPIKey) -eq $FALSE) {
99+
break;
100+
} else {
101+
$RegisterFailed = $TRUE;
102+
}
97103
} catch {
104+
$RegisterFailed = $TRUE;
105+
}
106+
107+
if ($RegisterFailed) {
98108
$SelfServiceAPIKey = (Get-IcingaAgentInstallerAnswerInput -Prompt ([string]::Format('Failed to register host within Icinga Director. Full error: "{0}". Please re-enter your SelfService API Key. If this prompt continues ensure you are using an Agent template or drop your host key at "Hosts -> {1} -> Agent"', $_.Exception.Message, (Get-IcingaHostname @Arguments))) -Default 'v' -DefaultInput $SelfServiceAPIKey).answer;
99109
}
100110
}

lib/core/installer/Install-Icinga.psm1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ function Install-Icinga()
1616
'LastInput' = '';
1717
'LastNotice' = '';
1818
'LastError' = '';
19+
'DirectorError' = '';
1920
'HeaderPreview' = '';
2021
'DirectorSelfService' = $FALSE;
2122
'DirectorRegisteredHost' = $FALSE;
23+
'DirectorInstallError' = $FALSE;
2224
'LastParent' = [System.Collections.ArrayList]@();
2325
'LastValues' = @();
2426
'DisabledEntries' = @{ };
@@ -61,6 +63,7 @@ function Install-Icinga()
6163
# In case we use the director, we require to first fetch all basic values from the Self-Service API then
6264
# require to register the host to fet the remaining content
6365
if ($IcingaConfiguration.ContainsKey('IfW-DirectorSelfServiceKey') -And $IcingaConfiguration.ContainsKey('IfW-DirectorUrl')) {
66+
Enable-IcingaFrameworkConsoleOutput;
6467
Resolve-IcingaForWindowsManagementConsoleInstallationDirectorTemplate;
6568
Resolve-IcingaForWindowsManagementConsoleInstallationDirectorTemplate -Register;
6669
Disable-IcingaFrameworkConsoleOutput;

lib/core/installer/Start-IcingaForWindowsInstallation.psm1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ function Start-IcingaForWindowsInstallation()
44
[switch]$Automated
55
);
66

7-
if ((Get-IcingaFrameworkDebugMode) -eq $FALSE) {
7+
if ($global:Icinga.InstallWizard.DirectorInstallError -eq $FALSE -And (Get-IcingaFrameworkDebugMode) -eq $FALSE) {
88
Clear-Host;
99
}
1010

1111
Write-IcingaConsoleNotice 'Starting Icinga for Windows installation';
1212

13+
if ($global:Icinga.InstallWizard.DirectorInstallError) {
14+
Write-IcingaConsoleError 'Failed to start Icinga for Windows installation, caused by an error while communicating with Icinga Director: {0}' -Objects $global:Icinga.InstallWizard.DirectorError;
15+
throw $global:Icinga.InstallWizard.DirectorError;
16+
return;
17+
}
18+
1319
$ConnectionType = Get-IcingaForWindowsInstallerStepSelection -InstallerStep 'Show-IcingaForWindowsInstallerMenuSelectConnection';
1420
$HostnameType = Get-IcingaForWindowsInstallerStepSelection -InstallerStep 'Show-IcingaForWindowsInstallerMenuSelectHostname';
1521
$FirewallType = Get-IcingaForWindowsInstallerStepSelection -InstallerStep 'Show-IcingaForWindowsInstallerMenuSelectOpenWindowsFirewall';

lib/core/installer/menu/installation/director/DirectorTemplate.psm1

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,44 @@ function Resolve-IcingaForWindowsManagementConsoleInstallationDirectorTemplate()
4646
};
4747
}
4848

49+
[bool]$RegisterFailed = $FALSE;
50+
4951
try {
5052
$SelfServiceKey = Register-IcingaDirectorSelfServiceHost -DirectorUrl $DirectorUrl -ApiKey $SelfServiceKey -Hostname $Hostname;
51-
$UsedEnteredKey = $SelfServiceKey;
53+
if ([string]::IsNullOrEmpty($SelfServiceKey) -eq $FALSE) {
54+
$UsedEnteredKey = $SelfServiceKey;
55+
} else {
56+
$RegisterFailed = $TRUE;
57+
}
5258
} catch {
59+
$RegisterFailed = $TRUE;
60+
}
61+
62+
if ($RegisterFailed) {
5363
Write-IcingaConsoleNotice 'Host seems already to be registered within Icinga Director. Trying local Api key if present'
5464
$SelfServiceKey = Get-IcingaPowerShellConfig -Path 'IcingaDirector.SelfService.ApiKey';
5565

5666
if ([string]::IsNullOrEmpty($SelfServiceKey)) {
5767
Write-IcingaConsoleNotice 'No local Api key was found and using your provided template key failed. Please ensure the host is not already registered and drop the set Self-Service key within the Icinga Director for this host.'
5868
}
5969
}
70+
6071
Add-IcingaForWindowsInstallerConfigEntry -Selection 'c' -Values $UsedEnteredKey -OverwriteValues -OverwriteMenu 'Show-IcingaForWindowsManagementConsoleInstallationEnterDirectorSelfServiceKey';
6172
}
6273

6374
try {
6475
$DirectorConfig = Get-IcingaDirectorSelfServiceConfig -DirectorUrl $DirectorUrl -ApiKey $SelfServiceKey;
6576
} catch {
6677
Set-IcingaForWindowsManagementConsoleMenu 'Show-IcingaForWindowsInstallerConfigurationSummary';
67-
$global:Icinga.InstallWizard.LastError = 'Failed to fetch host configuration with the given Director Url and Self-Service key. Please ensure the template key is correct and in case a previous host key was used, that it matches the one configured within the Icinga Director. In case this form was loaded previously with a key, it might be that the host key is no longer valid and requires to be dropped. In addition please ensure that this host can connect to the Icinga Director and the SSL certificate is trusted. Otherwise run "Enable-IcingaUntrustedCertificateValidation" before starting the management console. Otherwise modify the "DirectorSelfServiceKey" configuration element above with the correct key and try again.';
78+
$global:Icinga.InstallWizard.LastError = 'Failed to fetch host configuration with the given Director Url and Self-Service key. Please ensure the template key is correct and in case a previous host key was used, that it matches the one configured within the Icinga Director. In case this form was loaded previously with a key, it might be that the host key is no longer valid and requires to be dropped. In addition please ensure that this host can connect to the Icinga Director and the SSL certificate is trusted. Otherwise run "Enable-IcingaUntrustedCertificateValidation" before starting the management console. Otherwise modify the "DirectorSelfServiceKey" configuration element above with the correct key and try again.';
79+
$global:Icinga.InstallWizard.DirectorError = $global:Icinga.InstallWizard.LastError;
80+
$global:Icinga.InstallWizard.DirectorInstallError = $TRUE;
6881
return;
6982
}
7083

84+
$global:Icinga.InstallWizard.DirectorInstallError = $FALSE;
85+
$global:Icinga.InstallWizard.DirectorError = '';
86+
7187
# No we need to identify which host selection is matching our config
7288
$HostnameSelection = 1;
7389
$InstallPluginsSelection = 0;

lib/core/installer/tools/ShowInstallerMenu.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function Show-IcingaForWindowsInstallerMenu()
2222
[switch]$NoConfigSwap = $FALSE
2323
);
2424

25-
if ((Test-IcingaForWindowsInstallationHeaderPrint) -eq $FALSE -And (Get-IcingaFrameworkDebugMode) -eq $FALSE) {
25+
if ($global:Icinga.InstallWizard.DirectorInstallError -eq $FALSE -And (Test-IcingaForWindowsInstallationHeaderPrint) -eq $FALSE -And (Get-IcingaFrameworkDebugMode) -eq $FALSE) {
2626
Clear-Host;
2727
}
2828

lib/core/logging/Write-IcingaConsoleDebug.psm1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ function Write-IcingaConsoleDebug()
2323
{
2424
param (
2525
[string]$Message,
26-
[array]$Objects
26+
[array]$Objects,
27+
[switch]$DropMessage = $FALSE
2728
);
2829

2930
if ((Get-IcingaFrameworkDebugMode) -eq $FALSE) {
@@ -34,5 +35,6 @@ function Write-IcingaConsoleDebug()
3435
-Message $Message `
3536
-Objects $Objects `
3637
-ForeColor 'Blue' `
37-
-Severity 'Debug';
38+
-Severity 'Debug' `
39+
-DropMessage:$DropMessage;
3840
}

0 commit comments

Comments
 (0)