2020-04-28 09:24:57 -04:00
<#
. SYNOPSIS
2020-08-04 08:48:32 -04:00
Will fetch the ticket for certificate signing by using the Icinga Director
Self-Service API
2020-04-28 09:24:57 -04:00
. DESCRIPTION
2020-08-04 08:48:32 -04:00
Use the Self-Service API of the Icinga Director to connect to it and fetch the
ticket to sign Icinga 2 certificate requests
2020-04-28 09:24:57 -04:00
. FUNCTIONALITY
2020-08-04 08:48:32 -04:00
Fetches the ticket for certificate signing form the Icinga Director Self-Service API
2020-04-28 09:24:57 -04:00
. EXAMPLE
2020-08-04 08:48:32 -04:00
PS > Get-IcingaDirectorSelfServiceTicket -DirectorUrl ' https : / / example . com / icingaweb2 / director -ApiKey 457g6b98054v76vb5490ß276bv0457v6054b76 ;
2020-04-28 09:24:57 -04:00
. PARAMETER DirectorUrl
2020-08-04 08:48:32 -04:00
The URL pointing directly to the Icinga Web 2 Director module
2020-04-28 09:24:57 -04:00
. PARAMETER ApiKey
2020-08-04 08:48:32 -04:00
The host key to authenticate against the Self-Service API
2020-04-28 09:24:57 -04:00
. INPUTS
2020-08-04 08:48:32 -04:00
System . String
2020-04-28 09:24:57 -04:00
. OUTPUTS
2020-08-04 08:48:32 -04:00
System . Object
2020-04-28 09:24:57 -04:00
. LINK
2020-08-04 08:48:32 -04:00
https : / / github . com / Icinga / icinga-powershell -framework
2020-04-28 09:24:57 -04:00
#>
2019-11-02 12:42:39 -04:00
function Get-IcingaDirectorSelfServiceTicket ( )
{
2020-08-04 08:48:32 -04:00
param (
$DirectorUrl ,
$ApiKey = $null
) ;
2019-11-02 12:42:39 -04:00
2020-08-04 08:48:32 -04:00
if ( [ string ] :: IsNullOrEmpty ( $DirectorUrl ) ) {
Write-IcingaConsoleError 'Unable to fetch host ticket. No Director url has been specified' ;
return ;
}
2019-11-02 12:42:39 -04:00
2020-08-04 08:48:32 -04:00
if ( [ string ] :: IsNullOrEmpty ( $ApiKey ) ) {
Write-IcingaConsoleError 'Unable to fetch host ticket. No API key has been specified' ;
return ;
}
2019-11-02 12:42:39 -04:00
2020-08-04 08:48:32 -04:00
Set-IcingaTLSVersion ;
2020-07-31 04:20:53 -04:00
2020-08-04 08:48:32 -04:00
[ string ] $url = Join-WebPath -Path $DirectorUrl -ChildPath ( [ string ] :: Format ( '/self-service/ticket?key={0}' , $ApiKey ) ) ;
2019-11-02 12:42:39 -04:00
2021-09-09 12:53:48 -04:00
$response = Invoke-IcingaWebRequest -Uri $url -UseBasicParsing -Headers @ { 'accept' = 'application/json' ; 'X-Director-Accept' = 'application/json' } -Method 'POST' -NoErrorMessage ;
2019-11-02 12:42:39 -04:00
2020-08-04 08:48:32 -04:00
if ( $response . StatusCode -ne 200 ) {
2021-09-09 12:53:48 -04:00
$ErrorMessage = '' ;
switch ( $response . StatusCode ) {
404 {
2022-01-03 19:24:26 -05:00
$ErrorMessage = ( [ string ] :: Format ( '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.' , $DirectorUrl , $ApiKey ) ) ;
2021-09-09 12:53:48 -04:00
break ;
} ;
500 {
2022-01-14 16:18:59 -05:00
$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.' ;
2021-09-09 12:53:48 -04:00
break ;
} ;
901 {
$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.' ;
break ;
}
Default {
$ErrorMessage = ( [ string ] :: Format ( 'Failed to fetch certificate ticket from Icinga Director because of unhandled exception: {0}' , $response . StatusCode ) ) ;
break ;
} ;
}
Write-IcingaConsoleError $ErrorMessage -Objects $ApiKey , $DirectorUrl ;
throw $ErrorMessage ;
2020-08-04 08:48:32 -04:00
}
2019-11-02 12:42:39 -04:00
2020-08-04 08:48:32 -04:00
$JsonContent = ConvertFrom-Json -InputObject $response . Content ;
2019-11-02 12:42:39 -04:00
2020-08-04 08:48:32 -04:00
return $JsonContent ;
2019-11-02 12:42:39 -04:00
}