Merge pull request #642 from Icinga:feature/allow_to_configure_director_self_service_register_string

Feature: Adds support to configure Director Self-Service config string

This new feature allows to configure the Icinga Director Self-Service call during registration, by modifing the JSON-String. This will only apply to the first time registration of a host and is not modified again, unless the host and director keys for this specific host object are removed.

This works directly inside the IMC installation wizard as well as for the exported installation command or file.

By doing so, you can manually define the IP-Address with `address` key in your JSON as well as the display name of an object with `display_name`. Example:

```json
{ "address": "127.0.0.1", "display_name": "Example Host" }
```

The following constants are available inside the configuration:

`$ifw.hostaddress$`: Will use the default IP-Address automatically fetched from the system
`$ifw.hostname$`: The current hostname of the host, without FQDN and not modiying upper/lowercase
`$ifw.hostfqdn$`: The current FQDN of the host, without case modification

For lower and upper case modification, you can use `.tolower` or `.toupper` for the hostname and FQDN, example:

```json
{ "address": "127.0.0.1", "display_name": "$ifw.hostname.tolower$" }
```
This commit is contained in:
Lord Hepipud 2023-07-14 13:46:27 +02:00 committed by GitHub
commit 6220349bc4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 137 additions and 31 deletions

View file

@ -21,6 +21,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
### Enhancements
* [#544](https://github.com/Icinga/icinga-powershell-framework/issues/544) Adds support to configure the Icinga Director JSON string for registering hosts via self-service API
* [#619](https://github.com/Icinga/icinga-powershell-framework/pull/619) Adds feature to securely read enum provider values with new function `Get-IcingaProviderEnumData`
* [#623](https://github.com/Icinga/icinga-powershell-framework/issues/623) Adds support to provide the Icinga service user written as `user@domain`
* [#635](https://github.com/Icinga/icinga-powershell-framework/pull/635) Adds support for `Write-IcingaAgentApiConfig` function to configure the Icinga Agent TLS cipher list setting by new argument `-CipherList`

View file

@ -61,8 +61,26 @@ function Register-IcingaDirectorSelfServiceHost()
}
}
$Interface = Get-IcingaNetworkInterface $Endpoint;
$DirectorConfigJson = [string]::Format('{0} "address":"{2}" {1}', '{', '}', $Interface);
$Interface = Get-IcingaNetworkInterface $Endpoint;
if ($null -eq $Global:Icinga.InstallWizard.DirectorSelfServiceConfig) {
$DirectorConfigJson = [string]::Format('{{ "address": "{0}" }}', $Interface);
} else {
try {
$DirectorConfigJson = ConvertTo-Json -InputObject $Global:Icinga.InstallWizard.DirectorSelfServiceConfig -Compress -ErrorAction Stop;
$DirectorConfigJson = $DirectorConfigJson.Replace('$ifw.hostaddress$', $Interface);
$DirectorConfigJson = $DirectorConfigJson.Replace('$ifw.hostname.tolower$', (Get-IcingaHostname -AutoUseHostname 1 -LowerCase 1));
$DirectorConfigJson = $DirectorConfigJson.Replace('$ifw.hostname.toupper$', (Get-IcingaHostname -AutoUseHostname 1 -UpperCase 1));
$DirectorConfigJson = $DirectorConfigJson.Replace('$ifw.hostname$', (Get-IcingaHostname -AutoUseHostname 1));
$DirectorConfigJson = $DirectorConfigJson.Replace('$ifw.hostfqdn.tolower$', (Get-IcingaHostname -AutoUseFQDN 1 -LowerCase 1));
$DirectorConfigJson = $DirectorConfigJson.Replace('$ifw.hostfqdn.toupper$', (Get-IcingaHostname -AutoUseFQDN 1 -UpperCase 1));
$DirectorConfigJson = $DirectorConfigJson.Replace('$ifw.hostfqdn$', (Get-IcingaHostname -AutoUseFQDN 1));
} catch {
# Fallback to default
Write-IcingaConsosoleError 'Failed to deserialize your custom Icinga Director Self-Service configuration. Using Icinga for Windows defaults.'
$DirectorConfigJson = [string]::Format('{{ "address": "{0}" }}', $Interface);
}
}
$EndpointUrl = Join-WebPath -Path $DirectorUrl -ChildPath ([string]::Format('/self-service/register-host?name={0}&key={1}', $Hostname, $ApiKey));

View file

@ -21,34 +21,35 @@ function Install-Icinga()
if ($global:Icinga.ContainsKey('InstallWizard') -eq $FALSE) {
$global:Icinga.Add(
'InstallWizard', @{
'AdminShell' = (Test-AdministrativeShell);
'LastInput' = '';
'LastNotice' = '';
'LastWarning' = @();
'LastError' = @();
'DirectorError' = '';
'HeaderPreview' = '';
'DirectorSelfService' = $FALSE;
'DirectorRegisteredHost' = $FALSE;
'DirectorInstallError' = $FALSE;
'LastParent' = [System.Collections.ArrayList]@();
'LastValues' = @();
'DisabledEntries' = @{ };
'Config' = @{ };
'ConfigSwap' = @{ };
'ParentConfig' = $null;
'Menu' = 'Install-Icinga';
'NextCommand' = '';
'NextArguments' = $null;
'HeaderSelection' = $null;
'DisplayAdvanced' = $FALSE;
'ShowAdvanced' = $FALSE;
'ShowHelp' = $FALSE;
'ShowCommand' = $FALSE;
'DeleteValues' = $FALSE;
'HeaderPrint' = $FALSE;
'JumpToSummary' = $FALSE;
'Closing' = $FALSE;
'AdminShell' = (Test-AdministrativeShell);
'LastInput' = '';
'LastNotice' = '';
'LastWarning' = @();
'LastError' = @();
'DirectorError' = '';
'HeaderPreview' = '';
'DirectorSelfServiceConfig' = $null;
'DirectorSelfService' = $FALSE;
'DirectorRegisteredHost' = $FALSE;
'DirectorInstallError' = $FALSE;
'LastParent' = [System.Collections.ArrayList]@();
'LastValues' = @();
'DisabledEntries' = @{ };
'Config' = @{ };
'ConfigSwap' = @{ };
'ParentConfig' = $null;
'Menu' = 'Install-Icinga';
'NextCommand' = '';
'NextArguments' = $null;
'HeaderSelection' = $null;
'DisplayAdvanced' = $FALSE;
'ShowAdvanced' = $FALSE;
'ShowHelp' = $FALSE;
'ShowCommand' = $FALSE;
'DeleteValues' = $FALSE;
'HeaderPrint' = $FALSE;
'JumpToSummary' = $FALSE;
'Closing' = $FALSE;
}
);
} else {

View file

@ -31,6 +31,7 @@ function Add-IcingaForWindowsInstallationAdvancedEntries()
Show-IcingaForWindowsInstallationMenuEnterIcingaCAServer -Automated -Advanced;
Show-IcingaForWindowsInstallerMenuSelectInstallApiChecks -Automated -Advanced;
Show-IcingaForWindowsInstallerMenuSelectServiceRecovery -Automated -Advanced;
Show-IcingaForWindowsManagementConsoleInstallationEnterDirectorSelfServiceConfig -Automated -Advanced;
Enable-IcingaFrameworkConsoleOutput;

View file

@ -0,0 +1,65 @@
function Show-IcingaForWindowsManagementConsoleInstallationEnterDirectorSelfServiceConfig()
{
param (
[array]$Value = @(),
[string]$DefaultInput = 'c',
[switch]$JumpToSummary = $FALSE,
[switch]$Automated = $FALSE,
[switch]$Advanced = $FALSE
);
# Ensure we simply set the global variable for the Config in case we run in automation mode
if ($Automated) {
if ($null -ne $Value -And $null -ne $Value[0]) {
$Global:Icinga.InstallWizard.DirectorSelfServiceConfig = ConvertFrom-Json -InputObject $Value[0] -ErrorAction Stop;
return;
}
}
# Set the default if no value ist set
if ($Value -IsNot [array] -Or $null -eq $Value -or $Value.Count -eq 0) {
$Value.Clear();
if ($null -eq $Global:Icinga.InstallWizard.DirectorSelfServiceConfig) {
$Value += '{ "address": "$ifw.hostaddress$" }';
} else {
$Value += ConvertTo-Json -InputObject $Global:Icinga.InstallWizard.DirectorSelfServiceConfig -Depth 100 -Compress;
}
}
Show-IcingaForWindowsInstallerMenu `
-Header 'You can update the Icinga Director Self-Service config in this section. USE WITH CARE!' `
-Entries @(
@{
'Command' = 'Show-IcingaForWindowsInstallerConfigurationSummary';
'Help' = 'This is the configuration JSON-Object for the Icinga Director Self-Service API. You can set a custom IP-Address or define the display name of an object with "display_name" as key. Use this methid with caution! Not all configuration elements in general possible are accessible by using the Self-Service keys.';
}
) `
-DefaultIndex $DefaultInput `
-AddConfig `
-ConfigLimit 1 `
-DefaultValues @( $Value ) `
-MandatoryValue `
-JumpToSummary:$JumpToSummary `
-ConfigElement `
-Automated:$Automated `
-Advanced:$Advanced;
# Fetch the current JSON-String inserted by the user
[string]$ConfigString = Get-IcingaForWindowsInstallerValuesFromStep;
if ([string]::IsNullOrEmpty($ConfigString) -eq $FALSE) {
try {
# Validate that our JSON is correct
$Global:Icinga.InstallWizard.DirectorSelfServiceConfig = ConvertFrom-Json -InputObject $ConfigString -ErrorAction Stop;
} catch {
# Set some defaults to ensure we don't break the installer
Write-IcingaConsoleError ([string]::Format('The provided Icinga Director Self Service configuration "{0}" does not appear to be a valid JSON-String. E.g.: {{ "address": "$ifw.hostaddress$" }} without leading and ending "" before and after {{ }}', $ConfigString));
$Global:Icinga.InstallWizard.DirectorSelfServiceConfig = $null;
Set-IcingaForWindowsInstallerValuesFromStep -Values @( '{ }' );
}
} else {
$Global:Icinga.InstallWizard.DirectorSelfServiceConfig = $null;
}
}
Set-Alias -Name 'IfW-DirectorSelfServiceConfig' -Value 'Show-IcingaForWindowsManagementConsoleInstallationEnterDirectorSelfServiceConfig';

View file

@ -16,7 +16,7 @@ function Invoke-IcingaForWindowsManagementConsoleCustomConfig()
}
if ($cmdConfig.ContainsKey('Values') -And $null -ne $cmdConfig.Values) {
$cmdArguments.Add('Value', $cmdConfig.Values)
$cmdArguments.Add('Value', $cmdConfig.Values);
}
if ($cmdConfig.ContainsKey('Selection') -And $null -ne $cmdConfig.Selection) {
$cmdArguments.Add('DefaultInput', $cmdConfig.Selection)

View file

@ -0,0 +1,20 @@
function Set-IcingaForWindowsInstallerValuesFromStep()
{
param (
[string]$InstallerStep,
[string]$Parent,
[array]$Values = @()
);
$Step = Get-IcingaForWindowsManagementConsoleMenu;
if ([string]::IsNullOrEmpty($InstallerStep) -eq $FALSE) {
$Step = Get-IcingaForWindowsManagementConsoleAlias -Command $InstallerStep;
if ([string]::IsNullOrEmpty($Parent) -eq $FALSE) {
$Step = [string]::Format('{0}:{1}', $Step, $Parent);
}
}
$global:Icinga.InstallWizard.Config[$Step].Values = $Values;
}