|
| 1 | +function New-IcingaForWindowsRESTApi() |
| 2 | +{ |
| 3 | + # Allow us to parse the framework global data to this thread |
| 4 | + param ( |
| 5 | + $IcingaDaemonData, |
| 6 | + $Port, |
| 7 | + $RootFolder, |
| 8 | + $CertFile, |
| 9 | + $CertThumbprint, |
| 10 | + $RequireAuth |
| 11 | + ); |
| 12 | + |
| 13 | + # Import the framework library components and initialise it |
| 14 | + # as daemon |
| 15 | + Use-Icinga -LibOnly -Daemon; |
| 16 | + |
| 17 | + $Global:IcingaDaemonData = $IcingaDaemonData; |
| 18 | + |
| 19 | + # Add a synchronized hashtable to the global data background |
| 20 | + # daemon hashtable to write data to. In addition it will |
| 21 | + # allow to share data collected from this daemon with others |
| 22 | + $IcingaDaemonData.BackgroundDaemon.Add( |
| 23 | + 'IcingaPowerShellRestApi', |
| 24 | + [hashtable]::Synchronized(@{}) |
| 25 | + ); |
| 26 | + |
| 27 | + # Map our Icinga globals to a shorter variable |
| 28 | + $RestDaemon = $IcingaDaemonData.BackgroundDaemon.IcingaPowerShellRestApi; |
| 29 | + |
| 30 | + # This will add another hashtable to our previous |
| 31 | + # IcingaPowerShellRestApi hashtable to store actual |
| 32 | + # endpoint configurations for the API |
| 33 | + $RestDaemon.Add( |
| 34 | + 'RegisteredEndpoints', |
| 35 | + [hashtable]::Synchronized(@{}) |
| 36 | + ); |
| 37 | + |
| 38 | + # This will add another hashtable to our previous |
| 39 | + # IcingaPowerShellRestApi hashtable to store actual |
| 40 | + # command aliases for execution for the API |
| 41 | + $RestDaemon.Add( |
| 42 | + 'CommandAliases', |
| 43 | + [hashtable]::Synchronized(@{}) |
| 44 | + ); |
| 45 | + |
| 46 | + # This will add another hashtable to our previous |
| 47 | + # IcingaPowerShellRestApi hashtable to store actual |
| 48 | + # command aliases for execution for the API |
| 49 | + $RestDaemon.Add( |
| 50 | + 'ClientBlacklist', |
| 51 | + [hashtable]::Synchronized(@{}) |
| 52 | + ); |
| 53 | + |
| 54 | + # Make the root folder of our rest daemon module available |
| 55 | + # for every possible thread we require |
| 56 | + $RestDaemon.Add( |
| 57 | + 'RootFolder', $RootFolder |
| 58 | + ); |
| 59 | + |
| 60 | + $RESTEndpoints = Invoke-IcingaNamespaceCmdlets -Command 'Register-IcingaRESTAPIEndpoint*'; |
| 61 | + Write-IcingaDebugMessage -Message ( |
| 62 | + [string]::Format( |
| 63 | + 'Loading configuration for REST-Endpoints{0}{1}', |
| 64 | + (New-IcingaNewLine), |
| 65 | + ($RESTEndpoints | Out-String) |
| 66 | + ) |
| 67 | + ); |
| 68 | + |
| 69 | + Write-IcingaDebugMessage -Message ($RestDaemon | Out-String); |
| 70 | + |
| 71 | + foreach ($entry in $RESTEndpoints.Values) { |
| 72 | + [bool]$Success = Add-IcingaHashtableItem -Hashtable $RestDaemon.RegisteredEndpoints ` |
| 73 | + -Key $entry.Alias ` |
| 74 | + -Value $entry.Command; |
| 75 | + |
| 76 | + if ($Success -eq $FALSE) { |
| 77 | + Write-IcingaEventMessage ` |
| 78 | + -EventId 2100 ` |
| 79 | + -Namespace 'RESTApi' ` |
| 80 | + -Objects ([string]::Format('Adding duplicated REST endpoint "{0}" with command "{1}', $entry.Alias, $entry.Command)), $RESTEndpoints; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + $CommandAliases = Invoke-IcingaNamespaceCmdlets -Command 'Register-IcingaRESTApiCommandAliases*'; |
| 85 | + |
| 86 | + foreach ($entry in $CommandAliases.Values) { |
| 87 | + foreach ($component in $entry.Keys) { |
| 88 | + [bool]$Success = Add-IcingaHashtableItem -Hashtable $RestDaemon.CommandAliases ` |
| 89 | + -Key $component ` |
| 90 | + -Value $entry[$component]; |
| 91 | + |
| 92 | + if ($Success -eq $FALSE) { |
| 93 | + Write-IcingaEventMessage ` |
| 94 | + -EventId 2101 ` |
| 95 | + -Namespace 'RESTApi' ` |
| 96 | + -Objects ([string]::Format('Adding duplicated REST command aliases "{0}" for namespace "{1}', $entry[$component], $component)), $CommandAliases; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + Write-IcingaDebugMessage -Message ($RestDaemon.RegisteredEndpoints | Out-String); |
| 102 | + |
| 103 | + if ($Global:IcingaDaemonData.JEAContext) { |
| 104 | + if ($global:IcingaDaemonData.ContainsKey('SSLCertificate') -eq $FALSE -Or $null -eq $global:IcingaDaemonData.SSLCertificate) { |
| 105 | + Write-IcingaEventMessage -EventId 2001 -Namespace 'RESTApi'; |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + $Certificate = $global:IcingaDaemonData.SSLCertificate; |
| 110 | + } else { |
| 111 | + $Certificate = Get-IcingaSSLCertForSocket -CertFile $CertFile -CertThumbprint $CertThumbprint; |
| 112 | + } |
| 113 | + |
| 114 | + if ($null -eq $Certificate) { |
| 115 | + Write-IcingaEventMessage -EventId 2000 -Namespace 'RESTApi'; |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + $Socket = New-IcingaTCPSocket -Address $Address -Port $Port -Start; |
| 120 | + |
| 121 | + # Keep our code executed as long as the PowerShell service is |
| 122 | + # being executed. This is required to ensure we will execute |
| 123 | + # the code frequently instead of only once |
| 124 | + while ($TRUE) { |
| 125 | + $Connection = Open-IcingaTCPClientConnection ` |
| 126 | + -Client (New-IcingaTCPClient -Socket $Socket) ` |
| 127 | + -Certificate $Certificate; |
| 128 | + |
| 129 | + if (Test-IcingaRESTClientBlacklisted -Client $Connection.Client -ClientList $RestDaemon.ClientBlacklist) { |
| 130 | + Write-IcingaDebugMessage -Message 'A remote client which is trying to connect was blacklisted' -Objects $Connection.Client.Client; |
| 131 | + Close-IcingaTCPConnection -Client $Connection.Client; |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + if ((Test-IcingaRESTClientConnection -Connection $Connection) -eq $FALSE) { |
| 136 | + continue; |
| 137 | + } |
| 138 | + |
| 139 | + # API not yet ready |
| 140 | + if ($IcingaDaemonData.IcingaThreadContent.RESTApi.ApiRequests.Count -eq 0) { |
| 141 | + Close-IcingaTCPConnection -Client $Connection.Client; |
| 142 | + continue; |
| 143 | + } |
| 144 | + |
| 145 | + try { |
| 146 | + $NextRESTApiThreadId = (Get-IcingaNextRESTApiThreadId); |
| 147 | + |
| 148 | + if ($IcingaDaemonData.IcingaThreadContent.RESTApi.ApiRequests.ContainsKey($NextRESTApiThreadId) -eq $FALSE) { |
| 149 | + Close-IcingaTCPConnection -Client $Connection.Client; |
| 150 | + continue; |
| 151 | + } |
| 152 | + |
| 153 | + $IcingaDaemonData.IcingaThreadContent.RESTApi.ApiRequests.$NextRESTApiThreadId.Enqueue($Connection); |
| 154 | + } catch { |
| 155 | + $ExMsg = $_.Exception.Message; |
| 156 | + Write-IcingaEventMessage -Namespace 'RESTApi' -EvenId 2050 -Objects $ExMsg; |
| 157 | + } |
| 158 | + |
| 159 | + # Cleanup the error stack and remove not required data |
| 160 | + $Error.Clear(); |
| 161 | + # Force PowerShell to call the garbage collector to free memory |
| 162 | + [System.GC]::Collect(); |
| 163 | + } |
| 164 | +} |
0 commit comments